Skip to content

Commit

Permalink
complete JSON Pointer tests
Browse files Browse the repository at this point in the history
fixed #10
  • Loading branch information
koron committed Jul 23, 2016
1 parent 27f02ff commit c2b02d9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions error.go
Expand Up @@ -72,6 +72,10 @@ func (p *errorProxy) Nil() bool {
return false
}

func (p *errorProxy) Value() (interface{}, error) {
return nil, p
}

func (p *errorProxy) Bool() (bool, error) {
return false, p
}
Expand Down
54 changes: 54 additions & 0 deletions pointer_test.go
Expand Up @@ -30,3 +30,57 @@ func TestPointerInvalidQuery(t *testing.T) {
t.Fatalf("errorType should be EinvalidQuery but: %s", err.errorType)
}
}

func TestPointer(t *testing.T) {
f := func(q string, d, expected interface{}) {
p := Pointer(d, q)
v, err := p.Value()
if err != nil {
t.Errorf("Pointer:%q for %+v failed: %s", q, d, err)
return
}
assertEquals(t, v, expected, "Pointer:%q for %+v", q, d)
}

v := parseJSON(`{
"cities": [ "tokyo", 100, "osaka", 200, "hakata", 300 ],
"data": {
"custom": [ "male", 21, "female", 22 ]
}
}`)
f("", v, v)
f("/cities", v, parseJSON(`["tokyo",100,"osaka",200,"hakata",300]`))
f("/cities/0", v, "tokyo")
f("/cities/1", v, float64(100))
f("/cities/2", v, "osaka")
f("/cities/3", v, float64(200))
f("/cities/4", v, "hakata")
f("/cities/5", v, float64(300))
f("/data/custom", v, parseJSON(`["male",21,"female",22]`))

// Example from RFC6901 https://tools.ietf.org/html/rfc6901
w := parseJSON(`{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}`)
f("", w, w)
f("/foo", w, parseJSON(`["bar","baz"]`))
f("/foo/0", w, "bar")
f("/", w, float64(0))
f("/a~1b", w, float64(1))
f("/c%d", w, float64(2))
f("/e^f", w, float64(3))
f("/g|h", w, float64(4))
f("/i\\j", w, float64(5))
f("/k\"l", w, float64(6))
f("/ ", w, float64(7))
f("/m~0n", w, float64(8))
}
4 changes: 4 additions & 0 deletions proxy.go
Expand Up @@ -5,6 +5,10 @@ type Proxy interface {
// Nil returns true, if target value is nil.
Nil() bool

// Value returns a proxied value. If there are no values, it returns
// error.
Value() (interface{}, error)

// Bool returns its value. If value isn't the type, it returns error.
Bool() (bool, error)

Expand Down
14 changes: 14 additions & 0 deletions test.go
@@ -0,0 +1,14 @@
package dproxy

import (
"fmt"
"reflect"
"testing"
)

func assertEquals(t *testing.T, actual, expected interface{}, format string, a ...interface{}) {
if !reflect.DeepEqual(actual, expected) {
msg := fmt.Sprintf(format, a...)
t.Errorf("not equal: %s\nactual=%+v\nexpected=%+v", msg, actual, expected)
}
}
4 changes: 4 additions & 0 deletions value.go
Expand Up @@ -15,6 +15,10 @@ func (p *valueProxy) Nil() bool {
return p.value == nil
}

func (p *valueProxy) Value() (interface{}, error) {
return p.value, nil
}

func (p *valueProxy) Bool() (bool, error) {
switch v := p.value.(type) {
case bool:
Expand Down

0 comments on commit c2b02d9

Please sign in to comment.