sketchingpy.test.test_pillow_struct
1import unittest 2 3import PIL.Image 4import PIL.ImageDraw 5 6import sketchingpy.const 7import sketchingpy.pillow_struct 8import sketchingpy.transform 9 10 11class PillowStructTests(unittest.TestCase): 12 13 def setUp(self): 14 image = PIL.Image.new('RGBA', (1, 1), (255, 255, 255, 0)) 15 drawable = PIL.ImageDraw.Draw(image, 'RGBA') 16 self._example_image = sketchingpy.pillow_struct.WritableImage(image, drawable) 17 18 def test_rect(self): 19 rect = sketchingpy.pillow_struct.Rect(1, 2, 4, 8) 20 rect.set_center_x(10) 21 rect.set_center_y(20) 22 self.assertEqual(rect.get_x(), 8) 23 self.assertEqual(rect.get_y(), 16) 24 25 def test_transformed_writable(self): 26 writable = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 27 28 derived_1 = writable.get_with_offset(5, 10) 29 self.assertEqual(derived_1.get_x(), 15) 30 self.assertEqual(derived_1.get_y(), 30) 31 32 transformer = sketchingpy.transform.Transformer() 33 transformer.translate(5, 10) 34 35 derived_2 = writable.transform(transformer) 36 self.assertEqual(derived_2.get_x(), 15) 37 self.assertEqual(derived_2.get_y(), 30) 38 39 def test_transformed_line(self): 40 writable = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 41 42 derived_1 = writable.get_with_offset(5, 10) 43 self.assertEqual(derived_1._x1, 15) 44 self.assertEqual(derived_1._y1, 30) 45 46 transformer = sketchingpy.transform.Transformer() 47 transformer.translate(5, 10) 48 49 derived_2 = writable.transform(transformer) 50 self.assertEqual(derived_2._x1, 15) 51 self.assertEqual(derived_2._y1, 30) 52 53 def test_transformed_clear(self): 54 writable = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 55 56 derived_1 = writable.get_with_offset(5, 10) 57 self.assertIsNotNone(derived_1) 58 59 transformer = sketchingpy.transform.Transformer() 60 transformer.translate(5, 10) 61 62 derived_2 = writable.transform(transformer) 63 self.assertIsNotNone(derived_2) 64 65 def test_build_rect_with_mode_center(self): 66 output = sketchingpy.pillow_struct.build_rect_with_mode( 67 7, 68 8, 69 4, 70 4, 71 sketchingpy.const.CENTER 72 ) 73 self.assertEqual(output.get_x(), 5) 74 self.assertEqual(output.get_y(), 6) 75 self.assertEqual(output.get_width(), 4) 76 self.assertEqual(output.get_height(), 4) 77 78 def test_build_rect_with_mode_radius(self): 79 output = sketchingpy.pillow_struct.build_rect_with_mode( 80 7, 81 8, 82 4, 83 4, 84 sketchingpy.const.RADIUS 85 ) 86 self.assertEqual(output.get_x(), 3) 87 self.assertEqual(output.get_y(), 4) 88 self.assertEqual(output.get_width(), 8) 89 self.assertEqual(output.get_height(), 8) 90 91 def test_build_rect_with_mode_corner(self): 92 output = sketchingpy.pillow_struct.build_rect_with_mode( 93 7, 94 8, 95 4, 96 4, 97 sketchingpy.const.CORNER 98 ) 99 self.assertEqual(output.get_x(), 7) 100 self.assertEqual(output.get_y(), 8) 101 self.assertEqual(output.get_width(), 4) 102 self.assertEqual(output.get_height(), 4) 103 104 105 def test_build_rect_with_mode_corners(self): 106 output = sketchingpy.pillow_struct.build_rect_with_mode( 107 7, 108 8, 109 7 + 4, 110 8 + 4, 111 sketchingpy.const.CORNERS 112 ) 113 self.assertEqual(output.get_x(), 7) 114 self.assertEqual(output.get_y(), 8) 115 self.assertEqual(output.get_width(), 4) 116 self.assertEqual(output.get_height(), 4) 117 118 def test_macro(self): 119 macro = sketchingpy.pillow_struct.Macro(5, 10) 120 self.assertEqual(macro.get_width(), 5) 121 self.assertEqual(macro.get_height(), 10) 122 123 writable_1 = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 124 writable_2 = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 125 writable_3 = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 126 127 macro.append(writable_1) 128 macro.append(writable_2) 129 macro.append(writable_3) 130 131 self.assertEqual(len(macro.get()), 3) 132 133 def test_zero_rect(self): 134 rect = sketchingpy.pillow_struct.Rect(1, 2, 4, 8) 135 self.assertEqual(rect.get_x(), 1) 136 self.assertEqual(rect.get_y(), 2) 137 self.assertEqual(rect.get_width(), 4) 138 self.assertEqual(rect.get_height(), 8) 139 140 zeroed = sketchingpy.pillow_struct.zero_rect(rect) 141 self.assertEqual(zeroed.get_x(), 0) 142 self.assertEqual(zeroed.get_y(), 0) 143 self.assertEqual(zeroed.get_width(), 4) 144 self.assertEqual(zeroed.get_height(), 8) 145 146 def test_get_transformed(self): 147 transformer = sketchingpy.transform.Transformer() 148 transformer.translate(5, 10) 149 transformed = sketchingpy.pillow_struct.get_transformed( 150 transformer, 151 self._example_image.get_image(), 152 20, 153 30 154 ) 155 self.assertEqual(transformed.get_x(), 25) 156 self.assertEqual(transformed.get_y(), 40)
12class PillowStructTests(unittest.TestCase): 13 14 def setUp(self): 15 image = PIL.Image.new('RGBA', (1, 1), (255, 255, 255, 0)) 16 drawable = PIL.ImageDraw.Draw(image, 'RGBA') 17 self._example_image = sketchingpy.pillow_struct.WritableImage(image, drawable) 18 19 def test_rect(self): 20 rect = sketchingpy.pillow_struct.Rect(1, 2, 4, 8) 21 rect.set_center_x(10) 22 rect.set_center_y(20) 23 self.assertEqual(rect.get_x(), 8) 24 self.assertEqual(rect.get_y(), 16) 25 26 def test_transformed_writable(self): 27 writable = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 28 29 derived_1 = writable.get_with_offset(5, 10) 30 self.assertEqual(derived_1.get_x(), 15) 31 self.assertEqual(derived_1.get_y(), 30) 32 33 transformer = sketchingpy.transform.Transformer() 34 transformer.translate(5, 10) 35 36 derived_2 = writable.transform(transformer) 37 self.assertEqual(derived_2.get_x(), 15) 38 self.assertEqual(derived_2.get_y(), 30) 39 40 def test_transformed_line(self): 41 writable = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 42 43 derived_1 = writable.get_with_offset(5, 10) 44 self.assertEqual(derived_1._x1, 15) 45 self.assertEqual(derived_1._y1, 30) 46 47 transformer = sketchingpy.transform.Transformer() 48 transformer.translate(5, 10) 49 50 derived_2 = writable.transform(transformer) 51 self.assertEqual(derived_2._x1, 15) 52 self.assertEqual(derived_2._y1, 30) 53 54 def test_transformed_clear(self): 55 writable = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 56 57 derived_1 = writable.get_with_offset(5, 10) 58 self.assertIsNotNone(derived_1) 59 60 transformer = sketchingpy.transform.Transformer() 61 transformer.translate(5, 10) 62 63 derived_2 = writable.transform(transformer) 64 self.assertIsNotNone(derived_2) 65 66 def test_build_rect_with_mode_center(self): 67 output = sketchingpy.pillow_struct.build_rect_with_mode( 68 7, 69 8, 70 4, 71 4, 72 sketchingpy.const.CENTER 73 ) 74 self.assertEqual(output.get_x(), 5) 75 self.assertEqual(output.get_y(), 6) 76 self.assertEqual(output.get_width(), 4) 77 self.assertEqual(output.get_height(), 4) 78 79 def test_build_rect_with_mode_radius(self): 80 output = sketchingpy.pillow_struct.build_rect_with_mode( 81 7, 82 8, 83 4, 84 4, 85 sketchingpy.const.RADIUS 86 ) 87 self.assertEqual(output.get_x(), 3) 88 self.assertEqual(output.get_y(), 4) 89 self.assertEqual(output.get_width(), 8) 90 self.assertEqual(output.get_height(), 8) 91 92 def test_build_rect_with_mode_corner(self): 93 output = sketchingpy.pillow_struct.build_rect_with_mode( 94 7, 95 8, 96 4, 97 4, 98 sketchingpy.const.CORNER 99 ) 100 self.assertEqual(output.get_x(), 7) 101 self.assertEqual(output.get_y(), 8) 102 self.assertEqual(output.get_width(), 4) 103 self.assertEqual(output.get_height(), 4) 104 105 106 def test_build_rect_with_mode_corners(self): 107 output = sketchingpy.pillow_struct.build_rect_with_mode( 108 7, 109 8, 110 7 + 4, 111 8 + 4, 112 sketchingpy.const.CORNERS 113 ) 114 self.assertEqual(output.get_x(), 7) 115 self.assertEqual(output.get_y(), 8) 116 self.assertEqual(output.get_width(), 4) 117 self.assertEqual(output.get_height(), 4) 118 119 def test_macro(self): 120 macro = sketchingpy.pillow_struct.Macro(5, 10) 121 self.assertEqual(macro.get_width(), 5) 122 self.assertEqual(macro.get_height(), 10) 123 124 writable_1 = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 125 writable_2 = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 126 writable_3 = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 127 128 macro.append(writable_1) 129 macro.append(writable_2) 130 macro.append(writable_3) 131 132 self.assertEqual(len(macro.get()), 3) 133 134 def test_zero_rect(self): 135 rect = sketchingpy.pillow_struct.Rect(1, 2, 4, 8) 136 self.assertEqual(rect.get_x(), 1) 137 self.assertEqual(rect.get_y(), 2) 138 self.assertEqual(rect.get_width(), 4) 139 self.assertEqual(rect.get_height(), 8) 140 141 zeroed = sketchingpy.pillow_struct.zero_rect(rect) 142 self.assertEqual(zeroed.get_x(), 0) 143 self.assertEqual(zeroed.get_y(), 0) 144 self.assertEqual(zeroed.get_width(), 4) 145 self.assertEqual(zeroed.get_height(), 8) 146 147 def test_get_transformed(self): 148 transformer = sketchingpy.transform.Transformer() 149 transformer.translate(5, 10) 150 transformed = sketchingpy.pillow_struct.get_transformed( 151 transformer, 152 self._example_image.get_image(), 153 20, 154 30 155 ) 156 self.assertEqual(transformed.get_x(), 25) 157 self.assertEqual(transformed.get_y(), 40)
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
14 def setUp(self): 15 image = PIL.Image.new('RGBA', (1, 1), (255, 255, 255, 0)) 16 drawable = PIL.ImageDraw.Draw(image, 'RGBA') 17 self._example_image = sketchingpy.pillow_struct.WritableImage(image, drawable)
Hook method for setting up the test fixture before exercising it.
26 def test_transformed_writable(self): 27 writable = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 28 29 derived_1 = writable.get_with_offset(5, 10) 30 self.assertEqual(derived_1.get_x(), 15) 31 self.assertEqual(derived_1.get_y(), 30) 32 33 transformer = sketchingpy.transform.Transformer() 34 transformer.translate(5, 10) 35 36 derived_2 = writable.transform(transformer) 37 self.assertEqual(derived_2.get_x(), 15) 38 self.assertEqual(derived_2.get_y(), 30)
40 def test_transformed_line(self): 41 writable = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 42 43 derived_1 = writable.get_with_offset(5, 10) 44 self.assertEqual(derived_1._x1, 15) 45 self.assertEqual(derived_1._y1, 30) 46 47 transformer = sketchingpy.transform.Transformer() 48 transformer.translate(5, 10) 49 50 derived_2 = writable.transform(transformer) 51 self.assertEqual(derived_2._x1, 15) 52 self.assertEqual(derived_2._y1, 30)
54 def test_transformed_clear(self): 55 writable = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 56 57 derived_1 = writable.get_with_offset(5, 10) 58 self.assertIsNotNone(derived_1) 59 60 transformer = sketchingpy.transform.Transformer() 61 transformer.translate(5, 10) 62 63 derived_2 = writable.transform(transformer) 64 self.assertIsNotNone(derived_2)
66 def test_build_rect_with_mode_center(self): 67 output = sketchingpy.pillow_struct.build_rect_with_mode( 68 7, 69 8, 70 4, 71 4, 72 sketchingpy.const.CENTER 73 ) 74 self.assertEqual(output.get_x(), 5) 75 self.assertEqual(output.get_y(), 6) 76 self.assertEqual(output.get_width(), 4) 77 self.assertEqual(output.get_height(), 4)
79 def test_build_rect_with_mode_radius(self): 80 output = sketchingpy.pillow_struct.build_rect_with_mode( 81 7, 82 8, 83 4, 84 4, 85 sketchingpy.const.RADIUS 86 ) 87 self.assertEqual(output.get_x(), 3) 88 self.assertEqual(output.get_y(), 4) 89 self.assertEqual(output.get_width(), 8) 90 self.assertEqual(output.get_height(), 8)
92 def test_build_rect_with_mode_corner(self): 93 output = sketchingpy.pillow_struct.build_rect_with_mode( 94 7, 95 8, 96 4, 97 4, 98 sketchingpy.const.CORNER 99 ) 100 self.assertEqual(output.get_x(), 7) 101 self.assertEqual(output.get_y(), 8) 102 self.assertEqual(output.get_width(), 4) 103 self.assertEqual(output.get_height(), 4)
106 def test_build_rect_with_mode_corners(self): 107 output = sketchingpy.pillow_struct.build_rect_with_mode( 108 7, 109 8, 110 7 + 4, 111 8 + 4, 112 sketchingpy.const.CORNERS 113 ) 114 self.assertEqual(output.get_x(), 7) 115 self.assertEqual(output.get_y(), 8) 116 self.assertEqual(output.get_width(), 4) 117 self.assertEqual(output.get_height(), 4)
119 def test_macro(self): 120 macro = sketchingpy.pillow_struct.Macro(5, 10) 121 self.assertEqual(macro.get_width(), 5) 122 self.assertEqual(macro.get_height(), 10) 123 124 writable_1 = sketchingpy.pillow_struct.TransformedWritable(self._example_image, 10, 20) 125 writable_2 = sketchingpy.pillow_struct.TransformedLine(10, 20, 30, 40, '#C0C0C0', 1) 126 writable_3 = sketchingpy.pillow_struct.TransformedClear('#C0C0C0') 127 128 macro.append(writable_1) 129 macro.append(writable_2) 130 macro.append(writable_3) 131 132 self.assertEqual(len(macro.get()), 3)
134 def test_zero_rect(self): 135 rect = sketchingpy.pillow_struct.Rect(1, 2, 4, 8) 136 self.assertEqual(rect.get_x(), 1) 137 self.assertEqual(rect.get_y(), 2) 138 self.assertEqual(rect.get_width(), 4) 139 self.assertEqual(rect.get_height(), 8) 140 141 zeroed = sketchingpy.pillow_struct.zero_rect(rect) 142 self.assertEqual(zeroed.get_x(), 0) 143 self.assertEqual(zeroed.get_y(), 0) 144 self.assertEqual(zeroed.get_width(), 4) 145 self.assertEqual(zeroed.get_height(), 8)
147 def test_get_transformed(self): 148 transformer = sketchingpy.transform.Transformer() 149 transformer.translate(5, 10) 150 transformed = sketchingpy.pillow_struct.get_transformed( 151 transformer, 152 self._example_image.get_image(), 153 20, 154 30 155 ) 156 self.assertEqual(transformed.get_x(), 25) 157 self.assertEqual(transformed.get_y(), 40)
Inherited Members
- unittest.case.TestCase
- TestCase
- failureException
- longMessage
- maxDiff
- addTypeEqualityFunc
- addCleanup
- addClassCleanup
- tearDown
- setUpClass
- tearDownClass
- countTestCases
- defaultTestResult
- shortDescription
- id
- subTest
- run
- doCleanups
- doClassCleanups
- debug
- skipTest
- fail
- assertFalse
- assertTrue
- assertRaises
- assertWarns
- assertLogs
- assertNoLogs
- assertEqual
- assertNotEqual
- assertAlmostEqual
- assertNotAlmostEqual
- assertSequenceEqual
- assertListEqual
- assertTupleEqual
- assertSetEqual
- assertIn
- assertNotIn
- assertIs
- assertIsNot
- assertDictEqual
- assertDictContainsSubset
- assertCountEqual
- assertMultiLineEqual
- assertLess
- assertLessEqual
- assertGreater
- assertGreaterEqual
- assertIsNone
- assertIsNotNone
- assertIsInstance
- assertNotIsInstance
- assertRaisesRegex
- assertWarnsRegex
- assertRegex
- assertNotRegex
- failUnlessRaises
- failIf
- assertRaisesRegexp
- assertRegexpMatches
- assertNotRegexpMatches
- failUnlessEqual
- assertEquals
- failIfEqual
- assertNotEquals
- failUnlessAlmostEqual
- assertAlmostEquals
- failIfAlmostEqual
- assertNotAlmostEquals
- failUnless
- assert_