diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-08-19 13:54:31 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 49cfe858055fbb6bf5590c83eab1ffdcbeea9974 (patch) | |
| tree | 5d4e90da910adea3862b1932926ffe9ef9c158e0 /spec | |
| parent | add TestPilot:eval_once (diff) | |
| download | alive-49cfe858055fbb6bf5590c83eab1ffdcbeea9974.tar.gz alive-49cfe858055fbb6bf5590c83eab1ffdcbeea9974.zip | |
add (failing) array spec
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/lang/array_spec.moon | 128 | ||||
| -rw-r--r-- | spec/lang/literal_spec.moon (renamed from spec/lang/literals_spec.moon) | 0 |
2 files changed, 128 insertions, 0 deletions
diff --git a/spec/lang/array_spec.moon b/spec/lang/array_spec.moon new file mode 100644 index 0000000..97632e3 --- /dev/null +++ b/spec/lang/array_spec.moon @@ -0,0 +1,128 @@ +import TestPilot from require 'spec.test_setup' +import T, Array, Constant from require 'alv' + +describe "array", -> + test = TestPilot '' + + 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 + + it "length can be read using (len)", -> + rt = COPILOT\eval_once '(len (array 1))' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 1), rt.result + + rt = COPILOT\eval_once '(len (array 1 2 3))' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 3), rt.result + + 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 "argument error: TBD", 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 peek a value", -> + rt = COPILOT\eval_once '(get (array 1 2))' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 2), rt.result + + it "can get a value", -> + rt = COPILOT\eval_once '(get (array 1 2) 0)' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 1), rt.result + + it "checks index range", -> + err = assert.has.error -> COPILOT\eval_once '(get (array 1 2) -1 0)' + 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 "(insert)", -> + it "can append a value", -> + rt = COPILOT\eval_once '(insert (array "a" "b") "c")' + assert.is.true rt\is_const! + assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result + + it "can insert a value", -> + rt = COPILOT\eval_once '(set (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 '(set (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 '(set (array "a" "b") 1 "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 pop a value", -> + rt = COPILOT\eval_once '(remove (array "a" "b" "c" "d"))' + assert.is.true rt\is_const! + assert.is.equal svec3\mk_const({ 'a', 'b', 'c' }), rt.result + + 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 diff --git a/spec/lang/literals_spec.moon b/spec/lang/literal_spec.moon index 3db010d..3db010d 100644 --- a/spec/lang/literals_spec.moon +++ b/spec/lang/literal_spec.moon |
