github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/plans/objchange/lcs_test.go (about) 1 package objchange 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 func TestLongestCommonSubsequence(t *testing.T) { 11 tests := []struct { 12 xs []cty.Value 13 ys []cty.Value 14 want []cty.Value 15 }{ 16 { 17 []cty.Value{}, 18 []cty.Value{}, 19 []cty.Value{}, 20 }, 21 { 22 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 23 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 24 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 25 }, 26 { 27 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 28 []cty.Value{cty.NumberIntVal(3), cty.NumberIntVal(4)}, 29 []cty.Value{}, 30 }, 31 { 32 []cty.Value{cty.NumberIntVal(2)}, 33 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 34 []cty.Value{cty.NumberIntVal(2)}, 35 }, 36 { 37 []cty.Value{cty.NumberIntVal(1)}, 38 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 39 []cty.Value{cty.NumberIntVal(1)}, 40 }, 41 { 42 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(1)}, 43 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 44 []cty.Value{cty.NumberIntVal(1)}, // arbitrarily selected 1; 2 would also be valid 45 }, 46 { 47 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)}, 48 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)}, 49 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4)}, 50 }, 51 { 52 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)}, 53 []cty.Value{cty.NumberIntVal(4), cty.NumberIntVal(2), cty.NumberIntVal(5)}, 54 []cty.Value{cty.NumberIntVal(4)}, // 2 would also be valid 55 }, 56 { 57 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(5)}, 58 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)}, 59 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(5)}, 60 }, 61 62 // unknowns never compare as equal 63 { 64 []cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)}, 65 []cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)}, 66 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(3)}, 67 }, 68 { 69 []cty.Value{cty.UnknownVal(cty.Number)}, 70 []cty.Value{cty.UnknownVal(cty.Number)}, 71 []cty.Value{}, 72 }, 73 } 74 75 for _, test := range tests { 76 t.Run(fmt.Sprintf("%#v,%#v", test.xs, test.ys), func(t *testing.T) { 77 got := LongestCommonSubsequence(test.xs, test.ys) 78 79 wrong := func() { 80 t.Fatalf( 81 "wrong result\nX: %#v\nY: %#v\ngot: %#v\nwant: %#v", 82 test.xs, test.ys, got, test.want, 83 ) 84 } 85 86 if len(got) != len(test.want) { 87 wrong() 88 } 89 90 for i := range got { 91 if got[i] == cty.NilVal { 92 wrong() 93 } 94 if !got[i].RawEquals(test.want[i]) { 95 wrong() 96 } 97 } 98 }) 99 } 100 }