def testMessageObjectLength(self):
     builder = MessageBuilder()
     OUTPUTS = builder.build_message_class("OUTPUTS", register_defs["OUTPUTS"])
     outputs = OUTPUTS(reset1="b1", reset2="b0", cautions="x00")
     self.assertTrue(len(outputs.reset1) == 1)
     self.assertTrue(len(outputs.reset2) == 1)
     self.assertTrue(len(outputs.cautions) == 8)
     self.assertTrue(len(outputs.unused) == 22)
     self.assertTrue(OUTPUTS.bit_length == 32)
Beispiel #2
0
class TestMessageBuilder(unittest.TestCase):
    def setUp(self):
        self.builder = MessageBuilder(msg_fmts)

    def testInstantiaion(self):
        pass  # instantiation is covered in the setup. Just want a simple pass here.

    def testDefinitionLoading(self):
        GET_ADDR = self.builder.GET_ADDR
        self.assertTrue(isinstance(GET_ADDR, type))

    def testInvalidDefinition(self):
        with self.assertRaises(InvalidFieldException):
            self.builder.build_message_class("INVALID_DEF",
                                             invalid_def["INVALID_DEF"])

    def testMessageProduction_Normal(self):
        wrt_req_1 = self.builder.build_message("x001600089999999900000000")

    def testMessageProduction_MultipleMatches(self):
        with self.assertRaises(MultipleMatchingMessageDefinitionsException):
            wrt_req_1 = self.builder.build_message("x00150004FFFFFFFF")
    def testNestedFields(self):
        WRITE_REGISTER_REQUEST = self.builder.WRITE_REGISTER_REQUEST
        builder = MessageBuilder(register_defs)
        OUTPUTS = builder.build_message_class("OUTPUTS",
                                              register_defs["OUTPUTS"])
        INPUTS = builder.build_message_class("INPUTS", register_defs["INPUTS"])

        def verify_msg_outputs(msg):
            self.assertEqual(msg.data.context, OUTPUTS)
            self.assertEqual(type(msg.data), OUTPUTS)
            self.assertEqual(msg.data.reset1, "b1")
            self.assertEqual(msg.data.reset2, "b0")
            self.assertEqual(msg.data.cautions, "x00")
            self.assertEqual(msg.data.unused, "x0")

        def verify_msg_inputs(msg):
            self.assertEqual(msg.data.context, INPUTS)
            self.assertEqual(type(msg.data), INPUTS)
            self.assertEqual(msg.data.service_req, "b1")
            self.assertEqual(msg.data.voltage_ready, "b0")
            self.assertEqual(msg.data.exit_code, "x0000")
            self.assertEqual(msg.data.last_command_mid, "x0")
            self.assertEqual(msg.data.unused, "x00")

        send_msg_1 = WRITE_REGISTER_REQUEST(addr="x60000001", data="x80000000")
        send_msg_1.data.context = OUTPUTS
        verify_msg_outputs(send_msg_1)
        send_msg_1.data.context = INPUTS
        verify_msg_inputs(send_msg_1)

        send_msg_2 = WRITE_REGISTER_REQUEST(addr="x60000001",
                                            data=OUTPUTS(reset1="b1",
                                                         reset2="b0",
                                                         cautions="x00"))
        verify_msg_outputs(send_msg_2)
        send_msg_2.data.context = INPUTS
        verify_msg_inputs(send_msg_2)
 def testConstructionFromDefinition_ReadOnlyField(self):
     builder = MessageBuilder()
     OUTPUTS = builder.build_message_class("OUTPUTS",
                                           register_defs["OUTPUTS"])
     with self.assertRaises(InvalidFieldException):
         OUTPUTS(reset1="b1", reset2="b0", cautions="x00", unused="x000")
 def testConstructionFromDefinition_MissingArg(self):
     builder = MessageBuilder()
     OUTPUTS = builder.build_message_class("OUTPUTS",
                                           register_defs["OUTPUTS"])
     with self.assertRaises(MissingFieldDataException):
         OUTPUTS(reset1="b1")
 def testConstructionFromData_Invalid(self):
     builder = MessageBuilder()
     OUTPUTS = builder.build_message_class("OUTPUTS",
                                           register_defs["OUTPUTS"])
     with self.assertRaises(InvalidDataFormatException):
         OUTPUTS.from_data("b11100000000000000000000000000000111")
 def testMessageClassLength(self):
     builder = MessageBuilder()
     OUTPUTS = builder.build_message_class("OUTPUTS", register_defs["OUTPUTS"])
     self.assertTrue(len(OUTPUTS) == 32)
Beispiel #8
0
    def testFormatFieldRepr(self):
        builder = MessageBuilder()
        OUTPUTS = builder.build_message_class("OUTPUTS", register_defs["OUTPUTS"])

        self.assertTrue("Byte" in repr(OUTPUTS.format["cautions"]))