aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-01-27 15:35:35 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit5886aea80a41f20447e3a5d63d1f825a67d26ff8 (patch)
tree6d66937a6c9bfcf1ebd22c9ba60993a83c9e2cd7
parentclean up spec a bit (diff)
downloadalive-5886aea80a41f20447e3a5d63d1f825a67d26ff8.tar.gz
alive-5886aea80a41f20447e3a5d63d1f825a67d26ff8.zip
test if and when builtins
-rw-r--r--spec/lang/cond_spec.moon92
1 files changed, 92 insertions, 0 deletions
diff --git a/spec/lang/cond_spec.moon b/spec/lang/cond_spec.moon
new file mode 100644
index 0000000..89f7c7e
--- /dev/null
+++ b/spec/lang/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
+