aboutsummaryrefslogtreecommitdiffstats
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
parentthrow TestPilot errors instead of logging (diff)
downloadalive-7824aa34a5116696ba7325176b21e05514b099e0.tar.gz
alive-7824aa34a5116696ba7325176b21e05514b099e0.zip
spec/lang/fn
-rw-r--r--spec/lang/fn_spec.moon57
-rw-r--r--spec/lang/literals_spec.moon10
2 files changed, 62 insertions, 5 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
diff --git a/spec/lang/literals_spec.moon b/spec/lang/literals_spec.moon
index b0c869d..5bb0783 100644
--- a/spec/lang/literals_spec.moon
+++ b/spec/lang/literals_spec.moon
@@ -13,21 +13,21 @@ describe "literal", ->
assert.is.true test.active_module.root\is_const!
scope = (assert test.active_module.root.result)\unwrap T.scope
- it 'string', ->
+ it 'string is parsed and returned correctly', ->
assert.is.equal (Constant.str 'hello'), (scope\get 'str')\const!
- it 'number', ->
+ it 'number is parsed and returned correctly', ->
assert.is.equal (Constant.num 2), (scope\get 'num')\const!
- it 'boolean', ->
+ it 'boolean is parsed and returned correctly', ->
assert.is.equal (Constant.bool true), (scope\get 'bool')\const!
- it 'structs', ->
+ 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 'arrays', ->
+ 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!