-
Notifications
You must be signed in to change notification settings - Fork 9
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 #141 from codecrafters-io/CC-1057-rdb-ref
CC-1057: Refactor RDB Pesrsistence stages to use our RESP framework.
- Loading branch information
Showing
15 changed files
with
524 additions
and
346 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
dist | ||
|
||
# Tests generate this | ||
internal/dump.rdb | ||
**.rdb | ||
|
||
vendor/ | ||
.history/ | ||
|
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
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
54 changes: 54 additions & 0 deletions
54
internal/resp_assertions/unordered_string_array_assertion.go
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,54 @@ | ||
package resp_assertions | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
|
||
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value" | ||
) | ||
|
||
// UnorderedStringArrayAssertion : Order of the actual and expected values doesn't matter. | ||
// We sort the expected and actual values before comparing them. | ||
type UnorderedStringArrayAssertion struct { | ||
ExpectedValue []string | ||
} | ||
|
||
func NewUnorderedStringArrayAssertion(expectedValue []string) RESPAssertion { | ||
return UnorderedStringArrayAssertion{ExpectedValue: expectedValue} | ||
} | ||
|
||
func (a UnorderedStringArrayAssertion) 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()) | ||
} | ||
|
||
actualElementStringArray := make([]string, len(value.Array())) | ||
for i := range value.Array() { | ||
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) | ||
} | ||
actualElementStringArray[i] = value.Array()[i].String() | ||
} | ||
|
||
expectedValueArrayForPrinting, _ := json.Marshal(a.ExpectedValue) | ||
expectedValueStringArray := make([]string, len(a.ExpectedValue)) | ||
copy(expectedValueStringArray, a.ExpectedValue) | ||
sort.Strings(actualElementStringArray) | ||
sort.Strings(expectedValueStringArray) | ||
|
||
for i, expectedValue := range expectedValueStringArray { | ||
actualElement := actualElementStringArray[i] | ||
|
||
if actualElement != expectedValue { | ||
return fmt.Errorf("Expected: %v (in any order), got %v", string(expectedValueArrayForPrinting), value.FormattedString()) | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.