blob: 4befa7370ed7960f32d9e1230b3e2eb115e7e7e8 [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 },
88}
89
90func TestModules(t *testing.T) {
91 for _, testCase := range moduleTestCases {
92 blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil)
93 if len(errs) > 0 {
94 t.Errorf("Failed to read blueprint: %q", errs)
Dan Willemsenf33877b2015-06-23 23:34:49 -070095 }
Dan Willemsenead184a2015-06-23 22:47:38 -070096
97 buf := &bytes.Buffer{}
98 writer := &androidMkWriter{
99 blueprint: blueprint,
100 path: "",
101 mapScope: make(map[string][]*bpparser.Property),
102 Writer: bufio.NewWriter(buf),
103 }
104
105 module := blueprint.Defs[0].(*bpparser.Module)
106 writer.handleModule(module)
107 writer.Flush()
108
109 expect(t, testCase.blueprint, testCase.androidmk, buf.String())
110 }
111}
112
113// Trim left whitespace, and any trailing newlines. Leave inner blank lines and
114// right whitespace so that we can still check line continuations are correct
115func trim(str string) string {
116 var list []string
117 for _, s := range strings.Split(str, "\n") {
118 list = append(list, strings.TrimLeftFunc(s, unicode.IsSpace))
119 }
120 return strings.TrimRight(strings.Join(list, "\n"), "\n")
121}
122
123func expect(t *testing.T, testCase string, expected string, out string) {
124 expected = trim(expected)
125 out = trim(out)
126 if expected != out {
127 sep := " "
128 if strings.Index(expected, "\n") != -1 || strings.Index(out, "\n") != -1 {
129 sep = "\n"
130 }
131
132 t.Errorf("test case: %s", testCase)
133 t.Errorf("unexpected difference:")
134 t.Errorf(" expected:%s%s", sep, expected)
135 t.Errorf(" got:%s%s", sep, out)
Dan Willemsenf33877b2015-06-23 23:34:49 -0700136 }
137}