From f2b334d7d21707a6b50d3b99eb8f07250563f414 Mon Sep 17 00:00:00 2001 From: Gavin Schulz Date: Wed, 14 Jan 2015 22:42:15 -0800 Subject: [PATCH] Add more `forall` test cases. --- builtin/core.wren | 2 +- src/wren_core.c | 2 +- test/list/forall.wren | 6 +++--- test/list/forall_non_bool_returning_fn.wren | 1 + test/list/forall_non_function_arg.wren | 1 + 5 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 test/list/forall_non_bool_returning_fn.wren create mode 100644 test/list/forall_non_function_arg.wren diff --git a/builtin/core.wren b/builtin/core.wren index ef51470d..3fe304dc 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -17,7 +17,7 @@ class Sequence { forall(f) { for (element in this) { - if (f.call(element) != true) return false + if (!f.call(element)) return false } return true } diff --git a/src/wren_core.c b/src/wren_core.c index 822ee948..d5faecd1 100644 --- a/src/wren_core.c +++ b/src/wren_core.c @@ -60,7 +60,7 @@ static const char* libSource = "\n" " forall(f) {\n" " for (element in this) {\n" -" if (f.call(element) != true) return false\n" +" if (!f.call(element)) return false\n" " }\n" " return true\n" " }\n" diff --git a/test/list/forall.wren b/test/list/forall.wren index 3dcce8ed..55e3d29b 100644 --- a/test/list/forall.wren +++ b/test/list/forall.wren @@ -2,8 +2,8 @@ var a = [1, 2, 3] var b = a.forall {|x| x > 1 } IO.print(b) // expect: false -var c = a.forall {|x| x.toString } -IO.print(c) // expect: false - var d = a.forall {|x| x > 0 } IO.print(d) // expect: true + +var e = [].forall {|x| false } +IO.print(e) // expect: true diff --git a/test/list/forall_non_bool_returning_fn.wren b/test/list/forall_non_bool_returning_fn.wren new file mode 100644 index 00000000..a0cfaf94 --- /dev/null +++ b/test/list/forall_non_bool_returning_fn.wren @@ -0,0 +1 @@ +[1, 2, 3].forall {|x| "string" } // expect runtime error: String does not implement method '!' with 0 arguments. diff --git a/test/list/forall_non_function_arg.wren b/test/list/forall_non_function_arg.wren new file mode 100644 index 00000000..6c48c77a --- /dev/null +++ b/test/list/forall_non_function_arg.wren @@ -0,0 +1 @@ +[1, 2, 3].forall("string") // expect runtime error: String does not implement method 'call' with 1 argument. \ No newline at end of file