mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Added some catch tests and a basic nil getter.
This commit is contained in:
parent
e7f8ec9e5c
commit
dd63621453
|
@ -1,13 +1,13 @@
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include <catch.hpp>
|
||||||
#include <sol.hpp>
|
#include <sol.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include <tuple>
|
|
||||||
|
|
||||||
std::string free_func_yo() {
|
std::string free_function() {
|
||||||
std::cout << "free_func_yo()" << std::endl;
|
std::cout << "free_function()" << std::endl;
|
||||||
return "test";
|
return "test";
|
||||||
}
|
}
|
||||||
|
|
||||||
struct member_test {
|
struct object {
|
||||||
|
|
||||||
std::string operator() () {
|
std::string operator() () {
|
||||||
std::cout << "member_test()" << std::endl;
|
std::cout << "member_test()" << std::endl;
|
||||||
|
@ -16,32 +16,75 @@ struct member_test {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void run_test(sol::state& lua) {
|
void run_script(sol::state& lua) {
|
||||||
lua.script("assert(os.fun() == \"test\")\n"
|
lua.script("assert(os.fun() == \"test\")\n"
|
||||||
"assert(os.name == \"windows\")");
|
"assert(os.name == \"windows\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( ) {
|
TEST_CASE( "simple/set_global", "Check if the set_global works properly." ) {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
lua.open_libraries( sol::lib::base, sol::lib::os );
|
|
||||||
|
|
||||||
|
lua.set( "a", 9 );
|
||||||
|
REQUIRE_NOTHROW( lua.script( "if a ~= 9 then error('wrong value') end" ) );
|
||||||
|
|
||||||
|
lua.set( "d", "hello" );
|
||||||
|
REQUIRE_NOTHROW( lua.script( "if d ~= 'hello' then error('expected \\'hello\\', got '.. tostring(d)) end" ) );
|
||||||
|
|
||||||
|
lua.set( "e", std::string( "hello" ) );
|
||||||
|
REQUIRE_NOTHROW( lua.script( "if d ~= 'hello' then error('expected \\'hello\\', got '.. tostring(d)) end" ) );
|
||||||
|
|
||||||
|
lua.set( "f", true );
|
||||||
|
REQUIRE_NOTHROW( lua.script( "if f ~= true then error('wrong value') end" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "simple/get", "Tests if the get function works properly." ) {
|
||||||
|
sol::state lua;
|
||||||
|
|
||||||
|
lua.script( "a = 9" );
|
||||||
|
auto a = lua.get<int>( "a" );
|
||||||
|
REQUIRE( a == 9.0 );
|
||||||
|
|
||||||
|
lua.script( "b = nil" );
|
||||||
|
REQUIRE_NOTHROW( auto b = lua.get<sol::nil_t>( "b" ) );
|
||||||
|
|
||||||
|
lua.script( "d = 'hello'" );
|
||||||
|
auto d = lua.get<std::string>( "d" );
|
||||||
|
REQUIRE( d == "hello" );
|
||||||
|
|
||||||
|
lua.script( "e = true" );
|
||||||
|
auto e = lua.get<bool>( "e" );
|
||||||
|
REQUIRE( e == true );
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "simple/addition", "" ) {
|
||||||
|
sol::state lua;
|
||||||
|
|
||||||
|
lua.set( "b", 0.2 );
|
||||||
|
lua.script( "c = 9 + b" );
|
||||||
|
auto c = lua.get<double>( "c" );
|
||||||
|
|
||||||
|
REQUIRE( c == 9.2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
lua.get<sol::table>( "os" ).set( "name", "windows" );
|
lua.get<sol::table>( "os" ).set( "name", "windows" );
|
||||||
|
|
||||||
|
SECTION( "" )
|
||||||
lua.get<sol::table>( "os" ).set_function( "fun",
|
lua.get<sol::table>( "os" ).set_function( "fun",
|
||||||
[ ] ( ) {
|
[ ] ( ) {
|
||||||
std::cout << "stateless lambda()" << std::endl;
|
std::cout << "stateless lambda()" << std::endl;
|
||||||
return "test";
|
return "test";
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
lua.get<sol::table>( "os" ).set_function( "fun", &free_func_yo );
|
lua.get<sol::table>( "os" ).set_function( "fun", &free_function );
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
// l-value, can optomize
|
// l-value, can optomize
|
||||||
auto lval = member_test( );
|
auto lval = object( );
|
||||||
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), lval );
|
lua.get<sol::table>( "os" ).set_function( "fun", &object::operator(), lval );
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
// Tests will begin failing here
|
// Tests will begin failing here
|
||||||
// stateful lambda: non-convertible, unoptomizable
|
// stateful lambda: non-convertible, unoptomizable
|
||||||
|
@ -52,15 +95,16 @@ int main( ) {
|
||||||
return "test";
|
return "test";
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
// r-value, cannot optomize
|
// r-value, cannot optomize
|
||||||
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), member_test( ) );
|
lua.get<sol::table>( "os" ).set_function( "fun", &object::operator(), object( ) );
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
// r-value, cannot optomize
|
// r-value, cannot optomize
|
||||||
auto rval = member_test( );
|
auto rval = object( );
|
||||||
lua.get<sol::table>( "os" ).set_function( "fun", &member_test::operator(), std::move( rval ) );
|
lua.get<sol::table>( "os" ).set_function( "fun", &object::operator(), std::move( rval ) );
|
||||||
run_test( lua );
|
run_script( lua );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -45,7 +45,7 @@ inline T get_unsigned(lua_State* L, std::true_type, int index = -1) {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T get_unsigned(lua_State* L, std::false_type, int index = -1) {
|
inline T get_unsigned(lua_State* L, std::false_type, int index = -1) {
|
||||||
return lua_tointeger(L, index);
|
return static_cast<T>( lua_tointeger(L, index) );
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -62,6 +62,18 @@ inline T get_arithmetic(lua_State* L, std::true_type, int index = -1) {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T get_helper(lua_State* L, std::true_type, int index = -1) {
|
inline T get_helper(lua_State* L, std::true_type, int index = -1) {
|
||||||
|
return get_nil<T>(L, std::is_same<nil_t, T>(), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline T get_nil(lua_State* L, std::true_type, int index = -1) {
|
||||||
|
if (lua_isnil(L, index) == 0)
|
||||||
|
throw sol::sol_error("not nil");
|
||||||
|
return nil_t{};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline T get_nil(lua_State* L, std::false_type, int index = -1) {
|
||||||
// T is a class type
|
// T is a class type
|
||||||
return T(L, index);
|
return T(L, index);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user