Debian Codegraph @

syntax/quote_test.go

raw
1 // Copyright (c) 2021, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax
5
6 import (
7 "testing"
8
9 "github.com/go-quicktest/qt"
10 )
11
12 func TestQuote(t *testing.T) {
13 t.Parallel()
14 tests := [...]struct {
15 str string
16 lang LangVariant
17 want any
18 }{
19 {"", LangBash, `''`},
20 {"\a", LangBash, `$'\a'`},
21 {"\b", LangBash, `$'\b'`},
22 {"\f", LangBash, `$'\f'`},
23 {"\n", LangBash, `$'\n'`},
24 {"\r", LangBash, `$'\r'`},
25 {"\t", LangBash, `$'\t'`},
26 {"\v", LangBash, `$'\v'`},
27 {"null\x00", LangBash, &QuoteError{4, quoteErrNull}},
28 {"posix\x1b", LangPOSIX, &QuoteError{5, quoteErrPOSIX}},
29 {"posix\n", LangPOSIX, &QuoteError{5, quoteErrPOSIX}},
30 {"mksh16\U00086199", LangMirBSDKorn, &QuoteError{6, quoteErrMksh}},
31 {"\x1b\x1caaa", LangBash, `$'\x1b\x1caaa'`},
32 {"\x1b\x1caaa", LangMirBSDKorn, `$'\x1b\x1c'$'aaa'`},
33 {"\xff\x00", LangBash, &QuoteError{1, quoteErrNull}},
34 }
35
36 for _, test := range tests {
37 t.Run("", func(t *testing.T) {
38 t.Parallel()
39
40 got, gotErr := Quote(test.str, test.lang)
41 switch want := test.want.(type) {
42 case string:
43 qt.Assert(t, qt.Equals(got, want))
44 qt.Assert(t, qt.IsNil(gotErr))
45 case *QuoteError:
46 qt.Assert(t, qt.Equals(got, ""))
47 qt.Assert(t, qt.DeepEquals(gotErr, error(want)))
48 default:
49 t.Fatalf("unexpected type: %T", want)
50 }
51 })
52 }
53 }