Directory: | ./ |
---|---|
File: | test/lookup.c |
Date: | 2021-09-04 00:13:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 79 | 79 | 100.0% |
Branches: | 5 | 8 | 62.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <stdlib.h> | ||
2 | #include <unity.h> | ||
3 | |||
4 | #include "nosj.h" | ||
5 | #include "twitapi.h" | ||
6 | |||
7 | char *j = NULL; | ||
8 | struct json_token *t = NULL; | ||
9 | |||
10 | 10 | void setUp(void) | |
11 | { | ||
12 | struct json_parser p; | ||
13 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
|
10 | if (!t) { |
14 | 1 | j = (char *)twitapi_json; | |
15 | 1 | p = json_parse(j, NULL, 0); | |
16 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
17 | 1 | t = calloc(p.tokenidx, sizeof(*t)); | |
18 | 1 | p = json_parse(j, t, p.tokenidx); | |
19 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
20 | } | ||
21 | 10 | } | |
22 | |||
23 | 10 | void tearDown(void) | |
24 | { | ||
25 | // clean stuff up here | ||
26 | 10 | } | |
27 | |||
28 | 1 | static void test_lookup_single(void) | |
29 | { | ||
30 | 1 | size_t r = json_lookup(j, t, 0, "favorited"); | |
31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT_NOT_EQUAL(0, r); |
32 | 1 | TEST_ASSERT_EQUAL(JSON_FALSE, t[r].type); | |
33 | 1 | } | |
34 | |||
35 | 1 | static void test_lookup_key_not_exist(void) | |
36 | { | ||
37 | 1 | size_t r = json_lookup(j, t, 0, "user.foobar"); | |
38 | 1 | TEST_ASSERT_EQUAL(0, r); | |
39 | 1 | } | |
40 | |||
41 | 1 | static void test_lookup_array(void) | |
42 | { | ||
43 | 1 | size_t r = json_lookup(j, t, 0, "contributors[0]"); | |
44 | 1 | TEST_ASSERT_NOT_EQUAL_INT(0, r); | |
45 | 1 | TEST_ASSERT_EQUAL_INT(JSON_NUMBER, t[r].type); | |
46 | 1 | TEST_ASSERT_EQUAL_FLOAT(14927800.0, json_number_get(j, t, r)); | |
47 | 1 | } | |
48 | |||
49 | 1 | static void test_lookup_big_chain(void) | |
50 | { | ||
51 | 1 | size_t r = json_lookup(j, t, 0, "user.entities.url.urls[0].indices[1]"); | |
52 | 1 | TEST_ASSERT_NOT_EQUAL_INT(0, r); | |
53 | 1 | TEST_ASSERT_EQUAL_INT(JSON_NUMBER, t[r].type); | |
54 | 1 | TEST_ASSERT_EQUAL_FLOAT(22.0, json_number_get(j, t, r)); | |
55 | 1 | } | |
56 | |||
57 | 1 | static void test_lookup_two_index(void) | |
58 | { | ||
59 | /* no nested lists in twitapi.json :( */ | ||
60 | 1 | char *lj = "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, [0, 2, 4, 6, 8]]"; | |
61 | 1 | struct json_token *lt = NULL; | |
62 | 1 | struct json_parser p = json_parse(lj, NULL, 0); | |
63 | 1 | TEST_ASSERT_EQUAL_INT(p.error, JSONERR_NO_ERROR); | |
64 | 1 | lt = calloc(p.tokenidx, sizeof(*lt)); | |
65 | 1 | p = json_parse(lj, lt, p.tokenidx); | |
66 | 1 | TEST_ASSERT_EQUAL_INT(p.error, JSONERR_NO_ERROR); | |
67 | 1 | size_t r = json_lookup(lj, lt, 0, "[11][2]"); | |
68 | 1 | TEST_ASSERT_NOT_EQUAL_INT(0, r); | |
69 | 1 | TEST_ASSERT_EQUAL_INT(JSON_NUMBER, lt[r].type); | |
70 | 1 | TEST_ASSERT_EQUAL_FLOAT(4.0, json_number_get(lj, lt, r)); | |
71 | 1 | free(lt); | |
72 | 1 | } | |
73 | |||
74 | 1 | static void test_lookup_dot_not_object(void) | |
75 | { | ||
76 | 1 | size_t r = json_lookup(j, t, 0, "retweeted.yes"); | |
77 | 1 | TEST_ASSERT_EQUAL_INT(0, r); | |
78 | 1 | } | |
79 | |||
80 | 1 | static void test_lookup_index_not_array(void) | |
81 | { | ||
82 | 1 | size_t r = json_lookup(j, t, 0, "entities[1]"); | |
83 | 1 | TEST_ASSERT_EQUAL_INT(0, r); | |
84 | 1 | } | |
85 | |||
86 | 1 | static void test_lookup_non_integer_index(void) | |
87 | { | ||
88 | 1 | size_t r = json_lookup(j, t, 0, "entities.urls[abc]"); | |
89 | 1 | TEST_ASSERT_EQUAL_INT(0, r); | |
90 | 1 | } | |
91 | |||
92 | 1 | static void test_lookup_invalid_after_index(void) | |
93 | { | ||
94 | 1 | size_t r = json_lookup(j, t, 0, "user.entities.url.urls[0]indices"); | |
95 | 1 | TEST_ASSERT_EQUAL_INT(0, r); | |
96 | 1 | } | |
97 | |||
98 | 1 | static void test_lookup_invalid_index(void) | |
99 | { | ||
100 | 1 | size_t r = json_lookup(j, t, 0, "entities.urls[2]"); | |
101 | 1 | TEST_ASSERT_EQUAL_INT(0, r); | |
102 | 1 | } | |
103 | |||
104 | 1 | int main(void) | |
105 | { | ||
106 | 1 | UNITY_BEGIN(); | |
107 | 1 | RUN_TEST(test_lookup_single); | |
108 | 1 | RUN_TEST(test_lookup_key_not_exist); | |
109 | 1 | RUN_TEST(test_lookup_array); | |
110 | 1 | RUN_TEST(test_lookup_big_chain); | |
111 | 1 | RUN_TEST(test_lookup_two_index); | |
112 | 1 | RUN_TEST(test_lookup_dot_not_object); | |
113 | 1 | RUN_TEST(test_lookup_index_not_array); | |
114 | 1 | RUN_TEST(test_lookup_non_integer_index); | |
115 | 1 | RUN_TEST(test_lookup_invalid_after_index); | |
116 | 1 | RUN_TEST(test_lookup_invalid_index); | |
117 | 1 | return UNITY_END(); | |
118 | } | ||
119 |