|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from bridge import DrawingAPI1, DrawingAPI2, CircleShape |
| 5 | +from sys import version_info |
| 6 | + |
| 7 | +if version_info < (2, 7): |
| 8 | + import unittest2 as unittest |
| 9 | +else: |
| 10 | + import unittest |
| 11 | + |
| 12 | +from unittest.mock import patch |
| 13 | + |
| 14 | +class BridgeTest(unittest.TestCase): |
| 15 | + |
| 16 | + def test_bridge_shall_draw_with_concrete_implementation(cls): |
| 17 | + ci1 = DrawingAPI1() |
| 18 | + ci2 = DrawingAPI2() |
| 19 | + with patch.object(ci1, 'draw_circle') as mock_ci1_draw_circle,\ |
| 20 | + patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle: |
| 21 | + sh1 = CircleShape(1, 2, 3, ci1) |
| 22 | + sh1.draw() |
| 23 | + cls.assertEqual(mock_ci1_draw_circle.call_count, 1) |
| 24 | + sh2 = CircleShape(1, 2, 3, ci2) |
| 25 | + sh2.draw() |
| 26 | + cls.assertEqual(mock_ci2_draw_circle.call_count, 1) |
| 27 | + |
| 28 | + def test_bridge_shall_scale_with_own_implementation(cls): |
| 29 | + ci = DrawingAPI1() |
| 30 | + sh = CircleShape(1, 2, 3, ci) |
| 31 | + sh.scale(2) |
| 32 | + cls.assertEqual(sh._radius, 6) |
| 33 | + with patch.object(sh, 'scale') as mock_sh_scale_circle: |
| 34 | + sh.scale(2) |
| 35 | + cls.assertEqual(mock_sh_scale_circle.call_count, 1) |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + unittest.main() |
| 39 | + |
0 commit comments