Directory: | ./ |
---|---|
File: | test/parse_atomics.c |
Date: | 2021-09-04 00:13:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 76 | 76 | 100.0% |
Branches: | 29 | 58 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************/ /** | ||
2 | |||
3 | @file parse_atomics.c | ||
4 | |||
5 | @author Stephen Brennan | ||
6 | |||
7 | @date Created Wednesday, 25 November 2015 | ||
8 | |||
9 | @brief Tests for parsing true, false, null. | ||
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 | 8 | void setUp(void) | |
21 | { | ||
22 | // set stuff up here | ||
23 | 8 | } | |
24 | |||
25 | 8 | void tearDown(void) | |
26 | { | ||
27 | // clean stuff up here | ||
28 | 8 | } | |
29 | |||
30 | 1 | static void test_parse_true(void) | |
31 | { | ||
32 | 1 | char input[] = "true"; | |
33 | struct json_token tokens[1]; | ||
34 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].type == JSON_TRUE); |
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 3); |
41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].child == 0); |
42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].next == 0); |
43 | 1 | } | |
44 | |||
45 | 1 | static void test_parse_false(void) | |
46 | { | ||
47 | 1 | char input[] = "false"; | |
48 | struct json_token tokens[1]; | ||
49 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].type == JSON_FALSE); |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 4); |
56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].child == 0); |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].next == 0); |
58 | 1 | } | |
59 | |||
60 | 1 | static void test_parse_null(void) | |
61 | { | ||
62 | 1 | char input[] = "null"; | |
63 | struct json_token tokens[1]; | ||
64 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].type == JSON_NULL); |
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 3); |
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].child == 0); |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].next == 0); |
73 | 1 | } | |
74 | |||
75 | 1 | static void test_parse_empty_string(void) | |
76 | { | ||
77 | 1 | char input[] = ""; | |
78 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
80 | 1 | } | |
81 | |||
82 | 1 | static void test_parse_whitespace_string(void) | |
83 | { | ||
84 | 1 | char input[] = " \t\r\n"; | |
85 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
87 | 1 | } | |
88 | |||
89 | 1 | static void test_parse_invalid_true(void) | |
90 | { | ||
91 | 1 | char input[] = "trua"; | |
92 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
94 | 1 | } | |
95 | |||
96 | 1 | static void test_parse_invalid_false(void) | |
97 | { | ||
98 | 1 | char input[] = "flase"; | |
99 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
101 | 1 | } | |
102 | |||
103 | 1 | static void test_parse_invalid_null(void) | |
104 | { | ||
105 | 1 | char input[] = "nul"; | |
106 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN); |
108 | 1 | } | |
109 | |||
110 | 1 | int main(void) | |
111 | { | ||
112 | 1 | UNITY_BEGIN(); | |
113 | |||
114 | 1 | RUN_TEST(test_parse_true); | |
115 | 1 | RUN_TEST(test_parse_false); | |
116 | 1 | RUN_TEST(test_parse_null); | |
117 | 1 | RUN_TEST(test_parse_empty_string); | |
118 | 1 | RUN_TEST(test_parse_whitespace_string); | |
119 | 1 | RUN_TEST(test_parse_invalid_true); | |
120 | 1 | RUN_TEST(test_parse_invalid_false); | |
121 | 1 | RUN_TEST(test_parse_invalid_null); | |
122 | |||
123 | 1 | return UNITY_END(); | |
124 | } | ||
125 |