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