#include "tile/base/config/toml_configuration.h"

#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

namespace tile {
namespace {
const char *kTomlConfig = R"(
key1=1
key2=2
key3=1.9
key4=true

[key5]
key51="value51"
key52=52
)";
}

TEST(TomlConfiguration, Load)
{
    TomlConfiguration config;
    std::istringstream istr(kTomlConfig);
    ASSERT_TRUE(config.load(istr));
}

TEST(TomlConfiguration, Has)
{
    TomlConfiguration config;
    std::istringstream istr(kTomlConfig);
    ASSERT_TRUE(config.load(istr));
    ASSERT_TRUE(config.Has("key1"));
    ASSERT_TRUE(config.Has("key2"));
    ASSERT_TRUE(config.Has("key3"));
    ASSERT_TRUE(config.Has("key4"));
    ASSERT_TRUE(config.Has("key5"));
    ASSERT_TRUE(config.Has("key5.key51"));
    ASSERT_TRUE(config.Has("key5.key52"));
}

TEST(TomlConfiguration, SampleSet)
{
    TomlConfiguration config;
    std::istringstream istr(kTomlConfig);
    ASSERT_TRUE(config.load(istr));
    ASSERT_TRUE(config.GetInt32("key2"));
    ASSERT_EQ(*config.GetInt32("key2"), 2);

    config.SetInt("key2", 20);
}

TEST(TomlConfiguration, Enumerate)
{
    TomlConfiguration config;
    std::istringstream istr(kTomlConfig);
    ASSERT_TRUE(config.load(istr));
    auto keys = config.keys("");
    ASSERT_EQ(5, keys.size());
    ASSERT_THAT(keys, ::testing::UnorderedElementsAre("key1", "key2", "key3", "key4", "key5"));

    keys = config.keys("key5");
    // for (const auto &key : keys) { TILE_LOG_INFO("key5: {}", key); }
    ASSERT_EQ(2, keys.size());
    ASSERT_THAT(keys, ::testing::UnorderedElementsAre("key5.key51", "key5.key52"));

    GTEST_SKIP_("Fix me.");
    config.Remove("key5.key51");
    keys = config.keys("key5");
    ASSERT_EQ("key5.key52", keys[0]);
}

}// namespace tile
