Files
gitea/modules/util/diff_slice_test.go
T
silverwind 8abda62312 test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are
around 5% faster while being 7% smaller. No non-test code is touched.
2026-10-24 13:48:19 +00:00

46 lines
1.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDiffSliceBasic(t *testing.T) {
for _, testCase := range []struct {
name string
oldSlice, newSlice []int
expectedAdded, expectedRemoved []int
}{
// Typical integer cases
{"additions", []int{1, 2}, []int{1, 2, 3}, []int{3}, nil},
{"removals", []int{1, 2, 3}, []int{1, 2}, nil, []int{3}},
{"no changes", []int{1, 2}, []int{1, 2}, nil, nil},
{"empty slices", []int{}, []int{}, nil, nil},
{"overlapping elements", []int{1, 2, 4}, []int{2, 3, 4}, []int{3}, []int{1}},
{"order and duplicates", []int{1, 2, 2, 3}, []int{2, 4, 2, 5}, []int{4, 5}, []int{1, 3}},
// Test case from issue: newSlice contains [4, 4, 5] and oldSlice is [1]
// added should return [4, 5], not [4, 4, 5]
{"deduplicates added", []int{1}, []int{4, 4, 5}, []int{4, 5}, []int{1}},
{"deduplicates removed", []int{1, 1, 2}, []int{3}, []int{3}, []int{1, 2}},
{"deduplicates both", []int{1, 1, 2, 2}, []int{3, 3, 4, 4}, []int{3, 4}, []int{1, 2}},
} {
t.Run(testCase.name, func(t *testing.T) {
added, removed := DiffSlice(testCase.oldSlice, testCase.newSlice)
if testCase.expectedAdded == nil {
assert.Empty(t, added)
} else {
assert.Equal(t, testCase.expectedAdded, added)
}
if testCase.expectedRemoved == nil {
assert.Empty(t, removed)
} else {
assert.Equal(t, testCase.expectedRemoved, removed)
}
})
}
}