From afd32908d56df50fa08889713046c6af13d064ef Mon Sep 17 00:00:00 2001 From: Kevin Brightwell Date: Mon, 22 Aug 2016 12:19:03 -0400 Subject: [PATCH] Add test case for require_file with a usertype I was having problems with usertypes and require_file, so I figured I shoudl work in a less complicated testbed than mine, and worked here. There as not a test case, so I wrote this one. Additionally, adjusted bootstrap.py to allow for `--lua-version lua53` syntax since most older linux systems use lua52, its useful to override. --- bootstrap.py | 5 ++-- tests.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index f2202a67..0eb0188d 100755 --- a/bootstrap.py +++ b/bootstrap.py @@ -42,6 +42,7 @@ parser.add_argument('--cxx', metavar='', help='compiler name to use (d 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('--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-dir', metavar='', help='directory lua is in with include and lib subdirectories') parser.add_argument('--install-dir', metavar='', 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')])) 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): # Using normal lua lua_lib = lua_version[:-1] + '.' + lua_version[-1] @@ -101,7 +102,7 @@ if 'linux' in sys.platform: ldflags.extend(libraries([lua_lib])) elif 'darwin' in sys.platform: # 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): # Using normal lua lua_incl = lua_version[:-1] + '.' + lua_version[-1] diff --git a/tests.cpp b/tests.cpp index 552636ce..8bf4bbdd 100644 --- a/tests.cpp +++ b/tests.cpp @@ -3,10 +3,13 @@ #include #include -#include + +#include +#include #include #include -#include +#include + #include "test_stack_guard.hpp" bool func_opt_ret_bool(sol::optional 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); } +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", + sol::constructors>{}, + "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") { std::string code = "return { modfunc = function () return 221 end }";