github.com/pulumi/terraform@v1.4.0/pkg/addrs/instance_key_test.go (about) 1 package addrs 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestInstanceKeyString(t *testing.T) { 9 tests := []struct { 10 Key InstanceKey 11 Want string 12 }{ 13 { 14 IntKey(0), 15 `[0]`, 16 }, 17 { 18 IntKey(5), 19 `[5]`, 20 }, 21 { 22 StringKey(""), 23 `[""]`, 24 }, 25 { 26 StringKey("hi"), 27 `["hi"]`, 28 }, 29 { 30 StringKey("0"), 31 `["0"]`, // intentionally distinct from IntKey(0) 32 }, 33 { 34 // Quotes must be escaped 35 StringKey(`"`), 36 `["\""]`, 37 }, 38 { 39 // Escape sequences must themselves be escaped 40 StringKey(`\r\n`), 41 `["\\r\\n"]`, 42 }, 43 { 44 // Template interpolation sequences "${" must be escaped. 45 StringKey(`${hello}`), 46 `["$${hello}"]`, 47 }, 48 { 49 // Template control sequences "%{" must be escaped. 50 StringKey(`%{ for something in something }%{ endfor }`), 51 `["%%{ for something in something }%%{ endfor }"]`, 52 }, 53 { 54 // Dollar signs that aren't followed by { are not interpolation sequences 55 StringKey(`$hello`), 56 `["$hello"]`, 57 }, 58 { 59 // Percent signs that aren't followed by { are not control sequences 60 StringKey(`%hello`), 61 `["%hello"]`, 62 }, 63 } 64 65 for _, test := range tests { 66 testName := fmt.Sprintf("%#v", test.Key) 67 t.Run(testName, func(t *testing.T) { 68 got := test.Key.String() 69 want := test.Want 70 if got != want { 71 t.Errorf("wrong result\nreciever: %s\ngot: %s\nwant: %s", testName, got, want) 72 } 73 }) 74 } 75 }