|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
7 | 7 | "os"
|
| 8 | + "reflect" |
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/ggicci/owl"
|
@@ -61,13 +62,14 @@ func (s *BuildResolverTreeTestSuite) Test_0_Lookup_IsLeaf() {
|
61 | 62 | assert := assert.New(s.T())
|
62 | 63 | for _, expected := range s.expected {
|
63 | 64 | resolver := s.tree.Lookup(expected.LookupPath)
|
64 |
| - assert.Nil(s.tree.Lookup("SomeNonExistingPath")) |
65 | 65 | assert.NotNil(resolver)
|
66 | 66 | assert.Equal(expected.Index, resolver.Index)
|
67 | 67 | assert.Equal(expected.NumFields, len(resolver.Children))
|
68 | 68 | assert.Equal(expected.Directives, resolver.Directives)
|
69 | 69 | assert.Equal(expected.Leaf, resolver.IsLeaf())
|
70 | 70 | }
|
| 71 | + |
| 72 | + assert.Nil(s.tree.Lookup("SomeNonExistingPath")) |
71 | 73 | }
|
72 | 74 |
|
73 | 75 | func (s *BuildResolverTreeTestSuite) Test_1_GetDirective() {
|
@@ -159,6 +161,70 @@ func TestNew_NormalCasesSuites(t *testing.T) {
|
159 | 161 | ))
|
160 | 162 | }
|
161 | 163 |
|
| 164 | +func TestNew_SkipFieldsHavingNoDirectives(t *testing.T) { |
| 165 | + type AnotherForm struct { |
| 166 | + Username string `owl:"form=username"` |
| 167 | + Password string `owl:"form=password"` |
| 168 | + Hidden string // should be ignored |
| 169 | + Pagination *Pagination // should not be ignored |
| 170 | + } |
| 171 | + |
| 172 | + suite.Run(t, NewBuildResolverTreeTestSuite( |
| 173 | + AnotherForm{}, |
| 174 | + []*expectedResolver{ |
| 175 | + { |
| 176 | + Index: -1, |
| 177 | + LookupPath: "", |
| 178 | + NumFields: 3, |
| 179 | + Directives: nil, |
| 180 | + Leaf: false, |
| 181 | + }, |
| 182 | + { |
| 183 | + Index: 0, |
| 184 | + LookupPath: "Username", |
| 185 | + Directives: []*owl.Directive{ |
| 186 | + owl.NewDirective("form", "username"), |
| 187 | + }, |
| 188 | + Leaf: true, |
| 189 | + }, |
| 190 | + { |
| 191 | + Index: 1, |
| 192 | + LookupPath: "Password", |
| 193 | + NumFields: 0, |
| 194 | + Directives: []*owl.Directive{ |
| 195 | + owl.NewDirective("form", "password"), |
| 196 | + }, |
| 197 | + Leaf: true, |
| 198 | + }, |
| 199 | + { |
| 200 | + Index: 3, // Pagination is the 4th field, Hidden is the 3rd field. |
| 201 | + LookupPath: "Pagination", |
| 202 | + NumFields: 2, |
| 203 | + Directives: nil, |
| 204 | + Leaf: false, |
| 205 | + }, |
| 206 | + { |
| 207 | + Index: 0, |
| 208 | + LookupPath: "Pagination.Page", |
| 209 | + NumFields: 0, |
| 210 | + Directives: []*owl.Directive{ |
| 211 | + owl.NewDirective("form", "page"), |
| 212 | + }, |
| 213 | + Leaf: true, |
| 214 | + }, |
| 215 | + { |
| 216 | + Index: 1, |
| 217 | + LookupPath: "Pagination.Size", |
| 218 | + NumFields: 0, |
| 219 | + Directives: []*owl.Directive{ |
| 220 | + owl.NewDirective("form", "size"), |
| 221 | + }, |
| 222 | + Leaf: true, |
| 223 | + }, |
| 224 | + }, |
| 225 | + )) |
| 226 | +} |
| 227 | + |
162 | 228 | func TestNew_WithNilType(t *testing.T) {
|
163 | 229 | _, err := owl.New(nil)
|
164 | 230 | assert.ErrorIs(t, err, owl.ErrUnsupportedType)
|
@@ -368,7 +434,6 @@ func TestResolve_UnexportedField(t *testing.T) {
|
368 | 434 | // Resolve.
|
369 | 435 | gotValue, err := resolver.Resolve()
|
370 | 436 | assert.NoError(t, err)
|
371 |
| - assert.NotNil(t, gotValue) |
372 | 437 | gotUser, ok := gotValue.Interface().(*User)
|
373 | 438 | assert.True(t, ok)
|
374 | 439 | assert.Equal(t, "owl", gotUser.Name)
|
@@ -465,6 +530,44 @@ func TestResolve_DirectiveRuntimeContext(t *testing.T) {
|
465 | 530 | assert.ErrorContains(t, err, "field is required")
|
466 | 531 | }
|
467 | 532 |
|
| 533 | +func TestResolve_NestedExecution(t *testing.T) { |
| 534 | + type User struct { |
| 535 | + Name string `owl:"env=OWL_TEST_NAME"` |
| 536 | + Role string `owl:"env=OWL_TEST_ROLE"` |
| 537 | + } |
| 538 | + |
| 539 | + type Request struct { |
| 540 | + // NOTE: Login will be created and updated by login directive, |
| 541 | + // and its fields will also be updated by env directive. |
| 542 | + Login User `owl:"login"` |
| 543 | + Action string `owl:"env=OWL_TEST_ACTION"` |
| 544 | + } |
| 545 | + |
| 546 | + expected := &Request{ |
| 547 | + Login: User{ |
| 548 | + Name: "owl", // set by env |
| 549 | + Role: "admin", // set by login |
| 550 | + }, |
| 551 | + Action: "addAccount", |
| 552 | + } |
| 553 | + |
| 554 | + ns := owl.NewNamespace() |
| 555 | + ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader)) |
| 556 | + ns.RegisterDirectiveExecutor("login", owl.DirectiveExecutorFunc(func(dr *owl.DirectiveRuntime) error { |
| 557 | + u := User{Name: "hello", Role: "admin"} |
| 558 | + dr.Value.Elem().Set(reflect.ValueOf(u)) |
| 559 | + return nil |
| 560 | + })) |
| 561 | + os.Setenv("OWL_TEST_NAME", "owl") |
| 562 | + os.Setenv("OWL_TEST_ACTION", "addAccount") |
| 563 | + |
| 564 | + resolver, err := owl.New(Request{}, owl.WithNamespace(ns)) |
| 565 | + assert.NoError(t, err) |
| 566 | + gotValue, err := resolver.Resolve() |
| 567 | + assert.NoError(t, err) |
| 568 | + assert.Equal(t, expected, gotValue.Interface().(*Request)) |
| 569 | +} |
| 570 | + |
468 | 571 | func TestIterate(t *testing.T) {
|
469 | 572 | resolver, err := owl.New(UserSignUpForm{})
|
470 | 573 | assert.NoError(t, err)
|
|
0 commit comments