Преглед на файлове

tests: Use functions bundled in StateWrapper

Ole преди 9 години
родител
ревизия
53a655a507
променени са 4 файла, в които са добавени 93 реда и са изтрити 81 реда
  1. 6 6
      tests/auxiliary.cpp
  2. 13 13
      tests/functions.cpp
  3. 61 49
      tests/types.cpp
  4. 13 13
      tests/usertypes.cpp

+ 6 - 6
tests/auxiliary.cpp

@@ -22,14 +22,14 @@ TEST_CASE("setGlobal") {
 
 	luwra::setGlobal(state, "test", 1338);
 
-	REQUIRE(luaL_dostring(state, "return test") == LUA_OK);
+	REQUIRE(state.runString("return test") == LUA_OK);
 	REQUIRE(luwra::read<int>(state, -1) == 1338);
 }
 
 TEST_CASE("getGlobal") {
 	luwra::StateWrapper state;
 
-	REQUIRE(luaL_dostring(state, "test = 1337") == LUA_OK);
+	REQUIRE(state.runString("test = 1337") == LUA_OK);
 	REQUIRE(luwra::getGlobal<int>(state, "test") == 1337);
 }
 
@@ -46,7 +46,7 @@ TEST_CASE("setFields") {
 
 		lua_setglobal(state, "test");
 
-		REQUIRE(luaL_dostring(state, "return test.test, test[123]") == LUA_OK);
+		REQUIRE(state.runString("return test.test, test[123]") == LUA_OK);
 		REQUIRE(luwra::read<int>(state, -2) == 1338);
 		REQUIRE(luwra::read<int>(state, -1) == 456);
 	}
@@ -62,7 +62,7 @@ TEST_CASE("setFields") {
 
 		lua_setglobal(state, "test");
 
-		REQUIRE(luaL_dostring(state, "return test.test, test[123]") == LUA_OK);
+		REQUIRE(state.runString("return test.test, test[123]") == LUA_OK);
 		REQUIRE(luwra::read<int>(state, -2) == 1338);
 		REQUIRE(luwra::read<int>(state, -1) == 456);
 	}
@@ -71,7 +71,7 @@ TEST_CASE("setFields") {
 TEST_CASE("getField") {
 	luwra::StateWrapper state;
 
-	REQUIRE(luaL_dostring(state, "return {hello = 123}") == LUA_OK);
+	REQUIRE(state.runString("return {hello = 123}") == LUA_OK);
 	REQUIRE(luwra::getField<int>(state, -1, "hello") == 123);
 }
 
@@ -85,7 +85,7 @@ TEST_CASE("FieldVector") {
 
 	lua_setglobal(state, "test");
 
-	REQUIRE(luaL_dostring(state, "return test.test, test[test.test]") == LUA_OK);
+	REQUIRE(state.runString("return test.test, test[test.test]") == LUA_OK);
 	REQUIRE(luwra::read<int>(state, -2) == 7331);
 	REQUIRE(luwra::read<int>(state, -1) == 123);
 }

+ 13 - 13
tests/functions.cpp

@@ -36,10 +36,10 @@ TEST_CASE("FunctionWrapper") {
 		REQUIRE(cfun != nullptr);
 
 		// Register function
-		luwra::setGlobal(state, "test_function_noret_noparams", cfun);
+		state.set("test_function_noret_noparams", cfun);
 
 		// Invoke function
-		REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
+		REQUIRE(state.runString("test_function_noret_noparams()") == LUA_OK);
 		REQUIRE(lua_gettop(state) == 0);
 		REQUIRE(noret_environment == 1338);
 	}
@@ -54,10 +54,10 @@ TEST_CASE("FunctionWrapper") {
 		REQUIRE(cfun != nullptr);
 
 		// Register function
-		luwra::setGlobal(state, "test_function_noret", cfun);
+		state.set("test_function_noret", cfun);
 
 		// Invoke function
-		REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
+		REQUIRE(state.runString("test_function_noret(13, 37)") == LUA_OK);
 		REQUIRE(lua_gettop(state) == 0);
 		REQUIRE(noret_environment == req_environemt);
 	}
@@ -68,12 +68,12 @@ TEST_CASE("FunctionWrapper") {
 		REQUIRE(cfun != nullptr);
 
 		// Register function
-		luwra::setGlobal(state, "test_function_noparams", cfun);
+		state.set("test_function_noparams", cfun);
 
 		// Invoke function
-		REQUIRE(luaL_dostring(state, "return test_function_noparams()") == 0);
+		REQUIRE(state.runString("return test_function_noparams()") == LUA_OK);
 		REQUIRE(lua_gettop(state) == 1);
-		REQUIRE(luwra::Value<int>::read(state, -1) == test_function_noparams());
+		REQUIRE(luwra::read<int>(state, -1) == test_function_noparams());
 	}
 
 	SECTION("with return value, with parameters") {
@@ -82,12 +82,12 @@ TEST_CASE("FunctionWrapper") {
 		REQUIRE(cfun != nullptr);
 
 		// Register function
-		luwra::setGlobal(state, "test_function", cfun);
+		state.set("test_function", cfun);
 
 		// Invoke function
-		REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);
+		REQUIRE(state.runString("return test_function(37, 13)") == LUA_OK);
 		REQUIRE(lua_gettop(state) == 1);
-		REQUIRE(luwra::Value<int>::read(state, -1) == test_function(37, 13));
+		REQUIRE(luwra::read<int>(state, -1) == test_function(37, 13));
 	}
 }
 
@@ -95,19 +95,19 @@ TEST_CASE("NativeFunction") {
 	luwra::StateWrapper state;
 
 	SECTION("with return value") {
-		REQUIRE(luaL_dostring(state, "return function (x, y) return x + y end") == LUA_OK);
+		REQUIRE(state.runString("return function (x, y) return x + y end") == LUA_OK);
 
 		auto fun = luwra::read<luwra::NativeFunction<int>>(state, -1);
 		REQUIRE(fun(13, 37) == 50);
 	}
 
 	SECTION("without return value") {
-		REQUIRE(luaL_dostring(state, "return function (x, y) returnValue = x + y end") == LUA_OK);
+		REQUIRE(state.runString("return function (x, y) returnValue = x + y end") == LUA_OK);
 
 		auto fun = luwra::read<luwra::NativeFunction<void>>(state, -1);
 		fun(13, 37);
 
-		int returnValue = state["returnValue"];
+		int returnValue = state.get<int>("returnValue");
 		REQUIRE(returnValue == 50);
 	}
 }

+ 61 - 49
tests/types.cpp

@@ -6,52 +6,51 @@
 #include <utility>
 #include <type_traits>
 
-#if false
-
-template <typename I>
-struct NumericTest {
-	static
-	void test(lua_State* state) {
-		const I max_value = std::numeric_limits<I>::max();
-		const I min_value = std::numeric_limits<I>::lowest();
-		const I avg_value = (max_value + min_value) / 2;
-
-		// Largest value
-		CHECK((luwra::push(state, max_value) == 1));
-		CHECK((luwra::read<I>(state, -1) == max_value));
-
-		// Lowest value
-		CHECK((luwra::push(state, min_value) == 1));
-		CHECK((luwra::read<I>(state, -1) == min_value));
-
-		// Average value
-		CHECK((luwra::push(state, avg_value) == 1));
-		CHECK((luwra::read<I>(state, -1) == avg_value));
-	}
-};
-
-TEST_CASE("NumberLimits") {
-	luwra::StateWrapper state;
-
-	// Integer-based types
-	NumericTest<signed char>::test(state);
-	NumericTest<unsigned char>::test(state);
-	NumericTest<signed short>::test(state);
-	NumericTest<unsigned short>::test(state);
-	NumericTest<signed int>::test(state);
-	NumericTest<unsigned int>::test(state);
-	NumericTest<signed long int>::test(state);
-	NumericTest<unsigned long int>::test(state);
-	NumericTest<signed long long int>::test(state);
-	NumericTest<unsigned long long int>::test(state);
-
-	// Number-based types
-	NumericTest<float>::test(state);
-	NumericTest<double>::test(state);
-	NumericTest<long double>::test(state);
-}
-
-#endif /* false */
+// Numbers are royally fucked. They might or might not be stored in a floating-point number, which
+// makes testing for integer limits pointless.
+//
+// template <typename I>
+// struct NumericTest {
+// 	static
+// 	void test(lua_State* state) {
+// 		const I max_value = std::numeric_limits<I>::max();
+// 		const I min_value = std::numeric_limits<I>::lowest();
+// 		const I avg_value = (max_value + min_value) / 2;
+
+// 		// Largest value
+// 		CHECK((luwra::push(state, max_value) == 1));
+// 		CHECK((luwra::read<I>(state, -1) == max_value));
+
+// 		// Lowest value
+// 		CHECK((luwra::push(state, min_value) == 1));
+// 		CHECK((luwra::read<I>(state, -1) == min_value));
+
+// 		// Average value
+// 		CHECK((luwra::push(state, avg_value) == 1));
+// 		CHECK((luwra::read<I>(state, -1) == avg_value));
+// 	}
+// };
+
+// TEST_CASE("NumberLimits") {
+// 	luwra::StateWrapper state;
+
+// 	// Integer-based types
+// 	NumericTest<signed char>::test(state);
+// 	NumericTest<unsigned char>::test(state);
+// 	NumericTest<signed short>::test(state);
+// 	NumericTest<unsigned short>::test(state);
+// 	NumericTest<signed int>::test(state);
+// 	NumericTest<unsigned int>::test(state);
+// 	NumericTest<signed long int>::test(state);
+// 	NumericTest<unsigned long int>::test(state);
+// 	NumericTest<signed long long int>::test(state);
+// 	NumericTest<unsigned long long int>::test(state);
+
+// 	// Number-based types
+// 	NumericTest<float>::test(state);
+// 	NumericTest<double>::test(state);
+// 	NumericTest<long double>::test(state);
+// }
 
 TEST_CASE("Numbers") {
 	luwra::StateWrapper state;
@@ -111,18 +110,31 @@ TEST_CASE("Tuples") {
 	auto tuple = std::make_tuple(a, b, c);
 	REQUIRE(luwra::push(state, tuple) == 3);
 
+	REQUIRE(luwra::read<int>(state, -3) == a);
+	REQUIRE(luwra::read<std::string>(state, -2) == b);
+	REQUIRE(luwra::read<float>(state, -1) == c);
+
 	// Push nested tuple
 	auto tuple_nested = std::make_tuple(a, b, c, tuple);
 	REQUIRE(luwra::push(state, tuple_nested) == 6);
+
+	REQUIRE(luwra::read<int>(state, -6) == a);
+	REQUIRE(luwra::read<std::string>(state, -5) == b);
+	REQUIRE(luwra::read<float>(state, -4) == c);
+	REQUIRE(luwra::read<int>(state, -3) == a);
+	REQUIRE(luwra::read<std::string>(state, -2) == b);
+	REQUIRE(luwra::read<float>(state, -1) == c);
+
 }
 
 TEST_CASE("Boolean") {
 	luwra::StateWrapper state;
 
-	bool value = true;
+	REQUIRE(luwra::push(state, true) == 1);
+	REQUIRE(luwra::read<bool>(state, -1) == true);
 
-	REQUIRE(luwra::push(state, value) == 1);
-	REQUIRE(luwra::read<bool>(state, -1) == value);
+	REQUIRE(luwra::push(state, false) == 1);
+	REQUIRE(luwra::read<bool>(state, -1) == false);
 }
 
 TEST_CASE("Pushable") {

+ 13 - 13
tests/usertypes.cpp

@@ -19,7 +19,7 @@ TEST_CASE("UserTypeConstruction") {
 	luwra::registerUserType<A(int)>(state, "A");
 
 	// Construction
-	REQUIRE(luaL_dostring(state, "return A(73)") == 0);
+	REQUIRE(state.runString("return A(73)") == 0);
 
 	// Check
 	A* instance = luwra::read<A*>(state, -1);
@@ -60,35 +60,35 @@ TEST_CASE("UserTypeFields") {
 	lua_setglobal(state, "value");
 
 	// Unqualified get
-	REQUIRE(luaL_dostring(state, "return value:n()") == 0);
+	REQUIRE(state.runString("return value:n()") == 0);
 	REQUIRE(luwra::read<int>(state, -1) == value.n);
 
 	// Unqualified set
-	REQUIRE(luaL_dostring(state, "value:n(42)") == 0);
+	REQUIRE(state.runString("value:n(42)") == 0);
 	REQUIRE(value.n == 42);
 
 	// 'const'-qualified get
-	REQUIRE(luaL_dostring(state, "return value:cn()") == 0);
+	REQUIRE(state.runString("return value:cn()") == 0);
 	REQUIRE(luwra::read<int>(state, -1) == value.cn);
 
 	// 'const'-qualified set
-	REQUIRE(luaL_dostring(state, "value:cn(42)") == 0);
+	REQUIRE(state.runString("value:cn(42)") == 0);
 	REQUIRE(value.cn == 1338);
 
 	// 'volatile' get
-	REQUIRE(luaL_dostring(state, "return value:vn()") == 0);
+	REQUIRE(state.runString("return value:vn()") == 0);
 	REQUIRE(luwra::read<int>(state, -1) == value.vn);
 
 	// 'volatile' set
-	REQUIRE(luaL_dostring(state, "value:vn(42)") == 0);
+	REQUIRE(state.runString("value:vn(42)") == 0);
 	REQUIRE(value.vn == 42);
 
 	// 'const volatile'-qualified get
-	REQUIRE(luaL_dostring(state, "return value:cvn()") == 0);
+	REQUIRE(state.runString("return value:cvn()") == 0);
 	REQUIRE(luwra::read<int>(state, -1) == value.cvn);
 
 	// 'const volatile'-qualified set
-	REQUIRE(luaL_dostring(state, "value:cvn(42)") == 0);
+	REQUIRE(state.runString("value:cvn(42)") == 0);
 	REQUIRE(value.cvn == 1338);
 }
 
@@ -135,22 +135,22 @@ TEST_CASE("UserTypeMethods") {
 	lua_setglobal(state, "value");
 
 	// Unqualified method
-	REQUIRE(luaL_dostring(state, "return value:foo1(63)") == 0);
+	REQUIRE(state.runString("return value:foo1(63)") == 0);
 	REQUIRE(value.prop == 1400);
 	REQUIRE(luwra::read<int>(state, -1) == value.prop);
 
 	// 'const'-qualified method
-	REQUIRE(luaL_dostring(state, "return value:foo2(44)") == 0);
+	REQUIRE(state.runString("return value:foo2(44)") == 0);
 	REQUIRE(value.prop == 1400);
 	REQUIRE(luwra::read<int>(state, -1) == 1444);
 
 	// 'volatile'-qualified method
-	REQUIRE(luaL_dostring(state, "return value:foo3(400)") == 0);
+	REQUIRE(state.runString("return value:foo3(400)") == 0);
 	REQUIRE(value.prop == 1000);
 	REQUIRE(luwra::read<int>(state, -1) == value.prop);
 
 	// 'const volatile'-qualified method
-	REQUIRE(luaL_dostring(state, "return value:foo4(334)") == 0);
+	REQUIRE(state.runString("return value:foo4(334)") == 0);
 	REQUIRE(value.prop == 1000);
 	REQUIRE(luwra::read<int>(state, -1) == 666);
 }