• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package parser
16
17import (
18	"strings"
19	"testing"
20)
21
22var splitNTestCases = []struct {
23	in       *MakeString
24	expected []*MakeString
25	sep      string
26	n        int
27}{
28	{
29		in: &MakeString{
30			Strings: []string{
31				"a b c",
32				"d e f",
33				" h i j",
34			},
35			Variables: []Variable{
36				Variable{Name: SimpleMakeString("var1", NoPos)},
37				Variable{Name: SimpleMakeString("var2", NoPos)},
38			},
39		},
40		sep: " ",
41		n:   -1,
42		expected: []*MakeString{
43			SimpleMakeString("a", NoPos),
44			SimpleMakeString("b", NoPos),
45			&MakeString{
46				Strings: []string{"c", "d"},
47				Variables: []Variable{
48					Variable{Name: SimpleMakeString("var1", NoPos)},
49				},
50			},
51			SimpleMakeString("e", NoPos),
52			&MakeString{
53				Strings: []string{"f", ""},
54				Variables: []Variable{
55					Variable{Name: SimpleMakeString("var2", NoPos)},
56				},
57			},
58			SimpleMakeString("h", NoPos),
59			SimpleMakeString("i", NoPos),
60			SimpleMakeString("j", NoPos),
61		},
62	},
63	{
64		in: &MakeString{
65			Strings: []string{
66				"a b c",
67				"d e f",
68				" h i j",
69			},
70			Variables: []Variable{
71				Variable{Name: SimpleMakeString("var1", NoPos)},
72				Variable{Name: SimpleMakeString("var2", NoPos)},
73			},
74		},
75		sep: " ",
76		n:   3,
77		expected: []*MakeString{
78			SimpleMakeString("a", NoPos),
79			SimpleMakeString("b", NoPos),
80			&MakeString{
81				Strings: []string{"c", "d e f", " h i j"},
82				Variables: []Variable{
83					Variable{Name: SimpleMakeString("var1", NoPos)},
84					Variable{Name: SimpleMakeString("var2", NoPos)},
85				},
86			},
87		},
88	},
89}
90
91func TestMakeStringSplitN(t *testing.T) {
92	for _, test := range splitNTestCases {
93		got := test.in.SplitN(test.sep, test.n)
94		gotString := dumpArray(got)
95		expectedString := dumpArray(test.expected)
96		if gotString != expectedString {
97			t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString)
98		}
99	}
100}
101
102var valueTestCases = []struct {
103	in       *MakeString
104	expected string
105}{
106	{
107		in:       SimpleMakeString("a b", NoPos),
108		expected: "a b",
109	},
110	{
111		in:       SimpleMakeString("a\\ \\\tb\\\\", NoPos),
112		expected: "a \tb\\",
113	},
114	{
115		in:       SimpleMakeString("a\\b\\", NoPos),
116		expected: "a\\b\\",
117	},
118}
119
120func TestMakeStringValue(t *testing.T) {
121	for _, test := range valueTestCases {
122		got := test.in.Value(nil)
123		if got != test.expected {
124			t.Errorf("\nwith: %q\nwant: %q\n got: %q", test.in.Dump(), test.expected, got)
125		}
126	}
127}
128
129var splitWordsTestCases = []struct {
130	in       *MakeString
131	expected []*MakeString
132}{
133	{
134		in:       SimpleMakeString("", NoPos),
135		expected: []*MakeString{},
136	},
137	{
138		in: SimpleMakeString(" a b\\ c d", NoPos),
139		expected: []*MakeString{
140			SimpleMakeString("a", NoPos),
141			SimpleMakeString("b\\ c", NoPos),
142			SimpleMakeString("d", NoPos),
143		},
144	},
145	{
146		in: SimpleMakeString("  a\tb\\\t\\ c d  ", NoPos),
147		expected: []*MakeString{
148			SimpleMakeString("a", NoPos),
149			SimpleMakeString("b\\\t\\ c", NoPos),
150			SimpleMakeString("d", NoPos),
151		},
152	},
153	{
154		in: SimpleMakeString(`a\\ b\\\ c d`, NoPos),
155		expected: []*MakeString{
156			SimpleMakeString(`a\\`, NoPos),
157			SimpleMakeString(`b\\\ c`, NoPos),
158			SimpleMakeString("d", NoPos),
159		},
160	},
161}
162
163func TestMakeStringWords(t *testing.T) {
164	for _, test := range splitWordsTestCases {
165		got := test.in.Words()
166		gotString := dumpArray(got)
167		expectedString := dumpArray(test.expected)
168		if gotString != expectedString {
169			t.Errorf("with:\n%q\nexpected:\n%s\ngot:\n%s", test.in.Dump(), expectedString, gotString)
170		}
171	}
172}
173
174func dumpArray(a []*MakeString) string {
175	ret := make([]string, len(a))
176
177	for i, s := range a {
178		ret[i] = s.Dump()
179	}
180
181	return strings.Join(ret, "|||")
182}
183