Selaa lähdekoodia

tests: Add test cases for Path<A, K>

Ole 9 vuotta sitten
vanhempi
commit
a323fc4391
3 muutettua tiedostoa jossa 98 lisäystä ja 21 poistoa
  1. 1 1
      Makefile
  2. 22 20
      lib/luwra/tables.hpp
  3. 75 0
      tests/tables.cpp

+ 1 - 1
Makefile

@@ -7,7 +7,7 @@ EXEC            = exec
 TEST_DIR        := tests
 TEST_OUT        := $(TEST_DIR)/all
 TEST_SRCS       := all.cpp auxiliary.cpp types.cpp stack.cpp functions.cpp usertypes.cpp \
-                   wrappers.cpp
+                   wrappers.cpp tables.cpp
 TEST_DEPS       := $(TEST_SRCS:%.cpp=$(TEST_DIR)/%.d)
 TEST_OBJS       := $(TEST_SRCS:%.cpp=$(TEST_DIR)/%.o)
 

+ 22 - 20
lib/luwra/tables.hpp

@@ -52,7 +52,29 @@ namespace internal {
 			lua_pop(state, 1);
 		}
 	};
+}
+
+template <typename P, typename K>
+struct Value<internal::Path<P, K>> {
+	// Push the value to which the path points onto the stack.
+	static inline
+	size_t push(State* state, const internal::Path<P, K>& accessor) {
+		size_t pushedParents = luwra::push(state, accessor.parent);
+		if (pushedParents > 1)
+			lua_pop(state, static_cast<int>(pushedParents - 1));
+
+		size_t pushedKeys = luwra::push(state, accessor.key);
+		if (pushedKeys > 1)
+			lua_pop(state, static_cast<int>(pushedKeys - 1));
+
+		lua_rawget(state, -2);
+		lua_remove(state, -2);
+
+		return 1;
+	}
+};
 
+namespace internal {
 	template <typename A>
 	struct TableAccessor {
 		State* state;
@@ -95,26 +117,6 @@ namespace internal {
 	};
 }
 
-template <typename P, typename K>
-struct Value<internal::Path<P, K>> {
-	// Push the value to which the path points onto the stack.
-	static inline
-	size_t push(State* state, const internal::Path<P, K>& accessor) {
-		size_t pushedParents = luwra::push(state, accessor.parent);
-		if (pushedParents > 1)
-			lua_pop(state, static_cast<int>(pushedParents - 1));
-
-		size_t pushedKeys = luwra::push(state, accessor.key);
-		if (pushedKeys > 1)
-			lua_pop(state, static_cast<int>(pushedKeys - 1));
-
-		lua_rawget(state, -2);
-		lua_remove(state, -2);
-
-		return 1;
-	}
-};
-
 struct Table {
 	Reference ref;
 

+ 75 - 0
tests/tables.cpp

@@ -0,0 +1,75 @@
+#include <catch.hpp>
+#include <luwra.hpp>
+#include <string>
+
+TEST_CASE("Path<Table, string>") {
+	luwra::StateWrapper state;
+	REQUIRE(state.runString("value = 1337") == LUA_OK);
+
+	luwra::internal::Path<luwra::Table, std::string> path {state, "value"};
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<int>(state, -1) == 1337);
+	REQUIRE(path.read<int>(state) == 1337);
+
+	REQUIRE(lua_gettop(state) == 1);
+
+	path.write(state, 13.37);
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<double>(state, -1) == 13.37);
+	REQUIRE(path.read<double>(state) == 13.37);
+
+	REQUIRE(lua_gettop(state) == 2);
+}
+
+TEST_CASE("Path<Reference, string>") {
+	luwra::StateWrapper state;
+	REQUIRE(state.runString("value = 1337") == LUA_OK);
+
+	luwra::internal::Path<luwra::Reference, std::string> path {state.ref, "value"};
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<int>(state, -1) == 1337);
+	REQUIRE(path.read<int>(state) == 1337);
+
+	REQUIRE(lua_gettop(state) == 1);
+
+	path.write(state, 13.37);
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<double>(state, -1) == 13.37);
+	REQUIRE(path.read<double>(state) == 13.37);
+
+	REQUIRE(lua_gettop(state) == 2);
+}
+
+TEST_CASE("Path<Path<Reference, string>, string>") {
+	luwra::StateWrapper state;
+	REQUIRE(state.runString("value = {field = 1337}") == LUA_OK);
+
+	luwra::internal::Path<
+		luwra::internal::Path<
+			luwra::Reference,
+			std::string
+		>,
+		std::string
+	> path {{state.ref, "value"}, "field"};
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<int>(state, -1) == 1337);
+	REQUIRE(path.read<int>(state) == 1337);
+
+	REQUIRE(lua_gettop(state) == 1);
+
+	path.write(state, 13.37);
+
+	REQUIRE(luwra::push(state, path) == 1);
+	REQUIRE(luwra::read<double>(state, -1) == 13.37);
+	REQUIRE(path.read<double>(state) == 13.37);
+
+	REQUIRE(lua_gettop(state) == 2);
+
+	REQUIRE(state.runString("return value.field") == LUA_OK);
+	REQUIRE(luwra::read<double>(state, -1) == 13.37);
+}