aboutsummaryrefslogtreecommitdiffstats
path: root/spec/internal/result/sig_spec.moon
blob: 2d3304c2fb8b4a93728aa1a74361be25386f98f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import do_setup from require 'spec.test_setup'
import SigStream, Constant, T, Array from require 'alv'
import Op, Builtin from require 'alv.base'

setup do_setup

describe 'SigStream', ->
  it 'stringifies well', ->
    assert.is.equal "<num~ 4>", tostring SigStream T.num, 4
    assert.is.equal "<bool~ true>", tostring SigStream T.bool, true
    assert.is.equal "<bool~ false>", tostring SigStream T.bool, false

  describe ':unwrap', ->
    it 'returns the raw value!', ->
      assert.is.equal 3.14, (SigStream T.num, 3.14)\unwrap!
      assert.is.equal 'hi', (SigStream T.str, 'hi')\unwrap!
      assert.is.equal 'hi', (SigStream T.sym, 'hi')\unwrap!

    test 'can assert the type', ->
      assert.is.equal 3.14, (SigStream T.num, 3.14)\unwrap T.num
      assert.is.equal 'hi', (SigStream T.str, 'hi')\unwrap T.str
      assert.is.equal 'hi', (SigStream T.sym, 'hi')\unwrap T.sym
      assert.has_error -> (SigStream T.num, 3.14)\unwrap T.sym
      assert.has_error -> (SigStream T.str, 'hi')\unwrap T.num
      assert.has_error -> (SigStream T.sym, 'hi')\unwrap T.str

    test 'has __call shorthand', ->
      assert.is.equal 3.14, (SigStream T.num, 3.14)!
      assert.is.equal 'hi', (SigStream T.str, 'hi')!
      assert.is.equal 'hi', (SigStream T.sym, 'hi')!
      assert.is.equal 3.14, (SigStream T.num, 3.14) T.num
      assert.is.equal 'hi', (SigStream T.str, 'hi') T.str
      assert.is.equal 'hi', (SigStream T.sym, 'hi') T.sym
      assert.has_error -> (SigStream T.num, 3.14) T.sym
      assert.has_error -> (SigStream T.str, 'hi') T.num
      assert.has_error -> (SigStream T.sym, 'hi') T.str

  describe 'overrides __eq', ->
    it 'compares the type', ->
      val = SigStream T.num, 3
      assert.is.equal (SigStream T.num, 3), val
      assert.not.equal (SigStream T.str, '3'), val

      val = SigStream T.str, 'hello'
      assert.is.equal (SigStream T.str, 'hello'), val
      assert.not.equal (SigStream T.sym, 'hello'), val

    it 'compares the value', ->
      val = SigStream T.num, 3
      assert.is.equal (SigStream T.num, 3), val
      assert.not.equal (SigStream T.num, 4), val

    it 'can be compared to a Constant', ->
      val = SigStream T.num, 3
      assert.is.equal (Constant.num 3), val
      assert.not.equal (Constant.num 4), val

      val = SigStream T.str, 'hello'
      assert.is.equal (Constant.str 'hello'), val
      assert.not.equal (Constant.sym 'hello'), val

    it 'compares complex values', ->
      ta = Array 3, T.num
      tb = Array 3, T.num

      assert.is.equal (Constant ta, {1, 2, 3}), (SigStream tb, {1, 2, 3})
      assert.not.equal (Constant ta, {1, 2, 3}), (SigStream tb, {1, 2, 4})

  describe ':set', ->
    it 'sets the value', ->
      val = SigStream T.num, 3
      assert.is.equal (SigStream T.num, 3), val

      val\set 4
      assert.is.equal (SigStream T.num, 4), val
      assert.not.equal (SigStream T.num, 3), val

    it 'marks the value dirty', ->
      val = SigStream T.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 T.num, 2
      b = SigStream T.str, 'asdf'
      c = with SigStream T.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 T.num, 3
      b = with SigStream T.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!