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" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func ck(err error) { |
| 23 | if err != nil { |
| 24 | panic(err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | var 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 Willemsen | 5c43e07 | 2016-10-25 21:26:12 -0700 | [diff] [blame] | 52 | ck(w.Build("foo comment", "foo", []string{"o1", "o2"}, []string{"io1", "io2"}, |
| 53 | []string{"e1", "e2"}, []string{"i1", "i2"}, []string{"oo1", "oo2"})) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 54 | }, |
Dan Willemsen | 5c43e07 | 2016-10-25 21:26:12 -0700 | [diff] [blame] | 55 | output: "# foo comment\nbuild o1 o2 | io1 io2: foo e1 e2 | i1 i2 || oo1 oo2\n", |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 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) { |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 77 | ck(w.Subninja("build.ninja")) |
| 78 | }, |
| 79 | output: "subninja build.ninja\n", |
| 80 | }, |
| 81 | { |
| 82 | input: func(w *ninjaWriter) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 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()) |
Dan Willemsen | 5c43e07 | 2016-10-25 21:26:12 -0700 | [diff] [blame] | 97 | ck(w.Build("r comment", "r", []string{"foo.o"}, nil, []string{"foo.in"}, nil, nil)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 98 | ck(w.ScopedAssign("_arg", "arg value")) |
| 99 | }, |
| 100 | output: `pool p |
| 101 | depth = 3 |
| 102 | |
| 103 | # here comes a rule |
| 104 | rule r |
| 105 | command = echo out: $out in: $in _arg: $_arg |
| 106 | pool = p |
| 107 | |
Doug Evans | fcc6739 | 2015-11-08 12:21:58 -0800 | [diff] [blame] | 108 | # r comment |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 109 | build foo.o: r foo.in |
| 110 | _arg = arg value |
| 111 | `, |
| 112 | }, |
| 113 | } |
| 114 | |
| 115 | func 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 | } |