-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
80 lines (67 loc) · 2.02 KB
/
parser_test.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package parser_test
import (
P "github.com/darkhelmet/parser"
. "launchpad.net/gocheck"
)
func (s *S) TestStringFailure(c *C) {
value, err := P.Parse(P.String("null"), srd("facebook"))
c.Assert(err, NotNil)
c.Assert(value, Equals, nil)
}
func (s *S) TestStringSuccess(c *C) {
v := "null"
value, err := P.Parse(P.String(v), srd(v))
c.Assert(err, IsNil)
c.Assert(value, FitsTypeOf, v)
c.Assert(value, Equals, "null")
}
func (s *S) TestOrFailure(c *C) {
p := P.Or(P.String("batman"), P.String("robin"))
value, err := P.Parse(p, srd("joker"))
c.Assert(err, NotNil)
c.Assert(value, Equals, nil)
}
func (s *S) TestOrSuccessFirst(c *C) {
p := P.Or(P.String("batman"), P.String("robin"))
value, err := P.Parse(p, srd("batman"))
c.Assert(err, IsNil)
c.Assert(value, Equals, "batman")
}
func (s *S) TestOrSuccessRest(c *C) {
p := P.Or(P.String("batman"), P.String("robin"))
value, err := P.Parse(p, srd("robin"))
c.Assert(err, IsNil)
c.Assert(value, Equals, "robin")
}
func (s *S) TestByteFailure(c *C) {
value, err := P.Parse(P.Byte(':'), srd("batman"))
c.Assert(err, NotNil)
c.Assert(value, Equals, nil)
}
func (s *S) TestByteSuccess(c *C) {
value, err := P.Parse(P.Byte(':'), srd(":symbol"))
c.Assert(err, IsNil)
c.Assert(value, Equals, byte(':'))
}
var (
lBrace = P.Byte('{')
rBrace = P.Byte('}')
colon = P.Byte(':')
quote = `"`
)
func JsonObject(r *P.Reader) (result interface{}, rd *P.Reader, err error) {
key := P.Between(quote, quote, P.String("key"))
value := P.Between(quote, quote, P.String("value"))
p := P.And(lBrace, key, colon, value, rBrace)
result, rd, err = p(r)
if err != nil {
return
}
res := result.([]interface{})
return map[string]interface{}{res[1].(string): res[3]}, rd, nil
}
func (s *S) TestBasicJson(c *C) {
value, err := P.Parse(JsonObject, srd(`{"key":"value"}`))
c.Assert(err, IsNil)
c.Assert(value, DeepEquals, map[string]interface{}{"key": "value"})
}