github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/output_value_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package addrs
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/go-test/deep"
    12  )
    13  
    14  func TestAbsOutputValueInstanceEqual_true(t *testing.T) {
    15  	foo, diags := ParseModuleInstanceStr("module.foo")
    16  	if len(diags) > 0 {
    17  		t.Fatalf("unexpected diags: %s", diags.Err())
    18  	}
    19  	foobar, diags := ParseModuleInstanceStr("module.foo[1].module.bar")
    20  	if len(diags) > 0 {
    21  		t.Fatalf("unexpected diags: %s", diags.Err())
    22  	}
    23  
    24  	ovs := []AbsOutputValue{
    25  		foo.OutputValue("a"),
    26  		foobar.OutputValue("b"),
    27  	}
    28  	for _, r := range ovs {
    29  		t.Run(r.String(), func(t *testing.T) {
    30  			if !r.Equal(r) {
    31  				t.Fatalf("expected %#v to be equal to itself", r)
    32  			}
    33  		})
    34  	}
    35  }
    36  
    37  func TestAbsOutputValueInstanceEqual_false(t *testing.T) {
    38  	foo, diags := ParseModuleInstanceStr("module.foo")
    39  	if len(diags) > 0 {
    40  		t.Fatalf("unexpected diags: %s", diags.Err())
    41  	}
    42  	foobar, diags := ParseModuleInstanceStr("module.foo[1].module.bar")
    43  	if len(diags) > 0 {
    44  		t.Fatalf("unexpected diags: %s", diags.Err())
    45  	}
    46  
    47  	testCases := []struct {
    48  		left  AbsOutputValue
    49  		right AbsOutputValue
    50  	}{
    51  		{
    52  			foo.OutputValue("a"),
    53  			foo.OutputValue("b"),
    54  		},
    55  		{
    56  			foo.OutputValue("a"),
    57  			foobar.OutputValue("a"),
    58  		},
    59  	}
    60  	for _, tc := range testCases {
    61  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
    62  			if tc.left.Equal(tc.right) {
    63  				t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
    64  			}
    65  
    66  			if tc.right.Equal(tc.left) {
    67  				t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestParseAbsOutputValueStr(t *testing.T) {
    74  	tests := map[string]struct {
    75  		want    AbsOutputValue
    76  		wantErr string
    77  	}{
    78  		"module.foo": {
    79  			wantErr: "An output name is required",
    80  		},
    81  		"module.foo.output": {
    82  			wantErr: "An output name is required",
    83  		},
    84  		"module.foo.boop.beep": {
    85  			wantErr: "Output address must start with \"output.\"",
    86  		},
    87  		"module.foo.output[0]": {
    88  			wantErr: "An output name is required",
    89  		},
    90  		"output": {
    91  			wantErr: "An output name is required",
    92  		},
    93  		"output[0]": {
    94  			wantErr: "An output name is required",
    95  		},
    96  		"output.boop": {
    97  			want: AbsOutputValue{
    98  				Module: RootModuleInstance,
    99  				OutputValue: OutputValue{
   100  					Name: "boop",
   101  				},
   102  			},
   103  		},
   104  		"module.foo.output.beep": {
   105  			want: AbsOutputValue{
   106  				Module: mustParseModuleInstanceStr("module.foo"),
   107  				OutputValue: OutputValue{
   108  					Name: "beep",
   109  				},
   110  			},
   111  		},
   112  	}
   113  
   114  	for input, tc := range tests {
   115  		t.Run(input, func(t *testing.T) {
   116  			got, diags := ParseAbsOutputValueStr(input)
   117  			for _, problem := range deep.Equal(got, tc.want) {
   118  				t.Errorf(problem)
   119  			}
   120  			if len(diags) > 0 {
   121  				gotErr := diags.Err().Error()
   122  				if tc.wantErr == "" {
   123  					t.Errorf("got error, expected success: %s", gotErr)
   124  				} else if !strings.Contains(gotErr, tc.wantErr) {
   125  					t.Errorf("unexpected error\n got: %s\nwant: %s", gotErr, tc.wantErr)
   126  				}
   127  			} else {
   128  				if tc.wantErr != "" {
   129  					t.Errorf("got success, expected error: %s", tc.wantErr)
   130  				}
   131  			}
   132  		})
   133  	}
   134  }