Select Git revision
string_test.go
field_test.go 3.87 KiB
package field
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestField_GetField(t *testing.T) {
sch := Object(
"f1", Object(
"a", String(),
"b", String().SetSingleLocale(true),
),
"f3", Object( // inline object
true,
"a", String(),
"b", Object(true, "c", String()),
).SetSingleLocale(true),
"f4", Array(Object("a", String())),
"f5", Array(String()),
"f6", Object(true, "f6", Object("a", String())),
)
sch.EnableState()
tests := []struct {
name string
path string
want *State
}{
{"Object", "f1", &State{Name: "f1", DataPath: "f1", SchemaPath: "f1", Parent: sch}},
{"Object field", "f1.a", &State{Name: "a", DataPath: "f1.a", SchemaPath: "f1.a", Parent: sch.GetField("f1")}},
{"Field with SingleLocale", "f1.b", &State{Name: "b", DataPath: "f1.b", SchemaPath: "f1.b", Parent: sch.GetField("f1"), SingleLocale: true}},
{"Object with SingleLocale", "f3", &State{Name: "f3", DataPath: "f3", SchemaPath: "f3", Parent: sch, SingleLocale: true}},
{"Inline", "a", &State{Name: "a", DataPath: "a", SchemaPath: "f3.a", Parent: sch.GetField("f3"), SingleLocale: true, Inlined: true, HasInline: true}},
{"Inline of inline", "c", &State{Name: "c", DataPath: "c", SchemaPath: "f3.b.c", Parent: sch.GetField("f3.b"), SingleLocale: true, Inlined: true, HasInline: true}},
{"Inline of inline (direct)", "f3.b.c", &State{Name: "c", DataPath: "c", SchemaPath: "f3.b.c", Parent: sch.GetField("f3.b"), SingleLocale: true, Inlined: true, HasInline: true}},
{"Array of Objects", "f4", &State{Name: "f4", DataPath: "f4", SchemaPath: "f4", Parent: sch}},
{"Array of Objects (Item)", "f4.Item", &State{Name: "Item", DataPath: "f4", SchemaPath: "f4.Item", Parent: sch.GetField("f4")}},
{"Array of Objects (Item field)", "f4.Item.a", &State{Name: "a", DataPath: "f4.a", SchemaPath: "f4.Item.a", Parent: sch.GetField("f4.Item")}},
{"Array of Objects (Item field direct)", "f4.a", &State{Name: "a", DataPath: "f4.a", SchemaPath: "f4.Item.a", Parent: sch.GetField("f4.Item")}},
{"Array of Strings", "f5", &State{Name: "f5", DataPath: "f5", SchemaPath: "f5", Parent: sch}},
{"Array of Strings (Item)", "f5.Item", &State{Name: "Item", DataPath: "f5", SchemaPath: "f5.Item", Parent: sch.GetField("f5")}},
{"Inline Same name not found", "f6.a", nil},
{"Inline Same name (direct)", "f6.f6.a", &State{Name: "a", DataPath: "f6.a", SchemaPath: "f6.f6.a", Parent: sch.GetField("f6.f6"), HasInline: true}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sch.GetField(tt.path)
var st *State
if got != nil {
st = got.State
}
assert.Equal(t, tt.want, st)
sch.ClearState()
sch.EnableState()
if got != nil {
assert.Nil(t, got.State)
}
})
}
}
func TestField_ListFieldsRecursive(t *testing.T) {
sch := Object(
"f1", Object(
"f1a", String(),
"f1b", String().SetSingleLocale(true),
),
"f2", String(),
"f3", Object( // inline object
true,
"f3a", String(),
"f3b", Object(true, "f3bc", String()),
).SetSingleLocale(true),
"f4", Array(Object("f4a", String())),
"f5", Array(String()),
"f6", Object(true, "f6", Object("f6a", String())),
)
sch.EnableState()
fields := sch.ListFieldsRecursive()
assert.Len(t, fields, 16)
for _, f := range fields {
assert.NotNil(t, f.State)
assert.NotEmpty(t, f.State.Name)
}
}
func TestField_ListFieldsRecursive_WithFilter(t *testing.T) {
sch := Object(
"f1", Object(
"b", Object(
"c", String().SetSingleLocale(true),
),
),
"f2", Object(
"b", Object(
"c", String().SetSingleLocale(true),
),
).SetSingleLocale(true),
)
sch.EnableState()
fields := sch.ListFieldsRecursive(func(f *Field) bool { return f.SingleLocale == true })
assert.Len(t, fields, 3)
}
func TestField_CloneWithState(t *testing.T) {
f := Object("a", String())
fld := f.Clone(false)
assert.Nil(t, fld.State)
f.EnableState()
fld = f.Clone(false)
assert.NotNil(t, fld.State)
}