aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lang/fn_spec.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-08-14 11:10:40 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit7824aa34a5116696ba7325176b21e05514b099e0 (patch)
treec4aec68ff501632907b8ed1903ca2589e7212085 /spec/lang/fn_spec.moon
parentthrow TestPilot errors instead of logging (diff)
downloadalive-7824aa34a5116696ba7325176b21e05514b099e0.tar.gz
alive-7824aa34a5116696ba7325176b21e05514b099e0.zip
spec/lang/fn
Diffstat (limited to 'spec/lang/fn_spec.moon')
-rw-r--r--spec/lang/fn_spec.moon57
1 files changed, 57 insertions, 0 deletions
diff --git a/spec/lang/fn_spec.moon b/spec/lang/fn_spec.moon
new file mode 100644
index 0000000..41a90d1
--- /dev/null
+++ b/spec/lang/fn_spec.moon
@@ -0,0 +1,57 @@
+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', ->
+ COPILOT.active_module\spit '
+ (import* math)
+
+ (defn my-plus (a b)
+ (+ a b))
+
+ (my-plus 2 3)'
+ COPILOT\tick!
+ assert.is.true COPILOT.active_module.root\is_const!
+ result = assert COPILOT.active_module.root.result
+ assert.is.equal (Constant.num 5), result
+
+ it 'checks argument arity when invoked', ->
+ COPILOT.active_module\spit '
+ ([1]import* math)
+
+ ([2]defn my-plus (a b)
+ ([4]+ a b))
+
+ ([3]my-plus 2)'
+ err = assert.has.error COPILOT\tick
+ assert.matches "argument error: expected 2 arguments, found 1", err
+ assert.matches "while invoking function 'my%-plus' at %[3%]", err
+
+ COPILOT.active_module\spit '
+ ([1]import* math)
+
+ ([2]defn my-plus (a b)
+ ([4]+ a b))
+
+ ([3]my-plus 2 3 4)'
+ err = assert.has.error COPILOT\tick
+ 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', ->
+ COPILOT.active_module\spit '
+ ([1]
+ ([2]fn (a b) b)
+ 3 4)'
+ COPILOT\tick!
+ assert.is.equal (Constant.num 4), COPILOT.active_module.root\const!
+
+ COPILOT.active_module\spit '
+ ([1]
+ ([2]fn (a b) b)
+ 3)'
+ err = assert.has.error COPILOT\tick
+ assert.matches "argument error: expected 2 arguments, found 1", err
+ assert.matches "while invoking function %(unnamed%) at %[1%]", err