aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-11-03 16:35:31 +0000
committers-ol <s-ol@users.noreply.github.com>2019-11-03 16:35:31 +0000
commit72e7dee17015295cc691fade5b3edefd9c5bf059 (patch)
tree27778e3c222adaa35a74e0fd87f1898ada28bbae
parentupdate examples, fixes (diff)
downloadwatch-cad-72e7dee17015295cc691fade5b3edefd9c5bf059.tar.gz
watch-cad-72e7dee17015295cc691fade5b3edefd9c5bf059.zip
test is_once and is_live
-rw-r--r--spec/state_spec.moon46
-rw-r--r--state.moon26
2 files changed, 52 insertions, 20 deletions
diff --git a/spec/state_spec.moon b/spec/state_spec.moon
index 5826067..e63a595 100644
--- a/spec/state_spec.moon
+++ b/spec/state_spec.moon
@@ -1,11 +1,11 @@
-import Once, State from require 'state'
+import is_once, is_live, Once, Cursor, State from require 'state'
describe "Once", ->
it "stores a value", ->
a = Once 3
assert.is.equal 3, a.value
- it "can test for itself", ->
+ it "can identify instances", ->
assert.is_true Once.is_once Once 3
assert.is_true Once.is_once Once 2
assert.is_true Once.is_once Once nil
@@ -17,6 +17,40 @@ describe "Once", ->
assert.is_falsy Once.is_once "once impostor"
assert.is_falsy Once.is_once { a: 3 }
+describe "is_once", ->
+ it "returns false for live or nil values", ->
+ assert.is_false is_once "string"
+ assert.is_false is_once {}
+ assert.is_false is_once 3
+ assert.is_false is_once nil
+ assert.is_false is_once false
+
+ it "unwraps Once values", ->
+ tbl = {}
+ assert.is_equal 3, is_once Once 3
+ assert.is_equal tbl, is_once Once tbl
+ assert.is_equal "str", is_once Once "str"
+ assert.is_equal false, is_once Once false
+ assert.is_equal nil, is_once Once nil
+
+describe "is_live", ->
+ it "returns false for Once values", ->
+ assert.is_false is_live Once "string"
+ assert.is_false is_live Once {}
+ assert.is_false is_live Once 3
+ assert.is_false is_live Once nil
+ assert.is_false is_live Once false
+
+ it "passes live values through", ->
+ tbl = {}
+ assert.is_equal 3, is_live 3
+ assert.is_equal tbl, is_live tbl
+ assert.is_equal "str", is_live "str"
+ assert.is_equal "str", is_live "str"
+ assert.is_equal false, is_live false
+ assert.is_equal nil, is_live nil
+
+
describe "State", ->
local state
before_each ->
@@ -115,3 +149,11 @@ describe "State", ->
assert.is_equal 'initial', cursor\drive Once 'other'
assert.is_equal 'live in', cursor\drive 'live in'
+ it "can identify instances", ->
+ assert.is_true Cursor.is_cursor state.root
+ assert.is_true Cursor.is_cursor state.root.deep.nested
+
+ assert.is_false Cursor.is_cursor state
+ assert.is_false Cursor.is_cursor nil
+ assert.is_false Cursor.is_cursor 2
+ assert.is_false Cursor.is_cursor "test"
diff --git a/state.moon b/state.moon
index 5a3170c..7630fb1 100644
--- a/state.moon
+++ b/state.moon
@@ -4,23 +4,12 @@ class Once
new: (@value) =>
@is_once: (val) ->
- return unless val
- return unless 'table' == type val
+ return false unless val
+ return false unless 'table' == type val
val.__class == @@
-all = (f) -> (...) ->
- values = for i = 1, select '#', ...
- val = f select i, ...
- return unless val
- val
-
- unpack values
-
is_once = (val) -> (Once.is_once val) and val.value
-is_live = (val) -> (not is_once val) and val
-
-are_once = all is_once
-are_live = all is_live
+is_live = (val) -> (not Once.is_once val) and val
class Cursor
@__base.__index = do
@@ -65,8 +54,8 @@ class Cursor
__eq: (other) => @path == other.path
@is_cursor: (val) ->
- return unless val
- return unless 'table' == type val
+ return false unless val
+ return false unless 'table' == type val
val.__class == @@
class State
@@ -102,8 +91,9 @@ class State
@root = Cursor @
{
- :is_once, :are_once
- :is_live, :are_live
+ :is_once
+ :is_live
:Once
+ :Cursor
:State
}