diff options
| author | Clement Michaud <clement.michaud34@gmail.com> | 2019-11-17 11:47:07 +0100 |
|---|---|---|
| committer | Clément Michaud <clement.michaud34@gmail.com> | 2019-11-17 16:30:33 +0100 |
| commit | 3b2d733367c88621e4178301f2bcb4bc03613eee (patch) | |
| tree | 41ac41fc5b6cece04db85a08bfa7c32a022f7354 /internal/configuration/schema/validator_test.go | |
| parent | a06b69dd458e756f1a3d6867eb5b9f54560e2ee1 (diff) | |
Move source code into internal directory to follow standard project layout.
https://github.com/golang-standards/project-layout
Diffstat (limited to 'internal/configuration/schema/validator_test.go')
| -rw-r--r-- | internal/configuration/schema/validator_test.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/configuration/schema/validator_test.go b/internal/configuration/schema/validator_test.go new file mode 100644 index 000000000..5490a9ead --- /dev/null +++ b/internal/configuration/schema/validator_test.go @@ -0,0 +1,81 @@ +package schema_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/clems4ever/authelia/internal/configuration/schema" +) + +type TestNestedStruct struct { + MustBe5 int +} + +func (tns *TestNestedStruct) Validate(validator *schema.StructValidator) { + if tns.MustBe5 != 5 { + validator.Push(fmt.Errorf("MustBe5 must be 5")) + } +} + +type TestStruct struct { + MustBe10 int + NotEmpty string + SetDefault string + Nested TestNestedStruct + Nested2 TestNestedStruct + NilPtr *int + NestedPtr *TestNestedStruct +} + +func (ts *TestStruct) Validate(validator *schema.StructValidator) { + if ts.MustBe10 != 10 { + validator.Push(fmt.Errorf("MustBe10 must be 10")) + } + + if ts.NotEmpty == "" { + validator.Push(fmt.Errorf("NotEmpty must not be empty")) + } + + if ts.SetDefault == "" { + ts.SetDefault = "xyz" + } +} + +func TestValidator(t *testing.T) { + validator := schema.NewValidator() + + s := TestStruct{ + MustBe10: 5, + NotEmpty: "", + NestedPtr: &TestNestedStruct{}, + } + + err := validator.Validate(&s) + if err != nil { + panic(err) + } + + errs := validator.Errors() + assert.Equal(t, 4, len(errs)) + + assert.Equal(t, 2, len(errs["root"])) + assert.ElementsMatch(t, []error{ + fmt.Errorf("MustBe10 must be 10"), + fmt.Errorf("NotEmpty must not be empty")}, errs["root"]) + + assert.Equal(t, 1, len(errs["root.Nested"])) + assert.ElementsMatch(t, []error{ + fmt.Errorf("MustBe5 must be 5")}, errs["root.Nested"]) + + assert.Equal(t, 1, len(errs["root.Nested2"])) + assert.ElementsMatch(t, []error{ + fmt.Errorf("MustBe5 must be 5")}, errs["root.Nested2"]) + + assert.Equal(t, 1, len(errs["root.NestedPtr"])) + assert.ElementsMatch(t, []error{ + fmt.Errorf("MustBe5 must be 5")}, errs["root.NestedPtr"]) + + assert.Equal(t, "xyz", s.SetDefault) +} |
