Colin Cross | 6bde094 | 2016-11-04 14:36:38 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var vars = map[string]string{ |
| 23 | "var1": "abc", |
| 24 | "var2": "", |
| 25 | "var3": "def", |
| 26 | "💩": "😃", |
| 27 | } |
| 28 | |
| 29 | func expander(s string) (string, error) { |
| 30 | if val, ok := vars[s]; ok { |
| 31 | return val, nil |
| 32 | } else { |
| 33 | return "", fmt.Errorf("unknown variable %q", s) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | var expandTestCases = []struct { |
| 38 | in string |
| 39 | out string |
| 40 | err bool |
| 41 | }{ |
| 42 | { |
| 43 | in: "$(var1)", |
| 44 | out: "abc", |
| 45 | }, |
| 46 | { |
| 47 | in: "$( var1 )", |
| 48 | out: "abc", |
| 49 | }, |
| 50 | { |
| 51 | in: "def$(var1)", |
| 52 | out: "defabc", |
| 53 | }, |
| 54 | { |
| 55 | in: "$(var1)def", |
| 56 | out: "abcdef", |
| 57 | }, |
| 58 | { |
| 59 | in: "def$(var1)def", |
| 60 | out: "defabcdef", |
| 61 | }, |
| 62 | { |
| 63 | in: "$(var2)", |
| 64 | out: "", |
| 65 | }, |
| 66 | { |
| 67 | in: "def$(var2)", |
| 68 | out: "def", |
| 69 | }, |
| 70 | { |
| 71 | in: "$(var2)def", |
| 72 | out: "def", |
| 73 | }, |
| 74 | { |
| 75 | in: "def$(var2)def", |
| 76 | out: "defdef", |
| 77 | }, |
| 78 | { |
| 79 | in: "$(var1)$(var3)", |
| 80 | out: "abcdef", |
| 81 | }, |
| 82 | { |
| 83 | in: "$(var1)g$(var3)", |
| 84 | out: "abcgdef", |
| 85 | }, |
| 86 | { |
| 87 | in: "$$", |
Colin Cross | 2ee10a0 | 2016-11-21 17:22:15 -0800 | [diff] [blame] | 88 | out: "$$", |
Colin Cross | 6bde094 | 2016-11-04 14:36:38 -0700 | [diff] [blame] | 89 | }, |
| 90 | { |
| 91 | in: "$$(var1)", |
Colin Cross | 2ee10a0 | 2016-11-21 17:22:15 -0800 | [diff] [blame] | 92 | out: "$$(var1)", |
Colin Cross | 6bde094 | 2016-11-04 14:36:38 -0700 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | in: "$$$(var1)", |
Colin Cross | 2ee10a0 | 2016-11-21 17:22:15 -0800 | [diff] [blame] | 96 | out: "$$abc", |
Colin Cross | 6bde094 | 2016-11-04 14:36:38 -0700 | [diff] [blame] | 97 | }, |
| 98 | { |
| 99 | in: "$(var1)$$", |
Colin Cross | 2ee10a0 | 2016-11-21 17:22:15 -0800 | [diff] [blame] | 100 | out: "abc$$", |
Colin Cross | 6bde094 | 2016-11-04 14:36:38 -0700 | [diff] [blame] | 101 | }, |
| 102 | { |
| 103 | in: "$(💩)", |
| 104 | out: "😃", |
| 105 | }, |
| 106 | |
| 107 | // Errors |
| 108 | { |
| 109 | in: "$", |
| 110 | err: true, |
| 111 | }, |
| 112 | { |
| 113 | in: "$$$", |
| 114 | err: true, |
| 115 | }, |
| 116 | { |
| 117 | in: "$(var1)$", |
| 118 | err: true, |
| 119 | }, |
| 120 | { |
| 121 | in: "$(var1)$", |
| 122 | err: true, |
| 123 | }, |
| 124 | { |
| 125 | in: "$(var4)", |
| 126 | err: true, |
| 127 | }, |
| 128 | { |
| 129 | in: "$var1", |
| 130 | err: true, |
| 131 | }, |
| 132 | { |
| 133 | in: "$(var1", |
| 134 | err: true, |
| 135 | }, |
| 136 | { |
| 137 | in: "$a💩c", |
| 138 | err: true, |
| 139 | }, |
| 140 | } |
| 141 | |
| 142 | func TestExpand(t *testing.T) { |
| 143 | for _, test := range expandTestCases { |
| 144 | got, err := Expand(test.in, expander) |
| 145 | if err != nil && !test.err { |
| 146 | t.Errorf("%q: unexpected error %s", test.in, err.Error()) |
| 147 | } else if err == nil && test.err { |
| 148 | t.Errorf("%q: expected error, got %q", test.in, got) |
| 149 | } else if !test.err && got != test.out { |
| 150 | t.Errorf("%q: expected %q, got %q", test.in, test.out, got) |
| 151 | } |
| 152 | } |
| 153 | } |