Skip to content

Commit

Permalink
Add ValidationNotInEnumError
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Aug 19, 2024
1 parent cbb64ca commit 3fd6371
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions kaitai/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ func (e ValidationNotAnyOfError) Error() string {
)
}

// ValidationNotInEnumError signals validation failure: we required "Actual" value
// to be in the enum, but it turned out that it's not.
type ValidationNotInEnumError struct {
actual interface{}
locationInfo
}

// NewValidationNotInEnumError creates a new ValidationNotInEnumError instance.
func NewValidationNotInEnumError(actual interface{}, io *Stream, srcPath string) ValidationNotInEnumError {
return ValidationNotInEnumError{
actual,
newLocationInfo(io, srcPath),
}
}

// Actual is a getter of the actual value associated with the validation error.
func (e ValidationNotInEnumError) Actual() interface{} { return e.actual }

func (e ValidationNotInEnumError) Error() string {
return e.msgWithLocation(
validationFailedMsg(
fmt.Sprintf("not in the enum, got %v", e.actual),
),
)
}

// ValidationExprError signals validation failure: we required "Actual" value
// to match the expression, but it turned out that it doesn't.
type ValidationExprError struct {
Expand Down
22 changes: 22 additions & 0 deletions kaitai/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestValidationFailedError_interface(t *testing.T) {
{"ValidationLessThanError", NewValidationLessThanError(2, actual, io, srcPath)},
{"ValidationGreaterThanError", NewValidationGreaterThanError(2, actual, io, srcPath)},
{"ValidationNotAnyOfError", NewValidationNotAnyOfError(actual, io, srcPath)},
{"ValidationNotInEnumError", NewValidationNotInEnumError(actual, io, srcPath)},
{"ValidationExprError", NewValidationExprError(actual, io, srcPath)},
}
for _, tt := range tests {
Expand Down Expand Up @@ -270,6 +271,27 @@ func TestValidationNotAnyOfError_Error(t *testing.T) {
}
}

func TestValidationNotInEnumError_Error(t *testing.T) {
io := NewStream(bytes.NewReader([]byte("test")))
tests := []struct {
name string
e ValidationNotInEnumError
want string
}{
{
"integer", NewValidationNotInEnumError(-42, io, "/seq/0"),
"/seq/0: at pos 0: validation failed: not in the enum, got -42",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.Error(); got != tt.want {
t.Errorf("ValidationNotInEnumError.Error() = %q, want %q", got, tt.want)
}
})
}
}

func TestValidationExprError_Error(t *testing.T) {
io := NewStream(bytes.NewReader([]byte("test")))
tests := []struct {
Expand Down

0 comments on commit 3fd6371

Please sign in to comment.