Debian Codegraph @

src/tests/config_test.c

raw symbols SCIP
1 #define SDL_MAIN_HANDLED
2 #include <cbehave/cbehave.h>
3
4 #include <config_io.h>
5 #include <config_json.h>
6 #include <config_old.h>
7 #include <pic_manager.h>
8 #include <sounds.h>
9 #include <weapon.h>
10
11 // Stubs
12 Mix_Chunk *StrSound(const char *s)
13 {
14 UNUSED(s);
15 return NULL;
16 }
17 Pic *PicManagerGetPic(const PicManager *pm, const char *name)
18 {
19 UNUSED(pm);
20 UNUSED(name);
21 return NULL;
22 }
23 const WeaponClass *StrWeaponClass(const char *s)
24 {
25 UNUSED(s);
26 return NULL;
27 }
28 const char *JoyName(const int deviceIndex)
29 {
30 UNUSED(deviceIndex);
31 return NULL;
32 }
33 PicManager gPicManager;
34
35
36 FEATURE(load_default, "Load default config")
37 SCENARIO("Load a default config")
38 GIVEN("two configs")
39 Config config1, config2;
40
41 WHEN("I load them both with defaults")
42 // Note: default loaded before loading from file
43 config1 = ConfigLoad(NULL);
44 config2 = ConfigLoad(NULL);
45
46 THEN("the two configs should have the same values")
47 SHOULD_INT_EQUAL(
48 ConfigGetBool(&config1, "Game.FriendlyFire"),
49 ConfigGetBool(&config2, "Game.FriendlyFire"));
50 SHOULD_INT_EQUAL(
51 ConfigGetInt(&config1, "Graphics.Brightness"),
52 ConfigGetInt(&config2, "Graphics.Brightness"));
53 SCENARIO_END
54 FEATURE_END
55
56 FEATURE(save_and_load, "Save and load")
57 SCENARIO("Save and load a JSON config file")
58 GIVEN("a config file with some values, and I save the config to a JSON file")
59 Config config1 = ConfigLoad(NULL);
60 ConfigGet(&config1, "Game.FriendlyFire")->u.Bool.Value = true;
61 ConfigGet(&config1, "Graphics.Brightness")->u.Int.Value = 5;
62 ConfigSave(&config1, "tmp");
63
64 WHEN("I load a second config from that file")
65 Config config2 = ConfigLoad("tmp");
66
67 THEN("the two configs should have the same values")
68 SHOULD_INT_EQUAL(
69 ConfigGetBool(&config1, "Game.FriendlyFire"),
70 ConfigGetBool(&config2, "Game.FriendlyFire"));
71 SHOULD_INT_EQUAL(
72 ConfigGetInt(&config1, "Graphics.Brightness"),
73 ConfigGetInt(&config2, "Graphics.Brightness"));
74 SCENARIO_END
75 FEATURE_END
76
77 FEATURE(detect_version, "Detect config version")
78 SCENARIO("Detect JSON config version")
79 GIVEN("a config file with some values")
80 Config config1 = ConfigLoad(NULL);
81 ConfigGet(&config1, "Game.FriendlyFire")->u.Bool.Value = true;
82 ConfigGet(&config1, "Graphics.Brightness")->u.Int.Value = 5;
83 AND("I save the config to file in the JSON format")
84 ConfigSave(&config1, "tmp");
85
86 WHEN("I detect the version")
87 FILE *file = fopen("tmp", "r");
88 int version = ConfigGetVersion(file);
89 fclose(file);
90 AND("load a second config from that file")
91 Config config2 = ConfigLoad("tmp");
92
93 THEN("the version should be " TOSTRING(CONFIG_VERSION))
94 SHOULD_INT_EQUAL(version, CONFIG_VERSION);
95 AND("the two configs should have the same values")
96 SHOULD_INT_EQUAL(
97 ConfigGetBool(&config1, "Game.FriendlyFire"),
98 ConfigGetBool(&config2, "Game.FriendlyFire"));
99 SHOULD_INT_EQUAL(
100 ConfigGetInt(&config1, "Graphics.Brightness"),
101 ConfigGetInt(&config2, "Graphics.Brightness"));
102 SCENARIO_END
103 FEATURE_END
104
105 FEATURE(save_as_latest, "Save config as latest format by default")
106 SCENARIO("Save as JSON by default")
107 GIVEN("a config file with some values")
108 Config config = ConfigLoad(NULL);
109 ConfigGet(&config, "Game.FriendlyFire")->u.Bool.Value = true;
110 ConfigGet(&config, "Graphics.Brightness")->u.Int.Value = 5;
111 AND("I save the config to file")
112 ConfigSave(&config, "tmp");
113
114 WHEN("I detect the version")
115 FILE *file = fopen("tmp", "r");
116 int version = ConfigGetVersion(file);
117 fclose(file);
118
119 THEN("the version should be " TOSTRING(CONFIG_VERSION))
120 SHOULD_INT_EQUAL(version, CONFIG_VERSION);
121 SCENARIO_END
122 FEATURE_END
123
124 CBEHAVE_RUN(
125 "Config features are:",
126 TEST_FEATURE(load_default),
127 TEST_FEATURE(save_and_load),
128 TEST_FEATURE(detect_version),
129 TEST_FEATURE(save_as_latest)
130 )