|
|
@@ -0,0 +1,91 @@
|
|
|
+import pathlib
|
|
|
+
|
|
|
+test_file = pathlib.Path(__file__)
|
|
|
+test_dir = test_file.parent
|
|
|
+project_dir = test_dir.parent
|
|
|
+
|
|
|
+import sys
|
|
|
+sys.path.append(str(project_dir.absolute()))
|
|
|
+
|
|
|
+import source as communication
|
|
|
+
|
|
|
+class sample_message(communication.message):
|
|
|
+ _type = 0x0010
|
|
|
+
|
|
|
+ def __init__(self) -> None:
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+ self._add_field(communication.field("x_axis", int, 2))
|
|
|
+ self._add_field(communication.field("y_axis", int, 2))
|
|
|
+ self._add_field(communication.field("z_axis", int, 2))
|
|
|
+
|
|
|
+def main():
|
|
|
+ print("Starting test.")
|
|
|
+
|
|
|
+ print("Sample message size: " + str(sample_message().size))
|
|
|
+ print("Setting test message.")
|
|
|
+
|
|
|
+ testing = sample_message()
|
|
|
+ testing.set_x_axis(10)
|
|
|
+ testing.set_y_axis(20)
|
|
|
+ testing.set_z_axis(30)
|
|
|
+
|
|
|
+ print("Encoding.")
|
|
|
+
|
|
|
+ encoded = testing.encoder().code()
|
|
|
+
|
|
|
+ print("Encoded lenght: " + str(len(encoded)))
|
|
|
+ print("Encoded: " + str(encoded))
|
|
|
+
|
|
|
+ print("Decoding.")
|
|
|
+
|
|
|
+ decoded = communication.message_decoder(encoded).build(sample_message)
|
|
|
+
|
|
|
+ print("Checking.")
|
|
|
+
|
|
|
+ try:
|
|
|
+ if decoded.get_x_axis() != 10:
|
|
|
+ raise ValueError("X asis not validated.")
|
|
|
+
|
|
|
+ if decoded.get_y_axis() != 20:
|
|
|
+ raise ValueError("Y asis not validated.")
|
|
|
+
|
|
|
+ if decoded.get_z_axis() != 30:
|
|
|
+ raise ValueError("Z asis not validated.")
|
|
|
+
|
|
|
+ print("Validated property.")
|
|
|
+
|
|
|
+ except ValueError as error:
|
|
|
+ print("Not validated, error: " + str(error))
|
|
|
+
|
|
|
+ print("Checking decoder validation.")
|
|
|
+ print("Adding errors to coded message.")
|
|
|
+
|
|
|
+ too_long = encoded + bytes(10)
|
|
|
+ with_bad_id = int(2).to_bytes(2) + encoded[2:]
|
|
|
+ broken_message = encoded[0:5] + int(2).to_bytes(1) + encoded[6:]
|
|
|
+
|
|
|
+ try:
|
|
|
+ print("Testing too long message.")
|
|
|
+ communication.message_decoder(too_long).build(sample_message)
|
|
|
+ except Exception as error:
|
|
|
+ print("Work, raised: " + str(error))
|
|
|
+
|
|
|
+ try:
|
|
|
+ print("Testing message with bad id.")
|
|
|
+ communication.message_decoder(with_bad_id).build(sample_message)
|
|
|
+ except Exception as error:
|
|
|
+ print("Work, raised: " + str(error))
|
|
|
+
|
|
|
+ try:
|
|
|
+ print("Testing message with broken content")
|
|
|
+ communication.message_decoder(broken_message).build(sample_message)
|
|
|
+ except Exception as error:
|
|
|
+ print("Work, raised: " + str(error))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|