Debian Codegraph @

src/tests/json_test.c

raw symbols SCIP
1 #define SDL_MAIN_HANDLED
2 #include <cbehave/cbehave.h>
3
4 #include <json_utils.h>
5
6 #include <config.h>
7
8
9 // Stubs
10 Mix_Chunk *StrSound(const char *s)
11 {
12 UNUSED(s);
13 return NULL;
14 }
15 const char *JoyName(const int deviceIndex)
16 {
17 UNUSED(deviceIndex);
18 return NULL;
19 }
20 bool ConfigGetBool(Config *c, const char *name)
21 {
22 UNUSED(c);
23 UNUSED(name);
24 return false;
25 }
26 int ConfigGetJSONVersion(FILE *f)
27 {
28 UNUSED(f);
29 return 0;
30 }
31 bool ConfigIsOld(FILE *f)
32 {
33 UNUSED(f);
34 return false;
35 }
36 int PicManagerGetPic(void) { return 0; }
37 int StrWeaponClass(void) { return 0; }
38 int gPicManager;
39
40
41 FEATURE(json_format_string, "String format")
42 SCENARIO("Quote handling")
43 GIVEN("a JSON structure containing escaped quotes")
44 json_t *root = json_new_object();
45 AddStringPair(root, "Foo", "bar \"baz\"");
46 AddStringPair(root, "Spam", "I like \"spam\" and \"eggs\"");
47
48 WHEN("I format the structure into a string")
49 char *text;
50 json_tree_to_string(root, &text);
51 char *ftext = json_format_string(text);
52
53 THEN("the result should be valid JSON")
54 json_t *parsed = NULL;
55 SHOULD_INT_EQUAL(
56 (int)json_parse_document(&parsed, ftext), (int)JSON_OK);
57 AND("contain the same strings")
58 SHOULD_BE_TRUE(strstr(ftext, "I like \\\"spam\\\" and \\\"eggs\\\"") != NULL);
59 CFREE(text);
60 CFREE(ftext);
61 json_free_value(&root);
62 json_free_value(&parsed);
63 SCENARIO_END
64
65 SCENARIO("Small arrays")
66 GIVEN("a JSON string with a small array")
67 const char *text = "[1, 2]";
68
69 WHEN("I format the string")
70 char *ftext = json_format_string(text);
71
72 THEN("the result should have the array on one line")
73 SHOULD_STR_EQUAL(ftext, "[1, 2]");
74
75 CFREE(ftext);
76 SCENARIO_END
77
78 FEATURE_END
79
80 CBEHAVE_RUN("JSON features are:", TEST_FEATURE(json_format_string))