]> granicus.if.org Git - esp-idf/commitdiff
unit-test-app: Fix capability to use !<filter> to inverse that filter
authorAnton Maklakov <anton@espressif.com>
Thu, 1 Jun 2017 04:11:45 +0000 (12:11 +0800)
committerAnton Maklakov <anton@espressif.com>
Thu, 1 Jun 2017 04:23:14 +0000 (12:23 +0800)
tools/unit-test-app/components/unity/unity_platform.c

index fad3e3e59571f4b2b33bdf63637d09b4e9909341..229d80b5b54e3e7c9875f29867ccd3c0bc812217 100644 (file)
@@ -16,6 +16,8 @@
 static struct test_desc_t* s_unity_tests_first = NULL;
 static struct test_desc_t* s_unity_tests_last = NULL;
 
+// Inverse of the filter
+static bool s_invert = false;
 
 void unity_putc(int c)
 {
@@ -73,8 +75,33 @@ static void unity_run_single_test_by_index(int index)
     
 }
 
+static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
+{
+    if (s_invert)
+    {
+        printf("Inverse is not supported for that kind of filter\n");
+        return;
+    }
+    int test_index = strtol(filter, NULL, 10);
+    if (test_index >= 1 && test_index <= index_max)
+    {
+        uint32_t start;
+        RSR(CCOUNT, start);
+        unity_run_single_test_by_index(test_index - 1);
+        uint32_t end;
+        RSR(CCOUNT, end);
+        uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000);
+        printf("Test ran in %dms\n", ms);
+    }
+}
+
 static void unity_run_single_test_by_name(const char* filter)
-{  
+{
+    if (s_invert)
+    {
+        printf("Inverse is not supported for that kind of filter\n");
+        return;
+    }
     char tmp[256];
     strncpy(tmp, filter + 1, sizeof(tmp) - 1);
     tmp[strlen(filter) - 2] = 0;
@@ -89,6 +116,11 @@ static void unity_run_single_test_by_name(const char* filter)
 
 void unity_run_all_tests()
 {
+    if (s_invert)
+    {
+        printf("Inverse is not supported for that kind of filter\n");
+        return;
+    }
     for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
     {
         unity_run_single_test(test);
@@ -97,13 +129,14 @@ void unity_run_all_tests()
 
 void unity_run_tests_with_filter(const char* filter)
 {
-    bool invert = filter[0] == '!';
-    if (invert) {
-        filter++;
+    if (s_invert)
+    {
+        ++filter;
     }
+
     for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
     {
-        if ((strstr(test->desc, filter) != NULL) == !invert)
+        if ((strstr(test->desc, filter) != NULL) == !s_invert)
         {
             unity_run_single_test(test);
         }
@@ -155,31 +188,32 @@ void unity_run_menu()
 
         UNITY_BEGIN();
 
-        if (cmdline[0] == '*')
+        size_t idx = 0;
+        if (cmdline[idx] == '!')
+        {
+            s_invert = true;
+            ++idx;
+        }
+        else
+        {
+            s_invert = false;
+        }
+
+        if (cmdline[idx] == '*')
         {
             unity_run_all_tests();
         }
-        else if (cmdline[0] =='[')
+        else if (cmdline[idx] =='[')
         {
-            unity_run_tests_with_filter(cmdline);
+            unity_run_tests_with_filter(cmdline + idx);
         }
-        else if (cmdline[0] =='"')
+        else if (cmdline[idx] =='"')
         {
-            unity_run_single_test_by_name(cmdline);
+            unity_run_single_test_by_name(cmdline + idx);
         }
-        else
+        else if (isdigit((unsigned char)cmdline[idx]))
         {
-            int test_index = strtol(cmdline, NULL, 10);
-            if (test_index >= 1 && test_index <= test_count)
-            {
-                uint32_t start;
-                RSR(CCOUNT, start);
-                unity_run_single_test_by_index(test_index - 1);
-                uint32_t end;
-                RSR(CCOUNT, end);
-                uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000);
-                printf("Test ran in %dms\n", ms);
-            }
+            unity_run_single_test_by_index_parse(cmdline + idx, test_count);
         }
 
         UNITY_END();
@@ -187,4 +221,3 @@ void unity_run_menu()
         printf("Enter next test, or 'enter' to see menu\n");
     }
 }
-