def test_no_output(self): # Based on code from user1200039 http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python import sys from io import StringIO from LabC2 import sort_hand from GameUno import GameUno, RED, BLUE, WILD4 print(""" Most commonly your program fails this test if you use a print statement in your sort_hand function. You must not print anything in this function. Instead, you should return the location so that the test code can access it. Wrong: def add(a, b): ""\" Add a and b.""\" print(a+b) Right: def add(a, b): ""\" Return the sum of a and b.""\" return a+b """) oldout = sys.stdout buf=StringIO() game=GameUno() moves=[game.card(3,RED), game.card(7,BLUE), game.card(WILD4)] try: sys.stdout=buf sort_hand( moves) finally: sys.stdout=oldout self.assertEqual(len(buf.getvalue()),0)
def test_sort_mono20(self): print(""" This happens when you do not correctly sort_hand the last item in the list. """) from LabC2 import sort_hand from GameUno import GameUno, RED, GREEN, BLUE, YELLOW, \ REVERSE, SKIP, DRAW2, WILD, WILD4 game=GameUno() moves=[game.card(DRAW2,YELLOW), \ game.card(SKIP,RED), \ game.card(SKIP,YELLOW), \ game.card(REVERSE,GREEN), \ game.card(SKIP,BLUE), \ game.card(DRAW2,BLUE), \ game.card(REVERSE,YELLOW), \ game.card(SKIP,GREEN), \ game.card(REVERSE,RED), \ game.card(REVERSE,BLUE), \ game.card(DRAW2,GREEN), \ game.card(DRAW2,RED)] self.assertEqual(sort_hand(moves), None) self.assertEqual(moves, [game.card(DRAW2,BLUE), \ game.card(REVERSE,BLUE), \ game.card(SKIP,BLUE), \ game.card(DRAW2,GREEN), \ game.card(REVERSE,GREEN), \ game.card(SKIP,GREEN), \ game.card(DRAW2,RED), \ game.card(REVERSE,RED), \ game.card(SKIP,RED), \ game.card(DRAW2,YELLOW), \ game.card(REVERSE,YELLOW), \ game.card(SKIP,YELLOW)])
def test_sort_hand_empty(self): print(""" This happens when you do not correctly handle empty lists. """) from LabC2 import sort_hand from GameUno import GameUno, RED, GREEN, BLUE, YELLOW, \ REVERSE, SKIP, DRAW2, WILD, WILD4 game=GameUno() moves=[] self.assertEqual(sort_hand( moves),None) self.assertEqual(len( moves),0)
def test_sort_hand_single(self): print(""" This happens when you do not correctly handle lists with only a single entry. """) from LabC2 import sort_hand from GameUno import GameUno, RED, GREEN, BLUE, YELLOW, \ REVERSE, SKIP, DRAW2, WILD, WILD4 game=GameUno() moves=[game.card(10,BLUE)] self.assertEqual(sort_hand(moves),None) self.assertEqual(moves, [game.card(10,BLUE)])
def test_sort_hand_monocolor(self): print(""" This happens when you do not correctly return the first location of a value that is in the list multiple times. """) from LabC2 import sort_hand from GameUno import GameUno, RED, GREEN, BLUE, YELLOW, \ REVERSE, SKIP, DRAW2, WILD, WILD4 game=GameUno() moves=[game.card(2,BLUE), game.card(4,BLUE), game.card(DRAW2,BLUE),\ game.card(10,BLUE), game.card(REVERSE,BLUE), game.card(0,BLUE)] self.assertEqual(sort_hand(moves), None) self.assertEqual(moves, [game.card(0,BLUE), game.card(2,BLUE), \ game.card(4,BLUE), game.card(10,BLUE), game.card(DRAW2,BLUE), \ game.card(REVERSE,BLUE)])