Prechádzať zdrojové kódy

add test for packet formatters

Christopher Mullins 7 rokov pred
rodič
commit
21decdbbe4
1 zmenil súbory, kde vykonal 98 pridanie a 2 odobranie
  1. 98 2
      test/d1_mini/test_state.cpp

+ 98 - 2
test/d1_mini/test_state.cpp

@@ -2,13 +2,107 @@
 
 #include <FS.h>
 #include <Arduino.h>
-#include "unity.h"
 
 #include <GroupState.h>
 #include <GroupStateStore.h>
 #include <GroupStateCache.h>
 #include <GroupStatePersistence.h>
 
+#include <RgbCctPacketFormatter.h>
+#include <FUT091PacketFormatter.h>
+#include <Units.h>
+
+#include "unity.h"
+
+//================================================================================
+// Packet formatter
+//================================================================================
+
+template <typename T>
+void run_packet_test(uint8_t* packet, PacketFormatter* packetFormatter, const BulbId& expectedBulbId, const String& expectedKey, const T expectedValue) {
+  GroupStateStore stateStore(10, 0);
+  Settings settings;
+  RgbCctPacketFormatter formatter;
+  DynamicJsonBuffer jsonBuffer;
+  JsonObject& result = jsonBuffer.createObject();
+
+  packetFormatter->prepare(0, 0, &stateStore, &settings);
+  BulbId bulbId = packetFormatter->parsePacket(packet, result);
+
+  TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.deviceId, bulbId.deviceId, "Should get the expected device ID");
+  TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.groupId, bulbId.groupId, "Should get the expected group ID");
+  TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.deviceType, bulbId.deviceType, "Should get the expected remote type");
+
+  TEST_ASSERT_TRUE_MESSAGE(result.containsKey(expectedKey), "Parsed packet should be for expected command type");
+  TEST_ASSERT_TRUE_MESSAGE(result[expectedKey] == expectedValue, "Parsed packet should have expected value");
+}
+
+void test_fut092_packet_formatter() {
+  RgbCctPacketFormatter packetFormatter;
+
+  uint8_t onPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x66, 0xCA, 0x54, 0x66, 0xD2};
+  run_packet_test(
+    onPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_RGB_CCT), 
+    "state", 
+    "OFF"
+  );
+
+  uint8_t minColorTempPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x64, 0x3C, 0x47, 0x66, 0x31};
+  run_packet_test(
+    minColorTempPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_RGB_CCT), 
+    "color_temp", 
+    COLOR_TEMP_MIN_MIREDS
+  );
+
+  uint8_t maxColorTempPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x64, 0x94, 0x62, 0x66, 0x88};
+  run_packet_test(
+    maxColorTempPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_RGB_CCT), 
+    "color_temp", 
+    COLOR_TEMP_MAX_MIREDS
+  );
+}
+
+void test_fut091_packet_formatter() {
+  FUT091PacketFormatter packetFormatter;
+
+  uint8_t onPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x66, 0xCA, 0xBA, 0x66, 0xB5};
+  run_packet_test(
+    onPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_FUT091), 
+    "state", 
+    "OFF"
+  );
+
+  uint8_t minColorTempPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x64, 0x8D, 0xB9, 0x66, 0x71};
+  run_packet_test(
+    minColorTempPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_FUT091), 
+    "color_temp", 
+    COLOR_TEMP_MIN_MIREDS
+  );
+
+  uint8_t maxColorTempPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x64, 0x55, 0xB7, 0x66, 0x27};
+  run_packet_test(
+    maxColorTempPacket, 
+    &packetFormatter, 
+    BulbId(1, 1, REMOTE_TYPE_FUT091), 
+    "color_temp", 
+    COLOR_TEMP_MAX_MIREDS
+  );
+}
+
+//================================================================================
+// Group State
+//================================================================================
+
 GroupState color() {
   GroupState s;
 
@@ -241,6 +335,9 @@ void setup() {
   RUN_TEST(test_store);
   RUN_TEST(test_group_0);
 
+  RUN_TEST(test_fut091_packet_formatter);
+  RUN_TEST(test_fut092_packet_formatter);
+
   UNITY_END();
 }
 
@@ -248,5 +345,4 @@ void loop() {
   // nothing to be done here.
 }
 
-
 // #endif