class TestMessageComparison(unittest.TestCase): def setUp(self): builder = MessageBuilder() OUTPUTS = builder.build_message_class("OUTPUTS", register_defs["OUTPUTS"]) INPUTS = builder.build_message_class("INPUTS", register_defs["INPUTS"]) self.outputs_1 = OUTPUTS.from_data("b11100000000000000000000000000000") self.outputs_1_copy = OUTPUTS.from_data( "b11100000000000000000000000000000") self.outputs_2 = OUTPUTS.from_data("b11101000000000000000000000000000") self.inputs_1 = INPUTS.from_data("b11100000000000000000000000000000") def testSelfEqual(self): self.assertTrue(self.outputs_1 == self.outputs_1) self.assertTrue(self.outputs_1 == self.outputs_1.render()) def testCopyOfSelfEqual(self): self.assertTrue(self.outputs_1 == self.outputs_1_copy) self.assertTrue(self.outputs_1 == self.outputs_1_copy.render()) def testDifferentStructureSameDataEqual(self): self.builder = MessageBuilder() self.builder.load_definitions(msg_fmts) self.builder.load_definitions(register_defs) self.builder.load_definitions(caution_codes) WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 OUTPUTS = self.builder.OUTPUTS RANDOM_MEANING = self.builder.RANDOM_MEANING CAUTION_CODES = self.builder.CAUTION_CODES msg1 = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg1.or_field.context = RANDOM_MEANING msg1.data.context = OUTPUTS msg1.data.cautions.context = CAUTION_CODES msg2 = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg1.data.context = OUTPUTS self.assertEqual(msg1, msg2) self.assertEqual(msg1, msg2.render()) def testDifferentMessageSameDataNotEqual(self): self.assertNotEqual(self.outputs_1, self.inputs_1) self.assertEqual(self.outputs_1, self.inputs_1.render()) def testNotEqual(self): self.assertTrue(self.outputs_1 != self.outputs_2) self.assertTrue(self.outputs_1 != self.outputs_2.render())
class TestNestedMessages(unittest.TestCase): def setUp(self): self.builder = MessageBuilder() self.builder.load_definitions(msg_fmts) self.builder.load_definitions(register_defs) self.builder.load_definitions(caution_codes) def testMultipleLevelContexts(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST OUTPUTS = self.builder.OUTPUTS CAUTION_CODES = self.builder.CAUTION_CODES msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg.data.context = OUTPUTS msg.data.cautions.context = CAUTION_CODES self.assertEqual(msg.context, WRITE_REGISTER_REQUEST) self.assertEqual(type(msg), WRITE_REGISTER_REQUEST) self.assertEqual(msg.data.context, OUTPUTS) self.assertEqual(type(msg.data), OUTPUTS) self.assertEqual(msg.data.cautions.context, CAUTION_CODES) self.assertEqual(type(msg.data.cautions), CAUTION_CODES) def testMultipleLevelValues(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST OUTPUTS = self.builder.OUTPUTS CAUTION_CODES = self.builder.CAUTION_CODES msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg.data.context = OUTPUTS msg.data.cautions.context = CAUTION_CODES self.assertEqual(msg.mid, "x0016") self.assertEqual(msg.length, "x0008") self.assertEqual(msg.addr, "x60000001") self.assertEqual(msg.data, "x80000000") self.assertEqual(msg.data.reset1, "b1") self.assertEqual(msg.data.reset2, "b0") self.assertEqual(msg.data.cautions, "x00") self.assertEqual(msg.data.cautions.addr, "b0000") self.assertEqual(msg.data.cautions.access, "b0000") self.assertEqual(msg.data.unused, "x0000000") def testMultipleLevelValueUpdates(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 OUTPUTS = self.builder.OUTPUTS RANDOM_MEANING = self.builder.RANDOM_MEANING CAUTION_CODES = self.builder.CAUTION_CODES msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg.or_field.context = RANDOM_MEANING msg.data.context = OUTPUTS msg.data.cautions.context = CAUTION_CODES self.assertEqual(msg.data, "x80000000") self.assertEqual(msg.or_field, "xE0000001") self.assertEqual(msg.or_field.byte_1, "xE0") self.assertEqual(msg.or_field.byte_2, "x00") self.assertEqual(msg.or_field.byte_3, "x00") self.assertEqual(msg.or_field.byte_4, "x01") msg.data.cautions.addr = "x1" msg.data.cautions.access = "xF" self.assertEqual(msg.data, "x87C00000") self.assertEqual(msg.or_field, "xE7C00001") self.assertEqual(msg.or_field.byte_1, "xE7") self.assertEqual(msg.or_field.byte_2, "xC0") self.assertEqual(msg.or_field.byte_3, "x00") self.assertEqual(msg.or_field.byte_4, "x01") def testSetFieldAsMessage(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") self.assertEqual(msg.data, "x80000000") msg.data = self.builder.OUTPUTS.from_data("x8C000000") self.assertEqual(msg.data.reset1, "b1") self.assertEqual(msg.data.reset2, "b0") self.assertEqual(msg.data.cautions, "x30") self.assertEqual(msg.data.unused, "x00000") self.assertEqual(msg.data, "x8C000000") def testSetContextAsNone(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg.data.context = self.builder.OUTPUTS self.assertTrue("data.reset1" in msg.get_field_name_mapping( expand_nested=True)) msg.data.context = None self.assertTrue("data.reset1" not in msg.get_field_name_mapping( expand_nested=True)) def testSetIncompatibleContext(self): WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000001") with self.assertRaises(ContextDataMismatchException): msg.data.context = self.builder.OUTPUTS
class TestMessageTables(unittest.TestCase): def setUp(self): self.builder = MessageBuilder() self.builder.load_definitions(msg_fmts) self.builder.load_definitions(register_defs) self.builder.load_definitions(caution_codes) WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST_V2 OUTPUTS = self.builder.OUTPUTS RANDOM_MEANING = self.builder.RANDOM_MEANING CAUTION_CODES = self.builder.CAUTION_CODES self.msg = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") self.msg.or_field.context = RANDOM_MEANING self.msg.data.context = OUTPUTS self.msg.data.cautions.context = CAUTION_CODES def testExpandedTableRendering(self): nested_all_fmts = self.msg.render_table(formats=[ Field.Format.Hex, Field.Format.Dec, Field.Format.Oct, Field.Format.Bin ], expand_nested=True) self.assertEqual( remove_whitespace(nested_all_fmts), remove_whitespace(""" +WRITE_REGISTER_REQUEST_V2---------+-------------+--------------+-----------------------------------+ | Field Name | Hex | Dec | Oct | Bin | +----------------------+-----------+-------------+--------------+-----------------------------------+ | mid | x0016 | d00022 | o000026 | b0000000000010110 | | or_field.byte_1 | xe0 | d224 | o340 | b11100000 | | or_field.byte_2 | x00 | d000 | o000 | b00000000 | | or_field.byte_3 | x00 | d000 | o000 | b00000000 | | or_field.byte_4 | x01 | d001 | o001 | b00000001 | | addr | x60000001 | d1610612737 | o14000000001 | b01100000000000000000000000000001 | | data.reset1 | x1 | d1 | o1 | b1 | | data.reset2 | x0 | d0 | o0 | b0 | | data.cautions.addr | x0 | d00 | o00 | b0000 | | data.cautions.access | x0 | d00 | o00 | b0000 | | data.unused | x000000 | d0000000 | o00000000 | b0000000000000000000000 | +----------------------+-----------+-------------+--------------+-----------------------------------+ """), ) def testCollapsedTableRendering(self): not_nested_default_fmt = self.msg.render_table(formats=None, expand_nested=False) self.assertEqual( remove_whitespace(not_nested_default_fmt), remove_whitespace(""" +------------+-----------+ | Field Name | Value | +------------+-----------+ | mid | x0016 | | or_field | xe0000001 | | addr | x60000001 | | data | x80000000 | +------------+-----------+ """), ) def testCollapsedTableComparisonEqual(self): comp = self.msg.compare_tables(self.msg, expand_nested=False)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +------------+-----------+ +------------+-----------+ | Field Name | Value | | Field Name | Value | +------------+-----------+ +------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field | xe0000001 | == | or_field | xe0000001 | | addr | x60000001 | == | addr | x60000001 | | data | x80000000 | == | data | x80000000 | +------------+-----------+ +------------+-----------+ """), ) def testCollapsedTableComparisonNotEqual(self): msg2 = copy.deepcopy(self.msg) msg2.data = "x83000000" comp = self.msg.compare_tables(msg2, expand_nested=False)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +------------+-----------+ +------------+-----------+ | Field Name | Value | | Field Name | Value | +------------+-----------+ +------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field | xe0000001 | != | or_field | xe3000001 | | addr | x60000001 | == | addr | x60000001 | | data | x80000000 | != | data | x83000000 | +------------+-----------+ +------------+-----------+ """), ) def testExpandedTableComparisonEqual(self): comp = self.msg.compare_tables(self.msg, formats=None, expand_nested=True)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +WRITE_REGISTER_REQUEST_V2---------+ +WRITE_REGISTER_REQUEST_V2---------+ | Field Name | Value | | Field Name | Value | +----------------------+-----------+ +----------------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field.byte_1 | xe0 | == | or_field.byte_1 | xe0 | | or_field.byte_2 | x00 | == | or_field.byte_2 | x00 | | or_field.byte_3 | x00 | == | or_field.byte_3 | x00 | | or_field.byte_4 | x01 | == | or_field.byte_4 | x01 | | addr | x60000001 | == | addr | x60000001 | | data.reset1 | b1 | == | data.reset1 | b1 | | data.reset2 | b0 | == | data.reset2 | b0 | | data.cautions.addr | x0 | == | data.cautions.addr | x0 | | data.cautions.access | x0 | == | data.cautions.access | x0 | | data.unused | x000000 | == | data.unused | x000000 | +----------------------+-----------+ +----------------------+-----------+ """), ) def testExpandedTableComparisonNotEqual(self): msg2 = copy.deepcopy(self.msg) msg2.data = "x83000000" comp = self.msg.compare_tables(msg2, formats=None, expand_nested=True)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +WRITE_REGISTER_REQUEST_V2---------+ +WRITE_REGISTER_REQUEST_V2---------+ | Field Name | Value | | Field Name | Value | +----------------------+-----------+ +----------------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field.byte_1 | xe0 | != | or_field.byte_1 | xe3 | | or_field.byte_2 | x00 | == | or_field.byte_2 | x00 | | or_field.byte_3 | x00 | == | or_field.byte_3 | x00 | | or_field.byte_4 | x01 | == | or_field.byte_4 | x01 | | addr | x60000001 | == | addr | x60000001 | | data.reset1 | b1 | == | data.reset1 | b1 | | data.reset2 | b0 | == | data.reset2 | b0 | | data.cautions.addr | x0 | == | data.cautions.addr | x0 | | data.cautions.access | x0 | != | data.cautions.access | xc | | data.unused | x000000 | == | data.unused | x000000 | +----------------------+-----------+ +----------------------+-----------+ """), ) def testExpandedTableComparisonConflictingContexts(self): msg2 = self.builder.WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg2.data.context = self.builder.OUTPUTS with self.assertRaises(ConflictingContextsException): self.msg.compare_tables(msg2, formats=None, expand_nested=True) msg3 = self.builder.WRITE_REGISTER_REQUEST_V2(addr="x60000001", data="x80000000") msg3.data.context = self.builder.OUTPUTS with self.assertRaises(ConflictingContextsException): self.msg.compare_tables(msg3, formats=None, expand_nested=True) def testCollapsedTableComparisonConflictingContexts(self): msg2 = self.builder.WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000") msg2.data.context = self.builder.OUTPUTS with self.assertRaises(ConflictingContextsException): self.msg.compare_tables(msg2, formats=None, expand_nested=False) msg3 = self.builder.WRITE_REGISTER_REQUEST_V2(addr="x60000001", data="x80000000") msg3.data.context = self.builder.OUTPUTS comp = self.msg.compare_tables(msg3, formats=None, expand_nested=False)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +------------+-----------+ +------------+-----------+ | Field Name | Value | | Field Name | Value | +------------+-----------+ +------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field | xe0000001 | == | or_field | xe0000001 | | addr | x60000001 | == | addr | x60000001 | | data | x80000000 | == | data | x80000000 | +------------+-----------+ +------------+-----------+ """), ) def testCollapsedTableComparisonSharingContexts(self): msg2 = self.builder.WRITE_REGISTER_REQUEST_V2(addr="x60000001", data="x80000000") msg2.or_field.context = self.builder.RANDOM_MEANING comp = self.msg.compare_tables(msg2, formats=None, expand_nested=False)[1] self.assertEqual( remove_whitespace(comp), remove_whitespace(""" +------------+-----------+ +------------+-----------+ | Field Name | Value | | Field Name | Value | +------------+-----------+ +------------+-----------+ | mid | x0016 | == | mid | x0016 | | or_field | xe0000001 | == | or_field | xe0000001 | | addr | x60000001 | == | addr | x60000001 | | data | x80000000 | == | data | x80000000 | +------------+-----------+ +------------+-----------+ """), ) @unittest.expectedFailure def testExpandedTableComparisonSharingContexts(self): msg2 = self.builder.WRITE_REGISTER_REQUEST_V2(addr="x60000001", data="x80000000") msg2.or_field.context = self.builder.RANDOM_MEANING comp = self.msg.compare_tables(msg2, formats=None, expand_nested=True)[1]