Parcourir la source

Test casting a JsonValue to an int

Benoit Blanchon il y a 11 ans
Parent
commit
88510705be
2 fichiers modifiés avec 16 ajouts et 4 suppressions
  1. 5 0
      JsonGenerator/JsonValue.h
  2. 11 4
      JsonGeneratorTests/JsonValue_Cast_Tests.cpp

+ 5 - 0
JsonGenerator/JsonValue.h

@@ -64,6 +64,11 @@ namespace ArduinoJson
                 return "";
             }
 
+            operator int()
+            {
+                return content.asLong;
+            }
+
             size_t printTo(Print& p) const
             {
                 // handmade polymorphism

+ 11 - 4
JsonGeneratorTests/JsonValue_Cast_Tests.cpp

@@ -21,15 +21,22 @@ namespace JsonGeneratorTests
 
         TEST_METHOD(String)
         {            
-            value = "hello";
-            mustCastTo("hello");
+            setValueAndCheckCast("hello");
         }
 
+        TEST_METHOD(Integer)
+        {
+            setValueAndCheckCast(42);
+        }
+
+
     private:
 
-        void mustCastTo(const char* expected)
+        template<typename T>
+        void setValueAndCheckCast(T expected)
         {
-            const char* actual = value;
+            value = expected;
+            T actual = value;
             Assert::AreEqual(expected, actual);
         }
     };