...

Source file src/github.com/stretchr/objx/fixture_test.go

Documentation: github.com/stretchr/objx

     1  package objx_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/objx"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var fixtures = []struct {
    11  	// name is the name of the fixture (used for reporting
    12  	// failures)
    13  	name string
    14  	// data is the JSON data to be worked on
    15  	data string
    16  	// get is the argument(s) to pass to Get
    17  	get interface{}
    18  	// output is the expected output
    19  	output interface{}
    20  }{
    21  	{
    22  		name:   "Simple get",
    23  		data:   `{"name": "Mat"}`,
    24  		get:    "name",
    25  		output: "Mat",
    26  	},
    27  	{
    28  		name:   "Get with dot notation",
    29  		data:   `{"address": {"city": "Boulder"}}`,
    30  		get:    "address.city",
    31  		output: "Boulder",
    32  	},
    33  	{
    34  		name:   "Deep get with dot notation",
    35  		data:   `{"one": {"two": {"three": {"four": "hello"}}}}`,
    36  		get:    "one.two.three.four",
    37  		output: "hello",
    38  	},
    39  	{
    40  		name:   "Get missing with dot notation",
    41  		data:   `{"one": {"two": {"three": {"four": "hello"}}}}`,
    42  		get:    "one.ten",
    43  		output: nil,
    44  	},
    45  	{
    46  		name:   "Get with array notation",
    47  		data:   `{"tags": ["one", "two", "three"]}`,
    48  		get:    "tags[1]",
    49  		output: "two",
    50  	},
    51  	{
    52  		name:   "Get with array and dot notation",
    53  		data:   `{"types": { "tags": ["one", "two", "three"]}}`,
    54  		get:    "types.tags[1]",
    55  		output: "two",
    56  	},
    57  	{
    58  		name:   "Get with array and dot notation - field after array",
    59  		data:   `{"tags": [{"name":"one"}, {"name":"two"}, {"name":"three"}]}`,
    60  		get:    "tags[1].name",
    61  		output: "two",
    62  	},
    63  	{
    64  		name:   "Complex get with array and dot notation",
    65  		data:   `{"tags": [{"list": [{"one":"pizza"}]}]}`,
    66  		get:    "tags[0].list[0].one",
    67  		output: "pizza",
    68  	},
    69  	{
    70  		name:   "Get field from within string should be nil",
    71  		data:   `{"name":"Tyler"}`,
    72  		get:    "name.something",
    73  		output: nil,
    74  	},
    75  	{
    76  		name:   "Get field from within string (using array accessor) should be nil",
    77  		data:   `{"numbers":["one", "two", "three"]}`,
    78  		get:    "numbers[0].nope",
    79  		output: nil,
    80  	},
    81  }
    82  
    83  func TestFixtures(t *testing.T) {
    84  	for _, fixture := range fixtures {
    85  		m := objx.MustFromJSON(fixture.data)
    86  
    87  		// get the value
    88  		t.Logf("Running get fixture: \"%s\" (%v)", fixture.name, fixture)
    89  		value := m.Get(fixture.get.(string))
    90  
    91  		// make sure it matches
    92  		assert.Equal(t, fixture.output, value.Data(),
    93  			"Get fixture \"%s\" failed: %v", fixture.name, fixture,
    94  		)
    95  	}
    96  }
    97  

View as plain text