feat(parse_transition): first version of parse_transition, with tests

This commit is contained in:
swaits committed 2025-02-02 20:11:01 -08:00
commit bdc28c4e83
9 files changed
+342

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
-- supports requiring .moon files directly
require "moonscript".register!
+66
View File
@@ -0,0 +1,66 @@
require "src.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
it "parses a valid 'after' transition", ->
result = parse_transition "stateB after 3.14"
assert.same {target: "stateB", after: 3.14}, result
it "parses a valid 'every' transition", ->
result = parse_transition "stateC every 42"
assert.same {target: "stateC", every: 42}, result
-- ### Basic Error Cases ###
it "errors when input is not a string", ->
assert.has_error (-> parse_transition 123), "Transition must be a string"
it "errors when input does not have exactly three words", ->
assert.has_error (-> parse_transition "two words"), "Transition must consist of three words"
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 'every' transition", ->
assert.has_error (-> parse_transition "stateF every not_a_number"), "Invalid time value in '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", ->
-- Assuming negative times are invalid.
assert.has_error (-> parse_transition "stateNeg after -5"), "Invalid time value in 'after' 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 zero in an 'after' transition", ->
-- Zero is considered invalid.
assert.has_error (-> parse_transition "stateZero after 0"), "Invalid time value in 'after' 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 when the state name contains a space", ->
-- e.g. "state A" is not a valid Lua identifier.
assert.has_error (-> parse_transition "state A on eventX"), "Transition must consist of three words"
it "errors when the event name contains a space", ->
-- e.g. "event X" is not a valid Lua identifier.
assert.has_error (-> parse_transition "stateA on event X"), "Transition must consist of three words"
it "errors when the state name starts with a number", ->
-- Lua identifiers cannot start with a digit.
assert.has_error (-> parse_transition "1state on eventX"), "Invalid state name"
it "errors when the event name contains an invalid character", ->
-- For example, a dash is not allowed.
assert.has_error (-> parse_transition "stateA on event-X"), "Invalid event name"
+56
View File
@@ -0,0 +1,56 @@
require "src.util.string"
describe "string.split", ->
it "returns an empty table when given an empty string", ->
result = split ""
assert.is_table result
assert.are.equal 0, #result
it "returns an empty table when given a string containing only whitespace", ->
result = split " \t \n"
assert.is_table result
assert.are.equal 0, #result
it "splits a single word correctly", ->
result = split "hello"
assert.are.same { "hello" }, result
it "splits two words separated by a single space", ->
result = split "hello world"
assert.are.same { "hello", "world" }, result
it "handles multiple spaces between words", ->
result = split "hello world"
assert.are.same { "hello", "world" }, result
it "handles leading and trailing whitespace", ->
result = split " hello world "
assert.are.same { "hello", "world" }, result
it "handles tabs and newline characters as whitespace", ->
result = split "hello\tworld\nLua"
assert.are.same { "hello", "world", "Lua" }, result
it "treats punctuation as part of the word", ->
result = split "hello, world!"
assert.are.same { "hello,", "world!" }, result
it "splits a string with various whitespace characters", ->
result = split " one \t two\n three \n\t four"
assert.are.same { "one", "two", "three", "four" }, result
it "returns a new table on each call", ->
result1 = split "a b c"
result2 = split "a b c"
assert.not_equal result1, result2
it "errors when the input is not a string", ->
assert.has_error => split nil
assert.has_error => split 123
it "does not modify the input string", ->
s = " immutable test "
original = s
split s
assert.are.equal original, s
+104
View File
@@ -0,0 +1,104 @@
require "src.util.validators"
describe "validators", ->
describe "is_valid_timer", ->
it "returns falsy when passed nil", ->
result = is_valid_timer nil
-- nil is falsy in Lua.
assert.falsy result
it "returns false when passed false", ->
result = is_valid_timer false
assert.is_false result
it "returns false when passed 0", ->
result = is_valid_timer 0
assert.is_false result
it "returns false when passed a negative number", ->
result = is_valid_timer -1
assert.is_false result
it "returns true when passed a positive integer", ->
result = is_valid_timer 1
assert.is_true result
it "returns true when passed a positive decimal", ->
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 "errors when passed a non-number (boolean true)", ->
assert.has_error => is_valid_timer true
it "errors when passed a non-number (table)", ->
assert.has_error => is_valid_timer { foo: "bar" }
describe "is_valid_symbol", ->
-- When the input is not a string, it should immediately return false.
it "returns false when passed nil", ->
result = is_valid_symbol nil
assert.is_false result
it "returns false when passed a number", ->
result = is_valid_symbol 123
assert.is_false result
it "returns false when passed a table", ->
result = is_valid_symbol { }
assert.is_false result
it "returns false when passed a boolean", ->
result = is_valid_symbol false
assert.is_false result
-- When the input is a string:
it "returns false for an empty string", ->
result = is_valid_symbol ""
assert.is_false result
it "returns true for a single lowercase letter", ->
result = is_valid_symbol "a"
assert.is_true result
it "returns true for a single uppercase letter", ->
result = is_valid_symbol "Z"
assert.is_true result
it "returns true for a string of only letters", ->
result = is_valid_symbol "HelloWorld"
assert.is_true result
it "returns true for a string with mixed case letters", ->
result = is_valid_symbol "AbCdEf"
assert.is_true result
it "returns false for a string with numbers", ->
result = is_valid_symbol "abc123"
assert.is_false 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", ->
result = is_valid_symbol "a_b"
assert.is_false result
it "returns false for a string containing whitespace", ->
result = is_valid_symbol "abc def"
assert.is_false result
it "returns false for a string with leading/trailing whitespace", ->
result = is_valid_symbol " abc "
assert.is_false result
it "returns false for a string with non-ASCII letters", ->
result = is_valid_symbol "éclair"
assert.is_false result
it "returns false for a string with a hyphen", ->
result = is_valid_symbol "A-B"
assert.is_false result