Skip to content

Commit

Permalink
feat: Add ErrorAssertion for comparing error values in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed Jun 12, 2024
1 parent 7087489 commit 9e28d76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/resp/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (v *Value) Integer() int {
return v.integer
}

func (v *Value) Error() string {
return string(v.String())
}

func (v *Value) FormattedString() string {
switch v.Type {
case SIMPLE_STRING:
Expand Down
27 changes: 27 additions & 0 deletions internal/resp_assertions/error_assertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package resp_assertions

import (
"fmt"

resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
)

type ErrorAssertion struct {
ExpectedValue string
}

func NewErrorAssertion(expectedValue string) RESPAssertion {
return ErrorAssertion{ExpectedValue: expectedValue}
}

func (a ErrorAssertion) Run(value resp_value.Value) error {
if value.Type != resp_value.ERROR {
return fmt.Errorf("Expected error, got %s", value.Type)
}

if value.Error() != a.ExpectedValue {
return fmt.Errorf("Expected %s, got %s", a.ExpectedValue, value.Error())
}

return nil
}

0 comments on commit 9e28d76

Please sign in to comment.