Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: go/src/infra/libs/jsutil/jsutil_test.go

Issue 1153473008: A client/server helper wrapper for endpoints in Go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: rm newline Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/libs/jsutil/jsutil.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/jsutil/jsutil.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698