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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/jsutil/jsutil.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/jsutil/jsutil_test.go
diff --git a/go/src/infra/libs/jsutil/jsutil_test.go b/go/src/infra/libs/jsutil/jsutil_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b0e9b614a78f4c7e2c8b7f5c411412e1281c6d16
--- /dev/null
+++ b/go/src/infra/libs/jsutil/jsutil_test.go
@@ -0,0 +1,63 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package jsutil
+
+import (
+ "bytes"
+ "encoding/json"
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestGet(t *testing.T) {
+ t.Parallel()
+
+ const testDoc = `
+ [
+ {
+ "some": [1337, {"mixed": "values"}],
+ "wat": "thing"
+ },
+ {}
+ ]
+ `
+
+ var data interface{}
+ dec := json.NewDecoder(bytes.NewBufferString(testDoc))
+ dec.UseNumber()
+ if err := dec.Decode(&data); err != nil {
+ panic(err)
+ }
+
+ Convey("Test Get and GetError", t, func() {
+ Convey("can extract values", func() {
+ So(len(Get(data, 0).(map[string]interface{})), ShouldEqual, 2)
+ val, err := Get(data, 0, "some", 0).(json.Number).Int64()
+ So(err, ShouldBeNil)
+ So(val, ShouldEqual, 1337)
+ })
+
+ Convey("getting a value that's not there panics", func() {
+ So(func() {
+ Get(data, "nope")
+ }, ShouldPanic)
+ })
+
+ Convey("errors are resonable", func() {
+ _, err := GetError(data, 0, "some", 1, "mixed", 10)
+ So(err.Error(), ShouldContainSubstring,
+ "expected []interface{}, but got string")
+
+ _, err = GetError(data, 0, "some", 1, "mixed", "nonex")
+ So(err.Error(), ShouldContainSubstring,
+ "expected map[string]interface{}, but got string")
+
+ _, err = GetError(data, 0.1)
+ So(err.Error(), ShouldContainSubstring,
+ "expected string or int in pathElems, got float64 instead")
+ })
+ })
+}
« 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