|
| 1 | +package resp_assertions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value" |
| 7 | +) |
| 8 | + |
| 9 | +type ConfigGetDirResponseAssertion struct { |
| 10 | + ExpectedDirValue string |
| 11 | +} |
| 12 | + |
| 13 | +func NewConfigGetDirResponseAssertion(expectedDirValue string) RESPAssertion { |
| 14 | + return ConfigGetDirResponseAssertion{ExpectedDirValue: expectedDirValue} |
| 15 | +} |
| 16 | + |
| 17 | +func (a ConfigGetDirResponseAssertion) Run(value resp_value.Value) error { |
| 18 | + arrayTypeAssertion := DataTypeAssertion{ExpectedType: resp_value.ARRAY} |
| 19 | + |
| 20 | + if err := arrayTypeAssertion.Run(value); err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + |
| 24 | + if len(value.Array()) != 2 { |
| 25 | + return fmt.Errorf("Expected 2 elements in array, got %d (%s)", len(value.Array()), value.FormattedString()) |
| 26 | + } |
| 27 | + |
| 28 | + firstElement := value.Array()[0] |
| 29 | + secondElement := value.Array()[1] |
| 30 | + |
| 31 | + if firstElement.Type != resp_value.BULK_STRING { |
| 32 | + return fmt.Errorf("Expected element #1 to be a bulk string, got %s", firstElement.Type) |
| 33 | + } |
| 34 | + if firstElement.String() != "dir" { |
| 35 | + return fmt.Errorf("Expected element #1 to be %q, got %q", "dir", firstElement.String()) |
| 36 | + } |
| 37 | + |
| 38 | + if secondElement.Type != resp_value.BULK_STRING { |
| 39 | + return fmt.Errorf("Expected element #2 to be a bulk string, got %s", secondElement.Type) |
| 40 | + } |
| 41 | + if secondElement.String() != a.ExpectedDirValue && secondElement.String() != a.ExpectedDirValue+"/" { |
| 42 | + return fmt.Errorf("Expected element #2 to be %q, got %q", a.ExpectedDirValue, secondElement.String()) |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
0 commit comments