diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-24 21:39:27 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-24 21:39:27 +1300 |
| commit | 972a0b3354ebe860355e5fdecb34e8810ed82cbf (patch) | |
| tree | 3aa2aa5eb2acba8c9a197a71572ff5c5d4bd2f49 /spec | |
| parent | 33e84cba11e0644b39a3dbea7c4195dd82290858 (diff) | |
Not work: aliases and generics make matt a dull boy
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/practical/json_spec.cr | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/spec/practical/json_spec.cr b/spec/practical/json_spec.cr new file mode 100644 index 0000000..0b85658 --- /dev/null +++ b/spec/practical/json_spec.cr @@ -0,0 +1,44 @@ +require "../spec_helper" + +include Parcom + +# defining my own JSON alias in case of strange interactions +# with the built-in type +alias JSONValue = Nil \ + | Bool \ + | Int64 \ + | Float64 \ + | String \ + | Array(JSONValue) \ + | Hash(String, JSONValue) + +def json_null + Parser.token_sequence("null".chars).map { |_| nil.as(JSONValue) } +end + +def json_value : Parser(Char, JSONValue) + Parser.first_of([ + json_null, + ]) +end + +describe "example: JSON parsing" do + describe "json_null" do + it "parses the string 'null' and returns nil when successful" do + result = json_null.parse(Tokens.from_string("null")) + result.value.should be_nil + result.tokens.empty?.should be_true + + expect_raises(ParserFail) { json_null.parse(Tokens.from_string("")) } + end + end + + describe "json_value" do + it "parses null" do + result = json_value.parse(Tokens.from_string("null")) + result.value.should be_nil + result.tokens.empty?.should be_true + end + end +end + |
