github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/plans/objchange/lcs_test.go (about)

     1  package objchange
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/internal/lang/marks"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func TestLongestCommonSubsequence(t *testing.T) {
    12  	tests := []struct {
    13  		xs   []cty.Value
    14  		ys   []cty.Value
    15  		want []cty.Value
    16  	}{
    17  		{
    18  			[]cty.Value{},
    19  			[]cty.Value{},
    20  			[]cty.Value{},
    21  		},
    22  		{
    23  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    24  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    25  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    26  		},
    27  		{
    28  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    29  			[]cty.Value{cty.NumberIntVal(3), cty.NumberIntVal(4)},
    30  			[]cty.Value{},
    31  		},
    32  		{
    33  			[]cty.Value{cty.NumberIntVal(2)},
    34  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    35  			[]cty.Value{cty.NumberIntVal(2)},
    36  		},
    37  		{
    38  			[]cty.Value{cty.NumberIntVal(1)},
    39  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    40  			[]cty.Value{cty.NumberIntVal(1)},
    41  		},
    42  		{
    43  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(1)},
    44  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)},
    45  			[]cty.Value{cty.NumberIntVal(1)}, // arbitrarily selected 1; 2 would also be valid
    46  		},
    47  		{
    48  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)},
    49  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)},
    50  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4)},
    51  		},
    52  		{
    53  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)},
    54  			[]cty.Value{cty.NumberIntVal(4), cty.NumberIntVal(2), cty.NumberIntVal(5)},
    55  			[]cty.Value{cty.NumberIntVal(4)}, // 2 would also be valid
    56  		},
    57  		{
    58  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(5)},
    59  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)},
    60  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(5)},
    61  		},
    62  
    63  		// unknowns never compare as equal
    64  		{
    65  			[]cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)},
    66  			[]cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)},
    67  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(3)},
    68  		},
    69  		{
    70  			[]cty.Value{cty.UnknownVal(cty.Number)},
    71  			[]cty.Value{cty.UnknownVal(cty.Number)},
    72  			[]cty.Value{},
    73  		},
    74  
    75  		// marked values
    76  		{
    77  			[]cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo"), cty.NumberIntVal(3)},
    78  			[]cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo")},
    79  			[]cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo")},
    80  		},
    81  		{
    82  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2).Mark("foo"), cty.NumberIntVal(3)},
    83  			[]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(3)},
    84  			[]cty.Value{cty.NumberIntVal(3)},
    85  		},
    86  		{
    87  			[]cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2).Mark("foo")},
    88  			[]cty.Value{cty.NumberIntVal(2)},
    89  			[]cty.Value{},
    90  		},
    91  		{
    92  			[]cty.Value{
    93  				cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark(marks.Sensitive)}),
    94  				cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}),
    95  			},
    96  			[]cty.Value{
    97  				cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark(marks.Sensitive)}),
    98  				cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}),
    99  				cty.MapVal(map[string]cty.Value{"c": cty.StringVal("z")}),
   100  			},
   101  			[]cty.Value{
   102  				cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark(marks.Sensitive)}),
   103  				cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}),
   104  			},
   105  		},
   106  	}
   107  
   108  	for _, test := range tests {
   109  		t.Run(fmt.Sprintf("%#v,%#v", test.xs, test.ys), func(t *testing.T) {
   110  			got := LongestCommonSubsequence(test.xs, test.ys, ValueEqual)
   111  
   112  			wrong := func() {
   113  				t.Fatalf(
   114  					"wrong result\nX:    %#v\nY:    %#v\ngot:  %#v\nwant: %#v",
   115  					test.xs, test.ys, got, test.want,
   116  				)
   117  			}
   118  
   119  			if len(got) != len(test.want) {
   120  				wrong()
   121  			}
   122  
   123  			for i := range got {
   124  				if got[i] == cty.NilVal {
   125  					wrong()
   126  				}
   127  				if !got[i].RawEquals(test.want[i]) {
   128  					wrong()
   129  				}
   130  			}
   131  		})
   132  	}
   133  }