blob: 0b85658191bd5a7d9dfcdde7f2188ad07ba14e1c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
|