Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ func (a *Array) Value(index int) *Value {
return newValue(opChain, nil)
}

if index < 0 {
index = len(a.value) + index
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Expand Down Expand Up @@ -208,6 +211,9 @@ func (a *Array) HasValue(index int, value interface{}) *Array {
return a
}

if index < 0 {
index = len(a.value) + index
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Expand Down Expand Up @@ -262,6 +268,9 @@ func (a *Array) NotHasValue(index int, value interface{}) *Array {
return a
}

if index < 0 {
index = len(a.value) + index
}
if index < 0 || index >= len(a.value) {
opChain.fail(AssertionFailure{
Type: AssertInRange,
Expand Down
44 changes: 43 additions & 1 deletion array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,21 @@ func TestArray_Getters(t *testing.T) {
innerValue.chain.assert(t, success)
})

t.Run("value out of range", func(t *testing.T) {
t.Run("value negative", func(t *testing.T) {
reporter := newMockReporter(t)

data := []interface{}{"foo", 123.0}

value := NewArray(reporter, data)

innerValue := value.Value(-1)
assert.Equal(t, 123.0, innerValue.Raw())

value.chain.assert(t, success)
innerValue.chain.assert(t, success)
})

t.Run("value out of range (positive)", func(t *testing.T) {
reporter := newMockReporter(t)

data := []interface{}{"foo", 123.0}
Expand All @@ -364,6 +378,20 @@ func TestArray_Getters(t *testing.T) {
value.chain.assert(t, failure)
innerValue.chain.assert(t, failure)
})

t.Run("value out of range (negative)", func(t *testing.T) {
reporter := newMockReporter(t)

data := []interface{}{"foo", 123.0}

value := NewArray(reporter, data)

innerValue := value.Value(-3)
assert.NotNil(t, innerValue)

value.chain.assert(t, failure)
innerValue.chain.assert(t, failure)
})
}

func TestArray_IsEmpty(t *testing.T) {
Expand Down Expand Up @@ -1426,6 +1454,20 @@ func TestArray_HasValue(t *testing.T) {
wantHasValue: success,
wantNotHasValue: failure,
},
{
name: "negative index check",
array: []interface{}{
123,
[]interface{}{"456", 789},
map[string]interface{}{
"a": "b",
},
},
index: -3,
value: 123,
wantHasValue: success,
wantNotHasValue: failure,
},
{
name: "out of bounds check",
array: []interface{}{
Expand Down
Loading