コード例 #1
0
func TestBindAll(t *testing.T) {
	var obj struct {
		Id     int32
		Labels []string
		Pets   []struct {
			Name string
		}
	}
	var err = bind.Values(map[string][]string{
		"Id":           {"5"},
		"Labels":       {"foo", "bar"},
		"Pets[0].Name": {"Lassie"},
		"Pets[1].Name": {"Mabel"},
	}).All(&obj)

	if err != nil {
		t.Error(err)
	}
	if obj.Id != 5 ||
		!reflect.DeepEqual(obj.Labels, []string{"foo", "bar"}) ||
		obj.Pets[0].Name != "Lassie" ||
		obj.Pets[1].Name != "Mabel" {
		t.Errorf("Wrong data, got %#v", obj)
	}
}
コード例 #2
0
func runBindTests(t testing.TB, tests []bindTest) {
	for _, test := range tests {
		var binder = bind.Values(test.params)
		var pactual = reflect.New(reflect.TypeOf(test.expected))
		var err = binder.Field(pactual.Interface(), test.name)
		if err != nil {
			if !strings.HasPrefix(test.name, "err") {
				t.Errorf("%v: %v", test.name, err)
			}
			continue
		}

		var actual = pactual.Elem().Interface()
		if !reflect.DeepEqual(actual, test.expected) {
			t.Errorf("%v: expected %#v, got %#v", test.name, test.expected, actual)
		}
	}
}