Commits

Manfred Moitzi  committed d14bce3

added tests for 100% test coverage

  • Participants
  • Parent commits dd8947d

Comments (0)

Files changed (15)

File svgwrite/data/typechecker.py

         return False
 
     def is_integer(self, value):
+        if isinstance(value, float):
+            return False
         try:
             number = int(value)
             return True

File svgwrite/utils.py

     elif horiz == 'right':
         x = x - width
     elif horiz != 'left':
-        ValueError("Invalid horizontal position: '%s'" % horiz)
+        raise ValueError("Invalid horizontal position: '%s'" % horiz)
 
     if vert == 'middle':
         y = y - height / 2.
     elif vert == 'bottom':
         y = y - height
     elif vert != 'top':
-        ValueError("Invalid vertical position: '%s'" % vert)
+        raise ValueError("Invalid vertical position: '%s'" % vert)
 
     if xunit:
         x = "%s%s" %(x, xunit)

File tests/test_base_element.py

 
         # value out of range
         self.assertRaises(TypeError, element.value_to_string, 100000)
+        self.assertEqual('3.1416', element.value_to_string(3.141592))
 
     def test_is_unicode(self):
         element = MockBase()

File tests/test_drawing.py

 # License: GPLv3
 
 import sys
+import os
+
 import unittest
 from StringIO import StringIO
 
             'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
             'xmlns:xlink="http://www.w3.org/1999/xlink"><defs /></svg>')
 
+    def test_save(self):
+        fn = 'test_drawing.svg'
+        if os.path.exists(fn):
+            os.remove(fn)
+        dwg = Drawing(fn)
+        dwg.save()
+        self.assertTrue(os.path.exists(fn))
+        os.remove(fn)
+
+    def test_save_as(self):
+        fn = 'test_drawing.svg'
+        if os.path.exists(fn):
+            os.remove(fn)
+        dwg = Drawing()
+        dwg.saveas(fn)
+        self.assertTrue(os.path.exists(fn))
+        os.remove(fn)
+
 class TestDrawingTinyProfile(unittest.TestCase):
     def test_empty_drawing(self):
         dwg = Drawing(profile="tiny")

File tests/test_elementfactory.py

         element = self.factory.a('link')
         self.assertEqual(element.elementname, 'a')
 
+    def test_line(self):
+        element = self.factory.line((0,0), (1,1))
+        self.assertEqual(element.elementname, 'line')
+
+    def test_rect(self):
+        element = self.factory.rect((0,0), (1,1))
+        self.assertEqual(element.elementname, 'rect')
+
+    def test_circle(self):
+        element = self.factory.circle((0,0), 5)
+        self.assertEqual(element.elementname, 'circle')
+
+    def test_ellipse(self):
+        element = self.factory.ellipse((0,0), (5, 5))
+        self.assertEqual(element.elementname, 'ellipse')
+
+    def test_polygon(self):
+        element = self.factory.polygon([(0, 0), (5, 5)])
+        self.assertEqual(element.elementname, 'polygon')
+
+    def test_polyline(self):
+        element = self.factory.polyline([(0, 0), (5, 5)])
+        self.assertEqual(element.elementname, 'polyline')
+
+    def test_AttributeError(self):
+        try:
+            self.factory.test()
+            self.fail('AttributeError not raised.')
+        except AttributeError:
+            self.assertTrue(True)
+
 if __name__=='__main__':
     unittest.main()

File tests/test_full11_typechecker.py

     def setUp(self):
         self.checker = Full11TypeChecker()
 
+    def test_version(self):
+        self.assertEqual(('1.1', 'full'), self.checker.get_version())
+
     def test_is_anything(self):
         """ Everything is valid. """
         self.assertTrue(self.checker.is_anything('abcdef  :::\n \r \t all is valid äüß'))
     def test_is_integer(self):
         """ Integer also as String '100'. """
         # big numbers only valid for full profile
-        self.assertTrue(self.checker.is_number(100000))
-        self.assertTrue(self.checker.is_number(-100000))
-        self.assertTrue(self.checker.is_number('100000'))
-        self.assertTrue(self.checker.is_number('-100000'))
+        self.assertTrue(self.checker.is_integer(100000))
+        self.assertTrue(self.checker.is_integer(-100000))
+        self.assertTrue(self.checker.is_integer('100000'))
+        self.assertTrue(self.checker.is_integer('-100000'))
     def test_is_not_integer(self):
-        self.assertFalse(self.checker.is_number( (1,2) ))
-        self.assertFalse(self.checker.is_number('manfred'))
-        self.assertFalse(self.checker.is_number( dict(a=1, b=2) ))
-        self.assertTrue(self.checker.is_number(3.141592))
-        self.assertTrue(self.checker.is_number('3.141592'))
+        self.assertFalse(self.checker.is_integer( (1,2) ))
+        self.assertFalse(self.checker.is_integer('manfred'))
+        self.assertFalse(self.checker.is_integer( dict(a=1, b=2) ))
+        self.assertFalse(self.checker.is_integer(3.141592))
+        self.assertFalse(self.checker.is_integer('3.141592'))
 
     def test_is_percentage(self):
         self.assertTrue(self.checker.is_percentage(100))
     def test_is_four_numbers(self):
         self.assertTrue(self.checker.is_four_numbers(' 1, 2, 3, 4 '))
         self.assertTrue(self.checker.is_four_numbers(' 1  2 3  4 '))
+        self.assertTrue(self.checker.is_four_numbers((1,2,3,4)))
     def test_is_not_four_numbers(self):
         self.assertFalse(self.checker.is_four_numbers(' 1, 2, 3, '))
         self.assertFalse(self.checker.is_four_numbers(' 1, 2 '))
         self.assertFalse(self.checker.is_four_numbers(' 1 '))
+        self.assertFalse(self.checker.is_four_numbers((1,2,3)))
 
 if __name__=='__main__' :
     unittest.main()

File tests/test_image.py

         image = Image("http://localhost/test.jpg")
         self.assertEqual(image.tostring(), '<image xlink:href="http://localhost/test.jpg" />')
 
+    def test_constructor2(self):
+        image = Image("test.jpg", insert=(10,20), size=(30,40))
+        self.assertEqual(image.tostring(), '<image height="40" width="30" x="10" xlink:href="test.jpg" y="20" />')
+
     def test_errors(self):
         self.assertRaises(TypeError, Image, 1)
         self.assertRaises(TypeError, Image, 3.1415)

File tests/test_polyline.py

         self.assertTrue(isinstance(result, unicode))
         self.assertEqual(result, u"10,10 20cm,20em 30ex,30in 40mm,40pc 50pt,50px 60%,60%")
 
+    def test_float_points(self):
+        result = self.polyline.points_to_string([(10.12345,10.12345),(3.14151, 3.14151)])
+        self.assertEqual(result, u"10.1235,10.1235 3.1415,3.1415")
+
     def test_value_range(self):
         # invalid unit #'p'
         self.assertRaises(TypeError, self.polyline.points_to_string, [(100000,10)])

File tests/test_svg.py

 
 class TestSVG(unittest.TestCase):
     def test_constructor(self):
-        svg = SVG()
+        svg = SVG(insert=(10,20), size=(100,200))
         self.assertTrue(isinstance(svg, Symbol))
-        self.assertEqual(svg.tostring(), "<svg><defs /></svg>")
+        self.assertEqual(svg.tostring(), '<svg height="200" width="100" x="10" y="20"><defs /></svg>')
 
     def test_add_svg_as_subelement(self):
         svg = SVG(id='svg')

File tests/test_svgattributes.py

             }
         )
 
+    def test_no_default(self):
+        ma = SVGMultiAttribute({
+            'a': SVGAttribute('x',
+                              anim=True,
+                              types=frozenset([u'coordinate']),
+                              const=frozenset(['star'])),
+            'b': SVGAttribute('x',
+                              anim=False,
+                              types=frozenset([u'list-of-coordinate']),
+                              const=frozenset(['text']))
+            }
+        )
+        self.assertTrue('coordinate' in ma.get_types('c'))
+
     def test_get_types(self):
         coordinate = frozenset(['coordinate'])
         list_of_coordinates = frozenset(['list-of-coordinate'])

File tests/test_textarea.py

         textarea = TextArea('line1\n \nline2\n', profile='tiny')
         self.assertEqual(textarea.tostring(), '<textArea><tspan>line1</tspan><tbreak />' \
                          '<tspan> </tspan><tbreak /><tspan>line2</tspan><tbreak /></textArea>')
+    def test_line_increment(self):
+        textarea = TextArea('line1\n', profile='tiny')
+        textarea.line_increment('14')
+        self.assertEqual(textarea.tostring(), '<textArea line-increment="14"><tspan>line1</tspan><tbreak /></textArea>')
 
     def test_write_lines_prepending_linebreak(self):
         textarea = TextArea('\nline1\n', profile='tiny')

File tests/test_transformlistparser.py

         self.assertFalse(TransformListParser.is_valid("skewX(15),, skewY(15)"))
         self.assertFalse(TransformListParser.is_valid("skewX(15), skewY(15) ,"))
 
+    def test_parse(self):
+        self.assertRaises(NotImplementedError, TransformListParser.parse, 'test')
+
 if __name__=='__main__':
     unittest.main()

File tests/test_use.py

         self.assertTrue(isinstance(use, IXLink))
         self.assertEqual(use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />')
 
+    def test_constructor2(self):
+        use = Use('an_id', insert=(10, 20), size=(100, 200))
+        self.assertTrue(isinstance(use, ITransform))
+        self.assertTrue(isinstance(use, IXLink))
+        self.assertEqual(use.tostring(), '<use height="200" width="100" x="10" xlink:href="#an_id" y="20" />')
+
     def test_object_link(self):
         g = Group(id='test')
         use = Use(g)

File tests/test_utils.py

 import sys
 import unittest
 
-from svgwrite.utils import rgb, AutoID
+from svgwrite.utils import rgb, AutoID, get_unit
 from svgwrite.utils import strlist, rect_top_left_corner
 from svgwrite.utils import split_angle, split_coordinate
+from svgwrite import cm
 
 class TestRgb(unittest.TestCase):
     def test_rgb_8bit(self):
         res = rect_top_left_corner(insert=('10in','10in'), size=('10in','10in'), pos='bottom-center')
         self.assertEqual(res, ('5.0in','0.0in'))
 
-    def test_ivalid_units(self):
+    def test_invalid_units(self):
         # insert and size has to have the same units
         self.assertRaises(ValueError, rect_top_left_corner, insert=('10cm','10cm'), size=(10,10), pos='middle-center')
         self.assertRaises(ValueError, rect_top_left_corner, insert=('10mm','10mm'), size=('10cm','10cm'), pos='middle-center')
+        self.assertRaises(ValueError, rect_top_left_corner, insert=('10mm','10mm'), size=('10mm','10cm'), pos='middle-center')
+
+    def test_invalid_pos(self):
+        # insert and size has to have the same units
+        self.assertRaises(ValueError, rect_top_left_corner, insert=(1, 1), size=(1, 1), pos='middle-mitte')
+        self.assertRaises(ValueError, rect_top_left_corner, insert=(1, 1), size=(1, 1), pos='mitte-center')
 
 class TestSplitCoordinate(unittest.TestCase):
     def test_int_coordinates(self):
         self.assertEqual(res, (10.7, 'grad'))
 
     def test_invalid_str_angle(self):
-        self.assertRaises(ValueError, split_coordinate, '100km')
-        self.assertRaises(ValueError, split_coordinate, '100ccm')
-        self.assertRaises(ValueError, split_coordinate, '10,0deg')
-        self.assertRaises(ValueError, split_coordinate, '1.0.0deg')
+        self.assertRaises(ValueError, split_angle, '100km')
+        self.assertRaises(ValueError, split_angle, '100ccm')
+        self.assertRaises(ValueError, split_angle, '10,0deg')
+        self.assertRaises(ValueError, split_angle, '1.0.0deg')
 
 class TestAutoID(unittest.TestCase):
     def test_nextid(self):
         self.assertEqual('id7', AutoID.nextid(7))
         self.assertEqual('id8', AutoID.nextid())
 
+class TestGetUnit(unittest.TestCase):
+    def test_number(self):
+        self.assertEqual(None, get_unit(1))
+        self.assertEqual(None, get_unit(1.0))
+
+    def test_valid_units(self):
+        self.assertEqual('cm', get_unit('1cm'))
+        self.assertEqual('mm', get_unit('3.1415mm'))
+        self.assertEqual('%', get_unit('300%'))
+
+    def test_invalid_units(self):
+        self.assertRaises(ValueError, get_unit, '1m')
+
+class TestUnit(unittest.TestCase):
+    def test_cm(self):
+        self.assertEqual('5cm', 5*cm)
+
+    def test_call_cm(self):
+        self.assertEqual('5cm,7cm', cm(5, 7))
+
 if __name__=='__main__':
     unittest.main()

File tests/test_validator2.py

 from svgwrite.validator2 import get_validator
 
 class TestGetCoordinate(unittest.TestCase):
+    def test_invalid_profile(self):
+        self.assertRaises(ValueError, get_validator, profile='invalid')
+
+    def test_get_none_coordinate(self):
+        validator = get_validator('tiny', debug=True)
+        self.assertRaises(TypeError, validator.get_coordinate, None)
+
     def test_valid_units(self):
         validator = get_validator('tiny', debug=True)
         for value, number, unit in [(' 100px ', 100, 'px'),
             self.assertEqual(number2, number)
             self.assertEqual(unit2, unit)
 
-
     def test_not_valid_numbers(self):
         validator = get_validator('tiny', debug=True)
         for value in (' 1s00in ', ' 1s00mm ', ' 1s00% '):
         for value in (100000, '100000', -100000, '-100000'):
             validator.get_coordinate(value)
 
+    def test_valid_elementname(self):
+        validator = get_validator('full', debug=True)
+        self.assertTrue(validator.is_valid_elementname('text'))
+
+    def test_invalid_elementname(self):
+        validator = get_validator('full', debug=True)
+        self.assertFalse(validator.is_valid_elementname('textArea'))
+
+    def test_valid_children(self):
+        validator = get_validator('full', debug=True)
+        self.assertTrue(validator.is_valid_children('text', 'tspan'))
+
+    def test_invalid_children(self):
+        validator = get_validator('full', debug=True)
+        self.assertFalse(validator.is_valid_children('text', 'line'))
+
+    def test_check_invalid_children(self):
+        validator = get_validator('full', debug=True)
+        self.assertRaises(ValueError, validator.check_valid_children, 'text', 'line')
+
 class TestCheckCoordinate(unittest.TestCase):
     def test_valid_units(self):
         validator = get_validator('tiny', debug=True)