blob: f558617a9076444707a0641fa3b7771e652310e5 [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"
Colin Cross8a401482021-01-21 18:27:14 -080019 "strings"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070020 "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) {
Colin Cross95bec332023-10-31 15:15:30 -070053 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))
Jamie Gennis1bc967e2014-05-27 16:34:41 -070060 },
Colin Cross95bec332023-10-31 15:15:30 -070061 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",
Jamie Gennis1bc967e2014-05-27 16:34:41 -070062 },
63 {
64 input: func(w *ninjaWriter) {
Colin Cross8a401482021-01-21 18:27:14 -080065 ck(w.Build("foo comment", "foo",
66 testNinjaStrings(strings.Repeat("o", lineWidth)),
67 nil,
68 testNinjaStrings(strings.Repeat("i", lineWidth)),
Colin Cross95bec332023-10-31 15:15:30 -070069 nil, nil, nil, nil, nil, nil, nil, nil, nil, nil))
Colin Cross8a401482021-01-21 18:27:14 -080070 },
Colin Cross95bec332023-10-31 15:15:30 -070071 output: "# foo comment\nbuild $\n " + strings.Repeat("o", lineWidth) + ": $\n foo $\n " + strings.Repeat("i", lineWidth) + "\n",
Colin Cross8a401482021-01-21 18:27:14 -080072 },
73 {
74 input: func(w *ninjaWriter) {
Colin Cross95bec332023-10-31 15:15:30 -070075 ck(w.Default(nil, testNinjaStrings("foo"), []string{"bar"}))
Jamie Gennis1bc967e2014-05-27 16:34:41 -070076 },
Colin Cross95bec332023-10-31 15:15:30 -070077 output: "default bar foo\n",
Jamie Gennis1bc967e2014-05-27 16:34:41 -070078 },
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) {
Dan Willemsenab223a52018-07-05 21:56:59 -070093 ck(w.Subninja("build.ninja"))
94 },
95 output: "subninja build.ninja\n",
96 },
97 {
98 input: func(w *ninjaWriter) {
Jamie Gennis1bc967e2014-05-27 16:34:41 -070099 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())
Colin Cross8a401482021-01-21 18:27:14 -0800113 ck(w.Build("r comment", "r", testNinjaStrings("foo.o"),
Colin Cross95bec332023-10-31 15:15:30 -0700114 nil, testNinjaStrings("foo.in"), nil, nil, nil, nil,
115 nil, nil, nil, nil, nil, nil))
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700116 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
Doug Evansfcc67392015-11-08 12:21:58 -0800126# r comment
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700127build 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}
Colin Cross8a401482021-01-21 18:27:14 -0800145
Colin Cross6126fe82023-06-14 16:14:10 -0700146func testNinjaStrings(s ...string) []*ninjaString {
Colin Cross8a401482021-01-21 18:27:14 -0800147 ret, _ := parseNinjaStrings(nil, s)
148 return ret
149}