1// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package blueprint
16
17import (
18	"bytes"
19	"strings"
20	"testing"
21)
22
23func ck(err error) {
24	if err != nil {
25		panic(err)
26	}
27}
28
29var ninjaWriterTestCases = []struct {
30	input  func(w *ninjaWriter)
31	output string
32}{
33	{
34		input: func(w *ninjaWriter) {
35			ck(w.Comment("foo"))
36		},
37		output: "# foo\n",
38	},
39	{
40		input: func(w *ninjaWriter) {
41			ck(w.Pool("foo"))
42		},
43		output: "pool foo\n",
44	},
45	{
46		input: func(w *ninjaWriter) {
47			ck(w.Rule("foo"))
48		},
49		output: "rule foo\n",
50	},
51	{
52		input: func(w *ninjaWriter) {
53			ck(w.Build("foo comment", "foo", testNinjaStrings("o3", "o4"),
54				testNinjaStrings("io3", "io4"), testNinjaStrings("e3", "e4"),
55				testNinjaStrings("i3", "i4"), testNinjaStrings("oo3", "oo4"),
56				testNinjaStrings("v3", "v4"), []string{"o1", "o2"},
57				[]string{"io1", "io2"}, []string{"e1", "e2"},
58				[]string{"i1", "i2"}, []string{"oo1", "oo2"},
59				[]string{"v1", "v2"}, nil))
60		},
61		output: "# foo comment\nbuild o1 o2 o3 o4 | io1 io2 io3 io4: foo e1 e2 e3 e4 | i1 i2 i3 i4 || oo1 oo2 $\n        oo3 oo4 |@ v1 v2 v3 v4\n",
62	},
63	{
64		input: func(w *ninjaWriter) {
65			ck(w.Build("foo comment", "foo",
66				testNinjaStrings(strings.Repeat("o", lineWidth)),
67				nil,
68				testNinjaStrings(strings.Repeat("i", lineWidth)),
69				nil, nil, nil, nil, nil, nil, nil, nil, nil, nil))
70		},
71		output: "# foo comment\nbuild $\n        " + strings.Repeat("o", lineWidth) + ": $\n        foo $\n        " + strings.Repeat("i", lineWidth) + "\n",
72	},
73	{
74		input: func(w *ninjaWriter) {
75			ck(w.Default(nil, testNinjaStrings("foo"), []string{"bar"}))
76		},
77		output: "default bar foo\n",
78	},
79	{
80		input: func(w *ninjaWriter) {
81			ck(w.Assign("foo", "bar"))
82		},
83		output: "foo = bar\n",
84	},
85	{
86		input: func(w *ninjaWriter) {
87			ck(w.ScopedAssign("foo", "bar"))
88		},
89		output: "    foo = bar\n",
90	},
91	{
92		input: func(w *ninjaWriter) {
93			ck(w.Subninja("build.ninja"))
94		},
95		output: "subninja build.ninja\n",
96	},
97	{
98		input: func(w *ninjaWriter) {
99			ck(w.BlankLine())
100		},
101		output: "\n",
102	},
103	{
104		input: func(w *ninjaWriter) {
105			ck(w.Pool("p"))
106			ck(w.ScopedAssign("depth", "3"))
107			ck(w.BlankLine())
108			ck(w.Comment("here comes a rule"))
109			ck(w.Rule("r"))
110			ck(w.ScopedAssign("command", "echo out: $out in: $in _arg: $_arg"))
111			ck(w.ScopedAssign("pool", "p"))
112			ck(w.BlankLine())
113			ck(w.Build("r comment", "r", testNinjaStrings("foo.o"),
114				nil, testNinjaStrings("foo.in"), nil, nil, nil, nil,
115				nil, nil, nil, nil, nil, nil))
116			ck(w.ScopedAssign("_arg", "arg value"))
117		},
118		output: `pool p
119    depth = 3
120
121# here comes a rule
122rule r
123    command = echo out: $out in: $in _arg: $_arg
124    pool = p
125
126# r comment
127build foo.o: r foo.in
128    _arg = arg value
129`,
130	},
131}
132
133func TestNinjaWriter(t *testing.T) {
134	for i, testCase := range ninjaWriterTestCases {
135		buf := bytes.NewBuffer(nil)
136		w := newNinjaWriter(buf)
137		testCase.input(w)
138		if buf.String() != testCase.output {
139			t.Errorf("incorrect output for test case %d", i)
140			t.Errorf("  expected: %q", testCase.output)
141			t.Errorf("       got: %q", buf.String())
142		}
143	}
144}
145
146func testNinjaStrings(s ...string) []*ninjaString {
147	ret, _ := parseNinjaStrings(nil, s)
148	return ret
149}
150