blob: e16fa086d07b8dbd27c78e449114af273aae3707 [file] [log] [blame]
Dan Willemsenf33877b2015-06-23 23:34:49 -07001package main
2
3import (
Dan Willemsenead184a2015-06-23 22:47:38 -07004 "bufio"
5 "bytes"
Dan Willemsenf33877b2015-06-23 23:34:49 -07006 "strings"
7 "testing"
Dan Willemsenead184a2015-06-23 22:47:38 -07008 "unicode"
Dan Willemsenf33877b2015-06-23 23:34:49 -07009
10 bpparser "github.com/google/blueprint/parser"
11)
12
13var 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 Willemsenead184a2015-06-23 22:47:38 -070032 a \
33 b`,
Dan Willemsenf33877b2015-06-23 23:34:49 -070034 },
35 {
36 blueprint: `test = Var + "b"`,
37 expected: `$(Var)b`,
38 },
39 {
40 blueprint: `test = ["a"] + ["b"]`,
41 expected: `\
Dan Willemsenead184a2015-06-23 22:47:38 -070042 a\
43 b`,
Dan Willemsenf33877b2015-06-23 23:34:49 -070044 },
45}
46
47func 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 Willemsenead184a2015-06-23 22:47:38 -070055 expect(t, testCase.blueprint, testCase.expected, str)
56 }
57}
58
59var 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 Willemsen49f50452015-06-24 14:56:00 -070088 // 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 Willemsen3a4045d2015-06-24 15:37:17 -070099 // 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 Willemsenead184a2015-06-23 22:47:38 -0700118}
119
120func 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 Willemsenf33877b2015-06-23 23:34:49 -0700125 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700126
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
145func 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
153func 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 Willemsenf33877b2015-06-23 23:34:49 -0700166 }
167}