Permalink
Browse files

add tests

  • Loading branch information...
1 parent 35ca82f commit da481f094a2c735900da7448d77b73db0fb0d685 @mreiferson mreiferson committed May 12, 2012
Showing with 69 additions and 0 deletions.
  1. +69 −0 simplejson_test.go
View
69 simplejson_test.go
@@ -0,0 +1,69 @@
+package simplejson
+
+import (
+ "github.com/bmizerany/assert"
+ "io/ioutil"
+ "log"
+ "strconv"
+ "testing"
+)
+
+func TestSimplejson(t *testing.T) {
+ var ok bool
+ var err error
+
+ log.SetOutput(ioutil.Discard)
+
+ js, err := NewJson([]byte(`{
+ "test": {
+ "array": [1, "2", 3],
+ "int": 10,
+ "float": 5.150,
+ "bignum": 9223372036854775807,
+ "string": "simplejson"
+ }
+ }`))
+
+ assert.NotEqual(t, nil, js)
+ assert.Equal(t, nil, err)
+
+ _, ok = js.CheckGet("test")
+ assert.Equal(t, true, ok)
+
+ _, ok = js.CheckGet("missing_key")
+ assert.Equal(t, false, ok)
+
+ arr, _ := js.Get("test").Get("array").Array()
+ assert.NotEqual(t, nil, arr)
+ for i, v := range arr {
+ var iv int
+ switch v.(type) {
+ case float64:
+ iv = int(v.(float64))
+ case string:
+ iv, _ = strconv.Atoi(v.(string))
+ }
+ assert.Equal(t, i+1, iv)
+ }
+
+ i, _ := js.Get("test").Get("int").Int()
+ assert.Equal(t, 10, i)
+
+ f, _ := js.Get("test").Get("float").Float64()
+ assert.Equal(t, 5.150, f)
+
+ s, _ := js.Get("test").Get("string").String()
+ assert.Equal(t, "simplejson", s)
+
+ mi := js.Get("test").Get("int").MustInt()
+ assert.Equal(t, 10, mi)
+
+ mi2 := js.Get("test").Get("missing_int").MustInt(5150)
+ assert.Equal(t, 5150, mi2)
+
+ ms := js.Get("test").Get("string").MustString()
+ assert.Equal(t, "simplejson", ms)
+
+ ms2 := js.Get("test").Get("missing_string").MustString("fyea")
+ assert.Equal(t, "fyea", ms2)
+}

0 comments on commit da481f0

Please sign in to comment.