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