Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite config configgen unit tests to gtest. #33255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions config/src/tests/configgen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
vespa_add_executable(config_configgen_test_app TEST
SOURCES
configgen.cpp
configgen_test.cpp
DEPENDS
vespa_config
GTest::gtest
)
vespa_add_test(NAME config_configgen_test_app COMMAND config_configgen_test_app)
vespa_generate_config(config_configgen_test_app ../../test/resources/configdefinitions/motd.def)
vespa_add_executable(config_vector_inserter_test_app TEST
SOURCES
vector_inserter.cpp
vector_inserter_test.cpp
DEPENDS
vespa_config
GTest::gtest
)
vespa_add_test(NAME config_vector_inserter_test_app COMMAND config_vector_inserter_test_app)
vespa_add_executable(config_map_inserter_test_app TEST
SOURCES
map_inserter.cpp
map_inserter_test.cpp
DEPENDS
vespa_config
GTest::gtest
)
vespa_add_test(NAME config_map_inserter_test_app COMMAND config_map_inserter_test_app)
vespa_add_executable(config_value_converter_test_app TEST
SOURCES
value_converter.cpp
value_converter_test.cpp
DEPENDS
vespa_config
GTest::gtest
)
vespa_add_test(NAME config_value_converter_test_app COMMAND config_value_converter_test_app)
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/config/helper/configgetter.hpp>
#include "config-motd.h"
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/testkit/test_path.h>

using namespace config;

TEST("require that config type can be compiled") {
TEST(ConfiggenTest, require_that_config_type_can_be_compiled)
{
std::unique_ptr<MotdConfig> cfg = ConfigGetter<MotdConfig>::getConfig("motd",
FileSpec(TEST_PATH("motd.cfg")));
}

TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/config/configgen/map_inserter.hpp>
#include <vespa/vespalib/gtest/gtest.h>

using namespace config;
using namespace config::internal;
Expand All @@ -20,7 +20,8 @@ struct MyType{
int bar;
};

TEST("require that map of ints can be inserted") {
TEST(MapInserterTest, require_that_map_of_ints_can_be_inserted)
{
std::map<std::string, int32_t> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -29,13 +30,14 @@ TEST("require that map of ints can be inserted") {
root.setLong("baz", 6);
MapInserter<int32_t> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(3u, map.size());
ASSERT_EQUAL(3, map["foo"]);
ASSERT_EQUAL(2, map["bar"]);
ASSERT_EQUAL(6, map["baz"]);
ASSERT_EQ(3u, map.size());
ASSERT_EQ(3, map["foo"]);
ASSERT_EQ(2, map["bar"]);
ASSERT_EQ(6, map["baz"]);
}

TEST("require that map of struct can be inserted") {
TEST(MapInserterTest, require_that_map_of_struct_can_be_inserted)
{
std::map<std::string, MyType> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -47,14 +49,15 @@ TEST("require that map of struct can be inserted") {
two.setLong("bar", 6);
MapInserter<MyType> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(2u, map.size());
ASSERT_EQUAL(3, map["foo"].foo);
ASSERT_EQUAL(4, map["foo"].bar);
ASSERT_EQUAL(1, map["bar"].foo);
ASSERT_EQUAL(6, map["bar"].bar);
ASSERT_EQ(2u, map.size());
ASSERT_EQ(3, map["foo"].foo);
ASSERT_EQ(4, map["foo"].bar);
ASSERT_EQ(1, map["bar"].foo);
ASSERT_EQ(6, map["bar"].bar);
}

TEST("require that map of long can be inserted") {
TEST(MapInserterTest, require_that_map_of_long_can_be_inserted)
{
std::map<std::string, int64_t> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -63,13 +66,14 @@ TEST("require that map of long can be inserted") {
root.setLong("baz", 6);
MapInserter<int64_t> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(3u, map.size());
ASSERT_EQUAL(3, map["foo"]);
ASSERT_EQUAL(2, map["bar"]);
ASSERT_EQUAL(6, map["baz"]);
ASSERT_EQ(3u, map.size());
ASSERT_EQ(3, map["foo"]);
ASSERT_EQ(2, map["bar"]);
ASSERT_EQ(6, map["baz"]);
}

TEST("require that map of double can be inserted") {
TEST(MapInserterTest, require_that_map_of_double_can_be_inserted)
{
std::map<std::string, double> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -78,13 +82,14 @@ TEST("require that map of double can be inserted") {
root.setDouble("baz", 6.6);
MapInserter<double> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(3u, map.size());
ASSERT_EQUAL(3.1, map["foo"]);
ASSERT_EQUAL(2.4, map["bar"]);
ASSERT_EQUAL(6.6, map["baz"]);
ASSERT_EQ(3u, map.size());
ASSERT_EQ(3.1, map["foo"]);
ASSERT_EQ(2.4, map["bar"]);
ASSERT_EQ(6.6, map["baz"]);
}

TEST("require that map of bool can be inserted") {
TEST(MapInserterTest, require_that_map_of_bool_can_be_inserted)
{
std::map<std::string, bool> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -93,13 +98,14 @@ TEST("require that map of bool can be inserted") {
root.setBool("baz", true);
MapInserter<bool> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(3u, map.size());
ASSERT_EQ(3u, map.size());
ASSERT_TRUE(map["foo"]);
ASSERT_FALSE(map["bar"]);
ASSERT_TRUE(map["baz"]);
}

TEST("require that map of string can be inserted") {
TEST(MapInserterTest, require_that_map_of_string_can_be_inserted)
{
std::map<std::string, std::string> map;
Slime slime;
Cursor & root = slime.setObject();
Expand All @@ -108,10 +114,10 @@ TEST("require that map of string can be inserted") {
root.setString("baz", "foo");
MapInserter<std::string> inserter(map);
root.traverse(inserter);
ASSERT_EQUAL(3u, map.size());
ASSERT_EQUAL("foo", map["baz"]);
ASSERT_EQUAL("bar", map["bar"]);
ASSERT_EQUAL("baz", map["foo"]);
ASSERT_EQ(3u, map.size());
ASSERT_EQ("foo", map["baz"]);
ASSERT_EQ("bar", map["bar"]);
ASSERT_EQ("baz", map["foo"]);
}

TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/config/configgen/value_converter.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <climits>

using namespace config;
using namespace config::internal;
using namespace vespalib;
using namespace vespalib::slime;

struct MyType{
struct MyType {
MyType(const ConfigPayload & payload)
{
foo = payload.get()["foo"].asLong();
Expand All @@ -20,7 +20,8 @@ struct MyType{
int bar;
};

TEST("that int32_ts are converted") {
TEST(ValueConverterTest, that_int32_ts_are_converted)
{
Slime slime;
Cursor & root = slime.setArray();
root.addLong(3);
Expand All @@ -29,14 +30,15 @@ TEST("that int32_ts are converted") {
root.addLong(INT_MIN);
root.addDouble(3.14);
ValueConverter<int32_t> conv;
EXPECT_EQUAL(3, conv(root[0]));
EXPECT_EQUAL(-2, conv(root[1]));
EXPECT_EQUAL(INT_MAX, conv(root[2]));
EXPECT_EQUAL(INT_MIN, conv(root[3]));
EXPECT_EQUAL(3, conv(root[4]));
EXPECT_EQ(3, conv(root[0]));
EXPECT_EQ(-2, conv(root[1]));
EXPECT_EQ(INT_MAX, conv(root[2]));
EXPECT_EQ(INT_MIN, conv(root[3]));
EXPECT_EQ(3, conv(root[4]));
}

TEST("that int64_ts are converted") {
TEST(ValueConverterTest, that_int64_ts_are_converted)
{
Slime slime;
Cursor & root = slime.setArray();
root.addLong(3);
Expand All @@ -49,18 +51,19 @@ TEST("that int64_ts are converted") {
std::string ref = "{\"val\":9223372036854775807}";
Slime slime2;
JsonFormat::decode(ref, slime2);
EXPECT_EQUAL(std::numeric_limits<int64_t>::max(), slime2.get()["val"].asLong());
EXPECT_EQ(std::numeric_limits<int64_t>::max(), slime2.get()["val"].asLong());
ValueConverter<int64_t> conv;
EXPECT_EQUAL(3, conv(root[0]));
EXPECT_EQUAL(-2, conv(root[1]));
EXPECT_EQUAL(LONG_MAX, conv(root[2]));
EXPECT_EQUAL(LONG_MIN, conv(root[3]));
EXPECT_EQUAL(std::numeric_limits<int64_t>::max(), conv(root[4]));
EXPECT_EQUAL(std::numeric_limits<int64_t>::min(), conv(root[5]));
EXPECT_EQUAL(3, conv(root[6]));
EXPECT_EQ(3, conv(root[0]));
EXPECT_EQ(-2, conv(root[1]));
EXPECT_EQ(LONG_MAX, conv(root[2]));
EXPECT_EQ(LONG_MIN, conv(root[3]));
EXPECT_EQ(std::numeric_limits<int64_t>::max(), conv(root[4]));
EXPECT_EQ(std::numeric_limits<int64_t>::min(), conv(root[5]));
EXPECT_EQ(3, conv(root[6]));
}

TEST("that values can be parsed as strings") {
TEST(ValueConverterTest, that_values_can_be_parsed_as_strings)
{
Slime slime;
Cursor & root = slime.setObject();
root.setString("intval", "1234");
Expand All @@ -71,13 +74,14 @@ TEST("that values can be parsed as strings") {
ValueConverter<int64_t> longConv;
ValueConverter<bool> boolConv;
ValueConverter<double> doubleConv;
EXPECT_EQUAL(1234, intConv(root["intval"]));
EXPECT_EQUAL(42949672969, longConv(root["longval"]));
EXPECT_EQUAL(true, boolConv(root["boolval"]));
EXPECT_APPROX(3.14, doubleConv(root["doubleval"]), 0.0001);
EXPECT_EQ(1234, intConv(root["intval"]));
EXPECT_EQ(42949672969, longConv(root["longval"]));
EXPECT_EQ(true, boolConv(root["boolval"]));
EXPECT_NEAR(3.14, doubleConv(root["doubleval"]), 0.0001);
}

TEST("that incompatible types throws exceptions") {
TEST(ValueConverterTest, that_incompatible_types_throws_exceptions)
{
Slime slime;
Cursor & root = slime.setObject();
root.setBool("intval", true);
Expand All @@ -88,17 +92,18 @@ TEST("that incompatible types throws exceptions") {
ValueConverter<int64_t> longConv;
ValueConverter<bool> boolConv;
ValueConverter<double> doubleConv;
EXPECT_EXCEPTION(intConv(root["intval"]), InvalidConfigException, "");
EXPECT_EXCEPTION(longConv(root["longval"]), InvalidConfigException, "");
EXPECT_EXCEPTION(doubleConv(root["doubleval"]), InvalidConfigException, "");
EXPECT_EXCEPTION(boolConv(root["boolval"]), InvalidConfigException, "");
VESPA_EXPECT_EXCEPTION(intConv(root["intval"]), InvalidConfigException, "");
VESPA_EXPECT_EXCEPTION(longConv(root["longval"]), InvalidConfigException, "");
VESPA_EXPECT_EXCEPTION(doubleConv(root["doubleval"]), InvalidConfigException, "");
VESPA_EXPECT_EXCEPTION(boolConv(root["boolval"]), InvalidConfigException, "");
}

TEST("that non-valid fields throws exception") {
TEST(ValueConverterTest, that_non_valid_fields_throws_exception)
{
Slime slime;
Cursor & root = slime.setObject();
ValueConverter<int64_t> conv;
EXPECT_EXCEPTION(conv("longval", root["longval"]), InvalidConfigException, "Value for 'longval' required but not found");
VESPA_EXPECT_EXCEPTION(conv("longval", root["longval"]), InvalidConfigException, "Value for 'longval' required but not found");
}

TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()
Loading