github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/array_diff_test.go (about)

     1  package diff
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestArrayDiff(t *testing.T) {
    10  	listA := []string{"abc", "def", "ghi", "jkl"}
    11  	added, deleted, common := fromStringArray(listA).DiffsTo(listA)
    12  	require.Equal(t, []string{}, added)
    13  	require.Equal(t, []string{}, deleted)
    14  	require.ElementsMatch(t, listA, common)
    15  
    16  	listB := []string{"abc", "ghi", "jkl", "xyz", "fgh"}
    17  	added, deleted, common = fromStringArray(listA).DiffsTo(listB)
    18  	require.ElementsMatch(t, []string{"xyz", "fgh"}, added)
    19  	require.ElementsMatch(t, []string{"def"}, deleted)
    20  	require.ElementsMatch(t, []string{"abc", "ghi", "jkl"}, common)
    21  }
    22  
    23  func TestMapDiff(t *testing.T) {
    24  	mapA := map[string]interface{}{"abc": 1, "def": 2, "ghi": 3, "jkl": 4}
    25  	added, deleted, common := fromStringMap(mapA).DiffsTo(mapA)
    26  	require.Equal(t, map[string]interface{}{}, added)
    27  	require.Equal(t, map[string]interface{}{}, deleted)
    28  
    29  	commonDiffs := map[string]interface{}{"abc": Pair{1, 1}, "def": Pair{2, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
    30  	require.Equal(t, commonDiffs, common)
    31  
    32  	mapB := map[string]interface{}{"abc": 2, "ghi": 3, "jkl": 4, "xyz": 5, "fgh": 6}
    33  	added, deleted, common = fromStringMap(mapA).DiffsTo(mapB)
    34  	require.Equal(t, map[string]interface{}{"xyz": 5, "fgh": 6}, added)
    35  	require.Equal(t, map[string]interface{}{"def": 2}, deleted)
    36  	commonDiffs = map[string]interface{}{"abc": Pair{1, 2}, "ghi": Pair{3, 3}, "jkl": Pair{4, 4}}
    37  	require.Equal(t, commonDiffs, common)
    38  }