198 lines
6.4 KiB
C++
198 lines
6.4 KiB
C++
#include <memory>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#include <di/Configuration.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace Di;
|
|
using namespace std;
|
|
|
|
TEST(Configuration, Load) {
|
|
Configuration c;
|
|
ofstream testFile;
|
|
|
|
/* We need to make sure that the test file exists, so we need to create one
|
|
* the content of the file needs to be valid JSON as well
|
|
*/
|
|
testFile.open("test_libdipp_configuration.json", ofstream::out);
|
|
testFile << "{}" << endl;
|
|
testFile.close();
|
|
|
|
// Test Load with existing file and valid content
|
|
EXPECT_TRUE(c.load("test_libdipp_configuration.json"));
|
|
|
|
// Test Load with non existing file supplied as argument
|
|
EXPECT_FALSE(c.load("non-existing-file.abc"));
|
|
|
|
// Test Load with existing file supplied as argument and search on
|
|
EXPECT_TRUE(c.load("test_libdipp_configuration.json", true));
|
|
|
|
// Test Load with non existing file supplied as argument and search on
|
|
EXPECT_FALSE(c.load("non-existing-file.abc", true));
|
|
|
|
// Test Load with existing file supplied as argument and search off
|
|
EXPECT_TRUE(c.load("test_libdipp_configuration.json", false));
|
|
|
|
// Test Load with non existing file supplied as argument and search off
|
|
EXPECT_FALSE(c.load("non-existing-file.abc", false));
|
|
|
|
unlink("test_libdipp_configuration.json");
|
|
|
|
/* We need to make sure that the test file exists, so we need to create one
|
|
* the content of the file needs to be invalid JSON to get an error
|
|
* */
|
|
testFile.open("test_libdipp_configuration.json", ofstream::out);
|
|
testFile << "invalid" << endl;
|
|
testFile.close();
|
|
|
|
// Test Load with existing but invalid file
|
|
EXPECT_FALSE(c.load("test_libdipp_configuration.json"));
|
|
|
|
// Test Load with non existing file supplied as argument
|
|
EXPECT_FALSE(c.load("non-existing-file.abc"));
|
|
|
|
// Test Load with existing but invalid file supplied as argument and search on
|
|
EXPECT_FALSE(c.load("test_libdipp_configuration.json", true));
|
|
|
|
// Test Load with non existing file supplied as argument and search on
|
|
EXPECT_FALSE(c.load("non-existing-file.abc", true));
|
|
|
|
// Test Load with existing but invalid file supplied as argument and search off
|
|
EXPECT_FALSE(c.load("test_libdipp_configuration.json", false));
|
|
|
|
// Test Load with non existing file supplied as argument and search off
|
|
EXPECT_FALSE(c.load("non-existing-file.abc", false));
|
|
|
|
unlink("test_libdipp_configuration.json");
|
|
}
|
|
|
|
TEST(Configuration, setDefaultProperty) {
|
|
Configuration c;
|
|
bool test_bool;
|
|
string test_string;
|
|
ofstream testFile;
|
|
double testDouble;
|
|
|
|
testFile.open("test_libdipp_configuration.json", ofstream::out);
|
|
testFile << "{" << endl;
|
|
testFile << " \"test_string\" : \"test\"," << endl;
|
|
testFile << " \"test_bool\" : false," << endl;
|
|
testFile << " \"test_double\" : 5.6" << endl;
|
|
testFile << "}" << endl;
|
|
testFile.close();
|
|
|
|
c.setDefaultProperty("test_bool", true);
|
|
c.setDefaultProperty("test_string", string("string"));
|
|
c.setDefaultProperty("test_const_char", static_cast<const char *>("const_char"));
|
|
c.setDefaultProperty("test_const_char_nullptr", static_cast<const char *>(nullptr));
|
|
c.setDefaultProperty("test_double", 1.1);
|
|
c.setDefaultProperty("test_int", 1L);
|
|
|
|
EXPECT_TRUE(c.getProperty("test_bool", &test_bool));
|
|
EXPECT_EQ(true, test_bool);
|
|
EXPECT_FALSE(c.getProperty("test_bool_none", &test_bool));
|
|
EXPECT_EQ(true, test_bool); // It must be untouched
|
|
|
|
EXPECT_TRUE(c.getProperty("test_string", &test_string));
|
|
EXPECT_EQ(test_string, "string");
|
|
EXPECT_FALSE(c.getProperty("test_string_none", &test_string));
|
|
EXPECT_EQ(test_string, "string"); // It must be untouched
|
|
|
|
EXPECT_TRUE(c.getProperty("test_double", &testDouble));
|
|
EXPECT_FLOAT_EQ(testDouble, 1.1);
|
|
EXPECT_FALSE(c.getProperty("test_double_none", &testDouble));
|
|
EXPECT_FLOAT_EQ(testDouble, 1.1); // It must be untouched
|
|
|
|
// Reload overriding configuration
|
|
EXPECT_TRUE(c.load("test_libdipp_configuration.json", true));
|
|
|
|
EXPECT_TRUE(c.getProperty("test_string", &test_string));
|
|
EXPECT_EQ(test_string, "test");
|
|
|
|
EXPECT_TRUE(c.getProperty("test_bool", &test_bool));
|
|
EXPECT_EQ(false, test_bool);
|
|
|
|
EXPECT_TRUE(c.getProperty("test_double", &testDouble));
|
|
EXPECT_FLOAT_EQ(testDouble, 5.6);
|
|
|
|
/** Save */
|
|
|
|
c.save("test_libdipp_configuration_save.json");
|
|
unlink("test_libdipp_configuration.json");
|
|
}
|
|
|
|
TEST(Configuration, commentStripping) {
|
|
Configuration c;
|
|
ofstream testFile;
|
|
double tstDouble = 0.1;
|
|
int tstInt = 0;
|
|
bool tstBool = false;
|
|
|
|
/* We need to make sure that the test file exists, so we need to create one
|
|
* and open it for writing. We will overwrite any existing text.
|
|
* The content of the file needs to be valid JSON as well
|
|
*/
|
|
testFile.open("test_libdipp_configuration.json", ofstream::out);
|
|
testFile << "{" << endl;
|
|
testFile << " // this is a comment" << endl;
|
|
testFile << " \"test_bool\" : true," << endl;
|
|
testFile << " \"test_string\" : \"test\"," << endl;
|
|
testFile << " \"test_double\" : 1.1," << endl;
|
|
testFile << " /* another comment " << endl;
|
|
testFile << " another comment */" << endl;
|
|
testFile << " \"test_int\" : 1" << endl;
|
|
testFile << "}" << endl;
|
|
testFile.close();
|
|
|
|
EXPECT_TRUE(c.load("test_libdipp_configuration.json", true));
|
|
EXPECT_TRUE(c.getProperty("test_double", &tstDouble));
|
|
EXPECT_EQ(1.1, tstDouble);
|
|
EXPECT_TRUE(c.getProperty("test_bool", &tstBool));
|
|
EXPECT_TRUE(tstBool);
|
|
EXPECT_EQ(true, c.getProperty("test_int", &tstInt));
|
|
EXPECT_EQ(1, tstInt);
|
|
|
|
unlink("test_libdipp_configuration.json");
|
|
}
|
|
|
|
TEST(Configuration, setJsonArray) {
|
|
Configuration c;
|
|
ofstream testFile;
|
|
|
|
testFile.open("configuration_setJsonArray.json", ofstream::out);
|
|
testFile << "{" << endl;
|
|
testFile << " \"test_def_array\" : [\"test\",\"test2\"]," << endl;
|
|
testFile << " \"test_double\" : 5.6" << endl;
|
|
testFile << "}" << endl;
|
|
testFile.close();
|
|
|
|
c.load("configuration_setJsonArray.json");
|
|
|
|
shared_ptr<Json> arr = make_shared<Json>(Json::J_ARRAY);
|
|
arr->setString("aap");
|
|
arr->setString("noot");
|
|
arr->setString("mies");
|
|
c.setDefaultProperty("test_array", arr);
|
|
|
|
shared_ptr<Json> obj = make_shared<Json>(Json::J_OBJECT);
|
|
obj->setString("aap", "noot");
|
|
obj->setString("mies", "bert");
|
|
c.setDefaultProperty("test_object", obj);
|
|
c.setDefaultProperty("test_double", 1.1);
|
|
|
|
shared_ptr<Json> test;
|
|
EXPECT_TRUE(c.getArrayProperty("test_array", &test));
|
|
EXPECT_TRUE(test->isArray());
|
|
EXPECT_EQ(3, test->getArraySize());
|
|
test = nullptr;
|
|
|
|
EXPECT_TRUE(c.getArrayProperty("test_def_array", &test));
|
|
EXPECT_TRUE(test->isArray());
|
|
EXPECT_EQ(2, test->getArraySize());
|
|
test = nullptr;
|
|
|
|
EXPECT_FALSE(c.getArrayProperty("no_array", &test));
|
|
EXPECT_EQ(nullptr, test);
|
|
}
|