blob: 6227ea69b561d3d2c45317a2188edd3ec67aa293 [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"
19 "testing"
20)
21
22var ninjaParseTestCases = []struct {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070023 input string
24 vars []string
25 strs []string
26 err string
Jamie Gennis1bc967e2014-05-27 16:34:41 -070027}{
28 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070029 input: "abc def $ghi jkl",
30 vars: []string{"ghi"},
31 strs: []string{"abc def ", " jkl"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070032 },
33 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070034 input: "abc def $ghi$jkl",
35 vars: []string{"ghi", "jkl"},
36 strs: []string{"abc def ", "", ""},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070037 },
38 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070039 input: "foo $012_-345xyz_! bar",
40 vars: []string{"012_-345xyz_"},
41 strs: []string{"foo ", "! bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070042 },
43 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070044 input: "foo ${012_-345xyz_} bar",
45 vars: []string{"012_-345xyz_"},
46 strs: []string{"foo ", " bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070047 },
48 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070049 input: "foo ${012_-345xyz_} bar",
50 vars: []string{"012_-345xyz_"},
51 strs: []string{"foo ", " bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070052 },
53 {
Jamie Gennis30d8a3a2014-10-11 10:03:17 -070054 input: "foo $$ bar",
55 vars: nil,
56 strs: []string{"foo $$ bar"},
Jamie Gennis1bc967e2014-05-27 16:34:41 -070057 },
58 {
Colin Crossb2478932015-04-14 16:10:21 -070059 input: "$foo${bar}",
60 vars: []string{"foo", "bar"},
Colin Cross63d5d4d2015-04-20 16:41:55 -070061 strs: []string{"", "", ""},
Colin Crossb2478932015-04-14 16:10:21 -070062 },
63 {
64 input: "$foo$$",
65 vars: []string{"foo"},
Colin Cross63d5d4d2015-04-20 16:41:55 -070066 strs: []string{"", "$$"},
Colin Crossb2478932015-04-14 16:10:21 -070067 },
68 {
Colin Cross8c1c6c02015-04-14 16:28:51 -070069 input: "foo bar",
70 vars: nil,
71 strs: []string{"foo bar"},
72 },
73 {
Colin Cross8de48af2017-05-09 10:03:00 -070074 input: " foo ",
75 vars: nil,
76 strs: []string{"$ foo "},
77 },
78 {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070079 input: "foo $ bar",
80 err: "invalid character after '$' at byte offset 5",
81 },
82 {
83 input: "foo $",
84 err: "unexpected end of string after '$'",
85 },
86 {
87 input: "foo ${} bar",
88 err: "empty variable name at byte offset 6",
89 },
90 {
91 input: "foo ${abc!} bar",
92 err: "invalid character in variable name at byte offset 9",
93 },
94 {
95 input: "foo ${abc",
96 err: "unexpected end of string in variable name",
97 },
98}
99
100func TestParseNinjaString(t *testing.T) {
101 for _, testCase := range ninjaParseTestCases {
102 scope := newLocalScope(nil, "namespace")
Colin Cross8c1c6c02015-04-14 16:28:51 -0700103 expectedVars := []Variable{}
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700104 for _, varName := range testCase.vars {
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700105 v, err := scope.LookupVariable(varName)
106 if err != nil {
107 v, err = scope.AddLocalVariable(varName, "")
108 if err != nil {
109 t.Fatalf("error creating scope: %s", err)
110 }
111 }
112 expectedVars = append(expectedVars, v)
113 }
114
115 output, err := parseNinjaString(scope, testCase.input)
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700116 if err == nil {
117 if !reflect.DeepEqual(output.variables, expectedVars) {
118 t.Errorf("incorrect variable list:")
119 t.Errorf(" input: %q", testCase.input)
Colin Cross8c1c6c02015-04-14 16:28:51 -0700120 t.Errorf(" expected: %#v", expectedVars)
Jamie Gennis30d8a3a2014-10-11 10:03:17 -0700121 t.Errorf(" got: %#v", output.variables)
122 }
123 if !reflect.DeepEqual(output.strings, testCase.strs) {
124 t.Errorf("incorrect string list:")
125 t.Errorf(" input: %q", testCase.input)
126 t.Errorf(" expected: %#v", testCase.strs)
127 t.Errorf(" got: %#v", output.strings)
128 }
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700129 }
130 var errStr string
131 if err != nil {
132 errStr = err.Error()
133 }
134 if err != nil && err.Error() != testCase.err {
135 t.Errorf("unexpected error:")
136 t.Errorf(" input: %q", testCase.input)
137 t.Errorf(" expected: %q", testCase.err)
138 t.Errorf(" got: %q", errStr)
139 }
140 }
141}
142
143func TestParseNinjaStringWithImportedVar(t *testing.T) {
144 ImpVar := &staticVariable{name_: "ImpVar"}
145 impScope := newScope(nil)
146 impScope.AddVariable(ImpVar)
147 scope := newScope(nil)
148 scope.AddImport("impPkg", impScope)
149
150 input := "abc def ${impPkg.ImpVar} ghi"
151 output, err := parseNinjaString(scope, input)
152 if err != nil {
153 t.Fatalf("unexpected error: %s", err)
154 }
155
156 expect := []Variable{ImpVar}
157 if !reflect.DeepEqual(output.variables, expect) {
158 t.Errorf("incorrect output:")
159 t.Errorf(" input: %q", input)
160 t.Errorf(" expected: %#v", expect)
161 t.Errorf(" got: %#v", output)
162 }
163}