Merge pull request #188 from Nava2/require-testcase

Add test case for require_file with a usertype
This commit is contained in:
The Phantom Derpstorm 2016-08-22 14:23:17 -04:00 committed by GitHub
commit 868c99e1a5
2 changed files with 66 additions and 4 deletions

View File

@ -42,6 +42,7 @@ parser.add_argument('--cxx', metavar='<compiler>', help='compiler name to use (d
parser.add_argument('--cxx-flags', help='additional flags passed to the compiler', default='') parser.add_argument('--cxx-flags', help='additional flags passed to the compiler', default='')
parser.add_argument('--ci', action='store_true', help=argparse.SUPPRESS) parser.add_argument('--ci', action='store_true', help=argparse.SUPPRESS)
parser.add_argument('--testing', action='store_true', help=argparse.SUPPRESS) parser.add_argument('--testing', action='store_true', help=argparse.SUPPRESS)
parser.add_argument('--lua-version', help='Lua version, e.g. lua53', default='lua53')
parser.add_argument('--lua-lib', help='lua library name (without the lib on *nix).', default='lua') parser.add_argument('--lua-lib', help='lua library name (without the lib on *nix).', default='lua')
parser.add_argument('--lua-dir', metavar='<dir>', help='directory lua is in with include and lib subdirectories') parser.add_argument('--lua-dir', metavar='<dir>', help='directory lua is in with include and lib subdirectories')
parser.add_argument('--install-dir', metavar='<dir>', help='directory to install the headers to', default=install_dir); parser.add_argument('--install-dir', metavar='<dir>', help='directory to install the headers to', default=install_dir);
@ -82,7 +83,7 @@ if args.lua_dir:
ldflags.extend(library_includes([os.path.join(args.lua_dir, 'lib')])) ldflags.extend(library_includes([os.path.join(args.lua_dir, 'lib')]))
if 'linux' in sys.platform: if 'linux' in sys.platform:
lua_version = os.environ.get('LUA_VERSION', 'lua53') lua_version = os.environ.get('LUA_VERSION', args.lua_version)
if re.match(r'lua5[1-3]', lua_version): if re.match(r'lua5[1-3]', lua_version):
# Using normal lua # Using normal lua
lua_lib = lua_version[:-1] + '.' + lua_version[-1] lua_lib = lua_version[:-1] + '.' + lua_version[-1]
@ -101,7 +102,7 @@ if 'linux' in sys.platform:
ldflags.extend(libraries([lua_lib])) ldflags.extend(libraries([lua_lib]))
elif 'darwin' in sys.platform: elif 'darwin' in sys.platform:
# OSX # OSX
lua_version = os.environ.get('LUA_VERSION', 'lua53') lua_version = os.environ.get('LUA_VERSION', args.lua_version)
if re.match(r'lua5[1-3]', lua_version): if re.match(r'lua5[1-3]', lua_version):
# Using normal lua # Using normal lua
lua_incl = lua_version[:-1] + '.' + lua_version[-1] lua_incl = lua_version[:-1] + '.' + lua_version[-1]

View File

@ -3,10 +3,13 @@
#include <catch.hpp> #include <catch.hpp>
#include <sol.hpp> #include <sol.hpp>
#include <vector>
#include <fstream>
#include <iostream>
#include <map> #include <map>
#include <unordered_map> #include <unordered_map>
#include <iostream> #include <vector>
#include "test_stack_guard.hpp" #include "test_stack_guard.hpp"
bool func_opt_ret_bool(sol::optional<int> i) { bool func_opt_ret_bool(sol::optional<int> i) {
@ -316,6 +319,64 @@ TEST_CASE("object/conversions", "make sure all basic reference types can be made
REQUIRE(omn.get_type() == sol::type::nil); REQUIRE(omn.get_type() == sol::type::nil);
} }
TEST_CASE("state/require_file", "opening files as 'requires'") {
static const char FILE_NAME[] = "./tmp_thingy.lua";
std::fstream file(FILE_NAME, std::ios::out);
sol::state lua;
lua.open_libraries(sol::lib::base);
SECTION("with usertypes")
{
struct foo {
foo(int bar) : bar(bar) {}
const int bar;
};
lua.new_usertype<foo>("foo",
sol::constructors<sol::types<int>>{},
"bar", &foo::bar
);
file << "return { modfunc = function () return foo.new(221) end }" << std::endl;
file.close();
const sol::table thingy1 = lua.require_file("thingy", FILE_NAME);
CHECK(thingy1.valid());
const foo foo_v = thingy1["modfunc"]();
int val1 = foo_v.bar;
CHECK(val1 == 221);
}
SECTION("simple")
{
file << "return { modfunc = function () return 221 end }" << std::endl;
file.close();
const sol::table thingy1 = lua.require_file("thingy", FILE_NAME);
const sol::table thingy2 = lua.require_file("thingy", FILE_NAME);
CHECK(thingy1.valid());
CHECK(thingy2.valid());
int val1 = thingy1["modfunc"]();
int val2 = thingy2["modfunc"]();
CHECK(val1 == 221);
CHECK(val2 == 221);
// must have loaded the same table
CHECK(thingy1 == thingy2);
}
std::remove(FILE_NAME);
}
TEST_CASE("state/require_script", "opening strings as 'requires' clauses") { TEST_CASE("state/require_script", "opening strings as 'requires' clauses") {
std::string code = "return { modfunc = function () return 221 end }"; std::string code = "return { modfunc = function () return 221 end }";