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