about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/tools/parser.c2
-rw-r--r--tests/test_parser.c131
2 files changed, 131 insertions, 2 deletions
diff --git a/src/tools/parser.c b/src/tools/parser.c
index b679b9f2..595032c3 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -367,5 +367,3 @@ get_start(char *string, int tokens)
 
     return result_str;
 }
-
-
diff --git a/tests/test_parser.c b/tests/test_parser.c
index 58e3f3e7..e442baee 100644
--- a/tests/test_parser.c
+++ b/tests/test_parser.c
@@ -279,6 +279,124 @@ parse_cmd_freetext_with_many_quoted_and_many_spaces(void)
     assert_string_equals("and heres the free text", result[2]);
     g_strfreev(result);
 }
+
+void
+count_one_token(void)
+{
+    char *inp = "one";
+    int result = count_tokens(inp);
+
+    assert_int_equals(1, result);
+}
+
+void
+count_one_token_quoted_no_whitespace(void)
+{
+    char *inp = "\"one\"";
+    int result = count_tokens(inp);
+
+    assert_int_equals(1, result);
+}
+
+void
+count_one_token_quoted_with_whitespace(void)
+{
+    char *inp = "\"one two\"";
+    int result = count_tokens(inp);
+
+    assert_int_equals(1, result);
+}
+
+void
+count_two_tokens(void)
+{
+    char *inp = "one two";
+    int result = count_tokens(inp);
+
+    assert_int_equals(2, result);
+}
+
+void
+count_two_tokens_first_quoted(void)
+{
+    char *inp = "\"one and\" two";
+    int result = count_tokens(inp);
+
+    assert_int_equals(2, result);
+}
+
+void
+count_two_tokens_second_quoted(void)
+{
+    char *inp = "one \"two and\"";
+    int result = count_tokens(inp);
+
+    assert_int_equals(2, result);
+}
+
+void
+count_two_tokens_both_quoted(void)
+{
+    char *inp = "\"one and then\" \"two and\"";
+    int result = count_tokens(inp);
+
+    assert_int_equals(2, result);
+}
+
+void
+get_first_of_one(void)
+{
+    char *inp = "one";
+    char *result = get_start(inp, 2);
+
+    assert_string_equals("one", result);
+}
+
+void
+get_first_of_two(void)
+{
+    char *inp = "one two";
+    char *result = get_start(inp, 2);
+
+    assert_string_equals("one ", result);
+}
+
+void
+get_first_two_of_three(void)
+{
+    char *inp = "one two three";
+    char *result = get_start(inp, 3);
+
+    assert_string_equals("one two ", result);
+}
+
+void
+get_first_two_of_three_first_quoted(void)
+{
+    char *inp = "\"one\" two three";
+    char *result = get_start(inp, 3);
+
+    assert_string_equals("\"one\" two ", result);
+}
+
+void
+get_first_two_of_three_second_quoted(void)
+{
+    char *inp = "one \"two\" three";
+    char *result = get_start(inp, 3);
+
+    assert_string_equals("one \"two\" ", result);
+}
+
+void
+get_first_two_of_three_first_and_second_quoted(void)
+{
+    char *inp = "\"one\" \"two\" three";
+    char *result = get_start(inp, 3);
+
+    assert_string_equals("\"one\" \"two\" ", result);
+}
+
 void
 register_parser_tests(void)
 {
@@ -307,4 +425,17 @@ register_parser_tests(void)
     TEST(parse_cmd_freetext_with_quoted_and_space);
     TEST(parse_cmd_freetext_with_quoted_and_many_spaces);
     TEST(parse_cmd_freetext_with_many_quoted_and_many_spaces);
+    TEST(count_one_token);
+    TEST(count_one_token_quoted_no_whitespace);
+    TEST(count_one_token_quoted_with_whitespace);
+    TEST(count_two_tokens);
+    TEST(count_two_tokens_first_quoted);
+    TEST(count_two_tokens_second_quoted);
+    TEST(count_two_tokens_both_quoted);
+    TEST(get_first_of_one);
+    TEST(get_first_of_two);
+    TEST(get_first_two_of_three);
+    TEST(get_first_two_of_three_first_quoted);
+    TEST(get_first_two_of_three_second_quoted);
+    TEST(get_first_two_of_three_first_and_second_quoted);
 }