Exemple #1
0
def test_swell_or_shrink_repeatedly():
    """ Tests the   swell_or_shrink_repeatedly   method. """
    m1_tests.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 CircleChanger object, printing and drawing it.
    title = 'Testing: the  swell_or_shrink_repeatedly   method'
    m1_tests.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(
        200, 150, 30, 'blue',
        ('blue', 'yellow', 'green', 'aquamarine', 'brown'))
    print(circle_changer1)
    circle_changer1.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:')
    circle_changer1.animation_factor = 0.25  # faster animation

    circle_changer1.swell_or_shrink_repeatedly(50, 10)

    print(circle_changer1)
    circle_changer1.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.""")
Exemple #2
0
def test_init():
    """ Tests the   __init__   method of the CircleChanger class. """
    m1_tests.test_init()  # This runs OUR tests.

    # This is a VISUAL test.
    m1_tests.start_drawing('Testing: the   __init__   method')

    # Construct two CircleChanger objects:
    circle_changer1 = CircleChanger(100, 150, 100, 'blue',
                                    ('red', 'blue', 'green'))
    circle_changer2 = CircleChanger(300, 50, 30, 'yellow', ('green', 'gold'))

    # Print and draw them:
    print('After construction:')
    print(circle_changer1, '\n', circle_changer2, '\n', sep='')
    circle_changer1.draw(0.5)
    circle_changer2.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:
    circle_changer1.circle.fill_color = circle_changer1.colors[2]
    circle_changer2.circle.outline_thickness = 10
    print('After changing characteristics:')
    print(circle_changer1, '\n', circle_changer2, sep='')
    circle_changer1.draw()
    circle_changer2.draw("""
    Now the leftmost (formerly BLUE) circle is GREEN
    and the YELLOW circle has a thicker outline.""")
Exemple #3
0
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 CircleChanger objects, printing and drawing them.
    p1_test.start_drawing('Testing: the  swell_or_shrink_once   method')
    print('After construction:')
    circle_changer1 = CircleChanger(200, 150, 30, 'blue',
                                       ('blue', 'yellow', 'green',
                                        'aquamarine', 'brown'))
    print(circle_changer1)
    circle_changer1.draw()

    circle_changer2 = CircleChanger(400, 100, 50, 'red', ('green',))
    print(circle_changer2)
    circle_changer2.draw()

    circle_changer3 = CircleChanger(300, 200, 10, 'green',
                                       ('yellow', 'blue'))
    print(circle_changer3)
    circle_changer3.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 CircleChanger objects,
    #   apply the   swell_or_shrink_once  method, then redraw/reprint.
    print('\nAfter the first set of    swell_or_shrink_once   calls:')
    circle_changer1.swell_or_shrink_once(100)
    print(circle_changer1)
    circle_changer1.draw()

    circle_changer2.swell_or_shrink_once(-30)
    print(circle_changer2)
    circle_changer1.draw()

    circle_changer3.swell_or_shrink_once(40)
    print(circle_changer3)
    circle_changer3.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.""")
Exemple #4
0
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 CircleChanger objects, printing and drawing them.
    title = 'Testing: the  change_to_original_color   method'
    p1_test.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(100, 100, 100, 'black',
                                       ('blue', 'green'))
    circle_changer1.draw()
    print(circle_changer1)
    circle_changer2 = CircleChanger(280, 100, 100, 'purple',
                                       ('yellow', 'magenta', 'blue',
                                        'green', 'yellow', 'aquamarine'))
    print(circle_changer2)
    circle_changer2.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):
        circle_changer1.change_color(k % 2)
        circle_changer1.draw(0.05)

        circle_changer2.change_color(k % 6)
        circle_changer2.draw(0.05)

    circle_changer1.change_to_original_color()
    print(circle_changer1)
    circle_changer1.draw()

    circle_changer2.change_to_original_color()
    print(circle_changer2)
    circle_changer2.draw("""
    Should end as it started: BLACK and PURPLE.""")
Exemple #5
0
def test_change_to_next_color_in_tuple():
    """ Tests the   change_to_next_color_in_tuple   method. """
    #     m1_tests.change_to_next_color()  # This runs OUR tests.

    # This is a VISUAL test.
    # Construct 2 CircleChanger objects, printing and drawing them.
    title = 'Testing: the  change_to_next_color   method'
    m1_tests.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(100, 100, 100, 'black',
                                    ('blue', 'green', 'red'))
    circle_changer1.draw()
    print(circle_changer1)
    circle_changer2 = CircleChanger(
        280, 100, 40, 'purple',
        ('yellow', 'magenta', 'blue', 'green', 'yellow', 'aquamarine'))
    print(circle_changer2)
    circle_changer2.draw("""
    A BLACK circle at (100, 100) with radius 100,
    and a PURPLE circle at (280, 100) with radius 40.
    You will next see a bunch of colors,
    cycling through BLUE, GREEN, RED (for the left, larger circle)
    and YELLOW, MAGENTA, BLUE, GREEN, YELLOW (again) and AQUAMARINE
    (for the right, smaller circle).""")

    # Cycle through the CircleChanger's tuples of colors:
    print('\nAfter the first set of  change_to_next_color  calls:')
    for _ in range(16):
        circle_changer1.change_to_next_color_in_tuple()
        circle_changer1.draw(0.25)

        circle_changer2.change_to_next_color_in_tuple()
        circle_changer2.draw(0.25)

    circle_changer2.draw("""
    Should end with circles: BLUE and GREEN.""")
Exemple #6
0
def test_change_color():
    """ Tests the   change_color   method. """
    m1_tests.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 CircleChanger objects, printing and drawing them.
    title = 'Testing: the  change_color   method'
    m1_tests.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(100, 100, 70, 'black', ('blue', 'yellow'))
    circle_changer1.draw()
    print(circle_changer1)
    circle_changer2 = CircleChanger(
        350, 130, 50, 'purple',
        ('yellow', 'magenta', 'blue', 'green', 'yellow', 'aquamarine'))
    print(circle_changer2)
    circle_changer2.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:')

    circle_changer1.change_color(1)
    print(circle_changer1)
    circle_changer1.draw()

    circle_changer2.change_color(5)
    print(circle_changer2)
    circle_changer2.draw("""
    Same circles, but now YELLOW and AQUAMARINE.
    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 aquamarine.""")

    # Another test:
    for k in range(6):
        circle_changer1.change_color(k % 2)
        circle_changer1.draw()

        circle_changer2.change_color(k)
        circle_changer2.draw()

    circle_changer1.draw("""
    Should have finished with YELLOW and AQUAMARINE.""")

    # This tests   change_color   and   swell_and_shrink_once
    # repeatedly.
    for k in range(20, 0, -1):
        circle_changer1.change_color(k % 2)
        circle_changer1.draw(0.05)
        circle_changer1.swell_or_shrink_once((-40 // k) * (k**-1))
        circle_changer1.draw(0.05)

        circle_changer2.change_color(k % 6)
        circle_changer2.draw(0.05)
        circle_changer2.swell_or_shrink_once(50 // k)
        circle_changer1.draw(0.05)

    circle_changer1.draw("""
    Should have ended with two YELLOW circles:
    a TINY one on the LEFT and a HUGE one on the RIGHT.""")
Exemple #7
0
def test_swallow():
    """ Tests the   swallow   method. """
    m1_tests.test_swallow()  # This runs OUR tests.

    # This is a VISUAL test.
    # Construct 2 CircleChanger objects, printing and drawing them.
    title = 'Testing: the  swallow   method'
    m1_tests.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(200, 150, 50, 'blue',
                                    ('blue', 'yellow', 'green'))
    circle_changer1.draw()
    print(circle_changer1)
    circle_changer2 = CircleChanger(450, 180, 30, 'green',
                                    ('yellow', 'magenta'))
    print(circle_changer2)
    circle_changer2.draw("""
    A BLUE circle at (200, 150) with radius 50,
    and a GREEN circle at (450, 180) with radius 30.""")

    # Apply the   swallow   method.  Then print/draw the resulting
    # circle, drawing the original circles on top of it.
    print('\nAfter the first   swallow   call:')

    circle_changer3 = circle_changer1.swallow(circle_changer2)
    print(circle_changer3)
    circle_changer3.draw("""
    The RED circle should be centered at (325, 165) with radius about 126.
    It should cover approximately HALF of the BLUE and GREEN circles.""")

    circle_changer4 = CircleChanger(200, 150, 50, 'blue',
                                    ('blue', 'yellow', 'green'))
    circle_changer4.draw()
    circle_changer5 = CircleChanger(450, 180, 30, 'green',
                                    ('yellow', 'magenta'))
    circle_changer5.draw("""
    Here are the BLUE and GREEN circles again, on TOP of the RED circle.
    The RED should appear to be underneath approximately HALF
    of each of the BLUE and GREEN circles.""")

    # Test that the swallowing (red) CircleChanger
    # has a   colors   attribute that is the CONCATENATION
    # of the  colors   attributes of the swallowed CircleChangers.
    if circle_changer3.colors != (circle_changer4.colors +
                                  circle_changer5.colors):
        message = """The   colors   instance variable
    of the swallowing CircleChanger (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   circle_changer1.colors   is:
       (blue', 'yellow', 'green')
    and if   circle_changer2.colors   is:
       ('yellow', 'magenta')
    then   circle_changer1.swallow(circle_changer2)   should be:
       ('blue', 'yellow', 'green', 'yellow', 'magenta')
    and    circle_changer2.swallow(circle_changer1)   should be:
       ('yellow', 'magenta', 'blue', 'yellow', 'green')
    """
        time.sleep(0.5)
        print(message, file=sys.stderr)
        print('The   colors   instance  of   circle_changer1   is:',
              '\n   ',
              circle_changer1.colors,
              file=sys.stderr)
        print('The   colors   instance  of   circle_changer2   is:',
              '\n   ',
              circle_changer2.colors,
              file=sys.stderr)
        print('The   colors   instance  of the swallowing', file=sys.stderr)
        print('CircleChanger (circle_changer3)   is:',
              '\n   ',
              circle_changer3.colors,
              file=sys.stderr)
        print('but should be:', file=sys.stderr)
        print("  ('blue', 'yellow', 'green', 'yellow', 'magenta')",
              file=sys.stderr)
        time.sleep(1)
Exemple #8
0
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 CircleChanger objects, printing and drawing them.
    title = 'Testing: the  change_to_previous_color   method'
    p1_test.start_drawing(title)

    print('After construction:')
    circle_changer1 = CircleChanger(100, 100, 20, 'violet',
                                       ('dark red', 'green', 'tomato',
                                        'blue', 'plum', 'seashell',
                                        'red', 'peach puff', 'coral',
                                        'chocolate', 'brown'))
    circle_changer1.draw()
    print(circle_changer1)
    circle_changer2 = CircleChanger(200, 200, 50, 'orange',
                                       ('yellow', 'magenta', 'blue',
                                        'lavender', 'cyan', 'aquamarine',
                                        'black', 'salmon', 'hot pink',
                                        'forest green', 'spring green'))
    print(circle_changer2)
    circle_changer2.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):')

    circle_changer1.animation_factor = 0.1  # Faster animations
    circle_changer2.animation_factor = 0.1

    circle_changer1.swell_or_shrink_repeatedly(40, 16)
    circle_changer2.swell_or_shrink_repeatedly(-10, 12)
    circle_changer1.circle.fill_color = 'CHARTREUSE'
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.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:')

    circle_changer1.change_color(0)
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.draw()
    circle_changer2.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:')

    circle_changer1.swell_or_shrink_repeatedly(80, 13)
    circle_changer2.swell_or_shrink_repeatedly(20, 11)
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.draw()
    circle_changer2.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 CircleChanger.
    That should return the LEFT circle to CHARTREUSE.""")

    print('Just after circle_changer1.change_to_previous_color():')

    circle_changer2.draw(1)
    circle_changer1.change_to_previous_color()
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.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 CircleChanger.
    That CircleChanger 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 circle_changer2.change_to_previous_color():')

    circle_changer1.draw(1)  # To force a pause

    circle_changer2.change_to_previous_color()
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.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):')

    circle_changer1.swell_or_shrink_repeatedly(30, 11)
    circle_changer2.swell_or_shrink_repeatedly(50, 6)
    circle_changer1.circle.fill_color = 'AQUAMARINE'
    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.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):')

    circle_changer1.change_color(random.randrange(11))
    circle_changer2.change_color(random.randrange(11))

    circle_changer1.swell_or_shrink_repeatedly(10, 4)
    circle_changer2.swell_or_shrink_repeatedly(-10, 8)

    print(circle_changer1)
    print(circle_changer2)
    circle_changer1.draw()
    circle_changer2.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:')

    circle_changer1.animation_factor = 1  # Slow animations for finish
    circle_changer2.animation_factor = 1

    circle_changer1.draw(1)  # To force a pause

    circle_changer1.change_to_previous_color()
    print(circle_changer1)
    circle_changer1.draw(1)

    circle_changer2.change_to_previous_color()
    print(circle_changer2)
    circle_changer2.draw("""
    The LEFT circle should now be AQUAMARINE.
    The RIGHT circle should now be HOT PINK.
    This concludes this test.""")