blob: cc880e501580c08c9ee6be2bc4aa730b308edc1d [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// 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
Jamie Gennis1bc967e2014-05-27 16:34:41 -070015package 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) {
Dan Willemsen5c43e072016-10-25 21:26:12 -070052 ck(w.Build("foo comment", "foo", []string{"o1", "o2"}, []string{"io1", "io2"},
53 []string{"e1", "e2"}, []string{"i1", "i2"}, []string{"oo1", "oo2"}))
Jamie Gennis1bc967e2014-05-27 16:34:41 -070054 },
Dan Willemsen5c43e072016-10-25 21:26:12 -070055 output: "# foo comment\nbuild o1 o2 | io1 io2: foo e1 e2 | i1 i2 || oo1 oo2\n",
Jamie Gennis1bc967e2014-05-27 16:34:41 -070056 },
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) {
Dan Willemsenab223a52018-07-05 21:56:59 -070077 ck(w.Subninja("build.ninja"))
78 },
79 output: "subninja build.ninja\n",
80 },
81 {
82 input: func(w *ninjaWriter) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070083 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())
Dan Willemsen5c43e072016-10-25 21:26:12 -070097 ck(w.Build("r comment", "r", []string{"foo.o"}, nil, []string{"foo.in"}, nil, nil))
Jamie Gennis1bc967e2014-05-27 16:34:41 -070098 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
Doug Evansfcc67392015-11-08 12:21:58 -0800108# r comment
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700109build 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}