Skip to content

Commit 8b6b21c

Browse files
Merge pull request #232 from codecrafters-io/array-elements-assertion-fix
Fix Bug: ArrayElementsAssertion panics if value is an empty array
2 parents 8f6675e + d97fbd9 commit 8b6b21c

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

internal/resp_assertions/array_elements_assertion.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ArrayElementAssertionSpecification struct {
1515
}
1616

1717
type ArrayElementsAssertion struct {
18-
ArrayElementAssertionSpecification []ArrayElementAssertionSpecification
18+
ArrayElementAssertionSpecifications []ArrayElementAssertionSpecification
1919
}
2020

2121
func (a ArrayElementsAssertion) Run(value resp_value.Value) error {
@@ -27,11 +27,12 @@ func (a ArrayElementsAssertion) Run(value resp_value.Value) error {
2727

2828
array := value.Array()
2929

30-
if len(array) == 0 {
30+
// If the specification is empty, panic
31+
if len(a.ArrayElementAssertionSpecifications) == 0 {
3132
panic("Codecrafters Internal Error - ArrayElementsAssertion called with empty specifications")
3233
}
3334

34-
largestIndex := slices.MaxFunc(a.ArrayElementAssertionSpecification, func(a, b ArrayElementAssertionSpecification) int {
35+
largestIndex := slices.MaxFunc(a.ArrayElementAssertionSpecifications, func(a, b ArrayElementAssertionSpecification) int {
3536
return a.ArrayElementAssertion.Index - b.ArrayElementAssertion.Index
3637
}).ArrayElementAssertion.Index
3738

@@ -44,13 +45,13 @@ func (a ArrayElementsAssertion) Run(value resp_value.Value) error {
4445
}
4546

4647
// Sort the indexes so the assertion runs serially
47-
slices.SortFunc(a.ArrayElementAssertionSpecification, func(aea1, aea2 ArrayElementAssertionSpecification) int {
48+
slices.SortFunc(a.ArrayElementAssertionSpecifications, func(aea1, aea2 ArrayElementAssertionSpecification) int {
4849
return aea1.ArrayElementAssertion.Index - aea2.ArrayElementAssertion.Index
4950
})
5051

5152
multiAssertion := MultiAssertion{}
5253

53-
for _, arrayElementAssertionSpecification := range a.ArrayElementAssertionSpecification {
54+
for _, arrayElementAssertionSpecification := range a.ArrayElementAssertionSpecifications {
5455
multiAssertion.AssertionSpecifications = append(multiAssertion.AssertionSpecifications, AssertionSpecification{
5556
Assertion: arrayElementAssertionSpecification.ArrayElementAssertion,
5657
PreAssertionHook: arrayElementAssertionSpecification.PreAssertionHook,

internal/test_cases/acl_getuser_test_case.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (t *AclGetuserTestCase) RunForFlagsTemplateOnly(client *instrumented_resp_c
2727
Command: "ACL",
2828
Args: []string{"GETUSER", t.Username},
2929
Assertion: resp_assertions.ArrayElementsAssertion{
30-
ArrayElementAssertionSpecification: []resp_assertions.ArrayElementAssertionSpecification{
30+
ArrayElementAssertionSpecifications: []resp_assertions.ArrayElementAssertionSpecification{
3131
{
3232
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
3333
Index: 0,
@@ -83,7 +83,7 @@ func (t *AclGetuserTestCase) Run(client *instrumented_resp_connection.Instrument
8383
func (t *AclGetuserTestCase) addAssertionForFlags(assertion *resp_assertions.ArrayElementsAssertion, logger *logger.Logger) {
8484
// Assert for flags
8585
// Assertion for "flags" as the first element
86-
assertion.ArrayElementAssertionSpecification = append(assertion.ArrayElementAssertionSpecification,
86+
assertion.ArrayElementAssertionSpecifications = append(assertion.ArrayElementAssertionSpecifications,
8787
resp_assertions.ArrayElementAssertionSpecification{
8888
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
8989
Index: 0,
@@ -104,7 +104,7 @@ func (t *AclGetuserTestCase) addAssertionForFlags(assertion *resp_assertions.Arr
104104
})
105105

106106
// Assert the type of 2nd element to be array
107-
assertion.ArrayElementAssertionSpecification = append(assertion.ArrayElementAssertionSpecification,
107+
assertion.ArrayElementAssertionSpecifications = append(assertion.ArrayElementAssertionSpecifications,
108108
resp_assertions.ArrayElementAssertionSpecification{
109109
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
110110
Index: 1,
@@ -158,7 +158,7 @@ func (t *AclGetuserTestCase) addAssertionForFlags(assertion *resp_assertions.Arr
158158
})
159159
}
160160

161-
assertion.ArrayElementAssertionSpecification = append(assertion.ArrayElementAssertionSpecification,
161+
assertion.ArrayElementAssertionSpecifications = append(assertion.ArrayElementAssertionSpecifications,
162162
resp_assertions.ArrayElementAssertionSpecification{
163163
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
164164
Index: 1,
@@ -168,7 +168,7 @@ func (t *AclGetuserTestCase) addAssertionForFlags(assertion *resp_assertions.Arr
168168
}
169169

170170
func (t *AclGetuserTestCase) addAssertionForPasswords(assertion *resp_assertions.ArrayElementsAssertion, logger *logger.Logger) {
171-
assertion.ArrayElementAssertionSpecification = append(assertion.ArrayElementAssertionSpecification,
171+
assertion.ArrayElementAssertionSpecifications = append(assertion.ArrayElementAssertionSpecifications,
172172
resp_assertions.ArrayElementAssertionSpecification{
173173
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
174174
Index: 2,
@@ -196,7 +196,7 @@ func (t *AclGetuserTestCase) addAssertionForPasswords(assertion *resp_assertions
196196

197197
}
198198

199-
assertion.ArrayElementAssertionSpecification = append(assertion.ArrayElementAssertionSpecification,
199+
assertion.ArrayElementAssertionSpecifications = append(assertion.ArrayElementAssertionSpecifications,
200200
resp_assertions.ArrayElementAssertionSpecification{
201201
ArrayElementAssertion: resp_assertions.ArrayElementAssertion{
202202
Index: 3,

0 commit comments

Comments
 (0)