Merge remote-tracking branch 'zwparchman/develop' into develop

This commit is contained in:
ThePhD 2016-03-30 00:34:05 -04:00
commit 9804d7dd63
2 changed files with 29 additions and 0 deletions

View File

@ -178,6 +178,18 @@ public:
return tuple_get( types<Ret...>( ), std::index_sequence_for<Ret...>( ), std::forward_as_tuple(std::forward<Keys>(keys)...));
}
template<typename Ret, typename Key>
Ret get_with_default(Key key, Ret _default) const {
sol::optional<Ret> option = operator[](key);
if( option ){
return option.value();
}
else {
return _default;
}
}
template <typename T, typename... Keys>
decltype(auto) traverse_get( Keys&&... keys ) const {
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);

View File

@ -505,6 +505,23 @@ TEST_CASE("tables/variables", "Check if tables and variables work as intended")
REQUIRE_NOTHROW(lua.script("assert(os.name == \"windows\")"));
}
TEST_CASE("simple/get_default", "Test that table::get_default work corretly") {
sol::state lua;
auto bob_table = lua.create_table("bob");
int is_set=0;
int is_not_set=0;
bob_table.set("is_set",42);
is_set = bob_table.get_with_default("is_set",3);
is_not_set = bob_table.get_with_default("is_not_set",22);
REQUIRE(is_set == 42);
REQUIRE(is_not_set == 22);
}
TEST_CASE("tables/create", "Check if creating a table is kosher") {
sol::state lua;
lua["testtable"] = sol::table::create(lua.lua_state(), 0, 0, "Woof", "Bark", 1, 2, 3, 4);