From 8ac7373d9f4e675256c384ba7ce91a6d58742edf Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周一, 24 8月 2015 06:03:57 +0800
Subject: [PATCH] Resolve #40: Fix SCPI_ParamCopyText and add test

---
 libscpi/src/parser.c       |    4 ++++
 libscpi/test/test_parser.c |   41 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index 3475265..6a7b776 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -770,6 +770,10 @@
                         i_from++;
                     }
                 }
+                *copy_len = i_to;
+                if (i_to < buffer_len) {
+                    buffer[i_to] = 0;
+                }
                 break;
             default:
                 SCPI_ErrorPush(context, SCPI_ERROR_DATA_TYPE_ERROR);
diff --git a/libscpi/test/test_parser.c b/libscpi/test/test_parser.c
index 4b3111d..5365f84 100644
--- a/libscpi/test/test_parser.c
+++ b/libscpi/test/test_parser.c
@@ -407,6 +407,40 @@
 }
 
 
+#define TEST_ParamCopyText(data, mandatory, expected_value, expected_len, expected_result, expected_error_code) \
+{                                                                                       \
+    char value[100];                                                                    \
+    size_t value_len;                                                                   \
+    scpi_bool_t result;                                                                 \
+    int16_t errCode;                                                                    \
+                                                                                        \
+    SCPI_CoreCls(&scpi_context);                                                        \
+    scpi_context.input_count = 0;                                                       \
+    scpi_context.param_list.lex_state.buffer = data;                                    \
+    scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\
+    scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer;   \
+    result = SCPI_ParamCopyText(&scpi_context, value, sizeof(value), &value_len, mandatory);\
+    /*printf("%.*s\r\n",  (int)value_len, value);*/                                     \
+    errCode = SCPI_ErrorPop(&scpi_context);                                             \
+    CU_ASSERT_EQUAL(result, expected_result);                                           \
+    if (expected_result) {                                                              \
+        CU_ASSERT_STRING_EQUAL(value, expected_value);                                  \
+        CU_ASSERT_EQUAL(value_len, expected_len);                                       \
+    }                                                                                   \
+    CU_ASSERT_EQUAL(errCode, expected_error_code);                                      \
+}
+
+static void testSCPI_ParamCopyText(void) {
+    TEST_ParamCopyText("\'abc\'", TRUE, "abc", 3, TRUE, 0);
+    TEST_ParamCopyText("\"abc\"", TRUE, "abc", 3, TRUE, 0);
+    TEST_ParamCopyText("\'a\'", TRUE, "a", 1, TRUE, 0);
+    TEST_ParamCopyText("\'a\'\'c\'", TRUE, "a\'c", 3, TRUE, 0);
+    TEST_ParamCopyText("\'a\"c\'", TRUE, "a\"c", 3, TRUE, 0);
+    TEST_ParamCopyText("\"a\"\"c\"", TRUE, "a\"c", 3, TRUE, 0);
+    TEST_ParamCopyText("\"a\'c\"", TRUE, "a\'c", 3, TRUE, 0);
+}
+
+
 int main() {
     CU_pSuite pSuite = NULL;
 
@@ -425,9 +459,10 @@
     if ((NULL == CU_add_test(pSuite, "SCPI_ParamInt", testSCPI_ParamInt))
             || (NULL == CU_add_test(pSuite, "SCPI_ParamDouble", testSCPI_ParamDouble))
             || (NULL == CU_add_test(pSuite, "SCPI_ParamCharacters", testSCPI_ParamCharacters))
-	    || (NULL == CU_add_test(pSuite, "Commands handling", testCommandsHandling))
-	    || (NULL == CU_add_test(pSuite, "Error handling", testErrorHandling))
-	    || (NULL == CU_add_test(pSuite, "IEEE 488.2 Mandatory commands", testIEEE4882))
+            || (NULL == CU_add_test(pSuite, "SCPI_ParamCopyText", testSCPI_ParamCopyText))
+            || (NULL == CU_add_test(pSuite, "Commands handling", testCommandsHandling))
+            || (NULL == CU_add_test(pSuite, "Error handling", testErrorHandling))
+            || (NULL == CU_add_test(pSuite, "IEEE 488.2 Mandatory commands", testIEEE4882))
             ) {
         CU_cleanup_registry();
         return CU_get_error();

--
Gitblit v1.9.1