From db14c531b8ed6b8419eb20789f5c5be85cefce0c Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Mon, 28 Oct 2013 06:53:51 -0700 Subject: [PATCH] More tests for primitives. --- runtests | 2 +- src/primitives.c | 3 +++ test/number_abs.wren | 6 ++++++ test/string_contains.wren | 8 ++++++++ test/string_count.wren | 2 ++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/number_abs.wren create mode 100644 test/string_contains.wren create mode 100644 test/string_count.wren diff --git a/runtests b/runtests index bdcbcf91..18513718 100755 --- a/runtests +++ b/runtests @@ -164,7 +164,7 @@ def run_test(path): # Display the results. if len(fails) == 0: passed += 1 - print color.GREEN + 'PASS' + color.DEFAULT + ': ' + path + #print color.GREEN + 'PASS' + color.DEFAULT + ': ' + path else: failed += 1 print_line(color.RED + 'FAIL' + color.DEFAULT + ': ' + path) diff --git a/src/primitives.c b/src/primitives.c index e5d9a33a..246fc0d2 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -35,6 +35,9 @@ DEF_PRIMITIVE(string_contains) // TODO(bob): Check type of arg first! const char* search = ((ObjString*)args[1])->value; + // Corner case, the empty string contains the empty string. + if (strlen(string) == 0 && strlen(search) == 0) return (Value)makeNum(1); + // TODO(bob): Return bool. return (Value)makeNum(strstr(string, search) != NULL); } diff --git a/test/number_abs.wren b/test/number_abs.wren new file mode 100644 index 00000000..5612f46d --- /dev/null +++ b/test/number_abs.wren @@ -0,0 +1,6 @@ +io.write(123.abs) // expect: 123 +io.write(-123.abs) // expect: 123 +io.write(0.abs) // expect: 0 +io.write(-0.abs) // expect: 0 + +// TODO(bob): Floating point numbers. diff --git a/test/string_contains.wren b/test/string_contains.wren new file mode 100644 index 00000000..1560a450 --- /dev/null +++ b/test/string_contains.wren @@ -0,0 +1,8 @@ +io.write("".contains("")) // expect: 1 +io.write("anything".contains("")) // expect: 1 +io.write("something".contains("meth")) // expect: 1 +io.write("something".contains("some")) // expect: 1 +io.write("something".contains("ing")) // expect: 1 +io.write("something".contains("math")) // expect: 0 + +// TODO(bob): Passing non-string as argument. diff --git a/test/string_count.wren b/test/string_count.wren new file mode 100644 index 00000000..43aac2bc --- /dev/null +++ b/test/string_count.wren @@ -0,0 +1,2 @@ +io.write("".count) // expect: 0 +io.write("a string".count) // expect: 8