Skip to content

Commit

Permalink
Fix config_test
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Feb 14, 2016
1 parent 20a10ea commit dd0f76b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/cdogs/config_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "keyboard.h"
#include "log.h"

#define VERSION "7"

static void ConfigLoadVisit(Config *c, json_t *node);
void ConfigLoadJSON(Config *config, const char *filename)
Expand Down Expand Up @@ -174,7 +173,8 @@ void ConfigSaveJSON(const Config *config, const char *filename)
setlocale(LC_ALL, "");

root = json_new_object();
json_insert_pair_into_object(root, "Version", json_new_number(VERSION));
json_insert_pair_into_object(
root, "Version", json_new_number(TOSTRING(CONFIG_VERSION)));
ConfigSaveVisit(config, root);

json_tree_to_string(root, &text);
Expand Down
4 changes: 3 additions & 1 deletion src/cdogs/config_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2014, Cong Xu
Copyright (c) 2013-2014, 2016, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,6 +30,8 @@

#include "config.h"

#define CONFIG_VERSION 7

void ConfigLoadJSON(Config *config, const char *filename);
void ConfigSaveJSON(const Config *config, const char *filename);
int ConfigGetJSONVersion(FILE *f);
5 changes: 4 additions & 1 deletion src/cdogs/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2015, Cong Xu
Copyright (c) 2013-2016, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -65,6 +65,9 @@ extern int debug_level;
extern bool gTrue;
extern bool gFalse;

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

#define D_NORMAL 0
#define D_VERBOSE 1
#define D_MAX 2
Expand Down
45 changes: 12 additions & 33 deletions src/tests/config_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ PicManager gPicManager;

FEATURE(1, "Load default config")
SCENARIO("Load a default config")
{
Config config1, config2;
GIVEN("two configs")
GIVEN_END
Config config1, config2;

WHEN("I load them both with defaults")
// Note: default loaded before loading from file
config1 = ConfigLoad(NULL);
config2 = ConfigLoad(NULL);
WHEN_END

THEN("the two configs should have the same values")
SHOULD_INT_EQUAL(
Expand All @@ -59,25 +56,19 @@ FEATURE(1, "Load default config")
SHOULD_INT_EQUAL(
ConfigGetInt(&config1, "Graphics.Brightness"),
ConfigGetInt(&config2, "Graphics.Brightness"));
THEN_END
}
SCENARIO_END
FEATURE_END

FEATURE(2, "Save and load")
SCENARIO("Save and load a JSON config file")
{
Config config1, config2;
GIVEN("a config file with some values, and I save the config to a JSON file")
config1 = ConfigLoad(NULL);
Config config1 = ConfigLoad(NULL);
ConfigGet(&config1, "Game.FriendlyFire")->u.Bool.Value = true;
ConfigGet(&config1, "Graphics.Brightness")->u.Int.Value = 5;
ConfigSave(&config1, "tmp");
GIVEN_END

WHEN("I load a second config from that file")
config2 = ConfigLoad("tmp");
WHEN_END
Config config2 = ConfigLoad("tmp");

THEN("the two configs should have the same values")
SHOULD_INT_EQUAL(
Expand All @@ -86,41 +77,32 @@ FEATURE(2, "Save and load")
SHOULD_INT_EQUAL(
ConfigGetInt(&config1, "Graphics.Brightness"),
ConfigGetInt(&config2, "Graphics.Brightness"));
THEN_END
}
SCENARIO_END
FEATURE_END

FEATURE(3, "Detect config version")
SCENARIO("Detect JSON config version")
{
Config config1, config2;
int version;
FILE *file;
GIVEN("a config file with some values, and I save the config to file in the JSON format")
config1 = ConfigLoad(NULL);
Config config1 = ConfigLoad(NULL);
ConfigGet(&config1, "Game.FriendlyFire")->u.Bool.Value = true;
ConfigGet(&config1, "Graphics.Brightness")->u.Int.Value = 5;
ConfigSave(&config1, "tmp");
GIVEN_END

WHEN("I detect the version, and load a second config from that file")
file = fopen("tmp", "r");
version = ConfigGetVersion(file);
FILE *file = fopen("tmp", "r");
int version = ConfigGetVersion(file);
fclose(file);
config2 = ConfigLoad("tmp");
WHEN_END
Config config2 = ConfigLoad("tmp");

THEN("the version should be 6, and the two configs should have the same values")
SHOULD_INT_EQUAL(version, 6);
THEN("the version should be " TOSTRING(CONFIG_VERSION))
SHOULD_INT_EQUAL(version, CONFIG_VERSION);
AND("the two configs should have the same values")
SHOULD_INT_EQUAL(
ConfigGetBool(&config1, "Game.FriendlyFire"),
ConfigGetBool(&config2, "Game.FriendlyFire"));
SHOULD_INT_EQUAL(
ConfigGetInt(&config1, "Graphics.Brightness"),
ConfigGetInt(&config2, "Graphics.Brightness"));
THEN_END
}
SCENARIO_END
FEATURE_END

Expand All @@ -135,17 +117,14 @@ FEATURE(4, "Save config as latest format by default")
ConfigGet(&config, "Game.FriendlyFire")->u.Bool.Value = true;
ConfigGet(&config, "Graphics.Brightness")->u.Int.Value = 5;
ConfigSave(&config, "tmp");
GIVEN_END

WHEN("I detect the version")
file = fopen("tmp", "r");
version = ConfigGetVersion(file);
fclose(file);
WHEN_END

THEN("the version should be 6")
SHOULD_INT_EQUAL(version, 6);
THEN_END
THEN("the version should be " TOSTRING(CONFIG_VERSION))
SHOULD_INT_EQUAL(version, CONFIG_VERSION);
}
SCENARIO_END
FEATURE_END
Expand Down

0 comments on commit dd0f76b

Please sign in to comment.