示例#1
0
func TestConvertKeys(t *testing.T) {
	m := New().PickAll().ConvertKeys(func(s string) string {
		return "dummy_" + s
	}).Transform(user)
	assert.Contains(t, m, "dummy_ID")
	assert.Contains(t, m, "dummy_FirstName")
	assert.NotContains(t, m, "ID")
}
示例#2
0
func TestPickFunc(t *testing.T) {
	m := New().PickFunc(func(t interface{}) interface{} {
		return t.(time.Time).Format(time.RFC3339)
	}, "CreatedAt", "UpdatedAt").Transform(user)
	assert.Contains(t, m, "CreatedAt")
	assert.Contains(t, m, "UpdatedAt")
	assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339))
}
示例#3
0
func TestPickFuncIf(t *testing.T) {
	m := New().PickFuncIf(alwaysTrue, func(t interface{}) interface{} {
		return t.(time.Time).Format(time.RFC3339)
	}, "CreatedAt", "UpdatedAt").PickFuncIf(alwaysFalse, identity, "Email").Transform(user)
	assert.Contains(t, m, "CreatedAt")
	assert.Contains(t, m, "UpdatedAt")
	assert.NotContains(t, m, "Email")
	assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339))
}
示例#4
0
func TestPickIf(t *testing.T) {
	m := New().
		PickIf(alwaysTrue, "ID", "FirstName").
		PickIf(alwaysFalse, "Email").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "FirstName")
	assert.NotContains(t, m, "Email")
	assert.NotContains(t, m, "Age")
}
示例#5
0
func TestPick(t *testing.T) {
	m := New().Pick("ID", "Age").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "Age")
	assert.NotContains(t, m, "FirstName")
	m = New().Pick("ID").Pick("Age").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "Age")
	assert.Equal(t, m["ID"], user.ID)
}
示例#6
0
func TestDefaultCase(t *testing.T) {
	SetDefaultCase(SnakeCase)
	m := New().PickAll().Transform(user)
	assert.Contains(t, m, "first_name")
	SetDefaultCase(CamelCase)
	m = New().PickAll().Transform(user)
	assert.Contains(t, m, "firstName")
	SetDefaultCase(PascalCase)
	m = New().PickAll().Transform(user)
	assert.Contains(t, m, "FirstName")
}
示例#7
0
func TestOmitIf(t *testing.T) {
	m := New().PickAll().OmitIf(func(u interface{}) bool {
		return u.(User).HideName
	}, "FirstName", "LastName").Transform(user)
	assert.NotContains(t, m, "FirstName", "LastName")

	m = New().PickAll().OmitIf(func(u interface{}) bool {
		return u.(User).Age < 18
	}, "Birthday", "Age").Transform(user)
	assert.Contains(t, m, "Birthday")
	assert.Contains(t, m, "Age")
}
示例#8
0
func TestAddFunc(t *testing.T) {
	m := New().AddFunc("FullName", func(u interface{}) interface{} {
		return u.(User).FirstName + " " + u.(User).LastName
	}).Transform(user)
	assert.Contains(t, m, "FullName")
	assert.Equal(t, user.FirstName+" "+user.LastName, m["FullName"])
}
示例#9
0
func TestAddIf(t *testing.T) {
	m := New().AddIf(alwaysFalse, "Foo", "Bar").Transform(user)
	assert.NotContains(t, m, "Foo")
	m = New().AddIf(alwaysTrue, "Foo", "Bar").Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, "Bar", m["Foo"])
}
示例#10
0
func TestAddFuncIf(t *testing.T) {
	m := New().AddFuncIf(alwaysTrue, "Foo", func(u interface{}) interface{} {
		return "Bar"
	}).Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, m["Foo"], "Bar")
	m = New().AddFuncIf(alwaysFalse, "Foo", identity).Transform(user)
	assert.NotContains(t, m, "Foo")
}
示例#11
0
func TestCustomSerializer(t *testing.T) {
	m := NewCustomSerializer().WithPrivateinfo().WithBasicInfo().Transform(user)
	for _, field := range []string{"ID", "FirstName", "LastName", "Email"} {
		assert.Contains(t, m, field)
	}
}
示例#12
0
func TestUsePascalCase(t *testing.T) {
	m := New().UsePascalCase().PickAll().Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "FirstName")
}
示例#13
0
func TestUseCamelCase(t *testing.T) {
	m := New().UseCamelCase().PickAll().Transform(user)
	assert.Contains(t, m, "id")
	assert.Contains(t, m, "firstName")
	assert.NotContains(t, m, "FirstName")
}
示例#14
0
func TestAdd(t *testing.T) {
	m := New().Add("Foo", "Bar").Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, "Bar", m["Foo"])
}
示例#15
0
func TestOmit(t *testing.T) {
	m := New().PickAll().Omit("Birthday", "FirstName").Transform(user)
	assert.NotContains(t, m, "Birthday")
	assert.NotContains(t, m, "FirstName")
	assert.Contains(t, m, "ID")
}
示例#16
0
func TestPickAll(t *testing.T) {
	m := New().PickAll().Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "Age")
	assert.Contains(t, m, "FirstName")
}