Directory: | ./ |
---|---|
File: | test/parse_strings.c |
Date: | 2021-09-04 00:13:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 89 | 89 | 100.0% |
Branches: | 25 | 46 | 54.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************/ /** | ||
2 | |||
3 | @file parse_strings.c | ||
4 | |||
5 | @author Stephen Brennan | ||
6 | |||
7 | @date Created Tuesday, 24 November 2015 | ||
8 | |||
9 | @brief Test string parsing. | ||
10 | |||
11 | @copyright Copyright (c) 2015, Stephen Brennan. Released under the | ||
12 | Revised BSD License. See LICENSE.txt for details. | ||
13 | |||
14 | *******************************************************************************/ | ||
15 | |||
16 | #include <unity.h> | ||
17 | |||
18 | #include "nosj.h" | ||
19 | |||
20 | 11 | void setUp(void) | |
21 | { | ||
22 | // set stuff up here | ||
23 | 11 | } | |
24 | |||
25 | 11 | void tearDown(void) | |
26 | { | ||
27 | // clean stuff up here | ||
28 | 11 | } | |
29 | |||
30 | 1 | static void test_empty_string(void) | |
31 | { | ||
32 | 1 | char input[] = "\"\""; | |
33 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
37 | 1 | } | |
38 | |||
39 | 1 | static void test_single_char(void) | |
40 | { | ||
41 | 1 | char input[] = "\"a\""; | |
42 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
46 | 1 | } | |
47 | |||
48 | 1 | static void test_no_end(void) | |
49 | { | ||
50 | 1 | char input[] = "\"blah"; | |
51 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
53 | 1 | } | |
54 | |||
55 | 1 | static void test_escape(void) | |
56 | { | ||
57 | 1 | char input[] = "\"blah\\\"blah\""; | |
58 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
62 | 1 | } | |
63 | |||
64 | 1 | static void test_escaped_end(void) | |
65 | { | ||
66 | 1 | char input[] = "\"blah\\"; | |
67 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
69 | 1 | } | |
70 | |||
71 | 1 | static void test_valid_uesc(void) | |
72 | { | ||
73 | 1 | char input[] = "\"blah\\u1a2Bblah\""; | |
74 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
78 | 1 | } | |
79 | |||
80 | 1 | static void test_too_short_uesc(void) | |
81 | { | ||
82 | 1 | char input[] = "\"blah\\u1a\""; | |
83 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
85 | 1 | } | |
86 | |||
87 | 1 | static void test_string_end_uesc(void) | |
88 | { | ||
89 | 1 | char input[] = "\"blah\\u1a"; | |
90 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
92 | 1 | } | |
93 | |||
94 | 1 | static void test_invalid_char_uesc(void) | |
95 | { | ||
96 | 1 | char input[] = "\"blah\\u1aG-\""; | |
97 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
99 | 1 | } | |
100 | |||
101 | 1 | static void test_valid_esc(void) | |
102 | { | ||
103 | 1 | char input[] = "\"\\a\""; | |
104 | 1 | char valid[] = "\"\\/bfnrt"; | |
105 | size_t i; | ||
106 | struct json_parser p; | ||
107 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for (i = 0; valid[i] != '\0'; i++) { |
108 | 8 | input[2] = valid[i]; | |
109 | 8 | p = json_parse(input, NULL, 0); | |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | TEST_ASSERT(p.tokenidx == 1); |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
113 | } | ||
114 | 1 | } | |
115 | |||
116 | 1 | static void test_invalid_esc(void) | |
117 | { | ||
118 | 1 | char input[] = "\"\\a\""; | |
119 | 1 | char valid[] = "aAB12.,[(%!"; // something of a cross-section! | |
120 | size_t i; | ||
121 | struct json_parser p; | ||
122 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
|
12 | for (i = 0; valid[i] != '\0'; i++) { |
123 | 11 | input[2] = valid[i]; | |
124 | 11 | p = json_parse(input, NULL, 0); | |
125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
126 | } | ||
127 | 1 | } | |
128 | |||
129 | 1 | int main(void) | |
130 | { | ||
131 | 1 | UNITY_BEGIN(); | |
132 | |||
133 | 1 | RUN_TEST(test_empty_string); | |
134 | 1 | RUN_TEST(test_single_char); | |
135 | 1 | RUN_TEST(test_no_end); | |
136 | 1 | RUN_TEST(test_escape); | |
137 | 1 | RUN_TEST(test_escaped_end); | |
138 | 1 | RUN_TEST(test_valid_uesc); | |
139 | 1 | RUN_TEST(test_too_short_uesc); | |
140 | 1 | RUN_TEST(test_string_end_uesc); | |
141 | 1 | RUN_TEST(test_invalid_char_uesc); | |
142 | 1 | RUN_TEST(test_valid_esc); | |
143 | 1 | RUN_TEST(test_invalid_esc); | |
144 | |||
145 | 1 | return UNITY_END(); | |
146 | } | ||
147 |