|
| 1 | +package astutil_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/breml/logstash-config/ast" |
| 7 | + "github.com/breml/logstash-config/ast/astutil" |
| 8 | +) |
| 9 | + |
| 10 | +func TestQuote(t *testing.T) { |
| 11 | + tt := []struct { |
| 12 | + name string |
| 13 | + in string |
| 14 | + |
| 15 | + want []string |
| 16 | + wantErr []bool |
| 17 | + wantEscaped []string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "bareword", |
| 21 | + in: `bareword`, |
| 22 | + |
| 23 | + want: []string{ |
| 24 | + ast.DoubleQuoted: `"bareword"`, |
| 25 | + ast.SingleQuoted: `'bareword'`, |
| 26 | + ast.Bareword: `bareword`, |
| 27 | + }, |
| 28 | + wantErr: []bool{ |
| 29 | + ast.DoubleQuoted: false, |
| 30 | + ast.SingleQuoted: false, |
| 31 | + ast.Bareword: false, |
| 32 | + }, |
| 33 | + wantEscaped: []string{ |
| 34 | + ast.DoubleQuoted: `"bareword"`, |
| 35 | + ast.SingleQuoted: `'bareword'`, |
| 36 | + ast.Bareword: `bareword`, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "multiple words", |
| 41 | + in: `multiple words`, |
| 42 | + |
| 43 | + want: []string{ |
| 44 | + ast.DoubleQuoted: `"multiple words"`, |
| 45 | + ast.SingleQuoted: `'multiple words'`, |
| 46 | + ast.Bareword: ``, |
| 47 | + }, |
| 48 | + wantErr: []bool{ |
| 49 | + ast.DoubleQuoted: false, |
| 50 | + ast.SingleQuoted: false, |
| 51 | + ast.Bareword: true, |
| 52 | + }, |
| 53 | + wantEscaped: []string{ |
| 54 | + ast.DoubleQuoted: `"multiple words"`, |
| 55 | + ast.SingleQuoted: `'multiple words'`, |
| 56 | + ast.Bareword: `multiple_words`, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "double quote", |
| 61 | + in: `value with " (double quote)`, |
| 62 | + |
| 63 | + want: []string{ |
| 64 | + ast.DoubleQuoted: ``, |
| 65 | + ast.SingleQuoted: `'value with " (double quote)'`, |
| 66 | + ast.Bareword: ``, |
| 67 | + }, |
| 68 | + wantErr: []bool{ |
| 69 | + ast.DoubleQuoted: true, |
| 70 | + ast.SingleQuoted: false, |
| 71 | + ast.Bareword: true, |
| 72 | + }, |
| 73 | + wantEscaped: []string{ |
| 74 | + ast.DoubleQuoted: `"value with \" (double quote)"`, |
| 75 | + ast.SingleQuoted: `'value with " (double quote)'`, |
| 76 | + ast.Bareword: `value_with____double_quote_`, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "escaped double quote", |
| 81 | + in: `value with \" (escaped double quote)`, |
| 82 | + |
| 83 | + want: []string{ |
| 84 | + ast.DoubleQuoted: `"value with \" (escaped double quote)"`, |
| 85 | + ast.SingleQuoted: `'value with \" (escaped double quote)'`, |
| 86 | + ast.Bareword: ``, |
| 87 | + }, |
| 88 | + wantErr: []bool{ |
| 89 | + ast.DoubleQuoted: false, |
| 90 | + ast.SingleQuoted: false, |
| 91 | + ast.Bareword: true, |
| 92 | + }, |
| 93 | + wantEscaped: []string{ |
| 94 | + ast.DoubleQuoted: `"value with \" (escaped double quote)"`, |
| 95 | + ast.SingleQuoted: `'value with \" (escaped double quote)'`, |
| 96 | + ast.Bareword: `value_with_____escaped_double_quote_`, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "single quote", |
| 101 | + in: `value with ' (single quote)`, |
| 102 | + |
| 103 | + want: []string{ |
| 104 | + ast.DoubleQuoted: `"value with ' (single quote)"`, |
| 105 | + ast.SingleQuoted: ``, |
| 106 | + ast.Bareword: ``, |
| 107 | + }, |
| 108 | + wantErr: []bool{ |
| 109 | + ast.DoubleQuoted: false, |
| 110 | + ast.SingleQuoted: true, |
| 111 | + ast.Bareword: true, |
| 112 | + }, |
| 113 | + wantEscaped: []string{ |
| 114 | + ast.DoubleQuoted: `"value with ' (single quote)"`, |
| 115 | + ast.SingleQuoted: `'value with \' (single quote)'`, |
| 116 | + ast.Bareword: `value_with____single_quote_`, |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "escaped single quote", |
| 121 | + in: `value with \' (escaped single quote)`, |
| 122 | + |
| 123 | + want: []string{ |
| 124 | + ast.DoubleQuoted: `"value with \' (escaped single quote)"`, |
| 125 | + ast.SingleQuoted: `'value with \' (escaped single quote)'`, |
| 126 | + ast.Bareword: ``, |
| 127 | + }, |
| 128 | + wantErr: []bool{ |
| 129 | + ast.DoubleQuoted: false, |
| 130 | + ast.SingleQuoted: false, |
| 131 | + ast.Bareword: true, |
| 132 | + }, |
| 133 | + wantEscaped: []string{ |
| 134 | + ast.DoubleQuoted: `"value with \' (escaped single quote)"`, |
| 135 | + ast.SingleQuoted: `'value with \' (escaped single quote)'`, |
| 136 | + ast.Bareword: `value_with_____escaped_single_quote_`, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + quoteTypes := map[string]ast.StringAttributeType{ |
| 142 | + "double quote": ast.DoubleQuoted, |
| 143 | + "single quote": ast.SingleQuoted, |
| 144 | + "bareword": ast.Bareword, |
| 145 | + } |
| 146 | + |
| 147 | + for _, tc := range tt { |
| 148 | + t.Run(tc.name, func(t *testing.T) { |
| 149 | + if len(tc.want) != 4 && len(tc.wantErr) != 4 { |
| 150 | + } |
| 151 | + for name, quoteType := range quoteTypes { |
| 152 | + t.Run(name, func(t *testing.T) { |
| 153 | + got, err := astutil.Quote(tc.in, quoteType, false) |
| 154 | + if tc.wantErr[quoteType] != (err != nil) { |
| 155 | + t.Errorf("wantErr %t, err: %v", tc.wantErr[quoteType], err) |
| 156 | + } |
| 157 | + if tc.want[quoteType] != got { |
| 158 | + t.Errorf("want: %q, got: %q", tc.want[quoteType], got) |
| 159 | + } |
| 160 | + |
| 161 | + gotEscaped, _ := astutil.Quote(tc.in, quoteType, true) |
| 162 | + if tc.wantEscaped[quoteType] != gotEscaped { |
| 163 | + t.Errorf("want: %q, got: %q", tc.wantEscaped[quoteType], gotEscaped) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
0 commit comments