diff options
| author | Martin Atkins <mart@degeneration.co.uk> | 2019-09-09 16:08:19 -0700 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2019-09-09 16:08:19 -0700 |
| commit | 6c4344623b6ac528a57f9b80e4622acfab2fde40 (patch) | |
| tree | 83031084d3ab54abbe40e3fd749e439c8b28e9d1 /hclsyntax/scan_string_lit_test.go | |
| parent | 0f5ab3bd563c111077917020c0cbc8c211e1bff3 (diff) | |
Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
Diffstat (limited to 'hclsyntax/scan_string_lit_test.go')
| -rw-r--r-- | hclsyntax/scan_string_lit_test.go | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/hclsyntax/scan_string_lit_test.go b/hclsyntax/scan_string_lit_test.go new file mode 100644 index 0000000..c1b8cd3 --- /dev/null +++ b/hclsyntax/scan_string_lit_test.go @@ -0,0 +1,202 @@ +package hclsyntax + +import ( + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func TestScanStringLit(t *testing.T) { + tests := []struct { + Input string + WantQuoted []string + WantUnquoted []string + }{ + { + ``, + []string{}, + []string{}, + }, + { + `hello`, + []string{`hello`}, + []string{`hello`}, + }, + { + `hello world`, + []string{`hello world`}, + []string{`hello world`}, + }, + { + `hello\nworld`, + []string{`hello`, `\n`, `world`}, + []string{`hello\nworld`}, + }, + { + `hello\🥁world`, + []string{`hello`, `\🥁`, `world`}, + []string{`hello\🥁world`}, + }, + { + `hello\uabcdworld`, + []string{`hello`, `\uabcd`, `world`}, + []string{`hello\uabcdworld`}, + }, + { + `hello\uabcdabcdworld`, + []string{`hello`, `\uabcd`, `abcdworld`}, + []string{`hello\uabcdabcdworld`}, + }, + { + `hello\uabcworld`, + []string{`hello`, `\uabc`, `world`}, + []string{`hello\uabcworld`}, + }, + { + `hello\U01234567world`, + []string{`hello`, `\U01234567`, `world`}, + []string{`hello\U01234567world`}, + }, + { + `hello\U012345670123world`, + []string{`hello`, `\U01234567`, `0123world`}, + []string{`hello\U012345670123world`}, + }, + { + `hello\Uabcdworld`, + []string{`hello`, `\Uabcd`, `world`}, + []string{`hello\Uabcdworld`}, + }, + { + `hello\Uabcworld`, + []string{`hello`, `\Uabc`, `world`}, + []string{`hello\Uabcworld`}, + }, + { + `hello\uworld`, + []string{`hello`, `\u`, `world`}, + []string{`hello\uworld`}, + }, + { + `hello\Uworld`, + []string{`hello`, `\U`, `world`}, + []string{`hello\Uworld`}, + }, + { + `hello\u`, + []string{`hello`, `\u`}, + []string{`hello\u`}, + }, + { + `hello\U`, + []string{`hello`, `\U`}, + []string{`hello\U`}, + }, + { + `hello\`, + []string{`hello`, `\`}, + []string{`hello\`}, + }, + { + `hello$${world}`, + []string{`hello`, `$${`, `world}`}, + []string{`hello`, `$${`, `world}`}, + }, + { + `hello$$world`, + []string{`hello`, `$$`, `world`}, + []string{`hello`, `$$`, `world`}, + }, + { + `hello$world`, + []string{`hello`, `$`, `world`}, + []string{`hello`, `$`, `world`}, + }, + { + `hello$`, + []string{`hello`, `$`}, + []string{`hello`, `$`}, + }, + { + `hello$${`, + []string{`hello`, `$${`}, + []string{`hello`, `$${`}, + }, + { + `hello%%{world}`, + []string{`hello`, `%%{`, `world}`}, + []string{`hello`, `%%{`, `world}`}, + }, + { + `hello%%world`, + []string{`hello`, `%%`, `world`}, + []string{`hello`, `%%`, `world`}, + }, + { + `hello%world`, + []string{`hello`, `%`, `world`}, + []string{`hello`, `%`, `world`}, + }, + { + `hello%`, + []string{`hello`, `%`}, + []string{`hello`, `%`}, + }, + { + `hello%%{`, + []string{`hello`, `%%{`}, + []string{`hello`, `%%{`}, + }, + { + `hello\${world}`, + []string{`hello`, `\$`, `{world}`}, + []string{`hello\`, `$`, `{world}`}, + }, + { + `hello\%{world}`, + []string{`hello`, `\%`, `{world}`}, + []string{`hello\`, `%`, `{world}`}, + }, + { + "hello\nworld", + []string{`hello`, "\n", `world`}, + []string{`hello`, "\n", `world`}, + }, + { + "hello\rworld", + []string{`hello`, "\r", `world`}, + []string{`hello`, "\r", `world`}, + }, + { + "hello\r\nworld", + []string{`hello`, "\r\n", `world`}, + []string{`hello`, "\r\n", `world`}, + }, + } + + for _, test := range tests { + t.Run(test.Input, func(t *testing.T) { + t.Run("quoted", func(t *testing.T) { + slices := scanStringLit([]byte(test.Input), true) + got := make([]string, len(slices)) + for i, slice := range slices { + got[i] = string(slice) + } + if !reflect.DeepEqual(got, test.WantQuoted) { + t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.WantQuoted)) + } + }) + t.Run("unquoted", func(t *testing.T) { + slices := scanStringLit([]byte(test.Input), false) + got := make([]string, len(slices)) + for i, slice := range slices { + got[i] = string(slice) + } + if !reflect.DeepEqual(got, test.WantUnquoted) { + t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.WantUnquoted)) + } + }) + }) + } +} |
