blob: f5590e27da8547fabce03170f1248f60690d9161 [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 },
Dan Willemsenead184a2015-06-23 22:47:38 -0700120}
121
122func TestModules(t *testing.T) {
123 for _, testCase := range moduleTestCases {
124 blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil)
125 if len(errs) > 0 {
126 t.Errorf("Failed to read blueprint: %q", errs)
Dan Willemsenf33877b2015-06-23 23:34:49 -0700127 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700128
129 buf := &bytes.Buffer{}
130 writer := &androidMkWriter{
131 blueprint: blueprint,
132 path: "",
133 mapScope: make(map[string][]*bpparser.Property),
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}