summaryrefslogtreecommitdiff
path: root/android/expand_test.go
diff options
context:
space:
mode:
author Colin Cross <ccross@android.com> 2016-11-04 14:36:38 -0700
committer Colin Cross <ccross@android.com> 2016-11-04 15:59:23 -0700
commit6bde0948d2b0d1826098a0046da5a8a2ce017fb6 (patch)
treea6aa3955d5a6df366dcc2867018c9b5d39222aab /android/expand_test.go
parent19ce4bddf102a8f6e8d76d58c79c5141a9326042 (diff)
Add android.Expand
Add android.Expand to expand $() variables in properties. Test: expand_test Bug: 31948427 Change-Id: Id30856a1d21d02e8997fcf2358e4a5feeede05be
Diffstat (limited to 'android/expand_test.go')
-rw-r--r--android/expand_test.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/android/expand_test.go b/android/expand_test.go
new file mode 100644
index 000000000..ca50b3982
--- /dev/null
+++ b/android/expand_test.go
@@ -0,0 +1,153 @@
+// Copyright 2016 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package android
+
+import (
+ "fmt"
+ "testing"
+)
+
+var vars = map[string]string{
+ "var1": "abc",
+ "var2": "",
+ "var3": "def",
+ "💩": "😃",
+}
+
+func expander(s string) (string, error) {
+ if val, ok := vars[s]; ok {
+ return val, nil
+ } else {
+ return "", fmt.Errorf("unknown variable %q", s)
+ }
+}
+
+var expandTestCases = []struct {
+ in string
+ out string
+ err bool
+}{
+ {
+ in: "$(var1)",
+ out: "abc",
+ },
+ {
+ in: "$( var1 )",
+ out: "abc",
+ },
+ {
+ in: "def$(var1)",
+ out: "defabc",
+ },
+ {
+ in: "$(var1)def",
+ out: "abcdef",
+ },
+ {
+ in: "def$(var1)def",
+ out: "defabcdef",
+ },
+ {
+ in: "$(var2)",
+ out: "",
+ },
+ {
+ in: "def$(var2)",
+ out: "def",
+ },
+ {
+ in: "$(var2)def",
+ out: "def",
+ },
+ {
+ in: "def$(var2)def",
+ out: "defdef",
+ },
+ {
+ in: "$(var1)$(var3)",
+ out: "abcdef",
+ },
+ {
+ in: "$(var1)g$(var3)",
+ out: "abcgdef",
+ },
+ {
+ in: "$$",
+ out: "$",
+ },
+ {
+ in: "$$(var1)",
+ out: "$(var1)",
+ },
+ {
+ in: "$$$(var1)",
+ out: "$abc",
+ },
+ {
+ in: "$(var1)$$",
+ out: "abc$",
+ },
+ {
+ in: "$(💩)",
+ out: "😃",
+ },
+
+ // Errors
+ {
+ in: "$",
+ err: true,
+ },
+ {
+ in: "$$$",
+ err: true,
+ },
+ {
+ in: "$(var1)$",
+ err: true,
+ },
+ {
+ in: "$(var1)$",
+ err: true,
+ },
+ {
+ in: "$(var4)",
+ err: true,
+ },
+ {
+ in: "$var1",
+ err: true,
+ },
+ {
+ in: "$(var1",
+ err: true,
+ },
+ {
+ in: "$a💩c",
+ err: true,
+ },
+}
+
+func TestExpand(t *testing.T) {
+ for _, test := range expandTestCases {
+ got, err := Expand(test.in, expander)
+ if err != nil && !test.err {
+ t.Errorf("%q: unexpected error %s", test.in, err.Error())
+ } else if err == nil && test.err {
+ t.Errorf("%q: expected error, got %q", test.in, got)
+ } else if !test.err && got != test.out {
+ t.Errorf("%q: expected %q, got %q", test.in, test.out, got)
+ }
+ }
+}