Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 4 | "bytes" |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 5 | "strings" |
| 6 | "testing" |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 7 | "unicode" |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 8 | |
| 9 | bpparser "github.com/google/blueprint/parser" |
| 10 | ) |
| 11 | |
| 12 | var valueTestCases = []struct { |
| 13 | blueprint string |
| 14 | expected string |
| 15 | }{ |
| 16 | { |
| 17 | blueprint: `test = false`, |
| 18 | expected: `false`, |
| 19 | }, |
| 20 | { |
| 21 | blueprint: `test = Variable`, |
| 22 | expected: `$(Variable)`, |
| 23 | }, |
| 24 | { |
| 25 | blueprint: `test = "string"`, |
| 26 | expected: `string`, |
| 27 | }, |
| 28 | { |
| 29 | blueprint: `test = ["a", "b"]`, |
| 30 | expected: `\ |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 31 | a \ |
| 32 | b`, |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 33 | }, |
| 34 | { |
| 35 | blueprint: `test = Var + "b"`, |
| 36 | expected: `$(Var)b`, |
| 37 | }, |
| 38 | { |
| 39 | blueprint: `test = ["a"] + ["b"]`, |
| 40 | expected: `\ |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 41 | a\ |
| 42 | b`, |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 43 | }, |
| 44 | } |
| 45 | |
| 46 | func TestValueToString(t *testing.T) { |
| 47 | for _, testCase := range valueTestCases { |
| 48 | blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil) |
| 49 | if len(errs) > 0 { |
| 50 | t.Errorf("Failed to read blueprint: %q", errs) |
| 51 | } |
| 52 | |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 53 | str, err := valueToString(blueprint.Defs[0].(*bpparser.Assignment).Value) |
| 54 | if err != nil { |
| 55 | t.Error(err.Error()) |
| 56 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 57 | expect(t, testCase.blueprint, testCase.expected, str) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | var moduleTestCases = []struct { |
| 62 | blueprint string |
| 63 | androidmk string |
| 64 | }{ |
| 65 | // Target-only |
| 66 | { |
| 67 | blueprint: `cc_library_shared { name: "test", }`, |
| 68 | androidmk: `include $(CLEAR_VARS) |
| 69 | LOCAL_MODULE := test |
| 70 | include $(BUILD_SHARED_LIBRARY)`, |
| 71 | }, |
| 72 | // Host-only |
| 73 | { |
| 74 | blueprint: `cc_library_host_shared { name: "test", }`, |
| 75 | androidmk: `include $(CLEAR_VARS) |
| 76 | LOCAL_MODULE := test |
| 77 | include $(BUILD_HOST_SHARED_LIBRARY)`, |
| 78 | }, |
| 79 | // Target and Host |
| 80 | { |
| 81 | blueprint: `cc_library_shared { name: "test", host_supported: true, }`, |
| 82 | androidmk: `include $(CLEAR_VARS) |
| 83 | LOCAL_MODULE := test |
| 84 | include $(BUILD_SHARED_LIBRARY) |
| 85 | |
| 86 | include $(CLEAR_VARS) |
| 87 | LOCAL_MODULE := test |
| 88 | include $(BUILD_HOST_SHARED_LIBRARY)`, |
| 89 | }, |
Dan Willemsen | 49f5045 | 2015-06-24 14:56:00 -0700 | [diff] [blame] | 90 | // Static and Shared |
| 91 | { |
| 92 | blueprint: `cc_library { name: "test", }`, |
| 93 | androidmk: `include $(CLEAR_VARS) |
| 94 | LOCAL_MODULE := test |
| 95 | include $(BUILD_SHARED_LIBRARY) |
| 96 | |
| 97 | include $(CLEAR_VARS) |
| 98 | LOCAL_MODULE := test |
| 99 | include $(BUILD_STATIC_LIBRARY)`, |
| 100 | }, |
Dan Willemsen | 3a4045d | 2015-06-24 15:37:17 -0700 | [diff] [blame] | 101 | // Static and Shared / Target and Host |
| 102 | { |
| 103 | blueprint: `cc_library { name: "test", host_supported: true, }`, |
| 104 | androidmk: `include $(CLEAR_VARS) |
| 105 | LOCAL_MODULE := test |
| 106 | include $(BUILD_SHARED_LIBRARY) |
| 107 | |
| 108 | include $(CLEAR_VARS) |
| 109 | LOCAL_MODULE := test |
| 110 | include $(BUILD_STATIC_LIBRARY) |
| 111 | |
| 112 | include $(CLEAR_VARS) |
| 113 | LOCAL_MODULE := test |
| 114 | include $(BUILD_HOST_SHARED_LIBRARY) |
| 115 | |
| 116 | include $(CLEAR_VARS) |
| 117 | LOCAL_MODULE := test |
| 118 | include $(BUILD_HOST_STATIC_LIBRARY)`, |
| 119 | }, |
Colin Cross | b1a66c0 | 2015-06-29 16:24:57 -0700 | [diff] [blame^] | 120 | // Manual translation |
| 121 | { |
| 122 | blueprint: `/* Android.mk:start |
| 123 | # Manual translation |
| 124 | Android.mk:end */ |
| 125 | cc_library { name: "test", host_supported: true, }`, |
| 126 | androidmk: `# Manual translation`, |
| 127 | }, |
| 128 | // Ignored translation |
| 129 | { |
| 130 | blueprint: `/* Android.mk:ignore */ |
| 131 | cc_library { name: "test", host_supported: true, }`, |
| 132 | androidmk: ``, |
| 133 | }, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func TestModules(t *testing.T) { |
| 137 | for _, testCase := range moduleTestCases { |
| 138 | blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil) |
| 139 | if len(errs) > 0 { |
| 140 | t.Errorf("Failed to read blueprint: %q", errs) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 141 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 142 | |
| 143 | buf := &bytes.Buffer{} |
| 144 | writer := &androidMkWriter{ |
| 145 | blueprint: blueprint, |
| 146 | path: "", |
| 147 | mapScope: make(map[string][]*bpparser.Property), |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 148 | Writer: buf, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | module := blueprint.Defs[0].(*bpparser.Module) |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 152 | err := writer.handleModule(module) |
| 153 | if err != nil { |
| 154 | t.Errorf("Unexpected error %s", err.Error()) |
| 155 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 156 | |
| 157 | expect(t, testCase.blueprint, testCase.androidmk, buf.String()) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Trim left whitespace, and any trailing newlines. Leave inner blank lines and |
| 162 | // right whitespace so that we can still check line continuations are correct |
| 163 | func trim(str string) string { |
| 164 | var list []string |
| 165 | for _, s := range strings.Split(str, "\n") { |
| 166 | list = append(list, strings.TrimLeftFunc(s, unicode.IsSpace)) |
| 167 | } |
| 168 | return strings.TrimRight(strings.Join(list, "\n"), "\n") |
| 169 | } |
| 170 | |
| 171 | func expect(t *testing.T, testCase string, expected string, out string) { |
| 172 | expected = trim(expected) |
| 173 | out = trim(out) |
| 174 | if expected != out { |
| 175 | sep := " " |
| 176 | if strings.Index(expected, "\n") != -1 || strings.Index(out, "\n") != -1 { |
| 177 | sep = "\n" |
| 178 | } |
| 179 | |
| 180 | t.Errorf("test case: %s", testCase) |
| 181 | t.Errorf("unexpected difference:") |
| 182 | t.Errorf(" expected:%s%s", sep, expected) |
| 183 | t.Errorf(" got:%s%s", sep, out) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 184 | } |
| 185 | } |