summaryrefslogtreecommitdiff
path: root/internal/mapr/token_test.go
blob: 6ba7b3a8b8df9c45e6e7ddd5d2f2227225b4dce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package mapr

import "testing"

// TestTokensConsumeBacktickStripping covers the backtick-stripping path in
// tokensConsume, including the regression for a lone backtick token which
// previously panicked with "slice bounds out of range [1:0]".
func TestTokensConsumeBacktickStripping(t *testing.T) {
	type expected struct {
		str            string
		quotesStripped bool
	}
	tests := []struct {
		name  string
		input string
		want  []expected
	}{
		{
			name:  "empty string",
			input: "",
			want:  nil,
		},
		{
			name:  "single backtick",
			input: "`",
			want:  []expected{{str: "`", quotesStripped: false}},
		},
		{
			name:  "double backtick",
			input: "``",
			want:  []expected{{str: "", quotesStripped: true}},
		},
		{
			name:  "backtick quoted identifier",
			input: "`x`",
			want:  []expected{{str: "x", quotesStripped: true}},
		},
		{
			name:  "unterminated backtick",
			input: "`foo",
			want:  []expected{{str: "`foo", quotesStripped: false}},
		},
		{
			name:  "regular identifier",
			input: "foo",
			want:  []expected{{str: "foo", quotesStripped: false}},
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			defer func() {
				if r := recover(); r != nil {
					t.Fatalf("tokenize/tokensConsume panicked on %q: %v", tc.input, r)
				}
			}()

			tokens := tokenize(tc.input)
			_, got := tokensConsume(tokens)

			if len(got) != len(tc.want) {
				t.Fatalf("input %q: got %d tokens, want %d (tokens=%v)",
					tc.input, len(got), len(tc.want), got)
			}
			for i, w := range tc.want {
				if got[i].str != w.str {
					t.Errorf("input %q token %d: str=%q, want %q",
						tc.input, i, got[i].str, w.str)
				}
				if got[i].quotesStripped != w.quotesStripped {
					t.Errorf("input %q token %d: quotesStripped=%v, want %v",
						tc.input, i, got[i].quotesStripped, w.quotesStripped)
				}
			}
		})
	}
}