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	"testing"
20)
21
22func ck(err error) {
23	if err != nil {
24		panic(err)
25	}
26}
27
28var ninjaWriterTestCases = []struct {
29	input  func(w *ninjaWriter)
30	output string
31}{
32	{
33		input: func(w *ninjaWriter) {
34			ck(w.Comment("foo"))
35		},
36		output: "# foo\n",
37	},
38	{
39		input: func(w *ninjaWriter) {
40			ck(w.Pool("foo"))
41		},
42		output: "pool foo\n",
43	},
44	{
45		input: func(w *ninjaWriter) {
46			ck(w.Rule("foo"))
47		},
48		output: "rule foo\n",
49	},
50	{
51		input: func(w *ninjaWriter) {
52			ck(w.Build("foo comment", "foo", []string{"o1", "o2"}, []string{"io1", "io2"},
53				[]string{"e1", "e2"}, []string{"i1", "i2"}, []string{"oo1", "oo2"}, []string{"v1", "v2"}))
54		},
55		output: "# foo comment\nbuild o1 o2 | io1 io2: foo e1 e2 | i1 i2 || oo1 oo2 |@ v1 v2\n",
56	},
57	{
58		input: func(w *ninjaWriter) {
59			ck(w.Default("foo"))
60		},
61		output: "default foo\n",
62	},
63	{
64		input: func(w *ninjaWriter) {
65			ck(w.Assign("foo", "bar"))
66		},
67		output: "foo = bar\n",
68	},
69	{
70		input: func(w *ninjaWriter) {
71			ck(w.ScopedAssign("foo", "bar"))
72		},
73		output: "    foo = bar\n",
74	},
75	{
76		input: func(w *ninjaWriter) {
77			ck(w.Subninja("build.ninja"))
78		},
79		output: "subninja build.ninja\n",
80	},
81	{
82		input: func(w *ninjaWriter) {
83			ck(w.BlankLine())
84		},
85		output: "\n",
86	},
87	{
88		input: func(w *ninjaWriter) {
89			ck(w.Pool("p"))
90			ck(w.ScopedAssign("depth", "3"))
91			ck(w.BlankLine())
92			ck(w.Comment("here comes a rule"))
93			ck(w.Rule("r"))
94			ck(w.ScopedAssign("command", "echo out: $out in: $in _arg: $_arg"))
95			ck(w.ScopedAssign("pool", "p"))
96			ck(w.BlankLine())
97			ck(w.Build("r comment", "r", []string{"foo.o"}, nil, []string{"foo.in"}, nil, nil, nil))
98			ck(w.ScopedAssign("_arg", "arg value"))
99		},
100		output: `pool p
101    depth = 3
102
103# here comes a rule
104rule r
105    command = echo out: $out in: $in _arg: $_arg
106    pool = p
107
108# r comment
109build foo.o: r foo.in
110    _arg = arg value
111`,
112	},
113}
114
115func TestNinjaWriter(t *testing.T) {
116	for i, testCase := range ninjaWriterTestCases {
117		buf := bytes.NewBuffer(nil)
118		w := newNinjaWriter(buf)
119		testCase.input(w)
120		if buf.String() != testCase.output {
121			t.Errorf("incorrect output for test case %d", i)
122			t.Errorf("  expected: %q", testCase.output)
123			t.Errorf("       got: %q", buf.String())
124		}
125	}
126}
127