Skip to content

Commit

Permalink
Fixed map test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
verri committed Mar 11, 2021
1 parent b857394 commit 75f540b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/maps.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <LuaContext.hpp>
#include <gtest/gtest.h>

#include <boost/functional/hash.hpp>

TEST(Maps, MapString) {
LuaContext context;

Expand All @@ -22,7 +24,7 @@ TEST(Maps, MapStringReverse) {
map_type a{{"foo", 1}, {"bar", 2}};
context.writeVariable("a", a);

const auto b = context.readVariable<std::map<std::string, int>>("a");
const auto b = context.readVariable<map_type>("a");
EXPECT_EQ(1, b.at("foo"));
EXPECT_EQ(2, b.at("bar"));
}
Expand Down Expand Up @@ -59,3 +61,23 @@ TEST(Maps, MapVariant) {
EXPECT_EQ(1, b.at("foo"));
EXPECT_EQ(3, b.at(2));
}

TEST(Maps, UnorderedMapVariant) {
LuaContext context;

// if `std::string` would come first, any `int` key would be converted
// to `std::string` when reading.
using map_type = std::unordered_map<boost::variant<int, std::string>, int, boost::hash<boost::variant<int, std::string>>>;

map_type a{{"foo", 1}, {2, 3}};
EXPECT_EQ(1, a.at("foo"));
EXPECT_EQ(3, a.at(2));

context.writeVariable("a", a);
EXPECT_EQ(1, context.readVariable<int>("a", "foo"));
EXPECT_EQ(3, context.readVariable<int>("a", 2));

const auto b = context.readVariable<map_type>("a");
EXPECT_EQ(1, b.at("foo"));
EXPECT_EQ(3, b.at(2));
}

0 comments on commit 75f540b

Please sign in to comment.