diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-08 09:41:08 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:23:21 +0000 |
| commit | 99ca67938c0145db37182bdb709d9424a20dfee0 (patch) | |
| tree | 6df4e2b5fde650aa3c7e36893eb1780f932d56bd /spec/result | |
| parent | release v0.1 (diff) | |
| download | alive-99ca67938c0145db37182bdb709d9424a20dfee0.tar.gz alive-99ca67938c0145db37182bdb709d9424a20dfee0.zip | |
wip new type system and refactoring
- Result -> RTNode
- Stream -> Result
- ValueStream -> SigStream
- EventStream -> EvtStream
Diffstat (limited to 'spec/result')
| -rw-r--r-- | spec/result/const_spec.moon | 149 | ||||
| -rw-r--r-- | spec/result/sig_spec.moon | 109 |
2 files changed, 258 insertions, 0 deletions
diff --git a/spec/result/const_spec.moon b/spec/result/const_spec.moon new file mode 100644 index 0000000..6bd7999 --- /dev/null +++ b/spec/result/const_spec.moon @@ -0,0 +1,149 @@ +import do_setup from require 'spec.test_setup' +import Constant, RTNode, Scope, SimpleRegistry from require 'alv' +import Op, Builtin from require 'alv.base' +import Primitive from require 'alv.types' + +class TestOp extends Op + new: (...) => super ... + +class TestBuiltin extends Builtin + new: (...) => + +setup do_setup + +describe 'Constant', -> + describe '.wrap', -> + it 'wraps numbers', -> + got = Constant.wrap 3 + assert.is.equal (Primitive 'num'), got.type + assert.is.equal 3, got.value + + it 'wraps strings', -> + got = Constant.wrap "im a happy string" + assert.is.equal (Primitive 'str'), got.type + assert.is.equal "im a happy string", got.value + + it 'wraps Constants', -> + pi = Constant.num 3.14 + got = Constant.wrap pi + + assert.is.equal pi, got + + it 'wraps Opdefs', -> + got = Constant.wrap TestOp + + assert.is.equal (Primitive 'opdef'), got.type + assert.is.equal TestOp, got.value + + it 'wraps Bultins', -> + got = Constant.wrap TestBuiltin + + assert.is.equal (Primitive 'builtin'), got.type + assert.is.equal TestBuiltin, got.value + + it 'wraps Scopes', -> + sub = Scope! + got = Constant.wrap sub + + assert.is.equal (Primitive 'scope'), got.type + assert.is.equal sub, got.value + + it 'wraps tables', -> + pi = Constant.num 3.14 + got = Constant.wrap { :pi } + + assert.is.equal (Primitive 'scope'), got.type + assert.is.equal pi, (got.value\get 'pi')\const! + + describe ':unwrap', -> + it 'returns the raw value!', -> + assert.is.equal 3.14, (Constant.num 3.14)\unwrap! + assert.is.equal 'hi', (Constant.str 'hi')\unwrap! + assert.is.equal 'hi', (Constant.sym 'hi')\unwrap! + + test 'can assert the type', -> + assert.is.equal 3.14, (Constant.num 3.14)\unwrap Primitive 'num' + assert.is.equal 'hi', (Constant.str 'hi')\unwrap Primitive 'str' + assert.is.equal 'hi', (Constant.sym 'hi')\unwrap Primitive 'sym' + assert.has_error -> (Constant.num 3.14)\unwrap Primitive 'sym' + assert.has_error -> (Constant.str 'hi')\unwrap Primitive 'num' + assert.has_error -> (Constant.sym 'hi')\unwrap Primitive 'str' + + test 'has __call shorthand', -> + assert.is.equal 3.14, (Constant.num 3.14)! + assert.is.equal 'hi', (Constant.str 'hi')! + assert.is.equal 'hi', (Constant.sym 'hi')! + assert.is.equal 3.14, (Constant.num 3.14) Primitive 'num' + assert.is.equal 'hi', (Constant.str 'hi') Primitive 'str' + assert.is.equal 'hi', (Constant.sym 'hi') Primitive 'sym' + assert.has_error -> (Constant.num 3.14) Primitive 'sym' + assert.has_error -> (Constant.str 'hi') Primitive 'num' + assert.has_error -> (Constant.sym 'hi') Primitive 'str' + + describe 'overrides __eq', -> + it 'compares the type', -> + val = Constant.num 3 + assert.is.equal (Constant.num 3), val + assert.not.equal (Constant.str '3'), val + + val = Constant.str 'hello' + assert.is.equal (Constant.str 'hello'), val + assert.not.equal (Constant.sym 'hello'), val + + it 'compares the value', -> + val = Constant.num 3 + assert.is.equal (Constant.num 3), val + assert.not.equal (Constant.num 4), val + + it ':dirty is always false', -> + val = Constant.num 3 + assert.is.false val\dirty! + + val.value = 4 + assert.is.false val\dirty! + + describe ':eval', -> + it 'turns numbers into consts', -> + assert_noop = (val) -> + assert.is.equal val, val\eval!\const! + + assert_noop Constant.num 2 + assert_noop Constant.str 'hello' + + it 'looks up symbols in the scope', -> + scope = with Scope! + \set 'number', RTNode value: Constant.num 3 + \set 'hello', RTNode value: Constant.str "world" + \set 'goodbye', RTNode value: Constant.sym "again" + + assert_eval = (sym, val) -> + const = Constant.sym sym + assert.is.equal val, (const\eval scope)\const! + + assert_eval 'number', Constant.num 3 + assert_eval 'hello', Constant.str "world" + assert_eval 'goodbye', Constant.sym "again" + + it ':clones literals as themselves', -> + assert_noop = (val) -> assert.is.equal val, val\clone! + + assert_noop Constant.num 2 + assert_noop Constant.str 'hello' + assert_noop Constant.sym 'world' + + describe ':fork', -> + it 'is equal to the original', -> + a = Constant.num 2 + b = Constant.str 'asdf' + c = Constant (Primitive 'weird'), {}, '(raw)' + + aa, bb, cc = a\fork!, b\fork!, c\fork! + assert.is.equal a, aa + assert.is.equal b, bb + assert.is.equal c, cc + + assert.is.false aa\dirty! + assert.is.false bb\dirty! + assert.is.false cc\dirty! + + assert.is.equal c.raw, cc.raw diff --git a/spec/result/sig_spec.moon b/spec/result/sig_spec.moon new file mode 100644 index 0000000..4bc0a67 --- /dev/null +++ b/spec/result/sig_spec.moon @@ -0,0 +1,109 @@ +import do_setup from require 'spec.test_setup' +import SigStream, RTNode, Scope, SimpleRegistry from require 'alv' +import Op, Builtin from require 'alv.base' +import Primitive from require 'alv.types' + +class TestOp extends Op + new: (...) => super ... + +class TestBuiltin extends Builtin + new: (...) => + +setup do_setup + +describe 'SigStream', -> + describe ':unwrap', -> + it 'returns the raw value!', -> + assert.is.equal 3.14, (SigStream.num 3.14)\unwrap! + assert.is.equal 'hi', (SigStream.str 'hi')\unwrap! + assert.is.equal 'hi', (SigStream.sym 'hi')\unwrap! + + test 'can assert the type', -> + assert.is.equal 3.14, (SigStream.num 3.14)\unwrap Primitive 'num' + assert.is.equal 'hi', (SigStream.str 'hi')\unwrap Primitive 'str' + assert.is.equal 'hi', (SigStream.sym 'hi')\unwrap Primitive 'sym' + assert.has_error -> (SigStream.num 3.14)\unwrap Primitive 'sym' + assert.has_error -> (SigStream.str 'hi')\unwrap Primitive 'num' + assert.has_error -> (SigStream.sym 'hi')\unwrap Primitive 'str' + + test 'has __call shorthand', -> + assert.is.equal 3.14, (SigStream.num 3.14)! + assert.is.equal 'hi', (SigStream.str 'hi')! + assert.is.equal 'hi', (SigStream.sym 'hi')! + assert.is.equal 3.14, (SigStream.num 3.14) Primitive 'num' + assert.is.equal 'hi', (SigStream.str 'hi') Primitive 'str' + assert.is.equal 'hi', (SigStream.sym 'hi') Primitive 'sym' + assert.has_error -> (SigStream.num 3.14) Primitive 'sym' + assert.has_error -> (SigStream.str 'hi') Primitive 'num' + assert.has_error -> (SigStream.sym 'hi') Primitive 'str' + + describe 'overrides __eq', -> + it 'compares the type', -> + val = SigStream.num 3 + assert.is.equal (SigStream.num 3), val + assert.not.equal (SigStream.str '3'), val + + val = SigStream.str 'hello' + assert.is.equal (SigStream.str 'hello'), val + assert.not.equal (SigStream.sym 'hello'), val + + it 'compares the value', -> + val = SigStream.num 3 + assert.is.equal (SigStream.num 3), val + assert.not.equal (SigStream.num 4), val + + describe ':set', -> + it 'sets the value', -> + val = SigStream.num 3 + assert.is.equal (SigStream.num 3), val + + val\set 4 + assert.is.equal (SigStream.num 4), val + assert.not.equal (SigStream.num 3), val + + it 'marks the value dirty', -> + val = SigStream.num 3 + assert.is.false val\dirty! + + val\set 4 + assert.is.true val\dirty! + + describe ':fork', -> + it 'is equal to the original', -> + a = SigStream.num 2 + b = SigStream.str 'asdf' + c = with SigStream 'weird', {}, '(raw)' + \set {} + + aa, bb, cc = a\fork!, b\fork!, c\fork! + assert.is.equal a, aa + assert.is.equal b, bb + assert.is.equal c, cc + + assert.is.false aa\dirty! + assert.is.false bb\dirty! + assert.is.true cc\dirty! + + assert.is.equal c.raw, cc.raw + + it 'isolates the original from the fork', -> + a = SigStream.num 3 + b = with SigStream 'weird', {}, '(raw)' + \set {} + + aa, bb = a\fork!, b\fork! + + bb\set {false} + + assert.is.same {}, b! + assert.is.same {false}, bb! + assert.is.true b\dirty! + assert.is.true bb\dirty! + + COPILOT\next_tick! + aa\set 4 + + assert.is.equal 3, a! + assert.is.equal 4, aa! + assert.is.false a\dirty! + assert.is.true aa\dirty! |
