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 81 82 83 84 85 86 87 88 89
|
package json
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestUndefinedValue(t *testing.T) {
Convey("When unmarshalling JSON with undefined values", t, func() {
Convey("works for a single key", func() {
var jsonMap map[string]interface{}
key := "key"
value := "undefined"
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(Undefined)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldResemble, Undefined{})
})
Convey("works for multiple keys", func() {
var jsonMap map[string]interface{}
key1, key2, key3 := "key1", "key2", "key3"
value := "undefined"
data := fmt.Sprintf(`{"%v":%v,"%v":%v,"%v":%v}`,
key1, value, key2, value, key3, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue1, ok := jsonMap[key1].(Undefined)
So(ok, ShouldBeTrue)
So(jsonValue1, ShouldResemble, Undefined{})
jsonValue2, ok := jsonMap[key2].(Undefined)
So(ok, ShouldBeTrue)
So(jsonValue2, ShouldResemble, Undefined{})
jsonValue3, ok := jsonMap[key3].(Undefined)
So(ok, ShouldBeTrue)
So(jsonValue3, ShouldResemble, Undefined{})
})
Convey("works in an array", func() {
var jsonMap map[string]interface{}
key := "key"
value := "undefined"
data := fmt.Sprintf(`{"%v":[%v,%v,%v]}`,
key, value, value, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonArray, ok := jsonMap[key].([]interface{})
So(ok, ShouldBeTrue)
for _, _jsonValue := range jsonArray {
jsonValue, ok := _jsonValue.(Undefined)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldResemble, Undefined{})
}
})
Convey("cannot have a sign ('+' or '-')", func() {
var jsonMap map[string]interface{}
key := "key"
value := "undefined"
data := fmt.Sprintf(`{"%v":+%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldNotBeNil)
data = fmt.Sprintf(`{"%v":-%v}`, key, value)
err = Unmarshal([]byte(data), &jsonMap)
So(err, ShouldNotBeNil)
})
})
}
|