...
1 package objx_test
2
3 import (
4 "testing"
5
6 "github.com/stretchr/objx"
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestSimpleExample(t *testing.T) {
12
13 o := objx.MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`)
14
15
16 assert.Equal(t, o["name"], "Mat")
17
18
19 v := o.Get("name")
20 require.NotNil(t, v)
21
22
23 assert.False(t, v.IsInt())
24 assert.False(t, v.IsBool())
25 assert.True(t, v.IsStr())
26
27
28 assert.Equal(t, v.Str(), "Mat")
29
30
31 assert.Equal(t, 1, v.Int(1))
32
33
34 assert.Equal(t, "indian", o.Get("foods[0]").Data())
35
36
37 o.Set("foods[0]", "italian")
38 assert.Equal(t, "italian", o.Get("foods[0]").Str())
39
40
41 assert.Equal(t, "hobbiton", o.Get("location.county").Str())
42 }
43
View as plain text