Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Sanft <[email protected]>
  • Loading branch information
msanft committed Oct 20, 2023
1 parent 9ea28a4 commit e67af11
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions internal/validation/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func TestNewValidationErrorSingleFieldPtr(t *testing.T) {
require.Contains(t, err.Error(), fmt.Sprintf("validating pointerField: %s", assert.AnError))
}

func TestNewValidationErrorSingleFieldDoublePtr(t *testing.T) {
intp := new(int)
st := &ErrorTestDoc{
ExportedField: "abc",
OtherField: 42,
DoublePointerField: &intp,
}

err := NewValidationError(st, &st.DoublePointerField, assert.AnError)
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("validating doublePointerField: %s", assert.AnError))
}

func TestNewValidationErrorSingleFieldInexistent(t *testing.T) {
st := &ErrorTestDoc{
ExportedField: "abc",
Expand Down Expand Up @@ -98,15 +111,84 @@ func TestNewValidationErrorNestedFieldPtr(t *testing.T) {
require.Contains(t, err.Error(), fmt.Sprintf("validating nestedPointerField.otherField: %s", assert.AnError))
}

func TestNewValidationErrorNestedNestedField(t *testing.T) {
st := &ErrorTestDoc{
ExportedField: "abc",
OtherField: 42,
NestedField: NestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
NestedField: NestedNestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
},
},
}

err := NewValidationError(st, &st.NestedField.NestedField.OtherField, assert.AnError)
t.Log(err)
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("validating nestedField.nestedField.otherField: %s", assert.AnError))
}

func TestNewValidationErrorNestedNestedFieldPtr(t *testing.T) {
st := &ErrorTestDoc{
ExportedField: "abc",
OtherField: 42,
NestedField: NestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
NestedPointerField: &NestedNestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
},
},
}

err := NewValidationError(st, &st.NestedField.NestedField.OtherField, assert.AnError)
t.Log(err)
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("validating nestedField.nestedField.otherField: %s", assert.AnError))
}

func TestNewValidationErrorNestedPtrNestedFieldPtr(t *testing.T) {
st := &ErrorTestDoc{
ExportedField: "abc",
OtherField: 42,
NestedPointerField: &NestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
NestedPointerField: &NestedNestedErrorTestDoc{
ExportedField: "nested",
OtherField: 123,
},
},
}

err := NewValidationError(st, &st.NestedField.NestedField.OtherField, assert.AnError)
t.Log(err)
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("validating nestedField.nestedField.otherField: %s", assert.AnError))
}

type ErrorTestDoc struct {
ExportedField string `json:"exportedField" yaml:"exportedField"`
OtherField int `json:"otherField" yaml:"otherField"`
PointerField *int `json:"pointerField" yaml:"pointerField"`
DoublePointerField **int `json:"doublePointerField" yaml:"doublePointerField"`
NestedField NestedErrorTestDoc `json:"nestedField" yaml:"nestedField"`
NestedPointerField *NestedErrorTestDoc `json:"nestedPointerField" yaml:"nestedPointerField"`
}

type NestedErrorTestDoc struct {
ExportedField string `json:"exportedField" yaml:"exportedField"`
OtherField int `json:"otherField" yaml:"otherField"`
PointerField *int `json:"pointerField" yaml:"pointerField"`
NestedField NestedNestedErrorTestDoc `json:"nestedField" yaml:"nestedField"`
NestedPointerField *NestedNestedErrorTestDoc `json:"nestedPointerField" yaml:"nestedPointerField"`
}

type NestedNestedErrorTestDoc struct {
ExportedField string `json:"exportedField" yaml:"exportedField"`
OtherField int `json:"otherField" yaml:"otherField"`
PointerField *int `json:"pointerField" yaml:"pointerField"`
Expand Down

0 comments on commit e67af11

Please sign in to comment.