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 "strings" 20 "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) { 53 ck(w.Build("foo comment", "foo", testNinjaStrings("o1", "o2"), 54 testNinjaStrings("io1", "io2"), testNinjaStrings("e1", "e2"), 55 testNinjaStrings("i1", "i2"), testNinjaStrings("oo1", "oo2"), 56 testNinjaStrings("v1", "v2"), nil)) 57 }, 58 output: "# foo comment\nbuild o1 o2 | io1 io2: foo e1 e2 | i1 i2 || oo1 oo2 |@ v1 v2\n", 59 }, 60 { 61 input: func(w *ninjaWriter) { 62 ck(w.Build("foo comment", "foo", 63 testNinjaStrings(strings.Repeat("o", lineWidth)), 64 nil, 65 testNinjaStrings(strings.Repeat("i", lineWidth)), 66 nil, nil, nil, nil)) 67 }, 68 output: "# foo comment\nbuild $\n " + strings.Repeat("o", lineWidth) + ": foo $\n " + strings.Repeat("i", lineWidth) + "\n", 69 }, 70 { 71 input: func(w *ninjaWriter) { 72 ck(w.Default(nil, testNinjaStrings("foo")...)) 73 }, 74 output: "default foo\n", 75 }, 76 { 77 input: func(w *ninjaWriter) { 78 ck(w.Assign("foo", "bar")) 79 }, 80 output: "foo = bar\n", 81 }, 82 { 83 input: func(w *ninjaWriter) { 84 ck(w.ScopedAssign("foo", "bar")) 85 }, 86 output: " foo = bar\n", 87 }, 88 { 89 input: func(w *ninjaWriter) { 90 ck(w.Subninja("build.ninja")) 91 }, 92 output: "subninja build.ninja\n", 93 }, 94 { 95 input: func(w *ninjaWriter) { 96 ck(w.BlankLine()) 97 }, 98 output: "\n", 99 }, 100 { 101 input: func(w *ninjaWriter) { 102 ck(w.Pool("p")) 103 ck(w.ScopedAssign("depth", "3")) 104 ck(w.BlankLine()) 105 ck(w.Comment("here comes a rule")) 106 ck(w.Rule("r")) 107 ck(w.ScopedAssign("command", "echo out: $out in: $in _arg: $_arg")) 108 ck(w.ScopedAssign("pool", "p")) 109 ck(w.BlankLine()) 110 ck(w.Build("r comment", "r", testNinjaStrings("foo.o"), 111 nil, testNinjaStrings("foo.in"), nil, nil, nil, nil)) 112 ck(w.ScopedAssign("_arg", "arg value")) 113 }, 114 output: `pool p 115 depth = 3 116 117# here comes a rule 118rule r 119 command = echo out: $out in: $in _arg: $_arg 120 pool = p 121 122# r comment 123build foo.o: r foo.in 124 _arg = arg value 125`, 126 }, 127} 128 129func TestNinjaWriter(t *testing.T) { 130 for i, testCase := range ninjaWriterTestCases { 131 buf := bytes.NewBuffer(nil) 132 w := newNinjaWriter(buf) 133 testCase.input(w) 134 if buf.String() != testCase.output { 135 t.Errorf("incorrect output for test case %d", i) 136 t.Errorf(" expected: %q", testCase.output) 137 t.Errorf(" got: %q", buf.String()) 138 } 139 } 140} 141 142func testNinjaStrings(s ...string) []ninjaString { 143 ret, _ := parseNinjaStrings(nil, s) 144 return ret 145} 146