blob: 56ceca5612131cd2e242b0dffc81a767b9d6c118 [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 {
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 Willemsenead184a2015-06-23 22:47:38 -070031 a \
32 b`,
Dan Willemsenf33877b2015-06-23 23:34:49 -070033 },
34 {
35 blueprint: `test = Var + "b"`,
36 expected: `$(Var)b`,
37 },
38 {
39 blueprint: `test = ["a"] + ["b"]`,
40 expected: `\
Dan Willemsenead184a2015-06-23 22:47:38 -070041 a\
42 b`,
Dan Willemsenf33877b2015-06-23 23:34:49 -070043 },
44}
45
46func 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 Crossb0931242015-06-29 14:18:27 -070053 str, err := valueToString(blueprint.Defs[0].(*bpparser.Assignment).Value)
54 if err != nil {
55 t.Error(err.Error())
56 }
Dan Willemsenead184a2015-06-23 22:47:38 -070057 expect(t, testCase.blueprint, testCase.expected, str)
58 }
59}
60
61var 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 Willemsen49f50452015-06-24 14:56:00 -070090 // 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 Willemsen3a4045d2015-06-24 15:37:17 -0700101 // 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 Crossb1a66c02015-06-29 16:24:57 -0700120 // 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 Willemsenead184a2015-06-23 22:47:38 -0700134}
135
136func 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 Willemsenf33877b2015-06-23 23:34:49 -0700141 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700142
143 buf := &bytes.Buffer{}
144 writer := &androidMkWriter{
145 blueprint: blueprint,
146 path: "",
147 mapScope: make(map[string][]*bpparser.Property),
Colin Crossb0931242015-06-29 14:18:27 -0700148 Writer: buf,
Dan Willemsenead184a2015-06-23 22:47:38 -0700149 }
150
151 module := blueprint.Defs[0].(*bpparser.Module)
Colin Crossb0931242015-06-29 14:18:27 -0700152 err := writer.handleModule(module)
153 if err != nil {
154 t.Errorf("Unexpected error %s", err.Error())
155 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700156
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
163func 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
171func 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 Willemsenf33877b2015-06-23 23:34:49 -0700184 }
185}