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 "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) { 52 ck(w.Build("foo comment", "foo", []string{"o1", "o2"}, []string{"e1", "e2"}, 53 []string{"i1", "i2"}, []string{"oo1", "oo2"})) 54 }, 55 output: "# foo comment\nbuild o1 o2: foo e1 e2 | i1 i2 || oo1 oo2\n", 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) { 77 ck(w.BlankLine()) 78 }, 79 output: "\n", 80 }, 81 { 82 input: func(w *ninjaWriter) { 83 ck(w.Pool("p")) 84 ck(w.ScopedAssign("depth", "3")) 85 ck(w.BlankLine()) 86 ck(w.Comment("here comes a rule")) 87 ck(w.Rule("r")) 88 ck(w.ScopedAssign("command", "echo out: $out in: $in _arg: $_arg")) 89 ck(w.ScopedAssign("pool", "p")) 90 ck(w.BlankLine()) 91 ck(w.Build("r comment", "r", []string{"foo.o"}, []string{"foo.in"}, nil, nil)) 92 ck(w.ScopedAssign("_arg", "arg value")) 93 }, 94 output: `pool p 95 depth = 3 96 97# here comes a rule 98rule r 99 command = echo out: $out in: $in _arg: $_arg 100 pool = p 101 102# r comment 103build foo.o: r foo.in 104 _arg = arg value 105`, 106 }, 107} 108 109func TestNinjaWriter(t *testing.T) { 110 for i, testCase := range ninjaWriterTestCases { 111 buf := bytes.NewBuffer(nil) 112 w := newNinjaWriter(buf) 113 testCase.input(w) 114 if buf.String() != testCase.output { 115 t.Errorf("incorrect output for test case %d", i) 116 t.Errorf(" expected: %q", testCase.output) 117 t.Errorf(" got: %q", buf.String()) 118 } 119 } 120} 121