def test_should_pickup_trivial(self): # empty string, pattern all 1s self.string_handler = StringHandler(255) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) # empty string, pattern all 0s self.string_handler = StringHandler(0) self.assertFalse(self.string_handler.should_pickup(1)) self.assertTrue(self.string_handler.should_pickup(0))
def test_to_bin_255(self): self.string_handler = StringHandler(255) arr_check = [1, 1, 1, 1, 1, 1, 1, 1] check = True if len(arr_check) != len(self.string_handler.pattern): check = False if arr_check[0] != self.string_handler.pattern[0]: check = False self.assertTrue(check)
def test_to_bin_54(self): self.string_handler = StringHandler(54) arr_check = [0, 0, 1, 1, 0, 1, 1, 0] check = True if len(arr_check) != len(self.string_handler.pattern): check = False for i in arr_check: if arr_check[i] != self.string_handler.pattern[i]: check = False self.assertTrue(check)
def test_is_complete_trivial(self): # empty string, pattern only 1s self.string_handler = StringHandler(255) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.stringed_disks = [1, 1, 1, 1, 1, 1, 1] self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertTrue(self.string_handler.is_complete())
def test_should_pickup_not_trivial(self): # pattern 13 = [0, 0, 0, 0, 1, 1, 0, 1]_2 self.string_handler = StringHandler(13) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.should_pickup(1)) self.assertTrue(self.string_handler.should_pickup(0)) self.string_handler.string_disk(0) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) self.string_handler.string_disk(1) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0))
def test_process_primary_black(self): # Unwanted color self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Primary Color Detected", 10, "TEST MESSAGE") self.assertEqual(["Retract Blocker"], self.processor.process("Primary Black")) # No permission from protocol self.protocol = DummyProtocolHandler(False) self.processor = Processor(StringHandler(1), self.protocol, self.expectations) self.expectations.add("Primary Color Detected", 10, "TEST MESSAGE") self.assertEqual(["Retract Blocker"], self.processor.process("Primary Black")) # Wanted and permission self.protocol = DummyProtocolHandler(True) self.processor = Processor(StringHandler(1), self.protocol, self.expectations) self.expectations.add("Primary Color Detected", 10, "TEST MESSAGE") self.assertEqual(["Push Pusher"], self.processor.process("Primary Black"))
def test_process_primary_motion(self): # Uncompleted stringer self.string_handler = StringHandler(0) self.processor = Processor(self.string_handler, ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Extend Blocker"], self.processor.process("Primary Motion")) # Completed stringer self.string_handler.stringed_disks = [0] * 8 self.assertEqual(["Ignore"], self.processor.process("Primary Motion"))
def test_process_exception(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Tertiary Motion")) self.assertEqual(["Enter Error Mode"], self.processor.process("Secondary Black")) self.assertEqual(["Enter Error Mode"], self.processor.process("Secondary White")) self.assertEqual(["Enter Error Mode"], self.processor.process("Secondary Motion")) self.assertEqual(["Enter Error Mode"], self.processor.process("Confirm Blocker Retracted")) self.assertEqual(["Enter Error Mode"], self.processor.process("Confirm Pusher Pushed")) self.assertEqual(["Enter Error Mode"], self.processor.process("Primary Black")) self.assertEqual(["Enter Error Mode"], self.processor.process("Primary White")) self.assertEqual(["Enter Error Mode"], self.processor.process("Confirm Blocker Extended"))
def test_is_complete_not_trivial(self): # pattern 19 = [0, 0, 0, 1, 0, 0, 1, 1]_2, stringed to completion self.string_handler = StringHandler(19) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertTrue(self.string_handler.is_complete())
def test_process_ping(self): # No expired outputs self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("in", 5, "TEST MESSAGE") for _ in range(4): self.assertEqual(["Pong"], self.processor.process("Ping")) # Expired outputs self.assertEqual(["Enter Error Mode"], self.processor.process("Ping")) self.assertEqual(["Pong"], self.processor.process("Ping")) self.expectations.add("new", 0, "TEST MESSAGE") self.assertEqual(["Enter Error Mode"], self.processor.process("Ping"))
def test_get_next_color(self): # pattern consisting of only 1s, no disks stringed: self.string_handler = StringHandler(255) self.assertEqual(self.string_handler.get_next_color(), 1) # pattern consisting of only 0s, no disks stringed: self.string_handler = StringHandler(0) self.assertEqual(self.string_handler.get_next_color(), 0) # pattern 6 = [0, 0, 0, 0, 0, 1, 1, 0]_2, stringed completely: self.string_handler = StringHandler(6) self.assertEqual(self.string_handler.get_next_color(), 0) self.string_handler.stringed_disks = [0] self.assertEqual(self.string_handler.get_next_color(), 1) self.string_handler.stringed_disks = [1, 0] self.assertEqual(self.string_handler.get_next_color(), 1) self.string_handler.stringed_disks = [1, 1, 0] for i in range(5): self.assertEqual(self.string_handler.get_next_color(), 0) self.string_handler.stringed_disks.insert(0, 0)
def test_string_disk(self): # pattern consisting of only 1s, stringing 1: self.string_handler = StringHandler(255) self.is_correct = self.string_handler.string_disk(1) self.assertTrue(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [1]) # pattern consisting of only 1s, stringing 0: self.string_handler = StringHandler(255) self.is_correct = self.string_handler.string_disk(0) self.assertFalse(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0]) # pattern already partly stringed: self.string_handler = StringHandler(11) self.string_handler.stringed_disks = [1, 1] self.is_correct = self.string_handler.string_disk(0) self.assertTrue(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0, 1, 1]) self.is_correct = self.string_handler.string_disk(0) self.assertFalse(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0, 0, 1, 1])
def test_process_disk_stringed(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Tertiary Motion", 10, "TEST MESSAGE") self.assertEqual(["Ignore"], self.processor.process("Tertiary Motion"))
def test_process_blocker_extended(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Confirm Blocker Extended", 10, "TEST MESSAGE") self.assertEqual(["Scan Primary Color"], self.processor.process("Confirm Blocker Extended"))
def test_get_next_color_exception(self): self.string_handler = StringHandler(6) self.string_handler.stringed_disks = [0, 0, 0, 0, 0, 1, 1, 0] self.assertRaises(Exception, self.string_handler.get_next_color)
def test_process_unknown(self): self.processor = Processor(StringHandler(69), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Test")) self.assertEqual(["Enter Error Mode"], self.processor.process("Gibberish"))
def test_process_message_buffer_full(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Message Buffer Full"))
def test_process_unknown_command(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Unknown Command Sent"))
def test_process_unexpected_error(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Unexpected Error Occurred"))
def test_process_error_string_disk(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Error String Disk"))
class TestInit(TestCase): def test_constructor_exception(self): self.assertRaises(ValueError, StringHandler, -1) self.assertRaises(ValueError, StringHandler, 256) def test_get_next_color(self): # pattern consisting of only 1s, no disks stringed: self.string_handler = StringHandler(255) self.assertEqual(self.string_handler.get_next_color(), 1) # pattern consisting of only 0s, no disks stringed: self.string_handler = StringHandler(0) self.assertEqual(self.string_handler.get_next_color(), 0) # pattern 6 = [0, 0, 0, 0, 0, 1, 1, 0]_2, stringed completely: self.string_handler = StringHandler(6) self.assertEqual(self.string_handler.get_next_color(), 0) self.string_handler.stringed_disks = [0] self.assertEqual(self.string_handler.get_next_color(), 1) self.string_handler.stringed_disks = [1, 0] self.assertEqual(self.string_handler.get_next_color(), 1) self.string_handler.stringed_disks = [1, 1, 0] for i in range(5): self.assertEqual(self.string_handler.get_next_color(), 0) self.string_handler.stringed_disks.insert(0, 0) def test_get_next_color_exception(self): self.string_handler = StringHandler(6) self.string_handler.stringed_disks = [0, 0, 0, 0, 0, 1, 1, 0] self.assertRaises(Exception, self.string_handler.get_next_color) def test_string_disk(self): # pattern consisting of only 1s, stringing 1: self.string_handler = StringHandler(255) self.is_correct = self.string_handler.string_disk(1) self.assertTrue(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [1]) # pattern consisting of only 1s, stringing 0: self.string_handler = StringHandler(255) self.is_correct = self.string_handler.string_disk(0) self.assertFalse(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0]) # pattern already partly stringed: self.string_handler = StringHandler(11) self.string_handler.stringed_disks = [1, 1] self.is_correct = self.string_handler.string_disk(0) self.assertTrue(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0, 1, 1]) self.is_correct = self.string_handler.string_disk(0) self.assertFalse(self.is_correct) self.assertEqual(self.string_handler.stringed_disks, [0, 0, 1, 1]) def test_to_string_exception(self): self.string_handler = StringHandler(11) self.string_handler.stringed_disks = [0, 0, 0, 0, 1, 0, 1, 1] self.assertRaises(Exception, self.string_handler.string_disk, 1) self.assertRaises(Exception, self.string_handler.string_disk, 0) def test_to_bin_0(self): self.string_handler = StringHandler(1) arr_check = [0, 0, 0, 0, 0, 0, 0, 0] check = True if len(arr_check) != len(self.string_handler.pattern): check = False if arr_check[0] != self.string_handler.pattern[0]: check = False self.assertTrue(check) def test_to_bin_1(self): self.string_handler = StringHandler(1) arr_check = [0, 0, 0, 0, 0, 0, 0, 1] check = True if len(arr_check) != len(self.string_handler.pattern): check = False if arr_check[0] != self.string_handler.pattern[0]: check = False self.assertTrue(check) def test_to_bin_255(self): self.string_handler = StringHandler(255) arr_check = [1, 1, 1, 1, 1, 1, 1, 1] check = True if len(arr_check) != len(self.string_handler.pattern): check = False if arr_check[0] != self.string_handler.pattern[0]: check = False self.assertTrue(check) def test_to_bin_54(self): self.string_handler = StringHandler(54) arr_check = [0, 0, 1, 1, 0, 1, 1, 0] check = True if len(arr_check) != len(self.string_handler.pattern): check = False for i in arr_check: if arr_check[i] != self.string_handler.pattern[i]: check = False self.assertTrue(check) def test_is_complete_trivial(self): # empty string, pattern only 1s self.string_handler = StringHandler(255) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.stringed_disks = [1, 1, 1, 1, 1, 1, 1] self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertTrue(self.string_handler.is_complete()) def test_is_complete_not_trivial(self): # pattern 19 = [0, 0, 0, 1, 0, 0, 1, 1]_2, stringed to completion self.string_handler = StringHandler(19) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertFalse(self.string_handler.is_complete()) self.string_handler.string_disk(0) self.assertTrue(self.string_handler.is_complete()) def test_should_pickup_trivial(self): # empty string, pattern all 1s self.string_handler = StringHandler(255) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) # empty string, pattern all 0s self.string_handler = StringHandler(0) self.assertFalse(self.string_handler.should_pickup(1)) self.assertTrue(self.string_handler.should_pickup(0)) def test_should_pickup_not_trivial(self): # pattern 13 = [0, 0, 0, 0, 1, 1, 0, 1]_2 self.string_handler = StringHandler(13) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) self.string_handler.string_disk(1) self.assertFalse(self.string_handler.should_pickup(1)) self.assertTrue(self.string_handler.should_pickup(0)) self.string_handler.string_disk(0) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0)) self.string_handler.string_disk(1) self.assertTrue(self.string_handler.should_pickup(1)) self.assertFalse(self.string_handler.should_pickup(0))
def test_process_secondary_motion(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Secondary Motion", 10, "TEST MESSAGE") self.assertEqual(["Scan Secondary Color"], self.processor.process("Secondary Motion"))
def test_process_pusher_pushed(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Confirm Pusher Pushed", 10, "TEST MESSAGE") self.assertEqual(["Retract Blocker"], self.processor.process("Confirm Pusher Pushed"))
def test_to_string_exception(self): self.string_handler = StringHandler(11) self.string_handler.stringed_disks = [0, 0, 0, 0, 1, 0, 1, 1] self.assertRaises(Exception, self.string_handler.string_disk, 1) self.assertRaises(Exception, self.string_handler.string_disk, 0)
def test_process_blocker_retracted(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Confirm Blocker Retracted", 10, "TEST MESSAGE") self.assertEqual(["Ignore"], self.processor.process("Confirm Blocker Retracted"))
def test_process_secondary_neither(self): self.processor = Processor(StringHandler(0), ProtocolHandler(), ExpectationHandler()) self.assertEqual(["Enter Error Mode"], self.processor.process("Secondary Neither"))
def test_process_secondary_black(self): self.expectations = ExpectationHandler() self.processor = Processor(StringHandler(0), ProtocolHandler(), self.expectations) self.expectations.add("Secondary Black Detected", 10, "TEST MESSAGE") self.assertEqual(["Push Stringer"], self.processor.process("Secondary Black"))
def setup_processor(protocol_handler): s = StringHandler(get_number()) ph = protocol_handler eh = ExpectationHandler() return Processor(s, ph, eh)