github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/scan_string_lit_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hclsyntax 5 6 import ( 7 "reflect" 8 "testing" 9 10 "github.com/davecgh/go-spew/spew" 11 ) 12 13 func TestScanStringLit(t *testing.T) { 14 tests := []struct { 15 Input string 16 WantQuoted []string 17 WantUnquoted []string 18 }{ 19 { 20 ``, 21 []string{}, 22 []string{}, 23 }, 24 { 25 `hello`, 26 []string{`hello`}, 27 []string{`hello`}, 28 }, 29 { 30 `hello world`, 31 []string{`hello world`}, 32 []string{`hello world`}, 33 }, 34 { 35 `hello\nworld`, 36 []string{`hello`, `\n`, `world`}, 37 []string{`hello\nworld`}, 38 }, 39 { 40 `hello\🥁world`, 41 []string{`hello`, `\🥁`, `world`}, 42 []string{`hello\🥁world`}, 43 }, 44 { 45 `hello\uabcdworld`, 46 []string{`hello`, `\uabcd`, `world`}, 47 []string{`hello\uabcdworld`}, 48 }, 49 { 50 `hello\uabcdabcdworld`, 51 []string{`hello`, `\uabcd`, `abcdworld`}, 52 []string{`hello\uabcdabcdworld`}, 53 }, 54 { 55 `hello\uabcworld`, 56 []string{`hello`, `\uabc`, `world`}, 57 []string{`hello\uabcworld`}, 58 }, 59 { 60 `hello\U01234567world`, 61 []string{`hello`, `\U01234567`, `world`}, 62 []string{`hello\U01234567world`}, 63 }, 64 { 65 `hello\U012345670123world`, 66 []string{`hello`, `\U01234567`, `0123world`}, 67 []string{`hello\U012345670123world`}, 68 }, 69 { 70 `hello\Uabcdworld`, 71 []string{`hello`, `\Uabcd`, `world`}, 72 []string{`hello\Uabcdworld`}, 73 }, 74 { 75 `hello\Uabcworld`, 76 []string{`hello`, `\Uabc`, `world`}, 77 []string{`hello\Uabcworld`}, 78 }, 79 { 80 `hello\uworld`, 81 []string{`hello`, `\u`, `world`}, 82 []string{`hello\uworld`}, 83 }, 84 { 85 `hello\Uworld`, 86 []string{`hello`, `\U`, `world`}, 87 []string{`hello\Uworld`}, 88 }, 89 { 90 `hello\u`, 91 []string{`hello`, `\u`}, 92 []string{`hello\u`}, 93 }, 94 { 95 `hello\U`, 96 []string{`hello`, `\U`}, 97 []string{`hello\U`}, 98 }, 99 { 100 `hello\`, 101 []string{`hello`, `\`}, 102 []string{`hello\`}, 103 }, 104 { 105 `hello$${world}`, 106 []string{`hello`, `$${`, `world}`}, 107 []string{`hello`, `$${`, `world}`}, 108 }, 109 { 110 `hello$$world`, 111 []string{`hello`, `$$`, `world`}, 112 []string{`hello`, `$$`, `world`}, 113 }, 114 { 115 `hello$world`, 116 []string{`hello`, `$`, `world`}, 117 []string{`hello`, `$`, `world`}, 118 }, 119 { 120 `hello$`, 121 []string{`hello`, `$`}, 122 []string{`hello`, `$`}, 123 }, 124 { 125 `hello$${`, 126 []string{`hello`, `$${`}, 127 []string{`hello`, `$${`}, 128 }, 129 { 130 `hello%%{world}`, 131 []string{`hello`, `%%{`, `world}`}, 132 []string{`hello`, `%%{`, `world}`}, 133 }, 134 { 135 `hello%%world`, 136 []string{`hello`, `%%`, `world`}, 137 []string{`hello`, `%%`, `world`}, 138 }, 139 { 140 `hello%world`, 141 []string{`hello`, `%`, `world`}, 142 []string{`hello`, `%`, `world`}, 143 }, 144 { 145 `hello%`, 146 []string{`hello`, `%`}, 147 []string{`hello`, `%`}, 148 }, 149 { 150 `hello%%{`, 151 []string{`hello`, `%%{`}, 152 []string{`hello`, `%%{`}, 153 }, 154 { 155 `hello\${world}`, 156 []string{`hello`, `\$`, `{world}`}, 157 []string{`hello\`, `$`, `{world}`}, 158 }, 159 { 160 `hello\%{world}`, 161 []string{`hello`, `\%`, `{world}`}, 162 []string{`hello\`, `%`, `{world}`}, 163 }, 164 { 165 "hello\nworld", 166 []string{`hello`, "\n", `world`}, 167 []string{`hello`, "\n", `world`}, 168 }, 169 { 170 "hello\rworld", 171 []string{`hello`, "\r", `world`}, 172 []string{`hello`, "\r", `world`}, 173 }, 174 { 175 "hello\r\nworld", 176 []string{`hello`, "\r\n", `world`}, 177 []string{`hello`, "\r\n", `world`}, 178 }, 179 } 180 181 for _, test := range tests { 182 t.Run(test.Input, func(t *testing.T) { 183 t.Run("quoted", func(t *testing.T) { 184 slices := scanStringLit([]byte(test.Input), true) 185 got := make([]string, len(slices)) 186 for i, slice := range slices { 187 got[i] = string(slice) 188 } 189 if !reflect.DeepEqual(got, test.WantQuoted) { 190 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.WantQuoted)) 191 } 192 }) 193 t.Run("unquoted", func(t *testing.T) { 194 slices := scanStringLit([]byte(test.Input), false) 195 got := make([]string, len(slices)) 196 for i, slice := range slices { 197 got[i] = string(slice) 198 } 199 if !reflect.DeepEqual(got, test.WantUnquoted) { 200 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.WantUnquoted)) 201 } 202 }) 203 }) 204 } 205 }