func TestRuleWrapper_ZeroValue(t *testing.T) { cb := tests.Context("kego.io/system").Jauto().Sauto(parser.Parse) r := system.WrapRule(cb.Ctx(), &system.MapRule{ Object: &system.Object{Type: system.NewReference("kego.io/system", "@map")}, Rule: &system.Rule{}, Items: &system.StringRule{ Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")}, Rule: &system.Rule{}, }, }) v, err := r.ZeroValue(true) require.NoError(t, err) assert.IsType(t, map[string]*system.String{}, v.Interface()) assert.Nil(t, v.Interface()) v, err = r.ZeroValue(false) require.NoError(t, err) assert.IsType(t, map[string]*system.String{}, v.Interface()) assert.NotNil(t, v.Interface()) vv := v.Interface().(map[string]*system.String) vv["a"] = system.NewString("") r = system.WrapRule(cb.Ctx(), &system.MapRule{ Object: &system.Object{Type: system.NewReference("kego.io/system", "@array")}, Rule: &system.Rule{}, Items: &system.StringRule{ Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")}, Rule: &system.Rule{}, }, }) v, err = r.ZeroValue(true) require.NoError(t, err) assert.IsType(t, []*system.String{}, v.Interface()) assert.Nil(t, v.Interface()) v, err = r.ZeroValue(false) require.NoError(t, err) assert.IsType(t, []*system.String{}, v.Interface()) assert.NotNil(t, v.Interface()) va := v.Interface().([]*system.String) va = append(va, system.NewString("")) }
func TestAljni(t *testing.T) { ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil) Run(t, ctx, `{ "type": "multi", "aljni": 1.1 }`, TestValue(func(t *testing.T, v interface{}) { assert.NotNil(t, v.(*data.Multi).Aljni.GetAljn(ctx)) assert.IsType(t, new(data.Aljn), v.(*data.Multi).Aljni) assert.Equal(t, 1.1, v.(*data.Multi).Aljni.GetAljn(ctx).Value()) }), ) }
func TestAljni2(t *testing.T) { ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil) // Aljn2 is another alias number type that implements the Aljn default // interface. Run(t, ctx, `{ "type": "multi", "aljni": {"type": "aljn2", "value": 1.1} }`, TestValue(func(t *testing.T, v interface{}) { assert.NotNil(t, v.(*data.Multi).Aljni.GetAljn(ctx)) assert.IsType(t, new(data.Aljn2), v.(*data.Multi).Aljni) assert.Equal(t, 1.1, v.(*data.Multi).Aljni.GetAljn(ctx).Value()) }), ) }
func TestAljbi2(t *testing.T) { ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil) // aljb2 is another alias bool type that implements the aljb default // interface. Run(t, ctx, `{ "type": "multi", "aljbi": {"type": "aljb2", "value": true} }`, TestValue(func(t *testing.T, v interface{}) { assert.NotNil(t, v.(*data.Multi).Aljbi.GetAljb(ctx)) assert.IsType(t, new(data.Aljb2), v.(*data.Multi).Aljbi) assert.Equal(t, true, v.(*data.Multi).Aljbi.GetAljb(ctx).Value()) }), ) }
func TestNode_SetValueZero(t *testing.T) { cb, n := data.Setup(t) test := func(t *testing.T, n *node.Node, m *data.Multi) { sstring, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/system", "string") assert.True(t, ok) facea, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "facea") assert.True(t, ok) faceb, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "faceb") assert.True(t, ok) c1 := node.NewNode() require.NoError(t, c1.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "c")) require.NoError(t, c1.AddToMap(cb.Ctx(), n.Map["mss"], "c", true)) require.NoError(t, c1.SetValueZero(cb.Ctx(), true, nil)) assert.False(t, n.Map["mss"].Map["c"].Missing) assert.True(t, n.Map["mss"].Map["c"].Null) assert.Nil(t, m.Mss["c"]) c1a := node.NewNode() require.NoError(t, c1a.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "d")) require.NoError(t, c1a.AddToMap(cb.Ctx(), n.Map["mss"], "d", true)) require.NoError(t, c1a.SetValueZero(cb.Ctx(), false, nil)) assert.False(t, n.Map["mss"].Map["d"].Missing) assert.False(t, n.Map["mss"].Map["d"].Null) assert.NotNil(t, m.Mss["d"]) assert.Equal(t, "", m.Mss["d"].Value()) c2 := node.NewNode() require.NoError(t, c2.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "c")) require.NoError(t, c2.AddToMap(cb.Ctx(), n.Map["mm"], "c", true)) require.NoError(t, c2.SetValueZero(cb.Ctx(), true, nil)) assert.Nil(t, m.Mm["c"]) c2a := node.NewNode() require.NoError(t, c2a.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "d")) require.NoError(t, c2a.AddToMap(cb.Ctx(), n.Map["mm"], "d", true)) require.NoError(t, c2a.SetValueZero(cb.Ctx(), false, nil)) assert.NotNil(t, m.Mm["d"]) assert.Equal(t, "", m.Mm["d"].Js) require.NoError(t, n.Map["i"].SetValueZero(cb.Ctx(), true, faceb)) assert.Nil(t, m.I) assert.IsType(t, &data.Faceb{}, m.I) require.NoError(t, n.Map["sri"].SetValueZero(cb.Ctx(), false, facea)) assert.NotNil(t, m.Sri) assert.IsType(t, &data.Facea{}, m.Sri) c3 := node.NewNode() require.NoError(t, c3.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 4)) require.NoError(t, c3.AddToArray(cb.Ctx(), n.Map["ass"], 4, true)) require.NoError(t, c3.SetValueZero(cb.Ctx(), true, nil)) assert.Nil(t, m.Ass[4]) c3a := node.NewNode() require.NoError(t, c3a.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 5)) require.NoError(t, c3a.AddToArray(cb.Ctx(), n.Map["ass"], 5, true)) require.NoError(t, c3a.SetValueZero(cb.Ctx(), false, nil)) assert.NotNil(t, m.Ass[5]) c4 := node.NewNode() require.NoError(t, c4.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 2)) require.NoError(t, c4.AddToArray(cb.Ctx(), n.Map["am"], 2, true)) require.NoError(t, c4.SetValueZero(cb.Ctx(), true, nil)) assert.Nil(t, m.Am[2]) c4a := node.NewNode() require.NoError(t, c4a.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 3)) require.NoError(t, c4a.AddToArray(cb.Ctx(), n.Map["am"], 3, true)) require.NoError(t, c4a.SetValueZero(cb.Ctx(), false, nil)) assert.NotNil(t, m.Am[3]) c5 := node.NewNode() require.NoError(t, c5.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 3)) require.NoError(t, c5.AddToArray(cb.Ctx(), n.Map["anri"], 3, true)) require.NoError(t, c5.SetValueZero(cb.Ctx(), true, sstring)) assert.Nil(t, m.Anri[3]) assert.IsType(t, system.NewString(""), m.Anri[3]) c5a := node.NewNode() require.NoError(t, c5a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 4)) require.NoError(t, c5a.AddToArray(cb.Ctx(), n.Map["anri"], 4, true)) require.NoError(t, c5a.SetValueZero(cb.Ctx(), false, sstring)) assert.NotNil(t, m.Anri[4]) assert.IsType(t, system.NewString(""), m.Anri[4]) c6 := node.NewNode() require.NoError(t, c6.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 5)) require.NoError(t, c6.AddToArray(cb.Ctx(), n.Map["anri"], 5, true)) require.NoError(t, c6.SetValueZero(cb.Ctx(), true, facea)) assert.Nil(t, m.Anri[5]) assert.IsType(t, &data.Facea{}, m.Anri[5]) c6a := node.NewNode() require.NoError(t, c6a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 6)) require.NoError(t, c6a.AddToArray(cb.Ctx(), n.Map["anri"], 6, true)) require.NoError(t, c6a.SetValueZero(cb.Ctx(), false, facea)) assert.NotNil(t, m.Anri[6]) assert.IsType(t, &data.Facea{}, m.Anri[6]) c7 := node.NewNode() require.NoError(t, c7.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "d")) require.NoError(t, c7.AddToMap(cb.Ctx(), n.Map["mnri"], "d", true)) require.NoError(t, c7.SetValueZero(cb.Ctx(), true, sstring)) assert.Nil(t, m.Mnri["d"]) assert.IsType(t, system.NewString(""), m.Mnri["d"]) c7a := node.NewNode() require.NoError(t, c7a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "e")) require.NoError(t, c7a.AddToMap(cb.Ctx(), n.Map["mnri"], "e", true)) require.NoError(t, c7a.SetValueZero(cb.Ctx(), false, sstring)) assert.NotNil(t, m.Mnri["e"]) assert.IsType(t, system.NewString(""), m.Mnri["e"]) c8 := node.NewNode() require.NoError(t, c8.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "f")) require.NoError(t, c8.AddToMap(cb.Ctx(), n.Map["mnri"], "f", true)) require.NoError(t, c8.SetValueZero(cb.Ctx(), true, facea)) assert.Nil(t, m.Mnri["f"]) assert.IsType(t, &data.Facea{}, m.Mnri["f"]) c8a := node.NewNode() require.NoError(t, c8a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "g")) require.NoError(t, c8a.AddToMap(cb.Ctx(), n.Map["mnri"], "g", true)) require.NoError(t, c8a.SetValueZero(cb.Ctx(), false, facea)) assert.NotNil(t, m.Mnri["g"]) assert.IsType(t, &data.Facea{}, m.Mnri["g"]) } data.Run(t, n, n.Value.(*data.Multi), test) }