| Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 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 | |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 19 | "strings" |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func ck(err error) { |
| 24 | if err != nil { |
| 25 | panic(err) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | var 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 Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 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)) |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 60 | }, |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 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", |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | input: func(w *ninjaWriter) { |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 65 | ck(w.Build("foo comment", "foo", |
| 66 | testNinjaStrings(strings.Repeat("o", lineWidth)), |
| 67 | nil, |
| 68 | testNinjaStrings(strings.Repeat("i", lineWidth)), |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 69 | nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)) |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 70 | }, |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 71 | output: "# foo comment\nbuild $\n " + strings.Repeat("o", lineWidth) + ": $\n foo $\n " + strings.Repeat("i", lineWidth) + "\n", |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 72 | }, |
| 73 | { |
| 74 | input: func(w *ninjaWriter) { |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 75 | ck(w.Default(nil, testNinjaStrings("foo"), []string{"bar"})) |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 76 | }, |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 77 | output: "default bar foo\n", |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 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) { |
| Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 93 | ck(w.Subninja("build.ninja")) |
| 94 | }, |
| 95 | output: "subninja build.ninja\n", |
| 96 | }, |
| 97 | { |
| 98 | input: func(w *ninjaWriter) { |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 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()) |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 113 | ck(w.Build("r comment", "r", testNinjaStrings("foo.o"), |
| Colin Cross | 95bec33 | 2023-10-31 15:15:30 -0700 | [diff] [blame] | 114 | nil, testNinjaStrings("foo.in"), nil, nil, nil, nil, |
| 115 | nil, nil, nil, nil, nil, nil)) |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 116 | ck(w.ScopedAssign("_arg", "arg value")) |
| 117 | }, |
| 118 | output: `pool p |
| 119 | depth = 3 |
| 120 | |
| 121 | # here comes a rule |
| 122 | rule r |
| 123 | command = echo out: $out in: $in _arg: $_arg |
| 124 | pool = p |
| 125 | |
| Doug Evans | fcc6739 | 2015-11-08 12:21:58 -0800 | [diff] [blame] | 126 | # r comment |
| Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 127 | build foo.o: r foo.in |
| 128 | _arg = arg value |
| 129 | `, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | func 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 Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 145 | |
| Colin Cross | 6126fe8 | 2023-06-14 16:14:10 -0700 | [diff] [blame] | 146 | func testNinjaStrings(s ...string) []*ninjaString { |
| Colin Cross | 8a40148 | 2021-01-21 18:27:14 -0800 | [diff] [blame] | 147 | ret, _ := parseNinjaStrings(nil, s) |
| 148 | return ret |
| 149 | } |