git.s-ol.nu watch-cad / 72e7dee
test is_once and is_live s-ol 1 year, 10 months ago
2 changed file(s) with 52 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
0 import Once, State from require 'state'
0 import is_once, is_live, Once, Cursor, State from require 'state'
11
22 describe "Once", ->
33 it "stores a value", ->
44 a = Once 3
55 assert.is.equal 3, a.value
66
7 it "can test for itself", ->
7 it "can identify instances", ->
88 assert.is_true Once.is_once Once 3
99 assert.is_true Once.is_once Once 2
1010 assert.is_true Once.is_once Once nil
1515 assert.is_falsy Once.is_once 3
1616 assert.is_falsy Once.is_once "once impostor"
1717 assert.is_falsy Once.is_once { a: 3 }
18
19 describe "is_once", ->
20 it "returns false for live or nil values", ->
21 assert.is_false is_once "string"
22 assert.is_false is_once {}
23 assert.is_false is_once 3
24 assert.is_false is_once nil
25 assert.is_false is_once false
26
27 it "unwraps Once values", ->
28 tbl = {}
29 assert.is_equal 3, is_once Once 3
30 assert.is_equal tbl, is_once Once tbl
31 assert.is_equal "str", is_once Once "str"
32 assert.is_equal false, is_once Once false
33 assert.is_equal nil, is_once Once nil
34
35 describe "is_live", ->
36 it "returns false for Once values", ->
37 assert.is_false is_live Once "string"
38 assert.is_false is_live Once {}
39 assert.is_false is_live Once 3
40 assert.is_false is_live Once nil
41 assert.is_false is_live Once false
42
43 it "passes live values through", ->
44 tbl = {}
45 assert.is_equal 3, is_live 3
46 assert.is_equal tbl, is_live tbl
47 assert.is_equal "str", is_live "str"
48 assert.is_equal "str", is_live "str"
49 assert.is_equal false, is_live false
50 assert.is_equal nil, is_live nil
51
1852
1953 describe "State", ->
2054 local state
114148 assert.is_equal 'initial', cursor\drive Once 'other'
115149 assert.is_equal 'live in', cursor\drive 'live in'
116150
151 it "can identify instances", ->
152 assert.is_true Cursor.is_cursor state.root
153 assert.is_true Cursor.is_cursor state.root.deep.nested
154
155 assert.is_false Cursor.is_cursor state
156 assert.is_false Cursor.is_cursor nil
157 assert.is_false Cursor.is_cursor 2
158 assert.is_false Cursor.is_cursor "test"
33 new: (@value) =>
44
55 @is_once: (val) ->
6 return unless val
7 return unless 'table' == type val
6 return false unless val
7 return false unless 'table' == type val
88 val.__class == @@
99
10 all = (f) -> (...) ->
11 values = for i = 1, select '#', ...
12 val = f select i, ...
13 return unless val
14 val
15
16 unpack values
17
1810 is_once = (val) -> (Once.is_once val) and val.value
19 is_live = (val) -> (not is_once val) and val
20
21 are_once = all is_once
22 are_live = all is_live
11 is_live = (val) -> (not Once.is_once val) and val
2312
2413 class Cursor
2514 @__base.__index = do
6453 __eq: (other) => @path == other.path
6554
6655 @is_cursor: (val) ->
67 return unless val
68 return unless 'table' == type val
56 return false unless val
57 return false unless 'table' == type val
6958 val.__class == @@
7059
7160 class State
10190 @root = Cursor @
10291
10392 {
104 :is_once, :are_once
105 :is_live, :are_live
93 :is_once
94 :is_live
10695 :Once
96 :Cursor
10797 :State
10898 }