MCPcopy Create free account
hub / github.com/stretchr/objx / TestSimpleExample

Function TestSimpleExample

simple_example_test.go:9–40  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7)
8
9func TestSimpleExample(t *testing.T) {
10 // build a map from a JSON object
11 o := objx.MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`)
12
13 // Map can be used as a straight map[string]interface{}
14 assert.Equal(t, o["name"], "Mat")
15
16 // Get an Value object
17 v := o.Get("name")
18 require.NotNil(t, v)
19
20 // Test the contained value
21 assert.False(t, v.IsInt())
22 assert.False(t, v.IsBool())
23 assert.True(t, v.IsStr())
24
25 // Get the contained value
26 assert.Equal(t, v.Str(), "Mat")
27
28 // Get a default value if the contained value is not of the expected type or does not exist
29 assert.Equal(t, 1, v.Int(1))
30
31 // Get a value by using array notation
32 assert.Equal(t, "indian", o.Get("foods[0]").Data())
33
34 // Set a value by using array notation
35 o.Set("foods[0]", "italian")
36 assert.Equal(t, "italian", o.Get("foods[0]").Str())
37
38 // Get a value by using dot notation
39 assert.Equal(t, "hobbiton", o.Get("location.county").Str())
40}

Callers

nothing calls this directly

Calls 13

MustFromJSONFunction · 0.92
GetMethod · 0.80
IsIntMethod · 0.80
IsBoolMethod · 0.80
IsStrMethod · 0.80
StrMethod · 0.80
IntMethod · 0.80
DataMethod · 0.80
SetMethod · 0.80
EqualMethod · 0.45
NotNilMethod · 0.45
FalseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…