foxygit / fake-08-xbox360 Log in
commit 959d745bab44942f6b76e8fbd90405b6344513c1
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Sep 14 22:12:16 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:09 2020 -0600

    Add a way to write tests for the lua state
---
 source/picoluaapi.cpp  |  2 +-
 source/vm.cpp          | 25 +++++++++++++++++++++++++
 source/vm.h            |  2 ++
 test/carts/cliptest.p8 |  1 -
 test/endtoendtests.cpp | 12 ++++++++++--
 5 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index a227b3d..3c3cff0 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -629,7 +629,7 @@ int printh(lua_State *L) {
     if (lua_isstring(L, 1)){
         const char * str = "";
         str = lua_tolstring(L, 1, nullptr);
-        printf(str);
+        printf("%s", str);
     }
     return 0;
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index 2fac65d..be1ea1e 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -375,3 +375,28 @@ vector<string> Vm::GetCartList(){
 string Vm::GetBiosError() {
     return _cartLoadError;
 }
+
+bool Vm::ExecuteLua(string luaString, string callbackFunction){
+    int success = luaL_dostring(_luaState, luaString.c_str());
+
+    if (! success == LUA_OK){
+        //bad lua passed
+        Logger::Write("Error: %s\n", lua_tostring(_luaState, -1));
+        lua_pop(_luaState, 1);
+
+        return false;
+    }
+
+    if (callbackFunction.length() > 0) {
+        lua_getglobal(_luaState, callbackFunction.c_str());
+        lua_call(_luaState, 0, 1);
+
+        bool result = lua_toboolean(_luaState, -1);
+
+        lua_pop(_luaState, 0);
+
+        return result;
+    }
+
+    return true;
+}
diff --git a/source/vm.h b/source/vm.h
index 53bdbe0..eef4544 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -67,5 +67,7 @@ class Vm {
     void SetCartList(vector<string> cartList);
     vector<string> GetCartList();
     string GetBiosError();
+
+    bool ExecuteLua(string luaString, string callbackFunction);
 };

diff --git a/test/carts/cliptest.p8 b/test/carts/cliptest.p8
index e473602..bf2fd59 100644
--- a/test/carts/cliptest.p8
+++ b/test/carts/cliptest.p8
@@ -7,7 +7,6 @@ end

 function _draw()
  cls()
- printh("made it to lua draw call\n")
  camera(-64, 0)
  clip(0, 8, 128, 112)

diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index e95a926..2771f1f 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -31,8 +31,6 @@ bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {

     size_t imageBytes = image.size();

-    printf("Dimensions: %dx%d, size: %d\n", width, height, (int)imageBytes);
-
     for(size_t i = 0; i < imageBytes; i += 4) {
         //get argb values
         uint8_t r = image[i];
@@ -80,6 +78,16 @@ TEST_CASE("Loading and running carts") {

             CHECK(vm->GetFrameCount() == 3);
         }
+        SUBCASE("check lua state")
+        {
+            bool globalVarLoaded = vm->ExecuteLua(
+                "function globalVarTest()\n"
+                " return a == 1\n"
+                "end\n",
+                "globalVarTest");
+
+            CHECK(globalVarLoaded);
+        }

         vm->CloseCart();
     }