def test_entry_conditions_invalid(): with pytest.raises(ValueError) as excinfo: locke = Locke(5) locke.move_boats_through(15) assert str(excinfo.value) == "{} accepts {} boats max."\ .format(locke.size, locke.limit)
def test_locke_exit(self): f = StringIO() a = Locke(1) with redirect_stdout(f): a.__exit__(None, None, None) self.assertIn("Closing the doors.", f.getvalue()) self.assertIn("Restarting the pumps.", f.getvalue())
def test_move_boats_through(capsys): small_locke = Locke(5) small_locke.move_boats_through(5) captured = capsys.readouterr() assert captured.out == "Opening the doors.\n"\ "Closing the doors.\n"
def test_locke_errors_on_bad_inputs(bad_input): """given inputs other than positive ints or 0 the locke sends inputerror on initialization""" locke1 = Locke(5) boats = bad_input with pytest.raises(ValueError): with locke1: locke1.move_boats_through(boats)
def test_locke_typeerrors_on_str_inputs(): """given inputs other than positive ints or 0 the locke sends inputerror on initialization""" locke1 = Locke(5) boats = 'hello' with pytest.raises(TypeError): with locke1: locke1.move_boats_through(boats)
def test_locke_rejects_boats_count_over_limit(): """given a lock established with a boat limit of 5 when more than limit of boats are input the locke raises value error exception""" locke1 = Locke(5) boats = 6 with pytest.raises(ValueError): with locke1: locke1.move_boats_through(boats)
def test_enter(self): f = StringIO() locke = Locke(1) with redirect_stdout(f): a = locke.__enter__() self.assertIsInstance(a, Locke) self.assertIn("Stopping the pumps.", f.getvalue()) self.assertIn("Opening the doors.", f.getvalue())
def test_locke_init(): with pytest.raises(Exception) as excinfo: locke = Locke() assert str(excinfo.value) == "__init__() missing 1 required positional "\ "argument: 'limit'" locke = Locke(5) assert locke.limit == 5
def test_locke_accepts_boats_count_under_limit(): """given a lock established with a boat limit of 5 when 5 or less boats are input the locke works without exception""" locke1 = Locke(5) boats = 4 print(locke1.__dict__) with locke1: print(locke1.__dict__) assert locke1.move_boats_through(boats)
def test_move_boats_less_than_capacity(self): number_boats = 8 capacity = 10 locke = Locke(capacity) expected_output = "********* Moved 8 boats through the Locke successfully *********" capturedOutput = io.StringIO() sys.stdout = capturedOutput locke.move_boats_through(number_boats) sys.stdout = sys.__stdout__ self.assertEqual(capturedOutput.getvalue().strip(), expected_output.strip())
def enter_num_boats(): while True: try: num_boats = int(input('Please enter the number of boats in: ')) if not num_boats > 0: raise ValueError except ValueError: print('Please provide a whole number greater than zero.') else: flexible_locke = Locke(5) if num_boats <= 5 else Locke(10) with flexible_locke as locke: locke.move_boats_through(num_boats) break
def test_std_output_tells_story(capsys): """given a locke when moving boats through the standard output tells steps""" locke1 = Locke(5) boats = 4 with locke1: locke1.move_boats_through(boats) captured = capsys.readouterr().out.split("\n") assert captured[0] == "run pumps" assert captured[1] == "open door1" assert captured[2] == "moving boats" assert captured[3] == "closing door1" assert captured[4] == "starting pumps" assert captured[5] == "open door 2" assert captured[6] == "moving boats" assert captured[7] == "closing door 2"
def test_locke(self): f = StringIO() with redirect_stdout(f): with Locke(1): pass self.assertIn("Stopping the pumps.", f.getvalue()) self.assertIn("Opening the doors.", f.getvalue()) self.assertIn("Closing the doors.", f.getvalue()) self.assertIn("Restarting the pumps.", f.getvalue())
def test_move_boats_through(self): a = Locke(5) with self.assertRaises(ValueError): a.move_boats_through(6) try: a.move_boats_through(6) except ValueError as err: self.assertEqual(repr(err), "ValueError('Number of boat exceeds locke capacity 5',)")
def test_doors(capsys): locke = Locke(5) locke.open_doors() captured = capsys.readouterr() assert captured.out == "Opening the doors.\n" locke.close_doors() captured = capsys.readouterr() assert captured.out == "Closing the doors.\n"
def test_boats(): locke = Locke(5) locke.move_boats_through(5) assert locke.boats == 5
def test_entry_conditions_valid(): locke = Locke(5) locke.move_boats_through(5) assert locke.check_entry_conditions() is True
def test_move_boats_greater_than_capacity_fail(self): number_boats = 12 capacity = 10 locke = Locke(capacity) with self.assertRaises(ValueError): locke.move_boats_through(number_boats)
def test_large_locke(): small_locke = Locke(10) assert small_locke.size == "LARGE LOCKE"
def test_init(self): a = Locke(5) self.assertEqual(a._capacity_, 5) with self.assertRaises(ValueError): Locke(0)
from locke import Locke small_locke = Locke(5) large_locke = Locke(10) boats = 8 def message(boats, max_boats): print(f"moving {boats} boats through ballard lock with capacity {max_boats}") # Too many boats through a small locke will raise an exception with small_locke as new_locke: message(boats, new_locke.max_boats) new_locke.move_boats_through(boats) # A lock with sufficient capacity can move boats without incident. with large_locke as new_locke: message(boats, new_locke.max_boats) new_locke.move_boats_through(boats)
def test_small_locke(): small_locke = Locke(5) assert small_locke.size == "SMALL LOCKE"