1package mkcompare 2 3import ( 4 "github.com/google/go-cmp/cmp" 5 "reflect" 6 "testing" 7) 8 9func TestClassify(t *testing.T) { 10 tests := []struct { 11 name string 12 mLeft map[string]int 13 mRight map[string]int 14 wantLeft []string 15 wantCommon []string 16 wantRight []string 17 }{ 18 { 19 name: "one", 20 mLeft: map[string]int{"a": 1, "b": 2}, 21 mRight: map[string]int{"b": 3, "c": 4}, 22 wantLeft: []string{"a"}, 23 wantCommon: []string{"b"}, 24 wantRight: []string{"c"}, 25 }, 26 { 27 name: "two", 28 mLeft: map[string]int{"a": 1, "b": 2}, 29 mRight: map[string]int{"a": 3}, 30 wantLeft: []string{"b"}, 31 wantCommon: []string{"a"}, 32 wantRight: nil, 33 }, 34 } 35 for _, tt := range tests { 36 t.Run(tt.name, func(t *testing.T) { 37 gotLeft, gotCommon, gotRight := Classify(tt.mLeft, tt.mRight, func(_ string) bool { return true }) 38 if !reflect.DeepEqual(gotLeft, tt.wantLeft) { 39 t.Errorf("classify() gotLeft = %v, want %v", gotLeft, tt.wantLeft) 40 } 41 if !reflect.DeepEqual(gotCommon, tt.wantCommon) { 42 t.Errorf("classify() gotCommon = %v, want %v", gotCommon, tt.wantCommon) 43 } 44 if !reflect.DeepEqual(gotRight, tt.wantRight) { 45 t.Errorf("classify() gotRight = %v, want %v", gotRight, tt.wantRight) 46 } 47 }) 48 } 49} 50 51func Test_compareVariableValues(t *testing.T) { 52 tests := []struct { 53 name string 54 ref string 55 our string 56 sort bool 57 want_missing []string 58 want_extra []string 59 }{ 60 {name: "Same", ref: "x a b", our: "a b x", sort: true}, 61 {name: "diff1", ref: "a b c", our: "d a", sort: true, want_missing: []string{"b", "c"}, want_extra: []string{"d"}}, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 got_missing, got_extra := compareVariableValues(tt.ref, tt.our, tt.sort) 66 if diff := cmp.Diff(got_missing, tt.want_missing); diff != "" { 67 t.Errorf("missing items differ: %s", diff) 68 } 69 if diff := cmp.Diff(got_extra, tt.want_extra); diff != "" { 70 t.Errorf("extra items differ: %s", diff) 71 } 72 }) 73 } 74} 75 76func TestCompare(t *testing.T) { 77 refMod1 := MkModule{Type: "foo", Location: 1, Variables: map[string]string{"var1": "a", "var2": "b"}} 78 ourMod1 := MkModule{Type: "foo", Location: 3, Variables: map[string]string{"var1": "a", "var2": "c"}} 79 tests := []struct { 80 name string 81 refMod *MkModule 82 ourMod *MkModule 83 isGoodVar func(string) bool 84 want MkModuleDiff 85 }{ 86 { 87 name: "Ignored vars", 88 refMod: &refMod1, 89 ourMod: &ourMod1, 90 isGoodVar: func(v string) bool { return v == "var1" }, 91 want: MkModuleDiff{}, 92 }, 93 { 94 name: "Different values", 95 refMod: &refMod1, 96 ourMod: &ourMod1, 97 isGoodVar: func(_ string) bool { return true }, 98 want: MkModuleDiff{ 99 DiffVars: []MkVarDiff{{"var2", []string{"b"}, []string{"c"}}}, 100 }, 101 }, 102 { 103 name: "DifferentVars", 104 refMod: &refMod1, 105 ourMod: &MkModule{Type: "foo", Variables: map[string]string{"var2": "b", "var3": "c"}}, 106 isGoodVar: func(_ string) bool { return true }, 107 want: MkModuleDiff{ 108 MissingVars: []string{"var1"}, 109 ExtraVars: []string{"var3"}, 110 }, 111 }, 112 } 113 for _, tt := range tests { 114 t.Run(tt.name, func(t *testing.T) { 115 tt.want.Ref = tt.refMod 116 tt.want.Our = tt.ourMod 117 if got := Compare(tt.refMod, tt.ourMod, tt.isGoodVar); !reflect.DeepEqual(got, tt.want) { 118 t.Errorf("Compare() = %v, want %v (diff = %s)", got, tt.want, cmp.Diff(got, tt.want)) 119 } 120 }) 121 } 122} 123