-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from codecrafters-io/fix-rdb-persistence-stage-1
Add StringArrayAssertion and update test cases
- Loading branch information
Showing
6 changed files
with
228 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package resp_assertions | ||
|
||
import ( | ||
"fmt" | ||
|
||
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value" | ||
) | ||
|
||
type StringArrayAssertion struct { | ||
ExpectedValue []string | ||
} | ||
|
||
func NewStringArrayAssertion(expectedValue []string) RESPAssertion { | ||
return StringArrayAssertion{ExpectedValue: expectedValue} | ||
} | ||
|
||
func (a StringArrayAssertion) Run(value resp_value.Value) error { | ||
if value.Type != resp_value.ARRAY { | ||
return fmt.Errorf("Expected an array, got %s", value.Type) | ||
} | ||
|
||
if len(value.Array()) != len(a.ExpectedValue) { | ||
return fmt.Errorf("Expected %d elements in array, got %d (%s)", len(a.ExpectedValue), len(value.Array()), value.FormattedString()) | ||
} | ||
|
||
for i, expectedValue := range a.ExpectedValue { | ||
actualElement := value.Array()[i] | ||
|
||
if actualElement.Type != resp_value.BULK_STRING && actualElement.Type != resp_value.SIMPLE_STRING { | ||
return fmt.Errorf("Expected element #%d to be a string, got %s", i+1, actualElement.Type) | ||
} | ||
|
||
if actualElement.String() != expectedValue { | ||
return fmt.Errorf("Expected element #%d to be %q, got %q", i+1, expectedValue, actualElement.String()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.