From 23290ba44dd078957ad871607aa083daaa5f4991 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Tue, 14 Nov 2023 22:48:37 +0100 Subject: [PATCH 1/2] Update: automated testing improvements, also including ./extend .metta files now --- extend/example1.metta | 2 +- extend/example2.metta | 2 +- extend/test.py | 46 +++++++++++++++++++++++++++++++++++++++++++ test.sh | 1 + tests/test.sh | 3 ++- 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 extend/test.py diff --git a/extend/example1.metta b/extend/example1.metta index b4acfac..bc277b8 100644 --- a/extend/example1.metta +++ b/extend/example1.metta @@ -2,7 +2,7 @@ !(compile! compileme.metta) (= (rustmettafunc $a) 42) -!(facF 100) +!(facF 10) !(test1 dummy) !(test2 dummy) !(test3 dummy) diff --git a/extend/example2.metta b/extend/example2.metta index 9ad2b5f..691975a 100644 --- a/extend/example2.metta +++ b/extend/example2.metta @@ -6,5 +6,5 @@ (* $n (facF (- $n 1))))) ") -!(facF 42) +!(facF 10) diff --git a/extend/test.py b/extend/test.py new file mode 100644 index 0000000..e7055b0 --- /dev/null +++ b/extend/test.py @@ -0,0 +1,46 @@ +import os +import ast +import sys +basefile = "" +filename = sys.argv[1] +if filename.split("/")[-1][0].isupper() or "compileme.metta" in filename: #temp files are skipped + exit(0) +with open(filename) as file: + basefile = file.read() +print("Testing:", sys.argv[1]) +newfile = "" +for line in basefile.split("\n"): + if not line.startswith('")') and not line.startswith("))") and \ + not line.startswith("!(compile! ") and not line.startswith("!(extend-py mettamorph)"): + newfile += line + "\n" + if "!(compile! " in line and ".metta)" in line: + includefile = "./extend/" + line.split("!(compile! ")[1].split(")")[0] + with open(includefile) as file: + newfile += file.read() + "\n" + +with open("TEST.metta","w") as file: + file.write(newfile) + +os.system(f"metta {filename} > OUTPUT_IS.txt") +os.system(f"metta TEST.metta > OUTPUT_SHOULD.txt") + +with open("OUTPUT_IS.txt") as file: + OUTPUT_IS = file.read().replace("[(Compilation: skipped)]\n", "").replace("[(Compilation: success)]\n", "").replace("#t", "True").replace("#f", "False") +with open("OUTPUT_SHOULD.txt") as file: + OUTPUT_SHOULD = file.read().replace('"','').replace("'","") + +def SORT_LINES(name): + lines = "" + for line in name.split("\n"): + try: + my_list = ast.literal_eval(list_as_string) + my_list.sort() + lines += str(my_list) + "\n" + except: + lines += line + "\n" + +OUTPUT_IS = SORT_LINES(OUTPUT_IS) +OUTPUT_SHOULD = SORT_LINES(OUTPUT_SHOULD) +if OUTPUT_IS != OUTPUT_SHOULD: + print("FAILED:", filename) + diff --git a/test.sh b/test.sh index 758ac65..df51769 100644 --- a/test.sh +++ b/test.sh @@ -1 +1,2 @@ +find ./extend -name "*.metta" -exec python3 ./extend/test.py {} \; find ./tests -name "*.metta" -exec sh ./tests/test.sh {} \; diff --git a/tests/test.sh b/tests/test.sh index 276560d..d845d2c 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -1 +1,2 @@ -sh run.sh $1 2> /dev/null | grep "!=" && echo $1 +echo "Testing:" $1 +sh run.sh $1 2> /dev/null | grep "!=" && echo "FAILED:" $1 From 95003577ad2ac632cd869af927df58a3fe074c43 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Tue, 14 Nov 2023 23:17:15 +0100 Subject: [PATCH 2/2] Update: testing code comments --- extend/test.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/extend/test.py b/extend/test.py index e7055b0..2f0f196 100644 --- a/extend/test.py +++ b/extend/test.py @@ -1,6 +1,8 @@ import os import ast import sys + +#1. Load file and announce test: basefile = "" filename = sys.argv[1] if filename.split("/")[-1][0].isupper() or "compileme.metta" in filename: #temp files are skipped @@ -8,6 +10,8 @@ with open(filename) as file: basefile = file.read() print("Testing:", sys.argv[1]) + +#2. For compilation-omitted code, replace includes with included code and omit compilation instructions newfile = "" for line in basefile.split("\n"): if not line.startswith('")') and not line.startswith("))") and \ @@ -17,18 +21,18 @@ includefile = "./extend/" + line.split("!(compile! ")[1].split(")")[0] with open(includefile) as file: newfile += file.read() + "\n" - with open("TEST.metta","w") as file: file.write(newfile) +#3. Run both the original file with compilation and the compilation-omitted veresion thereof: os.system(f"metta {filename} > OUTPUT_IS.txt") os.system(f"metta TEST.metta > OUTPUT_SHOULD.txt") - with open("OUTPUT_IS.txt") as file: OUTPUT_IS = file.read().replace("[(Compilation: skipped)]\n", "").replace("[(Compilation: success)]\n", "").replace("#t", "True").replace("#f", "False") with open("OUTPUT_SHOULD.txt") as file: OUTPUT_SHOULD = file.read().replace('"','').replace("'","") +#4. Function to deal with alternative solution ordering def SORT_LINES(name): lines = "" for line in name.split("\n"): @@ -39,6 +43,7 @@ def SORT_LINES(name): except: lines += line + "\n" +#5. Compare the postprocessed outputs, reporting failure on mismatch OUTPUT_IS = SORT_LINES(OUTPUT_IS) OUTPUT_SHOULD = SORT_LINES(OUTPUT_SHOULD) if OUTPUT_IS != OUTPUT_SHOULD: