blob: 12179eddfeb1687ab056fea91df7f69cec53fe2b [file] [log] [blame]
Colin Cross6bde0942016-11-04 14:36:38 -07001// Copyright 2016 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 android
16
17import (
18 "fmt"
19 "testing"
20)
21
22var vars = map[string]string{
Colin Cross2647ced2019-07-11 10:58:17 -070023 "var1": "abc",
24 "var2": "",
25 "var3": "def",
26 "💩": "😃",
27 "escape": "${in}",
Colin Cross6bde0942016-11-04 14:36:38 -070028}
29
Colin Cross2647ced2019-07-11 10:58:17 -070030func expander(s string) (string, bool, error) {
Colin Cross6bde0942016-11-04 14:36:38 -070031 if val, ok := vars[s]; ok {
Colin Cross2647ced2019-07-11 10:58:17 -070032 return val, s == "escape", nil
Colin Cross6bde0942016-11-04 14:36:38 -070033 } else {
Colin Cross2647ced2019-07-11 10:58:17 -070034 return "", false, fmt.Errorf("unknown variable %q", s)
Colin Cross6bde0942016-11-04 14:36:38 -070035 }
36}
37
38var expandTestCases = []struct {
Colin Cross2647ced2019-07-11 10:58:17 -070039 in string
40 out string
41 out_escaped string
42 err bool
Colin Cross6bde0942016-11-04 14:36:38 -070043}{
44 {
Colin Cross2647ced2019-07-11 10:58:17 -070045 in: "$(var1)",
46 out: "abc",
47 out_escaped: "abc",
Colin Cross6bde0942016-11-04 14:36:38 -070048 },
49 {
Colin Cross2647ced2019-07-11 10:58:17 -070050 in: "$( var1 )",
51 out: "abc",
52 out_escaped: "abc",
Colin Cross6bde0942016-11-04 14:36:38 -070053 },
54 {
Colin Cross2647ced2019-07-11 10:58:17 -070055 in: "def$(var1)",
56 out: "defabc",
57 out_escaped: "defabc",
Colin Cross6bde0942016-11-04 14:36:38 -070058 },
59 {
Colin Cross2647ced2019-07-11 10:58:17 -070060 in: "$(var1)def",
61 out: "abcdef",
62 out_escaped: "abcdef",
Colin Cross6bde0942016-11-04 14:36:38 -070063 },
64 {
Colin Cross2647ced2019-07-11 10:58:17 -070065 in: "def$(var1)def",
66 out: "defabcdef",
67 out_escaped: "defabcdef",
Colin Cross6bde0942016-11-04 14:36:38 -070068 },
69 {
Colin Cross2647ced2019-07-11 10:58:17 -070070 in: "$(var2)",
71 out: "",
72 out_escaped: "",
Colin Cross6bde0942016-11-04 14:36:38 -070073 },
74 {
Colin Cross2647ced2019-07-11 10:58:17 -070075 in: "def$(var2)",
76 out: "def",
77 out_escaped: "def",
Colin Cross6bde0942016-11-04 14:36:38 -070078 },
79 {
Colin Cross2647ced2019-07-11 10:58:17 -070080 in: "$(var2)def",
81 out: "def",
82 out_escaped: "def",
Colin Cross6bde0942016-11-04 14:36:38 -070083 },
84 {
Colin Cross2647ced2019-07-11 10:58:17 -070085 in: "def$(var2)def",
86 out: "defdef",
87 out_escaped: "defdef",
Colin Cross6bde0942016-11-04 14:36:38 -070088 },
89 {
Colin Cross2647ced2019-07-11 10:58:17 -070090 in: "$(var1)$(var3)",
91 out: "abcdef",
92 out_escaped: "abcdef",
Colin Cross6bde0942016-11-04 14:36:38 -070093 },
94 {
Colin Cross2647ced2019-07-11 10:58:17 -070095 in: "$(var1)g$(var3)",
96 out: "abcgdef",
97 out_escaped: "abcgdef",
Colin Cross6bde0942016-11-04 14:36:38 -070098 },
99 {
Colin Cross2647ced2019-07-11 10:58:17 -0700100 in: "$$",
101 out: "$",
102 out_escaped: "$$",
Colin Cross6bde0942016-11-04 14:36:38 -0700103 },
104 {
Colin Cross2647ced2019-07-11 10:58:17 -0700105 in: "$$(var1)",
106 out: "$(var1)",
107 out_escaped: "$$(var1)",
Colin Cross6bde0942016-11-04 14:36:38 -0700108 },
109 {
Colin Cross2647ced2019-07-11 10:58:17 -0700110 in: "$$$(var1)",
111 out: "$abc",
112 out_escaped: "$$abc",
Colin Cross6bde0942016-11-04 14:36:38 -0700113 },
114 {
Colin Cross2647ced2019-07-11 10:58:17 -0700115 in: "$(var1)$$",
116 out: "abc$",
117 out_escaped: "abc$$",
Colin Cross6bde0942016-11-04 14:36:38 -0700118 },
119 {
Colin Cross2647ced2019-07-11 10:58:17 -0700120 in: "$(💩)",
121 out: "😃",
122 out_escaped: "😃",
123 },
124 {
125 in: "$$a$(escape)$$b",
126 out: "$a${in}$b",
127 out_escaped: "$$a${in}$$b",
Colin Cross6bde0942016-11-04 14:36:38 -0700128 },
129
130 // Errors
131 {
132 in: "$",
133 err: true,
134 },
135 {
136 in: "$$$",
137 err: true,
138 },
139 {
140 in: "$(var1)$",
141 err: true,
142 },
143 {
144 in: "$(var1)$",
145 err: true,
146 },
147 {
148 in: "$(var4)",
149 err: true,
150 },
151 {
152 in: "$var1",
153 err: true,
154 },
155 {
156 in: "$(var1",
157 err: true,
158 },
159 {
160 in: "$a💩c",
161 err: true,
162 },
163}
164
165func TestExpand(t *testing.T) {
166 for _, test := range expandTestCases {
Colin Cross2647ced2019-07-11 10:58:17 -0700167 got, err := Expand(test.in, func(s string) (string, error) {
168 s, _, err := expander(s)
169 return s, err
170 })
Colin Cross6bde0942016-11-04 14:36:38 -0700171 if err != nil && !test.err {
172 t.Errorf("%q: unexpected error %s", test.in, err.Error())
173 } else if err == nil && test.err {
174 t.Errorf("%q: expected error, got %q", test.in, got)
175 } else if !test.err && got != test.out {
176 t.Errorf("%q: expected %q, got %q", test.in, test.out, got)
177 }
178 }
179}
Colin Cross2647ced2019-07-11 10:58:17 -0700180
181func TestExpandNinjaEscaped(t *testing.T) {
182 for _, test := range expandTestCases {
183 got, err := ExpandNinjaEscaped(test.in, expander)
184 if err != nil && !test.err {
185 t.Errorf("%q: unexpected error %s", test.in, err.Error())
186 } else if err == nil && test.err {
187 t.Errorf("%q: expected error, got %q", test.in, got)
188 } else if !test.err && got != test.out_escaped {
189 t.Errorf("%q: expected %q, got %q", test.in, test.out, got)
190 }
191 }
192}