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