blob: cf5be7a2d7082f391b76c18a5b0ec0c704522c09 [file] [log] [blame]
Dan Willemsenf33877b2015-06-23 23:34:49 -07001package main
2
3import (
Dan Willemsenead184a2015-06-23 22:47:38 -07004 "bytes"
Dan Willemsenf33877b2015-06-23 23:34:49 -07005 "strings"
6 "testing"
Dan Willemsenead184a2015-06-23 22:47:38 -07007 "unicode"
Dan Willemsenf33877b2015-06-23 23:34:49 -07008
9 bpparser "github.com/google/blueprint/parser"
10)
11
12var valueTestCases = []struct {
13 blueprint string
14 expected string
15}{
16 {
17 blueprint: `test = false`,
18 expected: `false`,
19 },
20 {
Dan Willemsenf33877b2015-06-23 23:34:49 -070021 blueprint: `test = "string"`,
22 expected: `string`,
23 },
24 {
25 blueprint: `test = ["a", "b"]`,
26 expected: `\
Colin Crossb3245e92015-06-30 16:27:57 -070027 a \
28 b
29 `,
Dan Willemsenf33877b2015-06-23 23:34:49 -070030 },
31}
32
33func 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 Crossb0931242015-06-29 14:18:27 -070040 str, err := valueToString(blueprint.Defs[0].(*bpparser.Assignment).Value)
41 if err != nil {
42 t.Error(err.Error())
43 }
Dan Willemsenead184a2015-06-23 22:47:38 -070044 expect(t, testCase.blueprint, testCase.expected, str)
45 }
46}
47
48var 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 Willemsen49f50452015-06-24 14:56:00 -070077 // 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 Willemsen3a4045d2015-06-24 15:37:17 -070088 // 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 Willemsen27012122015-06-26 17:40:54 -0700107 // 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 Crossb1a66c02015-06-29 16:24:57 -0700134 // 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 Willemsenead184a2015-06-23 22:47:38 -0700148}
149
150func 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 Willemsenf33877b2015-06-23 23:34:49 -0700155 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700156
157 buf := &bytes.Buffer{}
158 writer := &androidMkWriter{
159 blueprint: blueprint,
160 path: "",
Colin Crossb0931242015-06-29 14:18:27 -0700161 Writer: buf,
Dan Willemsenead184a2015-06-23 22:47:38 -0700162 }
163
164 module := blueprint.Defs[0].(*bpparser.Module)
Colin Crossb0931242015-06-29 14:18:27 -0700165 err := writer.handleModule(module)
166 if err != nil {
167 t.Errorf("Unexpected error %s", err.Error())
168 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700169
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
176func 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
184func 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 Willemsenf33877b2015-06-23 23:34:49 -0700197 }
198}