Пример #1
0
func TestStructValue(t *testing.T) {
	var data SimpleType
	v := kiss.StructValue(reflect.ValueOf(data))

	typ := reflect.TypeOf(data)

	for i := 0; i < typ.NumField(); i++ {
		name := typ.Field(i).Name
		except := reflect.ValueOf(data).FieldByName(name).Interface()
		actual, ok := v.Get(name)
		if !ok {
			t.Errorf("get field %s %s", name, ok)
		} else if except != actual.Interface() {
			t.Errorf("get field %s fail, except %#v actual %#v", name, except, actual)
		}
	}
}
Пример #2
0
func TestExtend2(t *testing.T) {
	var t1 SimpleType
	t2 := map2
	v1 := reflect.ValueOf(&t1).Elem()

	dest := kiss.StructValue(v1)
	var src kiss.GetFunc = func(name string) (interface{}, bool) {
		x, ok := t2[name]
		return x, ok
	}
	kiss.Extend(dest, src)

	for i := 0; i < v1.Type().NumField(); i++ {
		f := v1.Type().Field(i)
		except, ok := t2[f.Name]
		actual := fmt.Sprint(v1.FieldByName(f.Name).Interface())
		if !ok || fmt.Sprint(except) != actual {
			t.Errorf("extend field %s fail, except %s, actual %s", f.Name, fmt.Sprint(except), actual)
		}
	}
}