feat(hsm): first version of hsm library functional
This commit is contained in:
12 files changed
+1371
-46
No files matched your search
@@ -1,19 +1,19 @@
|
||||
require "src.util.parse_transition"
|
||||
require "lib.util.parse_transition"
|
||||
|
||||
describe "parse_transition", ->
|
||||
|
||||
-- ### Valid Cases ###
|
||||
it "parses a valid 'on' transition", ->
|
||||
result = parse_transition "stateA on eventX"
|
||||
assert.same {target: "stateA", event: "eventX"}, result
|
||||
assert.same {target: "stateA", type: "on", event: "eventX"}, result
|
||||
|
||||
it "parses a valid 'after' transition", ->
|
||||
result = parse_transition "stateB after 3.14"
|
||||
assert.same {target: "stateB", after: 3.14}, result
|
||||
assert.same {target: "stateB", type: "after", duration: 3.14}, result
|
||||
|
||||
it "parses a valid 'every' transition", ->
|
||||
result = parse_transition "stateC every 42"
|
||||
assert.same {target: "stateC", every: 42}, result
|
||||
assert.same {target: "stateC", type: "every", duration: 42}, result
|
||||
|
||||
-- ### Basic Error Cases ###
|
||||
it "errors when input is not a string", ->
|
||||
@@ -25,29 +25,29 @@ describe "parse_transition", ->
|
||||
it "errors when transition type is unknown", ->
|
||||
assert.has_error (-> parse_transition "stateD unknown value"), "Unknown transition type"
|
||||
|
||||
it "errors when number conversion fails for 'after' transition", ->
|
||||
assert.has_error (-> parse_transition "stateE after not_a_number"), "Invalid time value in 'after' transition type"
|
||||
it "errors when number conversion fails for 'after/every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateE after not_a_number"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
it "errors when number conversion fails for 'every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateF every not_a_number"), "Invalid time value in 'every' transition type"
|
||||
it "errors when number conversion fails for 'after/every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateF every not_a_number"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
-- ### Additional Tests for Nil and Bad Numbers ###
|
||||
it "errors when nil is passed as transition", ->
|
||||
assert.has_error (-> parse_transition nil), "Transition must be a string"
|
||||
|
||||
it "errors for a negative number in an 'after' transition", ->
|
||||
it "errors for a negative number in an 'after/every' transition", ->
|
||||
-- Assuming negative times are invalid.
|
||||
assert.has_error (-> parse_transition "stateNeg after -5"), "Invalid time value in 'after' transition type"
|
||||
assert.has_error (-> parse_transition "stateNeg after -5"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
it "errors for a negative number in an 'every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateNeg every -5"), "Invalid time value in 'every' transition type"
|
||||
it "errors for a negative number in an 'after/every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateNeg every -5"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
it "errors for zero in an 'after' transition", ->
|
||||
it "errors for zero in an 'after/every' transition", ->
|
||||
-- Zero is considered invalid.
|
||||
assert.has_error (-> parse_transition "stateZero after 0"), "Invalid time value in 'after' transition type"
|
||||
assert.has_error (-> parse_transition "stateZero after 0"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
it "errors for zero in an 'every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateZero every 0"), "Invalid time value in 'every' transition type"
|
||||
it "errors for zero in an 'after/every' transition", ->
|
||||
assert.has_error (-> parse_transition "stateZero every 0"), "Invalid time value in 'after/every' transition type"
|
||||
|
||||
it "errors when the state name contains a space", ->
|
||||
-- e.g. "state A" is not a valid Lua identifier.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
require "src.util.string"
|
||||
require "lib.util.string"
|
||||
|
||||
describe "string.split", ->
|
||||
it "returns an empty table when given an empty string", ->
|
||||
|
||||
+14
-11
@@ -1,4 +1,4 @@
|
||||
require "src.util.validators"
|
||||
require "lib.util.validators"
|
||||
|
||||
describe "validators", ->
|
||||
describe "is_valid_timer", ->
|
||||
@@ -27,14 +27,17 @@ describe "validators", ->
|
||||
result = is_valid_timer 3.14
|
||||
assert.is_true result
|
||||
|
||||
it "errors when passed a non-number (string)", ->
|
||||
assert.has_error => is_valid_timer "10"
|
||||
it "returns false when passed a non-number (string)", ->
|
||||
result = is_valid_timer "10"
|
||||
assert.is_false result
|
||||
|
||||
it "errors when passed a non-number (boolean true)", ->
|
||||
assert.has_error => is_valid_timer true
|
||||
it "returns false when passed a non-number (boolean true)", ->
|
||||
result = is_valid_timer true
|
||||
assert.has_false result
|
||||
|
||||
it "errors when passed a non-number (table)", ->
|
||||
assert.has_error => is_valid_timer { foo: "bar" }
|
||||
it "returns false when passed a non-number (table)", ->
|
||||
result = is_valid_timer { foo: "bar" }
|
||||
assert.has_false result
|
||||
|
||||
describe "is_valid_symbol", ->
|
||||
-- When the input is not a string, it should immediately return false.
|
||||
@@ -75,17 +78,17 @@ describe "validators", ->
|
||||
result = is_valid_symbol "AbCdEf"
|
||||
assert.is_true result
|
||||
|
||||
it "returns false for a string with numbers", ->
|
||||
it "returns true for a string with numbers", ->
|
||||
result = is_valid_symbol "abc123"
|
||||
assert.is_false result
|
||||
assert.is_true result
|
||||
|
||||
it "returns false for a string with punctuation", ->
|
||||
result = is_valid_symbol "hello!"
|
||||
assert.is_false result
|
||||
|
||||
it "returns false for a string with an underscore", ->
|
||||
it "returns true for a string with an underscore", ->
|
||||
result = is_valid_symbol "a_b"
|
||||
assert.is_false result
|
||||
assert.is_true result
|
||||
|
||||
it "returns false for a string containing whitespace", ->
|
||||
result = is_valid_symbol "abc def"
|
||||
|
||||
Reference in new issue
Block a user