tests update

This commit is contained in:
Hamilton Kibbe 2014-09-30 23:42:02 -04:00
parent 88fb7f2311
commit f8449ad2b6
9 changed files with 176 additions and 11 deletions

View file

@ -18,6 +18,17 @@
def read(filename):
""" Read a gerber or excellon file and return a representative object.
Parameters
----------
filename : string
Filename of the file to read.
Returns
-------
file : CncFile subclass
CncFile object representing the file, either GerberFile or
ExcellonFile. Returns None if file is not an Excellon or Gerber file.
"""
import gerber
import excellon

View file

@ -29,7 +29,7 @@ class FileSettings(object):
Provides a common representation of gerber/excellon file settings
"""
def __init__(self, notation='absolute', units='inch',
zero_suppression='trailing', format=(2,5)):
zero_suppression='trailing', format=(2, 5)):
if notation not in ['absolute', 'incremental']:
raise ValueError('Notation must be either absolute or incremental')
self.notation = notation
@ -43,7 +43,7 @@ class FileSettings(object):
trailling')
self.zero_suppression = zero_suppression
if len(format) != 2:
if len(format) != 2:
raise ValueError('Format must be a tuple(n=2) of integers')
self.format = format
@ -52,13 +52,14 @@ class FileSettings(object):
return self.notation
elif key == 'units':
return self.units
elif key =='zero_suppression':
elif key == 'zero_suppression':
return self.zero_suppression
elif key == 'format':
return self.format
else:
raise KeyError()
class CncFile(object):
""" Base class for Gerber/Excellon files.
@ -101,7 +102,7 @@ class CncFile(object):
self.notation = 'absolute'
self.units = 'inch'
self.zero_suppression = 'trailing'
self.format = (2,5)
self.format = (2, 5)
self.filename = filename
@property

View file

@ -24,5 +24,5 @@ SVG is the only supported format.
"""
from svg import GerberSvgContext
from svgwrite_backend import GerberSvgContext

View file

@ -48,11 +48,6 @@ class FSParamStmt(ParamStmt):
notation = 'absolute' if stmt_dict.get('notation') == 'A' else 'incremental'
x = map(int, stmt_dict.get('x').strip())
format = (x[0], x[1])
if notation == 'incremental':
print('This file uses incremental notation. To quote the gerber \
file specification:\nIncremental notation is a source of \
endless confusion. Always use absolute notation.\n\nYou \
have been warned')
return cls(param, zeros, notation, format)
def __init__(self, param, zero_suppression='leading',
@ -172,7 +167,7 @@ class IPParamStmt(ParamStmt):
self.ip = ip
def to_gerber(self):
ip = 'POS' if self.ip == 'positive' else 'negative'
ip = 'POS' if self.ip == 'positive' else 'NEG'
return '%IP{0}*%'.format(ip)
def __str__(self):

50
gerber/tests/test_cnc.py Normal file
View file

@ -0,0 +1,50 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Hamilton Kibbe <ham@hamiltonkib.be>
from ..cnc import CncFile, FileSettings
from tests import *
def test_smoke_filesettings():
""" Smoke test FileSettings class
"""
fs = FileSettings()
def test_filesettings_defaults():
""" Test FileSettings default values
"""
fs = FileSettings()
assert_equal(fs.format, (2, 5))
assert_equal(fs.notation, 'absolute')
assert_equal(fs.zero_suppression, 'trailing')
assert_equal(fs.units, 'inch')
def test_filesettings_dict():
""" Test FileSettings Dict
"""
fs = FileSettings()
assert_equal(fs['format'], (2, 5))
assert_equal(fs['notation'], 'absolute')
assert_equal(fs['zero_suppression'], 'trailing')
assert_equal(fs['units'], 'inch')
def test_filesettings_assign():
""" Test FileSettings attribute assignment
"""
fs = FileSettings()
fs.units = 'test'
fs.notation = 'test'
fs.zero_suppression = 'test'
fs.format = 'test'
assert_equal(fs.units, 'test')
assert_equal(fs.notation, 'test')
assert_equal(fs.zero_suppression, 'test')
assert_equal(fs.format, 'test')
def test_smoke_cncfile():
pass

View file

@ -0,0 +1,87 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Hamilton Kibbe <ham@hamiltonkib.be>
from .tests import *
from ..statements import *
def test_FSParamStmt_factory():
""" Test FSParamStruct factory correctly handles parameters
"""
stmt = {'param': 'FS', 'zero': 'L', 'notation': 'A', 'x': '27'}
fs = FSParamStmt.from_dict(stmt)
assert_equal(fs.param, 'FS')
assert_equal(fs.zero_suppression, 'leading')
assert_equal(fs.notation, 'absolute')
assert_equal(fs.format, (2, 7))
stmt = {'param': 'FS', 'zero': 'T', 'notation': 'I', 'x': '27'}
fs = FSParamStmt.from_dict(stmt)
assert_equal(fs.param, 'FS')
assert_equal(fs.zero_suppression, 'trailing')
assert_equal(fs.notation, 'incremental')
assert_equal(fs.format, (2, 7))
def test_FSParamStmt_dump():
""" Test FSParamStmt to_gerber()
"""
stmt = {'param': 'FS', 'zero': 'L', 'notation': 'A', 'x': '27'}
fs = FSParamStmt.from_dict(stmt)
assert_equal(fs.to_gerber(), '%FSLAX27Y27*%')
stmt = {'param': 'FS', 'zero': 'T', 'notation': 'I', 'x': '25'}
fs = FSParamStmt.from_dict(stmt)
assert_equal(fs.to_gerber(), '%FSTIX25Y25*%')
def test_MOParamStmt_factory():
""" Test MOParamStruct factory correctly handles parameters
"""
stmt = {'param': 'MO', 'mo': 'IN'}
mo = MOParamStmt.from_dict(stmt)
assert_equal(mo.param, 'MO')
assert_equal(mo.mode, 'inch')
stmt = {'param': 'MO', 'mo': 'MM'}
mo = MOParamStmt.from_dict(stmt)
assert_equal(mo.param, 'MO')
assert_equal(mo.mode, 'metric')
def test_MOParamStmt_dump():
""" Test MOParamStmt to_gerber()
"""
stmt = {'param': 'MO', 'mo': 'IN'}
mo = MOParamStmt.from_dict(stmt)
assert_equal(mo.to_gerber(), '%MOIN*%')
stmt = {'param': 'MO', 'mo': 'MM'}
mo = MOParamStmt.from_dict(stmt)
assert_equal(mo.to_gerber(), '%MOMM*%')
def test_IPParamStmt_factory():
""" Test IPParamStruct factory correctly handles parameters
"""
stmt = {'param': 'IP', 'ip': 'POS'}
ip = IPParamStmt.from_dict(stmt)
assert_equal(ip.ip, 'positive')
stmt = {'param': 'IP', 'ip': 'NEG'}
ip = IPParamStmt.from_dict(stmt)
assert_equal(ip.ip, 'negative')
def test_IPParamStmt_dump():
""" Test IPParamStmt to_gerber()
"""
stmt = {'param': 'IP', 'ip': 'POS'}
ip = IPParamStmt.from_dict(stmt)
assert_equal(ip.to_gerber(), '%IPPOS*%')
stmt = {'param': 'IP', 'ip': 'NEG'}
ip = IPParamStmt.from_dict(stmt)
assert_equal(ip.to_gerber(), '%IPNEG*%')

18
gerber/tests/tests.py Normal file
View file

@ -0,0 +1,18 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Hamilton Kibbe <ham@hamiltonkib.be>
from nose.tools import assert_in
from nose.tools import assert_not_in
from nose.tools import assert_equal
from nose.tools import assert_not_equal
from nose.tools import assert_true
from nose.tools import assert_false
from nose.tools import assert_raises
from nose.tools import raises
from nose import with_setup
__all__ = ['assert_in', 'assert_not_in', 'assert_equal', 'assert_not_equal',
'assert_true', 'assert_false', 'assert_raises', 'raises',
'with_setup' ]

View file

@ -1,8 +1,11 @@
## The following requirements were added by pip --freeze:
Jinja2==2.7.3
MarkupSafe==0.23
Pygments==1.6
Sphinx==1.2.3
coverage==3.7.1
docutils==0.12
nose==1.3.4
pyparsing==2.0.2
svgwrite==1.1.6
wsgiref==0.1.2