aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/array_spec.moon
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-02-05 10:11:13 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commitc484cce5ab1f6acd14ecad30adb586a6d5fa0fd1 (patch)
treea45c702a83e150fea5b14b279e5f0cab9b65bc3e /spec/lib/array_spec.moon
parentexamples: more smoothing in love example :) (diff)
downloadalive-c484cce5ab1f6acd14ecad30adb586a6d5fa0fd1.tar.gz
alive-c484cce5ab1f6acd14ecad30adb586a6d5fa0fd1.zip
rearrange spec, fix for Lua 5.1
Diffstat (limited to 'spec/lib/array_spec.moon')
-rw-r--r--spec/lib/array_spec.moon148
1 files changed, 148 insertions, 0 deletions
diff --git a/spec/lib/array_spec.moon b/spec/lib/array_spec.moon
new file mode 100644
index 0000000..b55cec6
--- /dev/null
+++ b/spec/lib/array_spec.moon
@@ -0,0 +1,148 @@
+import TestPilot from require 'spec.test_setup'
+import T, Array from require 'alv'
+
+describe "array", ->
+ test = TestPilot '', '(import* array-)\n'
+
+ svec3 = Array 3, T.str
+
+ it "can contain any type", ->
+ COPILOT\eval_once '(array 1 2 3)'
+ COPILOT\eval_once '(array true false)'
+ COPILOT\eval_once '(array "a")'
+ COPILOT\eval_once '(array (array 1 2) (array 3 4))'
+
+ it "cannot contain mixed types", ->
+ err = assert.has.error -> COPILOT\eval_once '(array 1 false)'
+ assert.matches "argument error: couldn't match arguments", err
+
+ describe "(set)", ->
+ it "can swap values", ->
+ rt = COPILOT\eval_once '(set (array "f" "b" "c") 0 "a")'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ it "checks value type", ->
+ err = assert.has.error -> COPILOT\eval_once '(set (array 1) 0 "a")'
+ assert.matches "expected value of type num, not str", err
+
+ it "checks index range", ->
+ err = assert.has.error -> COPILOT\eval_once '(set (array 1 2) -1 0)'
+ assert.matches "index '%-1' out of range!", err
+
+ COPILOT\eval_once '(set (array 1 2) 0 0)'
+
+ COPILOT\eval_once '(set (array 1 2) 1 0)'
+
+ err = assert.has.error -> COPILOT\eval_once '(set (array 1 2) 2 0)'
+ assert.matches "index '2' out of range!", err
+
+ describe "(get)", ->
+ it "can get a value", ->
+ rt = COPILOT\eval_once '(get (array 1 2) 0)'
+ assert.is.true rt\is_const!
+ assert.is.equal '<num= 1>', tostring rt.result
+
+ it "checks index range", ->
+ err = assert.has.error -> COPILOT\eval_once '(get (array 1 2) -1)'
+ assert.matches "index '%-1' out of range!", err
+
+ COPILOT\eval_once '(get (array 1 2) 0)'
+
+ COPILOT\eval_once '(get (array 1 2) 1)'
+
+ err = assert.has.error -> COPILOT\eval_once '(get (array 1 2) 2)'
+ assert.matches "index '2' out of range!", err
+
+ describe '(head)', ->
+ it "can peek a value", ->
+ rt = COPILOT\eval_once '(head (array 1 2))'
+ assert.is.true rt\is_const!
+ assert.is.equal '<num= 1>', tostring rt.result
+
+ describe '(tail)', ->
+ it "gets rest of an array", ->
+ rt = COPILOT\eval_once '(tail (array 1))'
+ assert.is.true rt\is_const!
+ assert.is.same (Array 0, T.num), rt.result.type
+ assert.is.same {}, rt.result!
+
+ rt = COPILOT\eval_once '(tail (array 1 2))'
+ assert.is.true rt\is_const!
+ assert.is.same (Array 1, T.num), rt.result.type
+ assert.is.same { 2 }, rt.result!
+
+ rt = COPILOT\eval_once '(tail (array 1 2 3 4))'
+ assert.is.true rt\is_const!
+ assert.is.same (Array 3, T.num), rt.result.type
+ assert.is.same { 2, 3, 4 }, rt.result!
+
+ describe '(prepend)', ->
+ it "prepends to array", ->
+ rt = COPILOT\eval_once '(prepend (array 2) 1)'
+ assert.is.true rt\is_const!
+ assert.is.same (Array 2, T.num), rt.result.type
+ assert.is.same { 1, 2 }, rt.result!
+
+ rt = COPILOT\eval_once '(prepend (array 2 3 4) 1)'
+ assert.is.true rt\is_const!
+ assert.is.same (Array 4, T.num), rt.result.type
+ assert.is.same { 1, 2, 3, 4 }, rt.result!
+
+ describe "(insert)", ->
+ it "can insert a value", ->
+ rt = COPILOT\eval_once '(insert (array "b" "c") 0 "a")'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ rt = COPILOT\eval_once '(insert (array "a" "c") 1 "b")'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ rt = COPILOT\eval_once '(insert (array "a" "b") 2 "c")'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ it "checks index range", ->
+ err = assert.has.error -> COPILOT\eval_once '(insert (array 1 2) -1 0)'
+ assert.matches "index '%-1' out of range!", err
+
+ COPILOT\eval_once '(insert (array 1 2) 0 0)'
+
+ COPILOT\eval_once '(insert (array 1 2) 1 0)'
+
+ COPILOT\eval_once '(insert (array 1 2) 2 0)'
+
+ err = assert.has.error -> COPILOT\eval_once '(insert (array 1 2) 3 0)'
+ assert.matches "index '3' out of range!", err
+
+ describe "(remove)", ->
+ it "can remove a value", ->
+ rt = COPILOT\eval_once '(remove (array "d" "a" "b" "c") 0)'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ rt = COPILOT\eval_once '(remove (array "a" "b" "c" "d") 3)'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ it "checks index range", ->
+ err = assert.has.error -> COPILOT\eval_once '(remove (array 1 2 3) -1)'
+ assert.matches "index '%-1' out of range!", err
+
+ err = assert.has.error -> COPILOT\eval_once '(remove (array 1 2 3) 3)'
+ assert.matches "index '3' out of range!", err
+
+ it "can be concatenated with (concat)", ->
+ rt = COPILOT\eval_once '(concat (array "a" "b") (array "c"))'
+ assert.is.true rt\is_const!
+ assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result
+
+ it "size can be read using (size)", ->
+ rt = COPILOT\eval_once '(size (array 1))'
+ assert.is.true rt\is_const!
+ assert.is.equal '<num= 1>', tostring rt.result
+
+ rt = COPILOT\eval_once '(size (array 1 2 3))'
+ assert.is.true rt\is_const!
+ assert.is.equal '<num= 3>', tostring rt.result