commit 31280b5c646c632312362687a8b9d6c2f602b69b
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Sep 11 08:51:28 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:08 2020 -0600
Get cart patcher tests working, and support more shorthand assignments
---
source/cartPatcher.cpp | 12 ++++++++++--
test/Makefile | 13 +++---------
test/cartpatchertests.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 58 insertions(+), 17 deletions(-)
diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
index da05992..2365a83 100644
--- a/source/cartPatcher.cpp
+++ b/source/cartPatcher.cpp
@@ -34,8 +34,16 @@ const char * patchLuaFunction = R"#(
end
end
end)
- -- rewrite assignment operators
- lua = lua:gsub("(%S+)%s*([%+-%*/%%])=","%1 = %1 %2 ")
+ -- rewrite assignment operators (longest operators first)
+ lua = lua:gsub("(%S+)%s*>>>=","%1 = %1 >>> ")
+
+ lua = lua:gsub("(%S+)%s*%.%.=","%1 = %1 .. ")
+ lua = lua:gsub("(%S+)%s*^^=","%1 = %1 ^^ ")
+ lua = lua:gsub("(%S+)%s*<<=","%1 = %1 << ")
+ lua = lua:gsub("(%S+)%s*>>=","%1 = %1 >> ")
+
+ lua = lua:gsub("(%S+)%s*([%+-%*/%%\\^|&])=","%1 = %1 %2 ")
+
-- rewrite inspect operator "?"
lua = lua:gsub("^(%s*)?([^\n\r]*)","%1print(%2)")
-- convert binary literals to hex literals
diff --git a/test/Makefile b/test/Makefile
index 654fbe3..79cb339 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -6,8 +6,8 @@ CFLAGS := -Wall -g -c -O2 \
all: runtests
-#testrunner.a: testroot.o fontdatatests.o graphicstests.o audiotests.o cartpatchertests.o
- #g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
+testrunner.a: testroot.o fontdatatests.o graphicstests.o audiotests.o cartpatchertests.o
+ g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
testroot.o \
fontdata.o fontdatatests.o \
graphics.o graphicstests.o stringToDataHelpers.o \
@@ -16,13 +16,6 @@ all: runtests
-llua -ldl \
-o testrunner.a
-testrunner.a: testroot.o fontdatatests.o graphicstests.o audiotests.o
- g++ testroot.o \
- fontdata.o fontdatatests.o \
- graphics.o graphicstests.o stringToDataHelpers.o \
- Audio.o synth.o audiotests.o \
- -o testrunner.a
-
testroot.o: testroot.cpp doctest.h
g++ $(CFLAGS) testroot.cpp
@@ -43,7 +36,7 @@ audiotests.o: audiotests.cpp ../source/Audio.cpp ../source/Audio.h \
cartpatchertests.o: liblua.a cartpatchertests.cpp ../source/cartPatcher.cpp ../source/cartPatcher.h
g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
cartpatchertests.cpp ../source/cartPatcher.cpp \
- -llua
+ -llua -ldl
liblua.a:
diff --git a/test/cartpatchertests.cpp b/test/cartpatchertests.cpp
index c427957..a86b87f 100644
--- a/test/cartpatchertests.cpp
+++ b/test/cartpatchertests.cpp
@@ -3,9 +3,49 @@
#include "doctest.h"
#include "../source/cartPatcher.h"
-TEST_CASE("dummy testcase") {
- std::string patched = getPatchedLua("");
+TEST_CASE("Cart Patcher/Preprocessor tests") {
- //CHECK_EQ(patched.length(), 0);
- CHECK_EQ(0, 0);
-}
\ No newline at end of file
+ SUBCASE("patching lua handles empty string"){
+ std::string patched = getPatchedLua("");
+
+ CHECK_EQ(patched.length(), 0);
+ }
+ SUBCASE("assignment operators expanded"){
+ std::string operators[] = {
+ "+", "-", "*", "/", "\\", "%", "^", "..",
+ "|", "&", "^^", "<<", ">>", ">>>"
+ };
+
+ for (int i = 0; i < 14; i++) {
+ std::string unpatched = "foo" + operators[i] + "=bar";
+ std::string patched = getPatchedLua(unpatched.c_str());
+
+ CHECK(patched == "foo = foo " + operators[i] + " bar");
+ };
+ }
+ SUBCASE("!= converted"){
+ std::string patched = getPatchedLua("thing != other");
+
+ CHECK(patched == "thing ~= other");
+ }
+ SUBCASE("// c style comment converted"){
+ std::string patched = getPatchedLua("//this is a comment");
+
+ CHECK(patched == "--this is a comment");
+ }
+ SUBCASE("shorthand if statment converted"){
+ std::string patched = getPatchedLua("if (pl.drilling>0) pl.drilling-=1\n");
+
+ CHECK(patched == "if pl.drilling>0 then pl.drilling = pl.drilling - 1 end\n");
+
+ patched = getPatchedLua("if (thingIsTrue()) doADifferentThing()\n");
+
+ CHECK(patched == "if thingIsTrue() then doADifferentThing() end\n");
+ }
+ SUBCASE("shorthand if else statment converted"){
+ std::string patched = getPatchedLua("if (cond2) print(\"cond2\") else print(\"not cond2\")\n");
+
+ CHECK(patched == "if cond2 then print(\"cond2\") else print(\"not cond2\") end\n");
+ }
+
+}