浏览代码

tests: assertion macros

Max Bruckner 8 年之前
父节点
当前提交
c6e1a281f9
共有 6 个文件被更改,包括 70 次插入58 次删除
  1. 14 0
      tests/common.h
  2. 24 24
      tests/parse_array.c
  3. 7 8
      tests/parse_number.c
  4. 6 7
      tests/parse_object.c
  5. 14 13
      tests/parse_string.c
  6. 5 6
      tests/parse_value.c

+ 14 - 0
tests/common.h

@@ -28,4 +28,18 @@
 extern void reset(cJSON *item);
 extern void reset(cJSON *item);
 extern char *read_file(const char *filename);
 extern char *read_file(const char *filename);
 
 
+/* assertion helper macros */
+#define assert_has_type(item, item_type) TEST_ASSERT_BITS_MESSAGE(0xFF, item_type, item->type, "Item doesn't have expected type.")
+#define assert_has_no_reference(item) TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, item->type, "Item should not have a string as reference.")
+#define assert_has_no_const_string(item) TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, item->type, "Item should not have a const string.")
+#define assert_has_valuestring(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->valuestring, "Valuestring is NULL.")
+#define assert_has_no_valuestring(item) TEST_ASSERT_NULL_MESSAGE(item->valuestring, "Valuestring is not NULL.")
+#define assert_has_string(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->string, "String is NULL")
+#define assert_has_no_string(item) TEST_ASSERT_NULL_MESSAGE(item->string, "String is not NULL.")
+#define assert_not_in_list(item) \
+	TEST_ASSERT_NULL_MESSAGE(item->next, "Linked list next pointer is not NULL.");\
+	TEST_ASSERT_NULL_MESSAGE(item->prev, "Linked list previous pointer is not NULL.")
+#define assert_has_child(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->child, "Item doesn't have a child.")
+#define assert_has_no_child(item) TEST_ASSERT_NULL_MESSAGE(item->child, "Item has a child.")
+
 #endif
 #endif

+ 24 - 24
tests/parse_array.c

@@ -32,17 +32,16 @@ static cJSON item[1];
 
 
 static const unsigned char *error_pointer = NULL;
 static const unsigned char *error_pointer = NULL;
 
 
-static void assert_is_array(cJSON *string_item)
+static void assert_is_array(cJSON *array_item)
 {
 {
-    TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
-
-    TEST_ASSERT_NULL_MESSAGE(string_item->next, "Linked list next pointer is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(string_item->prev, "Linked list previous pointer is not NULL");
-    TEST_ASSERT_BITS_MESSAGE(0xFF, cJSON_Array, string_item->type, "Item type is not array.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, string_item->type, "Item should not have a string as reference.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, string_item->type, "Item should not have a const string.");
-    TEST_ASSERT_NULL_MESSAGE(string_item->valuestring, "Valuestring is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(string_item->string, "String is not NULL.");
+    TEST_ASSERT_NOT_NULL_MESSAGE(array_item, "Item is NULL.");
+
+    assert_not_in_list(array_item);
+    assert_has_type(array_item, cJSON_Array);
+    assert_has_no_reference(array_item);
+    assert_has_no_const_string(array_item);
+    assert_has_no_valuestring(array_item);
+    assert_has_no_string(array_item);
 }
 }
 
 
 static void assert_not_array(const char *json)
 static void assert_not_array(const char *json)
@@ -59,9 +58,10 @@ static void assert_parse_array(const char *json)
 static void parse_array_should_parse_empty_arrays(void)
 static void parse_array_should_parse_empty_arrays(void)
 {
 {
     assert_parse_array("[]");
     assert_parse_array("[]");
-    TEST_ASSERT_NULL(item->child);
+    assert_has_no_child(item);
+
     assert_parse_array("[\n\t]");
     assert_parse_array("[\n\t]");
-    TEST_ASSERT_NULL(item->child);
+    assert_has_no_child(item);
 }
 }
 
 
 
 
@@ -69,38 +69,38 @@ static void parse_array_should_parse_arrays_with_one_element(void)
 {
 {
 
 
     assert_parse_array("[1]");
     assert_parse_array("[1]");
-    TEST_ASSERT_NOT_NULL(item->child);
-    TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type);
+    assert_has_child(item);
+    assert_has_type(item->child, cJSON_Number);
     reset(item);
     reset(item);
 
 
     assert_parse_array("[\"hello!\"]");
     assert_parse_array("[\"hello!\"]");
-    TEST_ASSERT_NOT_NULL(item->child);
-    TEST_ASSERT_BITS(0xFF, cJSON_String, item->child->type);
+    assert_has_child(item);
+    assert_has_type(item->child, cJSON_String);
     TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring);
     TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring);
     reset(item);
     reset(item);
 
 
     assert_parse_array("[[]]");
     assert_parse_array("[[]]");
-    TEST_ASSERT_NOT_NULL(item->child);
+    assert_has_child(item);
     assert_is_array(item->child);
     assert_is_array(item->child);
-    TEST_ASSERT_NULL(item->child->child);
+    assert_has_no_child(item->child);
     reset(item);
     reset(item);
 
 
     assert_parse_array("[null]");
     assert_parse_array("[null]");
-    TEST_ASSERT_NOT_NULL(item->child);
-    TEST_ASSERT_BITS(0xFF, cJSON_NULL, item->child->type);
+    assert_has_child(item);
+    assert_has_type(item->child, cJSON_NULL);
     reset(item);
     reset(item);
 }
 }
 
 
 static void parse_array_should_parse_arrays_with_multiple_elements(void)
 static void parse_array_should_parse_arrays_with_multiple_elements(void)
 {
 {
     assert_parse_array("[1\t,\n2, 3]");
     assert_parse_array("[1\t,\n2, 3]");
-    TEST_ASSERT_NOT_NULL(item->child);
+    assert_has_child(item);
     TEST_ASSERT_NOT_NULL(item->child->next);
     TEST_ASSERT_NOT_NULL(item->child->next);
     TEST_ASSERT_NOT_NULL(item->child->next->next);
     TEST_ASSERT_NOT_NULL(item->child->next->next);
     TEST_ASSERT_NULL(item->child->next->next->next);
     TEST_ASSERT_NULL(item->child->next->next->next);
-    TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->type);
-    TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->type);
-    TEST_ASSERT_BITS(0xFF, cJSON_Number, item->child->next->next->type);
+    assert_has_type(item->child, cJSON_Number);
+    assert_has_type(item->child->next, cJSON_Number);
+    assert_has_type(item->child->next->next, cJSON_Number);
     reset(item);
     reset(item);
 
 
     {
     {

+ 7 - 8
tests/parse_number.c

@@ -34,14 +34,13 @@ static void assert_is_number(cJSON *number_item)
 {
 {
     TEST_ASSERT_NOT_NULL_MESSAGE(number_item, "Item is NULL.");
     TEST_ASSERT_NOT_NULL_MESSAGE(number_item, "Item is NULL.");
 
 
-    TEST_ASSERT_NULL_MESSAGE(number_item->next, "Linked list next pointer is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(number_item->prev, "Linked list previous pointer is not NULL");
-    TEST_ASSERT_NULL_MESSAGE(number_item->child, "Child pointer is not NULL.");
-    TEST_ASSERT_BITS_MESSAGE(0xFF, cJSON_Number, number_item->type, "Message type is not number.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, number_item->type, "Item should not have a string as reference.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, number_item->type, "Item should not have a const string.");
-    TEST_ASSERT_NULL_MESSAGE(number_item->valuestring, "Valuestring is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(number_item->string, "String is not NULL.");
+    assert_not_in_list(number_item);
+    assert_has_no_child(number_item);
+    assert_has_type(number_item, cJSON_Number);
+    assert_has_no_reference(number_item);
+    assert_has_no_const_string(number_item);
+    assert_has_no_valuestring(number_item);
+    assert_has_no_string(number_item);
 }
 }
 
 
 static void assert_parse_number(const char *string, int integer, double real)
 static void assert_parse_number(const char *string, int integer, double real)

+ 6 - 7
tests/parse_object.c

@@ -36,13 +36,12 @@ static void assert_is_object(cJSON *object_item)
 {
 {
     TEST_ASSERT_NOT_NULL_MESSAGE(object_item, "Item is NULL.");
     TEST_ASSERT_NOT_NULL_MESSAGE(object_item, "Item is NULL.");
 
 
-    TEST_ASSERT_NULL_MESSAGE(object_item->next, "Linked list next pointer is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(object_item->prev, "Linked list previous pointer is not NULL");
-    TEST_ASSERT_BITS_MESSAGE(0xFF, cJSON_Object, object_item->type, "Item type is not object.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, object_item->type, "Item should not have a string as reference.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, object_item->type, "Item should not have a const string.");
-    TEST_ASSERT_NULL_MESSAGE(object_item->valuestring, "Valuestring is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(object_item->string, "String is not NULL.");
+    assert_not_in_list(object_item);
+    assert_has_type(object_item, cJSON_Object);
+    assert_has_no_reference(object_item);
+    assert_has_no_const_string(object_item);
+    assert_has_no_valuestring(object_item);
+    assert_has_no_string(object_item);
 }
 }
 
 
 static void assert_is_child(cJSON *child_item, const char *name, int type)
 static void assert_is_child(cJSON *child_item, const char *name, int type)

+ 14 - 13
tests/parse_string.c

@@ -36,14 +36,13 @@ static void assert_is_string(cJSON *string_item)
 {
 {
     TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
     TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
 
 
-    TEST_ASSERT_NULL_MESSAGE(string_item->next, "Linked list next pointer is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(string_item->prev, "Linked list previous pointer is not NULL");
-    TEST_ASSERT_NULL_MESSAGE(string_item->child, "Child pointer is not NULL.");
-    TEST_ASSERT_BITS_MESSAGE(0xFF, cJSON_String, string_item->type, "Item type is not string.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 1, string_item->type, "Item should have a string as reference.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, string_item->type, "Item should not have a const string.");
-    TEST_ASSERT_NOT_NULL_MESSAGE(string_item->valuestring, "Valuestring is NULL.");
-    TEST_ASSERT_NULL_MESSAGE(string_item->string, "String is not NULL.");
+    assert_not_in_list(string_item);
+    assert_has_no_child(string_item);
+    assert_has_type(string_item, cJSON_String);
+    assert_has_no_reference(string_item);
+    assert_has_no_const_string(string_item);
+    assert_has_valuestring(string_item);
+    assert_has_no_string(string_item);
 }
 }
 
 
 static void assert_parse_string(const char *string, const char *expected)
 static void assert_parse_string(const char *string, const char *expected)
@@ -55,6 +54,8 @@ static void assert_parse_string(const char *string, const char *expected)
     item->valuestring = NULL;
     item->valuestring = NULL;
 }
 }
 
 
+#define assert_not_parse_string(string) TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Malformed string should not be accepted")
+
 static void parse_string_should_parse_strings(void)
 static void parse_string_should_parse_strings(void)
 {
 {
     assert_parse_string("\"\"", "");
     assert_parse_string("\"\"", "");
@@ -74,19 +75,19 @@ static void parse_string_should_parse_utf16_surrogate_pairs(void)
 
 
 static void parse_string_should_not_parse_non_strings(void)
 static void parse_string_should_not_parse_non_strings(void)
 {
 {
-    TEST_ASSERT_NULL(parse_string(item, (const unsigned char*)"this\" is not a string\"", &error_pointer));
-    TEST_ASSERT_NULL(parse_string(item, (const unsigned char*) "", &error_pointer));
+    assert_not_parse_string("this\" is not a string\"");
+    assert_not_parse_string("");
 }
 }
 
 
 static void parse_string_should_not_parse_invalid_backslash(void)
 static void parse_string_should_not_parse_invalid_backslash(void)
 {
 {
-    TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)"Abcdef\\123", &error_pointer), "Invalid backshlash should not be accepted.");
-    TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)"Abcdef\\e23", &error_pointer), "Invalid backshlash should not be accepted.");
+    assert_not_parse_string("Abcdef\\123");
+    assert_not_parse_string("Abcdef\\e23");
 }
 }
 
 
 static void parse_string_should_not_overflow_with_closing_backslash(void)
 static void parse_string_should_not_overflow_with_closing_backslash(void)
 {
 {
-    TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)"\"000000000000000000\\", &error_pointer), "Malformed string should not be accepted.");
+    assert_not_parse_string("\"000000000000000000\\");
 }
 }
 
 
 static void parse_string_should_parse_bug_94(void)
 static void parse_string_should_parse_bug_94(void)

+ 5 - 6
tests/parse_value.c

@@ -35,12 +35,11 @@ static void assert_is_value(cJSON *value_item, int type)
 {
 {
     TEST_ASSERT_NOT_NULL_MESSAGE(value_item, "Item is NULL.");
     TEST_ASSERT_NOT_NULL_MESSAGE(value_item, "Item is NULL.");
 
 
-    TEST_ASSERT_NULL_MESSAGE(value_item->next, "Linked list next pointer is not NULL.");
-    TEST_ASSERT_NULL_MESSAGE(value_item->prev, "Linked list previous pointer is not NULL");
-    TEST_ASSERT_BITS_MESSAGE(0xFF, type, value_item->type, "Message type is not number.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, value_item->type, "Item should not have a string as reference.");
-    TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, value_item->type, "Item should not have a const string.");
-    TEST_ASSERT_NULL_MESSAGE(value_item->string, "String is not NULL.");
+    assert_not_in_list(value_item);
+    assert_has_type(value_item, type);
+    assert_has_no_reference(value_item);
+    assert_has_no_const_string(value_item);
+    assert_has_no_string(value_item);
 }
 }
 
 
 static void assert_parse_value(const char *string, int type)
 static void assert_parse_value(const char *string, int type)