diff options
| author | s-ol <s+removethis@s-ol.nu> | 2022-02-05 10:11:13 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | c484cce5ab1f6acd14ecad30adb586a6d5fa0fd1 (patch) | |
| tree | a45c702a83e150fea5b14b279e5f0cab9b65bc3e /spec/lib/builtins | |
| parent | examples: more smoothing in love example :) (diff) | |
| download | alive-c484cce5ab1f6acd14ecad30adb586a6d5fa0fd1.tar.gz alive-c484cce5ab1f6acd14ecad30adb586a6d5fa0fd1.zip | |
rearrange spec, fix for Lua 5.1
Diffstat (limited to 'spec/lib/builtins')
| -rw-r--r-- | spec/lib/builtins/cond_spec.moon | 92 | ||||
| -rw-r--r-- | spec/lib/builtins/do_spec.moon | 27 | ||||
| -rw-r--r-- | spec/lib/builtins/fn_spec.moon | 51 | ||||
| -rw-r--r-- | spec/lib/builtins/literal_spec.moon | 33 | ||||
| -rw-r--r-- | spec/lib/builtins/thread_spec.moon | 24 |
5 files changed, 227 insertions, 0 deletions
diff --git a/spec/lib/builtins/cond_spec.moon b/spec/lib/builtins/cond_spec.moon new file mode 100644 index 0000000..89f7c7e --- /dev/null +++ b/spec/lib/builtins/cond_spec.moon @@ -0,0 +1,92 @@ +import TestPilot from require 'spec.test_setup' +import T, Struct, Array, Constant from require 'alv' + +describe "if", -> + COPILOT = TestPilot! + + it "checks truthiness", -> + for truthy in *{'true', '1', '-1', '1234', '(array 1 2 3)', '"test"', '""'} + with COPILOT\eval_once "(if #{truthy} 'yes' 'no')" + assert.is.true \is_const! + assert.is.equal 'yes', .result! + + for falsy in *{'false', '0'} + with COPILOT\eval_once "(if #{falsy} 'yes' 'no')" + assert.is.true \is_const! + assert.is.equal 'no', .result! + + it "can be used without else clause", -> + with COPILOT\eval_once '(if true "yes")' + assert.is.true \is_const! + assert.is.equal 'yes', .result! + + with COPILOT\eval_once '(if false "yes")' + assert.is.true \is_const! + assert.is.equal nil, .result + + it "doesn't evaluate the untaken branch", -> + with COPILOT\eval_once '(if true "yes" (error/doesnt-exist))' + assert.is.true \is_const! + assert.is.equal 'yes', .result! + + with COPILOT\eval_once '(if false (error/doesnt-exist) "no")' + assert.is.true \is_const! + assert.is.equal 'no', .result! + + it "errors on non-const choice", -> + err = assert.has.error -> + COPILOT\eval_once '(import* time) (if (ramp 1) "yes" "no")' + assert.matches "'if'%-expression needs to be constant", err + + it "forwards any result", -> + with COPILOT\eval_once ' + (import* time) + (if true (every 1 (array 1 2 3)))' + assert.is.false \is_const! + assert.is.equal '<num[3]! nil>', tostring .result + +describe "when", -> + COPILOT = TestPilot! + + it "checks truthiness", -> + for truthy in *{'true', '1', '-1', '1234', '(array 1 2 3)', '"test"', '""'} + with COPILOT\eval_once "(when #{truthy} 'yes')" + assert.is.true \is_const! + assert.is.equal 'yes', .result! + + for falsy in *{'false', '0'} + with COPILOT\eval_once "(when #{falsy} 'yes')" + assert.is.true \is_const! + assert.is.nil .result + + it "doesn't evaluate if falsy", -> + with COPILOT\eval_once '(when false (error/doesnt-exist))' + assert.is.true \is_const! + assert.is.nil, .result + + with COPILOT\eval_once ' + (when false + (error/doesnt-exist) + (error/doesnt-exist) + (error/doesnt-exist))' + assert.is.true \is_const! + assert.is.nil, .result + + it "errors on non-const choice", -> + err = assert.has.error -> + COPILOT\eval_once '(import* time) (when (ramp 1) 1 2 3)' + assert.matches "'when'%-expression needs to be constant", err + + it "forwards any result", -> + with COPILOT\eval_once ' + (import* time) + (when true + (every 1 (array 1 2 3)) + 1 2 3)' + assert.is.false \is_const! + assert.is.equal '<num~ 3>', tostring .result + + with COPILOT\eval_once '(when true (array 1 2 3))' + assert.is.true \is_const! + assert.is.equal '<num[3]= [1 2 3]>', tostring .result + diff --git a/spec/lib/builtins/do_spec.moon b/spec/lib/builtins/do_spec.moon new file mode 100644 index 0000000..fd2825b --- /dev/null +++ b/spec/lib/builtins/do_spec.moon @@ -0,0 +1,27 @@ +import TestPilot from require 'spec.test_setup' + +describe "do", -> + COPILOT = TestPilot! + + it "can be empty", -> + with COPILOT\eval_once '(do)' + assert.is.true \is_const! + assert.is.nil .result + + it "returns the last result, if any", -> + with COPILOT\eval_once '(do 1 2 3)' + assert.is.true \is_const! + assert.is.equal '<num= 3>', tostring .result + + with COPILOT\eval_once '(do 1 2 (def _ 3))' + assert.is.true \is_const! + assert.is.nil .result + + it "passes through side-effects", -> + with COPILOT\eval_once ' + (import* time) + (do + (every 0.5 "bang! side-effect") + 3)' + assert.is.false \is_const! + assert.is.equal '<num= 3>', tostring .result diff --git a/spec/lib/builtins/fn_spec.moon b/spec/lib/builtins/fn_spec.moon new file mode 100644 index 0000000..af27cf3 --- /dev/null +++ b/spec/lib/builtins/fn_spec.moon @@ -0,0 +1,51 @@ +import TestPilot from require 'spec.test_setup' +import T, Struct, Array, Constant from require 'alv' + +describe "function", -> + COPILOT = TestPilot! + + it "returns constant results when constant", -> + rt = COPILOT\eval_once ' + (import* math) + + (defn my-plus (a b) + (+ a b)) + + (my-plus 2 3)' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 5), rt.result + + it "checks argument arity when invoked", -> + err = assert.has.error -> COPILOT\eval_once ' + ([1]import* math) + + ([2]defn my-plus (a b) + ([4]+ a b)) + + ([3]my-plus 2)' + assert.matches "argument error: expected 2 arguments, found 1", err + assert.matches "while invoking function 'my%-plus' at %[3%]", err + + err = assert.has.error -> COPILOT\eval_once ' + ([1]import* math) + + ([2]defn my-plus (a b) + ([4]+ a b)) + + ([3]my-plus 2 3 4)' + assert.matches "argument error: expected 2 arguments, found 3", err + assert.matches "while invoking function 'my%-plus' at %[3%]", err + + it "can be anonymously invoked", -> + rt = COPILOT\eval_once ' + ([1] + ([2]fn (a b) b) + 3 4)' + assert.is.equal (Constant.num 4), rt\const! + + err = assert.has.error -> COPILOT\eval_once ' + ([1] + ([2]fn (a b) b) + 3)' + assert.matches "argument error: expected 2 arguments, found 1", err + assert.matches "while invoking function %(unnamed%) at %[1%]", err diff --git a/spec/lib/builtins/literal_spec.moon b/spec/lib/builtins/literal_spec.moon new file mode 100644 index 0000000..3db010d --- /dev/null +++ b/spec/lib/builtins/literal_spec.moon @@ -0,0 +1,33 @@ +import TestPilot from require 'spec.test_setup' +import T, Struct, Array, Constant from require 'alv' + +describe "literal", -> + TestPilot ' + (def str "hello" + num 2 + bool true + curl ([5]struct "a" 2 "b" false) + sqre ([7]array 1 2 3 4)) + (export*)' + + assert.is.true COPILOT.active_module.root\is_const! + scope = (assert COPILOT.active_module.root.result)\unwrap T.scope + + it "string is parsed and returned correctly", -> + assert.is.equal (Constant.str 'hello'), (scope\get 'str')\const! + + it "number is parsed and returned correctly", -> + assert.is.equal (Constant.num 2), (scope\get 'num')\const! + + it "boolean is parsed and returned correctly", -> + assert.is.equal (Constant.bool true), (scope\get 'bool')\const! + + it "struct is parsed and returned correctly", -> + struct = (scope\get 'curl')\const! + assert.is.equal (Struct a: T.num, b: T.bool), struct.type + assert.is.same { a: 2, b: false }, struct! + + it "array is parsed and returned correctly", -> + array = (scope\get 'sqre')\const! + assert.is.equal (Array 4, T.num), array.type + assert.is.same {1, 2, 3, 4}, array! diff --git a/spec/lib/builtins/thread_spec.moon b/spec/lib/builtins/thread_spec.moon new file mode 100644 index 0000000..4b81119 --- /dev/null +++ b/spec/lib/builtins/thread_spec.moon @@ -0,0 +1,24 @@ +import TestPilot from require 'spec.test_setup' + +describe "thread macros", -> + COPILOT = TestPilot! + + it "thread forward (->)", -> + rt = COPILOT\eval_once ' + (import* math) + #((/ (+ 10 2) 8) = 1.5) + (-> 10 + (+ 2) + (/ 8))' + assert.is.true rt\is_const! + assert.is.equal '<num= 1.5>', tostring rt.result + + it "thread last forward (->>)", -> + rt = COPILOT\eval_once ' + (import* math) + #((/ 10 (+ 2 2)) = 2.5) + (->> 2 + (+ 2) + (/ 10))' + assert.is.true rt\is_const! + assert.is.equal '<num= 2.5>', tostring rt.result |
