// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include #include #include "config-my.h" #include "config-motd.h" #include using namespace config; template struct RawFixture { RawSpec spec; std::unique_ptr cfg; RawFixture() : spec("myField \"foo\"\n"), cfg(ConfigGetter::getConfig("test", spec)) { } }; TEST_F("requireThatConfigIsWrittenToFile", RawFixture) { FileConfigWriter writer("test_1.json"); ASSERT_TRUE(writer.write(*f.cfg, JsonConfigFormatter())); struct stat st; int ret = stat("test_1.json", &st); ASSERT_EQUAL(0, ret); ASSERT_TRUE(st.st_size > 0); } TEST_F("requireThatCanPrintAsJson", RawFixture) { FileConfigWriter writer("test_2.json"); ASSERT_TRUE(writer.write(*f.cfg, JsonConfigFormatter())); FileConfigReader reader("test_2.json"); std::unique_ptr cfg2 = reader.read(JsonConfigFormatter()); ASSERT_TRUE(*cfg2 == *f.cfg); } TEST_F("requireThatCanPrintToOstream", RawFixture) { std::ostringstream ss; OstreamConfigWriter writer(ss); ASSERT_TRUE(writer.write(*f.cfg)); ASSERT_EQUAL("myField \"foo\"\n", ss.str()); } TEST_F("requireThatCanReadFromIstream", RawFixture) { std::stringstream ss; ss << "myField \"foo\"\n"; IstreamConfigReader reader(ss); std::unique_ptr cfg = reader.read(); ASSERT_EQUAL(std::string("foo"), cfg->myField); } TEST_F("requireThatCanPrintToAscii", RawFixture) { vespalib::asciistream ss; AsciiConfigWriter writer(ss); ASSERT_TRUE(writer.write(*f.cfg)); ASSERT_EQUAL("myField \"foo\"\n", ss.str()); } TEST_F("requireThatCanPrintAsConfigFormat", RawFixture) { FileConfigWriter writer("test_3.cfg"); ASSERT_TRUE(writer.write(*f.cfg)); FileConfigReader reader("test_3.cfg"); std::unique_ptr cfg2 = reader.read(); ASSERT_TRUE(*cfg2 == *f.cfg); } TEST_F("require that invalid file throws exception", RawFixture) { FileConfigReader reader("nonexistant.cfg"); EXPECT_EXCEPTION(reader.read(), vespalib::IllegalArgumentException, "Unable to open file"); } TEST_F("requireThatCanLoadWrittenWithConfigFormat", RawFixture) { FileConfigWriter writer("test_3.cfg"); ASSERT_TRUE(writer.write(*f.cfg)); std::unique_ptr cfg2 = ConfigGetter::getConfig("test_3", FileSpec("test_3.cfg")); ASSERT_TRUE(*cfg2 == *f.cfg); } TEST("requireThatAllFieldsArePrintedCorrectly") { std::unique_ptr cfg = ConfigGetter::getConfig( "motd", FileSpec(TEST_PATH("motd.cfg"))); FileConfigWriter writer("motd2.cfg"); ASSERT_TRUE(writer.write(*cfg, FileConfigFormatter())); std::unique_ptr cfg2 = ConfigGetter::getConfig( "motd2", FileSpec("motd2.cfg")); ASSERT_TRUE(*cfg2 == *cfg); } TEST("require that reading cfg format throws exception") { FileConfigReader reader("test_1.json"); EXPECT_EXCEPTION(reader.read(FileConfigFormatter()), vespalib::IllegalArgumentException, "Reading cfg format is not supported"); } TEST_MAIN() { TEST_RUN_ALL(); }