Improved tests, added a build.ninja for TeamCity, and fixed formatting again to work with Rapptz's style.

This commit is contained in:
ThePhD 2013-12-11 11:18:13 -05:00
parent dd63621453
commit 350f430d74
8 changed files with 250 additions and 125 deletions

3
.gitignore vendored
View File

@ -1,6 +1,5 @@
obj/*
bin/*
*.ninja*
demacro.txt
Shinobi2
dev.*
@ -16,3 +15,5 @@ x64/
*.sln
*.gitattributes
liblua.a
sol/include/
.ninja*

27
build.ninja Normal file
View File

@ -0,0 +1,27 @@
# This file has been generated by shinobi version 0.9.5
ninja_required_version = 1.3
builddir = bin
objdir = obj
cxx = g++
cxxflags = -std=c++11 -pedantic -pedantic-errors -Wextra -Wall -O2 -DNDEBUG
incflags = -I"." -I"./include" -I"." -I"./lua-5.2.2/src/" -I"./Catch/include/"
linkflags = -static liblua.a -L"./lib"
rule compile
command = $cxx -MMD -MF $out.d $cxxflags -c $in -o $out $incflags
deps = gcc
depfile = $out.d
description = Compiling $in
rule link
command = $cxx $in -o $out $linkflags
description = Creating $out
build $objdir/sol.scratch.o: compile sol.scratch.cpp
build $builddir/sol.scratch: link $objdir/sol.scratch.o
build install: phony $builddir/sol.scratch
default install

View File

@ -16,6 +16,11 @@ struct object {
};
int plop_xyz(int x, int y, std::string z) {
std::cout << x << " " << y << " " << z << std::endl;
return 11;
}
void run_script(sol::state& lua) {
lua.script("assert(os.fun() == \"test\")\n"
"assert(os.name == \"windows\")");
@ -66,6 +71,90 @@ TEST_CASE( "simple/addition", "" ) {
REQUIRE(c == 9.2);
}
TEST_CASE("simple/if", "") {
sol::state lua;
std::string program = "if true then f = 0.1 else f = 'test' end";
lua.script(program);
auto f = lua.get<double>("f");
REQUIRE(f == 0.1);
}
TEST_CASE("simple/evalStream", "The VM evaluates a stream's contents using a reader") {
sol::state lua;
std::stringstream sscript;
int g = 9;
sscript << "g = " << g << ";";
REQUIRE_NOTHROW(lua.script(sscript.str()));
REQUIRE(lua.get<double>("g") == 9.0);
}
TEST_CASE("simple/callWithParameters", "Lua function is called with a few parameters from C++") {
sol::state lua;
REQUIRE_NOTHROW(lua.script("function my_add(i, j, k) return i + j + k end"));
auto f = lua.get<sol::function>("my_add");
REQUIRE_NOTHROW(f.call<int>(1, 2, 3));
REQUIRE_THROWS(f.call<int>(1, 2, "arf"));
}
TEST_CASE("simple/callCppFunction", "C++ function is called from lua") {
sol::state lua;
lua.set_function("plop_xyz", plop_xyz);
lua.script("x = plop_xyz(2, 6, 'hello')");
REQUIRE(lua.get<int>("x") == 11);
}
TEST_CASE("simple/callLambda", "A C++ lambda is exposed to lua and called") {
sol::state lua;
int x = 0;
lua.set_function("foo", [ &x ] { x = 1; });
lua.script("foo()");
REQUIRE(x == 1);
}
TEST_CASE("advanced/callLambdaReturns", "Checks for lambdas returning values") {
sol::state lua;
lua.set_function("a", [ ] { return 42; });
lua.set_function("b", [ ] { return 42u; });
lua.set_function("c", [ ] { return 3.14; });
lua.set_function("d", [ ] { return 6.28f; });
lua.set_function("e", [ ] { return "lol"; });
lua.set_function("f", [ ] { return true; });
lua.set_function("g", [ ] { return std::string("str"); });
lua.set_function("h", [ ] { });
lua.set_function("i", [ ] { return sol::nil; });
}
TEST_CASE("advanced/callLambda2", "A C++ lambda is exposed to lua and called") {
sol::state lua;
int x = 0;
lua.set_function("set_x", [ &] (int new_x) {
x = new_x;
return 0;
});
lua.script("set_x(9)");
REQUIRE(x == 9);
}
TEST_CASE("negative/basicError", "Check if error handling works correctly") {
sol::state lua;
REQUIRE_THROWS(lua.script("nil[5]"));
}
/*
lua.get<sol::table>("os").set("name", "windows");

View File

@ -30,22 +30,22 @@ namespace sol {
class function : virtual public reference {
private:
template<typename... Ret>
std::tuple<Ret...> call(types<Ret...>, std::size_t n) {
lua_pcall(state(), n, sizeof...(Ret), 0);
std::tuple<Ret...> invoke(types<Ret...>, std::size_t n) {
lua_pcall(state(), static_cast<int>(n), sizeof...(Ret), 0);
return stack::pop_call(state(), std::make_tuple<Ret...>, types<Ret...>());
}
template<typename Ret>
Ret call(types<Ret>, std::size_t n) {
lua_pcall(state(), n, 1, 0);
Ret invoke(types<Ret>, std::size_t n) {
lua_pcall(state(), static_cast<int>(n), 1, 0);
return stack::pop<Ret>(state());
}
void call(types<void>, std::size_t n) {
void invoke(types<void>, std::size_t n) {
lua_pcall(state(), static_cast<uint32_t>(n), 0, 0);
}
void call(types<>, std::size_t n) {
void invoke(types<>, std::size_t n) {
lua_pcall(state(), static_cast<uint32_t>(n), 0, 0);
}
@ -56,10 +56,10 @@ public:
}
template<typename... Ret, typename... Args>
auto invoke(Args&&... args) -> decltype(call(types<Ret...>(), sizeof...(Args))) {
auto call(Args&&... args) -> decltype(invoke(types<Ret...>(), sizeof...(Args))) {
push();
stack::push_args(state(), std::forward<Args>(args)...);
return call(types<Ret...>(), sizeof...(Args));
return invoke(types<Ret...>(), sizeof...(Args));
}
};
} // sol

View File

@ -115,13 +115,21 @@ public:
}
}
void eval(const std::string& code) {
script(code);
}
void script(const std::string& code) {
if(luaL_dostring(L.get(), code.c_str())) {
lua_error(L.get());
}
}
void open_file(const std::string& filename) {
void eval_file(const std::string& filename) {
script_file(filename);
}
void script_file(const std::string& filename) {
if (luaL_dofile(L.get(), filename.c_str())) {
lua_error(L.get());
}