Example #1
0
func TestCompare(t *testing.T) {
	t1 := newSimpleType()
	t1.A_string = "string"
	t1.A_string_p = &t1.A_string
	t1.A_interface = int(0)

	t2 := newSimpleType()
	t2.A_string = "string"
	t2.A_string_p = &t2.A_string
	t2.A_interface = float64(0)

	for name1, f1 := range gotype.ToMap(reflect.ValueOf(t1)) {
		for name2, f2 := range gotype.ToMap(reflect.ValueOf(&t2)) {
			if gotype.CanCompareKind(gotype.Underlying(f1).Kind(), gotype.Underlying(f2).Kind()) {
				equal := gotype.Equal(f1.Interface(), f2.Interface())
				if !equal {
					t.Errorf("Test Equal fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}

				greater := gotype.Greater(f1.Interface(), f2.Interface())
				if greater {
					t.Errorf("Test Greater fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}

				less := gotype.Less(f1.Interface(), f2.Interface())
				if less {
					t.Errorf("Test Less fail for %s (%s) and %s (%s)", name1, f1, name2, f2)
				}
			}
		}
	}
}
Example #2
0
func TestToBool(t *testing.T) {
	data := newSimpleType()
	data.A_string = "true"
	data.A_string_p = &data.A_string
	data.A_interface = "true"

	v := reflect.ValueOf(&data)
	for name, f := range gotype.ToMap(v) {
		_, err := gotype.ToBool(f)
		if err != nil {
			t.Errorf("field %s (%v) convert to bool fail, %s", name, f.Interface(), err)
		}
	}
}
Example #3
0
func TestToString(t *testing.T) {
	data := newSimpleType()
	data.A_string = "string"
	data.A_string_p = &data.A_string
	data.A_interface = "string"

	v := reflect.ValueOf(data)
	for name, f := range gotype.ToMap(v) {
		t.Logf("test to string  %s %s %s '\n", name, f, f.Kind())
		_, err := gotype.ToString(f)
		if err != nil {
			t.Errorf("field %s (%v) convert to string fail, %s", name, f.Interface(), err)
		}
	}

}
Example #4
0
func TestToMap(t *testing.T) {
	data := newSimpleType()
	data.A_string = "string"
	data.A_string_p = &data.A_string
	data.A_interface = "string"

	v := reflect.ValueOf(data)
	m := gotype.ToMap(v)
	for i := 0; i < v.Type().NumField(); i++ {
		name := v.Type().Field(i).Name
		fv := v.FieldByName(name)
		actual, ok := m[name]
		if !ok {
			t.Errorf("field %s (%v) does not exists", name, fv)
		}
		if actual != fv {
			t.Errorf("convert %s fail, expect %s, actual %v", name, fv, actual)
		}
	}
}