From 0d23fbd41a7aaaa6c13d361abeb633b2cade03ca Mon Sep 17 00:00:00 2001 From: Rauli Laine Date: Thu, 17 Aug 2017 00:46:23 +0300 Subject: [PATCH] Add initial test cases --- .editorconfig | 2 +- tests/framework.plorth | 22 ++++++ tests/test-array.plorth | 145 ++++++++++++++++++++++++++++++++++++++ tests/test-boolean.plorth | 13 ++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 tests/framework.plorth create mode 100644 tests/test-array.plorth create mode 100644 tests/test-boolean.plorth diff --git a/.editorconfig b/.editorconfig index 5df7273..21d189b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,7 +6,7 @@ end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true -[*.{cpp,hpp}] +[*.{cpp,hpp,plorth}] indent_style = space indent_size = 2 diff --git a/tests/framework.plorth b/tests/framework.plorth new file mode 100644 index 0000000..7822151 --- /dev/null +++ b/tests/framework.plorth @@ -0,0 +1,22 @@ +: expect-quote-to-return-true + call >boolean not + ( "Assertion failed." value-error throw ) + if +; + +: test-case + ( + array? + ( ( expect-quote-to-return-true ) swap for-each ) + ( expect-quote-to-return-true ) + if-else + ) + ( + swap + >string "✘ " swap + println + throw + ) + try + >string "✔ " swap + println + clear +; diff --git a/tests/test-array.plorth b/tests/test-array.plorth new file mode 100644 index 0000000..eb64293 --- /dev/null +++ b/tests/test-array.plorth @@ -0,0 +1,145 @@ +"./framework.plorth" import + +"length" +[ + ( [] length nip 0 = ), + ( [1, 2, 3] length nip 3 = ), +] +test-case + +"includes?" +[ + ( 2 [1, 2, 3] includes? nip ), + ( 4 [1, 2, 3] includes? not nip ), +] +test-case + +"index-of" +[ + ( 2 [1, 2, 3] index-of nip 1 = ), + ( 4 [1, 2, 3] index-of nip null = ), +] +test-case + +"find" +[ + ( ( 2 > ) [1, 2, 3] find nip 3 = ), + ( ( 3 > ) [1, 2, 3] find nip null = ), +] +test-case + +"find-index" +[ + ( ( 2 > ) [1, 2, 3] find-index nip 2 = ), + ( ( 3 > ) [1, 2, 3] find-index nip null = ), +] +test-case + +"every?" +[ + ( ( number? nip ) [1, 2, 3] every? nip ), + ( ( number? nip ) [1, "foo", 3] every? nip not ), +] +test-case + +"some?" +[ + ( ( number? nip ) ["foo", "bar", 1] some? nip ), + ( ( number? nip ) ["foo", "bar"] some? nip not ), +] +test-case + +"reverse" +[ + ( [1, 2, 3] reverse [3, 2, 1] = ), +] +test-case + +"uniq" +[ + ( [1, 1, 2, 2, 3, 3] uniq [1, 2, 3] = ), +] +test-case + +"extract" +[ + ( ["test"] extract "test" = ), +] +test-case + +"join" +[ + ( ", " ["foo", "bar"] join "foo, bar" = ), +] +test-case + +"for-each" +[ + ( (drop true) [1] for-each ), +] +test-case + +"map" +[ + ( ( 1 + ) [1, 2, 3] map [2, 3, 4] = ), +] +test-case + +"filter" +[ + ( ( 1 > ) [1, 2, 3] filter [2, 3] = ), + ( ( 3 > ) [1, 2, 3] filter [] = ), +] +test-case + +"reduce" +[ + ( ( + ) [1, 2, 3] reduce 6 = ), +] +test-case + +"+" +[ + ( [1] [2] + [1, 2] = ), + ( [] [] + length nip 0 = ), + ( [1] [] + length nip 1 = ), +] +test-case + +"*" +[ + ( 2 [1, 2] * [1, 2, 1, 2] = ), + ( 1 [] * length nip 0 = ), + ( 0 [1, 2, 3] * length nip 0 = ), +] +test-case + +"&" +[ + ( [1, 1, 2, 3, 4] [1, 4, 4] & [1, 4] = ), +] +test-case + +"|" +[ + ( [1, 1, 2, 2] [2, 2, 3, 3, 4] | [1, 2, 3, 4] = ), +] +test-case + +"@" +[ + ( 0 [1, 2, 3] @ nip 1 = ), + ( -1 [1, 2, 3] @ nip 3 = ), + ( -2 [1, 2, 3] @ nip 2 = ), + ( 2 [1, 2, 3] @ nip 3 = ), +] +test-case + +"!" +[ + ( "foo" 0 [1, 2, 3] ! ["foo", 2, 3] = ), + ( "foo" -1 [1, 2, 3] ! [1, 2, "foo"] = ), + ( "foo" -2 [1, 2, 3] ! [1, "foo", 3] = ), + ( "foo" 2 [1, 2, 3] ! [1, 2, "foo"] = ), +] +test-case diff --git a/tests/test-boolean.plorth b/tests/test-boolean.plorth new file mode 100644 index 0000000..4883ac9 --- /dev/null +++ b/tests/test-boolean.plorth @@ -0,0 +1,13 @@ +"./framework.plorth" import + +"and" +( true true and ) expect + +"or" +( false true or ) expect + +"xor" +( true false xor ) expect + +"not" +( true not false = ) expect