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 | }, |
Colin Cross | b1a66c0 | 2015-06-29 16:24:57 -0700 | [diff] [blame] | 107 | // Manual translation |
| 108 | { |
| 109 | blueprint: `/* Android.mk:start |
| 110 | # Manual translation |
| 111 | Android.mk:end */ |
| 112 | cc_library { name: "test", host_supported: true, }`, |
| 113 | androidmk: `# Manual translation`, |
| 114 | }, |
| 115 | // Ignored translation |
| 116 | { |
| 117 | blueprint: `/* Android.mk:ignore */ |
| 118 | cc_library { name: "test", host_supported: true, }`, |
| 119 | androidmk: ``, |
| 120 | }, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func TestModules(t *testing.T) { |
| 124 | for _, testCase := range moduleTestCases { |
| 125 | blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil) |
| 126 | if len(errs) > 0 { |
| 127 | t.Errorf("Failed to read blueprint: %q", errs) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 128 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 129 | |
| 130 | buf := &bytes.Buffer{} |
| 131 | writer := &androidMkWriter{ |
| 132 | blueprint: blueprint, |
| 133 | path: "", |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 134 | Writer: buf, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | module := blueprint.Defs[0].(*bpparser.Module) |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 138 | err := writer.handleModule(module) |
| 139 | if err != nil { |
| 140 | t.Errorf("Unexpected error %s", err.Error()) |
| 141 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 142 | |
| 143 | expect(t, testCase.blueprint, testCase.androidmk, buf.String()) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Trim left whitespace, and any trailing newlines. Leave inner blank lines and |
| 148 | // right whitespace so that we can still check line continuations are correct |
| 149 | func trim(str string) string { |
| 150 | var list []string |
| 151 | for _, s := range strings.Split(str, "\n") { |
| 152 | list = append(list, strings.TrimLeftFunc(s, unicode.IsSpace)) |
| 153 | } |
| 154 | return strings.TrimRight(strings.Join(list, "\n"), "\n") |
| 155 | } |
| 156 | |
| 157 | func expect(t *testing.T, testCase string, expected string, out string) { |
| 158 | expected = trim(expected) |
| 159 | out = trim(out) |
| 160 | if expected != out { |
| 161 | sep := " " |
| 162 | if strings.Index(expected, "\n") != -1 || strings.Index(out, "\n") != -1 { |
| 163 | sep = "\n" |
| 164 | } |
| 165 | |
| 166 | t.Errorf("test case: %s", testCase) |
| 167 | t.Errorf("unexpected difference:") |
| 168 | t.Errorf(" expected:%s%s", sep, expected) |
| 169 | t.Errorf(" got:%s%s", sep, out) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 170 | } |
| 171 | } |