miller/pkg/mlrval/mlrval_collections_test.go
John Kerl 2bec10fc54
Fix govet lint findings; fix masked unset-on-array error path (#2129)
* plans/lintfixes.md

* plans/lintfixes.md

* Fix remaining govet lint findings

- Rename MarshalJSON -> FormatAsJSON on Mlrval and Mlrmap (govet
  stdmethods): the methods shadowed json.Marshaler with an
  incompatible signature.
- Remove unreachable return after exhaustive if-else in
  pkg/mlrval/mlrval_collections.go (govet unreachable).
- Update plans/lintfixes.md with current status: 84 findings remain
  (50 errcheck, 34 staticcheck).

Part of #2109.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Return nil on successful single-index array unset

removeIndexedOnArray removed the element on the in-bounds path but
then fell through to return an "array index out of bounds for unset"
error, so the success path never returned nil. Callers currently
ignore the error, which masked this; return nil on success so that
upcoming errcheck fixes can propagate the error meaningfully. This
matches removeIndexedOnMap, which returns nil on success.

Add unit tests for RemoveIndexed on arrays.

Part of #2109.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 10:31:08 -04:00

44 lines
1.2 KiB
Go

package mlrval
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRemoveIndexedOnArrayInBounds(t *testing.T) {
array := FromArray([]*Mlrval{FromInt(10), FromInt(20), FromInt(30)})
err := array.RemoveIndexed([]*Mlrval{FromInt(2)})
assert.Nil(t, err)
elements := array.GetArray()
assert.Equal(t, 2, len(elements))
assert.Equal(t, int64(10), elements[0].intf.(int64))
assert.Equal(t, int64(30), elements[1].intf.(int64))
}
func TestRemoveIndexedOnArrayNegativeIndex(t *testing.T) {
array := FromArray([]*Mlrval{FromInt(10), FromInt(20), FromInt(30)})
// Negative indices are aliased from the end, so -1 removes the last element.
err := array.RemoveIndexed([]*Mlrval{FromInt(-1)})
assert.Nil(t, err)
elements := array.GetArray()
assert.Equal(t, 2, len(elements))
assert.Equal(t, int64(10), elements[0].intf.(int64))
assert.Equal(t, int64(20), elements[1].intf.(int64))
}
func TestRemoveIndexedOnArrayOutOfBounds(t *testing.T) {
array := FromArray([]*Mlrval{FromInt(10), FromInt(20), FromInt(30)})
err := array.RemoveIndexed([]*Mlrval{FromInt(4)})
assert.NotNil(t, err)
assert.Equal(t, 3, len(array.GetArray()))
err = array.RemoveIndexed([]*Mlrval{FromInt(0)})
assert.NotNil(t, err)
assert.Equal(t, 3, len(array.GetArray()))
}