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