blob: c1e05f79078d81d9492a2d7a077624e8ec411d0b [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// Copyright 2014 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
Jamie Gennis1bc967e2014-05-27 16:34:41 -070015package blueprint
16
17import (
18 "reflect"
Colin Cross19ff7272019-06-19 23:25:39 -070019 "strconv"
20 "strings"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070021 "testing"
22)
23
24var ninjaParseTestCases = []struct {
Colin Cross2ce594e2020-01-29 12:58:03 -080025 input string
26 vars []string
27 strs []string
28 literal bool
29 err string
Jamie Gennis1bc967e2014-05-27 16:34:41 -070030}{
31 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070032 input: "abc def $ghi jkl",
33 vars: []string{"ghi"},
34 strs: []string{"abc def ", " jkl"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070035 },
36 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070037 input: "abc def $ghi$jkl",
38 vars: []string{"ghi", "jkl"},
39 strs: []string{"abc def ", "", ""},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070040 },
41 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070042 input: "foo $012_-345xyz_! bar",
43 vars: []string{"012_-345xyz_"},
44 strs: []string{"foo ", "! bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070045 },
46 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070047 input: "foo ${012_-345xyz_} bar",
48 vars: []string{"012_-345xyz_"},
49 strs: []string{"foo ", " bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070050 },
51 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070052 input: "foo ${012_-345xyz_} bar",
53 vars: []string{"012_-345xyz_"},
54 strs: []string{"foo ", " bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070055 },
56 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070057 input: "foo $$ bar",
58 vars: nil,
59 strs: []string{"foo $$ bar"},
Colin Cross2ce594e2020-01-29 12:58:03 -080060 // this is technically a literal, but not recognized as such due to the $$
Jamie Gennis1bc967e2014-05-27 16:34:41 -070061 },
62 {
Colin Crossb2478932015-04-14 16:10:21 -070063 input: "$foo${bar}",
64 vars: []string{"foo", "bar"},
Colin Cross63d5d4d2015-04-20 16:41:55 -070065 strs: []string{"", "", ""},
Colin Crossb2478932015-04-14 16:10:21 -070066 },
67 {
68 input: "$foo$$",
69 vars: []string{"foo"},
Colin Cross63d5d4d2015-04-20 16:41:55 -070070 strs: []string{"", "$$"},
Colin Crossb2478932015-04-14 16:10:21 -070071 },
72 {
Colin Cross2ce594e2020-01-29 12:58:03 -080073 input: "foo bar",
74 vars: nil,
75 strs: []string{"foo bar"},
76 literal: true,
Colin Cross8c1c6c02015-04-14 16:28:51 -070077 },
78 {
Colin Cross2ce594e2020-01-29 12:58:03 -080079 input: " foo ",
80 vars: nil,
81 strs: []string{"$ foo "},
82 literal: true,
Colin Cross8de48af2017-05-09 10:03:00 -070083 },
84 {
Colin Cross2ce594e2020-01-29 12:58:03 -080085 input: " $foo ",
86 vars: []string{"foo"},
87 strs: []string{"$ ", " "},
88 }, {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070089 input: "foo $ bar",
90 err: "invalid character after '$' at byte offset 5",
91 },
92 {
93 input: "foo $",
94 err: "unexpected end of string after '$'",
95 },
96 {
97 input: "foo ${} bar",
98 err: "empty variable name at byte offset 6",
99 },
100 {
101 input: "foo ${abc!} bar",
102 err: "invalid character in variable name at byte offset 9",
103 },
104 {
105 input: "foo ${abc",
106 err: "unexpected end of string in variable name",
107 },
108}
109
110func TestParseNinjaString(t *testing.T) {
111 for _, testCase := range ninjaParseTestCases {
112 scope := newLocalScope(nil, "namespace")
Colin Cross8c1c6c02015-04-14 16:28:51 -0700113 expectedVars := []Variable{}
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700114 for _, varName := range testCase.vars {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700115 v, err := scope.LookupVariable(varName)
116 if err != nil {
117 v, err = scope.AddLocalVariable(varName, "")
118 if err != nil {
119 t.Fatalf("error creating scope: %s", err)
120 }
121 }
122 expectedVars = append(expectedVars, v)
123 }
124
Colin Cross2ce594e2020-01-29 12:58:03 -0800125 var expected ninjaString
126 if len(testCase.strs) > 0 {
127 if testCase.literal {
128 expected = literalNinjaString(testCase.strs[0])
129 } else {
130 expected = &varNinjaString{
131 strings: testCase.strs,
132 variables: expectedVars,
133 }
134 }
135 }
136
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700137 output, err := parseNinjaString(scope, testCase.input)
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700138 if err == nil {
Colin Cross2ce594e2020-01-29 12:58:03 -0800139 if !reflect.DeepEqual(output, expected) {
140 t.Errorf("incorrect ninja string:")
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700141 t.Errorf(" input: %q", testCase.input)
Colin Cross2ce594e2020-01-29 12:58:03 -0800142 t.Errorf(" expected: %#v", expected)
143 t.Errorf(" got: %#v", output)
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700144 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700145 }
146 var errStr string
147 if err != nil {
148 errStr = err.Error()
149 }
150 if err != nil && err.Error() != testCase.err {
151 t.Errorf("unexpected error:")
152 t.Errorf(" input: %q", testCase.input)
153 t.Errorf(" expected: %q", testCase.err)
154 t.Errorf(" got: %q", errStr)
155 }
156 }
157}
158
159func TestParseNinjaStringWithImportedVar(t *testing.T) {
160 ImpVar := &staticVariable{name_: "ImpVar"}
161 impScope := newScope(nil)
162 impScope.AddVariable(ImpVar)
163 scope := newScope(nil)
164 scope.AddImport("impPkg", impScope)
165
166 input := "abc def ${impPkg.ImpVar} ghi"
167 output, err := parseNinjaString(scope, input)
168 if err != nil {
169 t.Fatalf("unexpected error: %s", err)
170 }
171
172 expect := []Variable{ImpVar}
Colin Cross2ce594e2020-01-29 12:58:03 -0800173 if !reflect.DeepEqual(output.(*varNinjaString).variables, expect) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700174 t.Errorf("incorrect output:")
175 t.Errorf(" input: %q", input)
176 t.Errorf(" expected: %#v", expect)
177 t.Errorf(" got: %#v", output)
178 }
179}
Colin Cross19ff7272019-06-19 23:25:39 -0700180
181func BenchmarkNinjaString_Value(b *testing.B) {
182 b.Run("constant", func(b *testing.B) {
183 for _, l := range []int{1, 10, 100, 1000} {
184 ns := simpleNinjaString(strings.Repeat("a", l))
185 b.Run(strconv.Itoa(l), func(b *testing.B) {
186 for n := 0; n < b.N; n++ {
187 ns.Value(nil)
188 }
189 })
190 }
191 })
192 b.Run("variable", func(b *testing.B) {
193 for _, l := range []int{1, 10, 100, 1000} {
194 scope := newLocalScope(nil, "")
195 scope.AddLocalVariable("a", strings.Repeat("b", l/3))
196 ns, _ := parseNinjaString(scope, strings.Repeat("a", l/3)+"${a}"+strings.Repeat("a", l/3))
197 b.Run(strconv.Itoa(l), func(b *testing.B) {
198 for n := 0; n < b.N; n++ {
199 ns.Value(nil)
200 }
201 })
202 }
203 })
204 b.Run("variables", func(b *testing.B) {
205 for _, l := range []int{1, 2, 3, 4, 5, 10, 100, 1000} {
206 scope := newLocalScope(nil, "")
207 str := strings.Repeat("a", 10)
208 for i := 0; i < l; i++ {
209 scope.AddLocalVariable("a"+strconv.Itoa(i), strings.Repeat("b", 10))
210 str += "${a" + strconv.Itoa(i) + "}"
211 }
212 ns, _ := parseNinjaString(scope, str)
213 b.Run(strconv.Itoa(l), func(b *testing.B) {
214 for n := 0; n < b.N; n++ {
215 ns.Value(nil)
216 }
217 })
218 }
219 })
220
221}