blob: 87170ed58bf143d11436dbd21e5646d341e759e6 [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 },
Colin Crossb1a66c02015-06-29 16:24:57 -0700107 // 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 Willemsenead184a2015-06-23 22:47:38 -0700121}
122
123func 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 Willemsenf33877b2015-06-23 23:34:49 -0700128 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700129
130 buf := &bytes.Buffer{}
131 writer := &androidMkWriter{
132 blueprint: blueprint,
133 path: "",
Colin Crossb0931242015-06-29 14:18:27 -0700134 Writer: buf,
Dan Willemsenead184a2015-06-23 22:47:38 -0700135 }
136
137 module := blueprint.Defs[0].(*bpparser.Module)
Colin Crossb0931242015-06-29 14:18:27 -0700138 err := writer.handleModule(module)
139 if err != nil {
140 t.Errorf("Unexpected error %s", err.Error())
141 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700142
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
149func 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
157func 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 Willemsenf33877b2015-06-23 23:34:49 -0700170 }
171}