OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package jsutil |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "encoding/json" |
| 10 "testing" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 func TestGet(t *testing.T) { |
| 16 t.Parallel() |
| 17 |
| 18 const testDoc = ` |
| 19 [ |
| 20 { |
| 21 "some": [1337, {"mixed": "values"}], |
| 22 "wat": "thing" |
| 23 }, |
| 24 {} |
| 25 ] |
| 26 ` |
| 27 |
| 28 var data interface{} |
| 29 dec := json.NewDecoder(bytes.NewBufferString(testDoc)) |
| 30 dec.UseNumber() |
| 31 if err := dec.Decode(&data); err != nil { |
| 32 panic(err) |
| 33 } |
| 34 |
| 35 Convey("Test Get and GetError", t, func() { |
| 36 Convey("can extract values", func() { |
| 37 So(len(Get(data, 0).(map[string]interface{})), ShouldEqu
al, 2) |
| 38 val, err := Get(data, 0, "some", 0).(json.Number).Int64(
) |
| 39 So(err, ShouldBeNil) |
| 40 So(val, ShouldEqual, 1337) |
| 41 }) |
| 42 |
| 43 Convey("getting a value that's not there panics", func() { |
| 44 So(func() { |
| 45 Get(data, "nope") |
| 46 }, ShouldPanic) |
| 47 }) |
| 48 |
| 49 Convey("errors are resonable", func() { |
| 50 _, err := GetError(data, 0, "some", 1, "mixed", 10) |
| 51 So(err.Error(), ShouldContainSubstring, |
| 52 "expected []interface{}, but got string") |
| 53 |
| 54 _, err = GetError(data, 0, "some", 1, "mixed", "nonex") |
| 55 So(err.Error(), ShouldContainSubstring, |
| 56 "expected map[string]interface{}, but got string
") |
| 57 |
| 58 _, err = GetError(data, 0.1) |
| 59 So(err.Error(), ShouldContainSubstring, |
| 60 "expected string or int in pathElems, got float6
4 instead") |
| 61 }) |
| 62 }) |
| 63 } |
OLD | NEW |