def test_swell_or_shrink_repeatedly(): """ Tests the swell_or_shrink_repeatedly method. """ p1_test.test_swell_or_shrink_once() # This runs OUR tests random.seed(999) # Lets us determine the results of the randomness # This is a VISUAL test. # Construct 1 Animated_Circle object, printing and drawing it. title = 'Testing: the swell_or_shrink_repeatedly method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle( 200, 150, 30, 'blue', ('blue', 'yellow', 'green', 'aqua', 'brown')) print(animated_circle1) animated_circle1.draw(""" A BLUE circle at (200, 150) with radius 30 and thickness 1.""") # Apply the swell_or_shrink_repeatedly method. print('\nAfter the first swell_or_shrink_repeatedly call:') animated_circle1.animation_factor = 0.25 # faster animation animated_circle1.swell_or_shrink_repeatedly(50, 10) print(animated_circle1) animated_circle1.draw(""" The circle should have swelled/shrunk by 50 10 times, changing color and thickness each time. It should end with the radius that it began (30), GREEN with thickness 10.""")
def test_swell_to_prime(): """ Tests the swell_to_prime method. """ p1_test.test_swell_to_prime() # This runs OUR tests. # This is a VISUAL test. # Construct 2 Animated_Circle objects, printing and drawing them. title = 'Testing: the swell_to_prime method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle(50, 25, 20, 'black', ('blue', 'green')) animated_circle1.draw() print(animated_circle1) animated_circle2 = Animated_Circle( 200, 100, 89, 'purple', ('yellow', 'magenta', 'blue', 'green', 'yellow', 'aqua')) print(animated_circle2) animated_circle2.draw(""" A BLACK circle at (50, 25) with radius 20, and a PURPLE circle at (200, 100) with radius 89.""") # Apply the swell_to_prime method and print/draw the results. print('\nAfter the first set of swell_to_prime calls:') animated_circle1.swell_to_prime() print(animated_circle1) animated_circle1.draw() animated_circle2.swell_to_prime() print(animated_circle2) animated_circle2.draw(""" The LEFT circle should have grown from radius 20 to radius 23. The RIGHT circle should have grown from radius 89 to radius 97. Both should be 3 pixels from the top of the window.""")
def test_init(): """ Tests the __init__ method of the Animated_Circle class. """ p1_test.test_init() # This runs OUR tests. # This is a VISUAL test. p1_test.start_drawing('Testing: the __init__ method') # Construct two Animated_Circle objects: animated_circle1 = Animated_Circle(100, 150, 100, 'blue', ('red', 'blue', 'green')) animated_circle2 = Animated_Circle(300, 50, 30, 'yellow', ('green', 'gold')) # Print and draw them: print('After construction:') print(animated_circle1, '\n', animated_circle2, '\n', sep='') animated_circle1.draw(0.5) animated_circle2.draw(""" A BLUE circle at (100, 150) with radius 100 and thickness 1, and a YELLOW circle at (250, 50) with radius 30 and thickness 1.""") # Change some of their characteristics, then print and redraw them: animated_circle1.circle.fill_color = animated_circle1.colors[2] animated_circle2.circle.outline_thickness = 10 print('After changing characteristics:') print(animated_circle1, '\n', animated_circle2, sep='') animated_circle1.draw() animated_circle2.draw(""" Now the leftmost (formerly BLUE) circle is GREEN and the YELLOW circle has a thicker outline.""")
def test_swell_or_shrink_once(): """ Tests the swell_or_shrink_once method. """ p1_test.test_swell_or_shrink_once() # This runs OUR tests random.seed(42) # Lets us determine the results of the randomness # This is a VISUAL test. # Construct 3 Animated_Circle objects, printing and drawing them. p1_test.start_drawing('Testing: the swell_or_shrink_once method') print('After construction:') animated_circle1 = Animated_Circle( 200, 150, 30, 'blue', ('blue', 'yellow', 'green', 'aqua', 'brown')) print(animated_circle1) animated_circle1.draw() animated_circle2 = Animated_Circle(400, 100, 50, 'red', ('green', )) print(animated_circle2) animated_circle2.draw() animated_circle3 = Animated_Circle(300, 200, 10, 'green', ('yellow', 'blue')) print(animated_circle3) animated_circle3.draw(""" A BLUE circle at (200, 150) with radius 30 and thickness 1, and a RED circle at (400, 100) with radius 50 and thickness 1, and a GREEN circle at (300, 200) with radius 10 and thickness 1.""") # For each of the three Animated_Circle objects, # apply the swell_or_shrink_once method, then redraw/reprint. print('\nAfter the first set of swell_or_shrink_once calls:') animated_circle1.swell_or_shrink_once(100) print(animated_circle1) animated_circle1.draw() animated_circle2.swell_or_shrink_once(-30) print(animated_circle2) animated_circle1.draw() animated_circle3.swell_or_shrink_once(40) print(animated_circle3) animated_circle3.draw(""" After the first swell_or_shrink, now: Left circle is bigger (radius 130), still BLUE but thickness 13, Right circle is smaller (radius 20), GREEN with thickness 3, Middle circle is bigger (radius 50), YELLOW with thickness 6.""")
def test_change_to_original_color(): """ Tests the change_to_original_color method. """ p1_test.test_change_to_original_color() # This runs OUR tests. random.seed(123) # Lets us determine the results of the randomness # This is a VISUAL test. # Construct 2 Animated_Circle objects, printing and drawing them. title = 'Testing: the change_to_original_color method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle(100, 100, 100, 'black', ('blue', 'green')) animated_circle1.draw() print(animated_circle1) animated_circle2 = Animated_Circle( 280, 100, 100, 'purple', ('yellow', 'magenta', 'blue', 'green', 'yellow', 'aqua')) print(animated_circle2) animated_circle2.draw(""" A BLACK circle at (100, 100) with radius 100, and a PURPLE circle at (280, 100) with radius 100. You will next see a bunch of colors, ending with BLACK and PURPLE again.""") # Flash through many color changes. Then apply the # change_to_original_color method and print/draw the results. print('\nAfter the first set of change_to_original_color calls:') for k in range(30): animated_circle1.change_color(k % 2) animated_circle1.draw(0.05) animated_circle2.change_color(k % 6) animated_circle2.draw(0.05) animated_circle1.change_to_original_color() print(animated_circle1) animated_circle1.draw() animated_circle2.change_to_original_color() print(animated_circle2) animated_circle2.draw(""" Should end as it started: BLACK and PURPLE.""")
def test_change_to_previous_color(): """ Tests the change_to_previous_color method. """ p1_test.test_change_to_previous_color() # This runs OUR tests. random.seed(4242) # Lets us determine the results of the randomness # This is a VISUAL test. # Construct 2 Animated_Circle objects, printing and drawing them. title = 'Testing: the change_to_previous_color method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle( 100, 100, 20, 'violet', ('dark red', 'green', 'tomato', 'blue', 'plum', 'seashell', 'red', 'peach puff', 'coral', 'chocolate', 'brown')) animated_circle1.draw() print(animated_circle1) animated_circle2 = Animated_Circle( 200, 200, 50, 'orange', ('yellow', 'magenta', 'blue', 'lavender', 'cyan', 'aqua', 'black', 'salmon', 'hot pink', 'forest green', 'spring green')) print(animated_circle2) animated_circle2.draw(""" A VIOLET circle at (100, 100) with radius 20, and an ORANGE circle at (200, 200) with radius 50. Next the circles will flash through swelling/shrinking along with color and thickness changes.""") # Apply: # -- swell_and_shrink_repeatedly() (so the colors change) # -- change_color() # -- swell_or_shrink_repeatedly() (so the colors change again) # -- change_to_previous_color() # The latter should have changed the color back to # . print('\nAfter various color changes (but none via change_color):') animated_circle1.animation_factor = 0.1 # Faster animations animated_circle2.animation_factor = 0.1 animated_circle1.swell_or_shrink_repeatedly(40, 16) animated_circle2.swell_or_shrink_repeatedly(-10, 12) animated_circle1.circle.fill_color = 'CHARTREUSE' print(animated_circle1) print(animated_circle2) animated_circle1.draw(""" A CHARTREUSE circle on the left and a BLACK circle on the right. The very next action will be a CHANGE_COLOR on the LEFT one.""") print('\nJust after the call to change_color:') animated_circle1.change_color(0) print(animated_circle1) print(animated_circle2) animated_circle1.draw() animated_circle2.draw(""" A DARK RED circle on the left and a BLACK circle on the right. Next, the circles will again flash through swelling/shrinking along with color and thickness changes.""") print('\nJust after various color changes:') animated_circle1.swell_or_shrink_repeatedly(80, 13) animated_circle2.swell_or_shrink_repeatedly(20, 11) print(animated_circle1) print(animated_circle2) animated_circle1.draw() animated_circle2.draw(""" A SEASHELL circle on the left and a FOREST GREEN circle on the right. The very next action will be a change_to_previous_color applied to the LEFT Animated_Circle. That should return the LEFT circle to CHARTREUSE.""") print('Just after animated_circle1.change_to_previous_color():') animated_circle2.draw(1) animated_circle1.change_to_previous_color() print(animated_circle1) print(animated_circle2) animated_circle1.draw(""" CHARTREUSE on the left, since that is what it was just BEFORE the most recent call to change_color on the LEFT. The RIGHT remains FOREST GREEN. Next will be a change_to_previous_color applied to the RIGHT Animated_Circle. That Animated_Circle has not yet had ANY calls to change_color so the RIGHT should become some arbitrary color (anything is OK, but the program should not crash).""") print('Just after animated_circle2.change_to_previous_color():') animated_circle1.draw(1) # To force a pause animated_circle2.change_to_previous_color() print(animated_circle1) print(animated_circle2) animated_circle1.draw(""" The LEFT remains CHARTREUSE. The RIGHT should have changed to some arbitrary color (anything is OK, but the program should not crash). Next we do more color changes (but none via change_color).""") print('Just after more color changes (but none via change_color):') animated_circle1.swell_or_shrink_repeatedly(30, 11) animated_circle2.swell_or_shrink_repeatedly(50, 6) animated_circle1.circle.fill_color = 'AQUAMARINE' print(animated_circle1) print(animated_circle2) animated_circle1.draw(""" An AQUAMARINE circle on the left and a HOT PINK circle on the right. Next we call change_color on both Animated_Circles, then various color changes on both (but none via change_color).""") print('\nJust after calling color_change on both') print('Animated_Circles, along with various additional') print('color changes (but none via color_change):') animated_circle1.change_color(random.randrange(11)) animated_circle2.change_color(random.randrange(11)) animated_circle1.swell_or_shrink_repeatedly(10, 4) animated_circle2.swell_or_shrink_repeatedly(-10, 8) print(animated_circle1) print(animated_circle2) animated_circle1.draw() animated_circle2.draw(""" A BROWN circle on the left and a BLUE circle on the right. Next, the calls to change_to_previous_color. They should change the LEFT back to AQUAMARINE and the RIGHT back to HOT PINK.""") print('Just after change_to_previous_color on') print('both Animated_Circles:') animated_circle1.animation_factor = 1 # Slow animations for finish animated_circle2.animation_factor = 1 animated_circle1.draw(1) # To force a pause animated_circle1.change_to_previous_color() print(animated_circle1) animated_circle1.draw(1) animated_circle2.change_to_previous_color() print(animated_circle2) animated_circle2.draw(""" The LEFT circle should now be AQUAMARINE. The RIGHT circle should now be HOT PINK. This concludes this test.""")
def test_change_color(): """ Tests the change_color method. """ p1_test.test_change_color() # This runs OUR tests. random.seed(77) # Lets us determine the results of the randomness # This is a VISUAL test. # Construct 2 Animated_Circle objects, printing and drawing them. title = 'Testing: the change_color method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle(100, 100, 70, 'black', ('blue', 'yellow')) animated_circle1.draw() print(animated_circle1) animated_circle2 = Animated_Circle( 350, 130, 50, 'purple', ('yellow', 'magenta', 'blue', 'green', 'yellow', 'aqua')) print(animated_circle2) animated_circle2.draw(""" A BLACK circle at (100, 100) with radius 70, and a PURPLE circle at (350, 130) with radius 50.""") # Apply the change_color method. Then print/draw the results. print('\nAfter the first set of change_color calls:') animated_circle1.change_color(1) print(animated_circle1) animated_circle1.draw() animated_circle2.change_color(5) print(animated_circle2) animated_circle2.draw(""" Same circles, but now YELLOW and AQUA. The next test will cycle the LEFT circle through: blue and yellow (repeating as needed) and the RIGHT circle through: yellow, magenta, blue, green, yellow, and aqua.""") # Another test: for k in range(6): animated_circle1.change_color(k % 2) animated_circle1.draw() animated_circle2.change_color(k) animated_circle2.draw() animated_circle1.draw(""" Should have finished with YELLOW and AQUA.""") # This tests change_color and swell_and_shrink_once # repeatedly. for k in range(20, 0, -1): animated_circle1.change_color(k % 2) animated_circle1.draw(0.05) animated_circle1.swell_or_shrink_once((-40 // k) * (k**-1)) animated_circle1.draw(0.05) animated_circle2.change_color(k % 6) animated_circle2.draw(0.05) animated_circle2.swell_or_shrink_once(50 // k) animated_circle1.draw(0.05) animated_circle1.draw(""" Should have ended with two YELLOW circles: a TINY one on the LEFT and a HUGE one on the RIGHT.""")
def test_swallow(): """ Tests the swallow method. """ p1_test.test_swallow() # This runs OUR tests. # This is a VISUAL test. # Construct 2 Animated_Circle objects, printing and drawing them. title = 'Testing: the swallow method' p1_test.start_drawing(title) print('After construction:') animated_circle1 = Animated_Circle(200, 150, 50, 'blue', ('blue', 'yellow', 'green')) animated_circle1.draw() print(animated_circle1) animated_circle2 = Animated_Circle(250, 180, 30, 'green', ('yellow', 'magenta')) print(animated_circle2) animated_circle2.draw(""" A BLUE circle at (200, 150) with radius 100, and a GREEN circle at (350, 200) with radius 70.""") # Apply the swallow method. Then print/draw the resulting # circle, drawing the original circles on top of it. print('\nAfter the first swallow call:') animated_circle3 = animated_circle1.swallow(animated_circle2) print(animated_circle3) animated_circle3.draw(""" The RED circle should be centered at (225, 165) with radius 80. It should completely cover the BLUE and GREEN circles.""") animated_circle4 = Animated_Circle(200, 150, 50, 'blue', ('blue', 'yellow', 'green')) animated_circle4.draw() animated_circle5 = Animated_Circle(250, 180, 30, 'green', ('yellow', 'magenta')) animated_circle5.draw(""" Here are the BLUE and GREEN circles again, on TOP of the RED circle. The RED should appear to BARELY cover the BLUE but be MORE THAN NECESSARY to cover the GREEN.""") # Test that the swallowing (red) Animated_Circle # has a colors attribute that is the CONCATENATION # of the colors attributes of the swallowed Animated_Circles. if animated_circle3.colors != (animated_circle4.colors + animated_circle5.colors): message = """The colors instance variable of the swallowing Animated_Circle (the one RETURNED by the swallow method) is wrong. It should be the CONCATENATION of the colors instance variables of the two SWALLOWED Animated Circle objects. For example, if animated_circle1.colors is: (blue', 'yellow', 'green') and if animated_circle2.colors is: ('yellow', 'magenta') then animated_circle1.swallow(animated_circle2) should be: ('blue', 'yellow', 'green', 'yellow', 'magenta') and animated_circle2.swallow(animated_circle1) should be: ('yellow', 'magenta', 'blue', 'yellow', 'green') """ time.sleep(0.5) print(message, file=sys.stderr) print('The colors instance of animated_circle1 is:', '\n ', animated_circle1.colors, file=sys.stderr) print('The colors instance of animated_circle2 is:', '\n ', animated_circle2.colors, file=sys.stderr) print('The colors instance of the swallowing', file=sys.stderr) print('Animated_Circle (animated_circle3) is:', '\n ', animated_circle3.colors, file=sys.stderr) print('but should be:', file=sys.stderr) print(" ('blue', 'yellow', 'green', 'yellow', 'magenta')", file=sys.stderr) time.sleep(1)