File: fixture_test.go

package info (click to toggle)
golang-objx 0.0~git20150928.0.1a9d0bb-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 324 kB
  • sloc: makefile: 5
file content (98 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package objx

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

var fixtures = []struct {
	// name is the name of the fixture (used for reporting
	// failures)
	name string
	// data is the JSON data to be worked on
	data string
	// get is the argument(s) to pass to Get
	get interface{}
	// output is the expected output
	output interface{}
}{
	{
		name:   "Simple get",
		data:   `{"name": "Mat"}`,
		get:    "name",
		output: "Mat",
	},
	{
		name:   "Get with dot notation",
		data:   `{"address": {"city": "Boulder"}}`,
		get:    "address.city",
		output: "Boulder",
	},
	{
		name:   "Deep get with dot notation",
		data:   `{"one": {"two": {"three": {"four": "hello"}}}}`,
		get:    "one.two.three.four",
		output: "hello",
	},
	{
		name:   "Get missing with dot notation",
		data:   `{"one": {"two": {"three": {"four": "hello"}}}}`,
		get:    "one.ten",
		output: nil,
	},
	{
		name:   "Get with array notation",
		data:   `{"tags": ["one", "two", "three"]}`,
		get:    "tags[1]",
		output: "two",
	},
	{
		name:   "Get with array and dot notation",
		data:   `{"types": { "tags": ["one", "two", "three"]}}`,
		get:    "types.tags[1]",
		output: "two",
	},
	{
		name:   "Get with array and dot notation - field after array",
		data:   `{"tags": [{"name":"one"}, {"name":"two"}, {"name":"three"}]}`,
		get:    "tags[1].name",
		output: "two",
	},
	{
		name:   "Complex get with array and dot notation",
		data:   `{"tags": [{"list": [{"one":"pizza"}]}]}`,
		get:    "tags[0].list[0].one",
		output: "pizza",
	},
	{
		name:   "Get field from within string should be nil",
		data:   `{"name":"Tyler"}`,
		get:    "name.something",
		output: nil,
	},
	{
		name:   "Get field from within string (using array accessor) should be nil",
		data:   `{"numbers":["one", "two", "three"]}`,
		get:    "numbers[0].nope",
		output: nil,
	},
}

func TestFixtures(t *testing.T) {

	for _, fixture := range fixtures {

		m := MustFromJSON(fixture.data)

		// get the value
		t.Logf("Running get fixture: \"%s\" (%v)", fixture.name, fixture)
		value := m.Get(fixture.get.(string))

		// make sure it matches
		assert.Equal(t, fixture.output, value.data,
			"Get fixture \"%s\" failed: %v", fixture.name, fixture,
		)

	}

}