blob: 703dd4d8f664556bb00749b24ac951382d5b26b7 [file] [log] [blame]
Spandan Dasaacf2372022-05-11 21:46:29 +00001#!/usr/bin/env python
2#
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import unittest
18
19from io import StringIO
20
21from ninja_writer import Writer
22from ninja_syntax import Variable, Rule, BuildAction
23
24class TestWriter(unittest.TestCase):
25
26 def test_simple_writer(self):
27 with StringIO() as f:
28 writer = Writer(f)
29 writer.add_variable(Variable(name="cflags", value="-Wall"))
30 writer.add_newline()
31 cc = Rule(name="cc")
32 cc.add_variable(name="command", value="gcc $cflags -c $in -o $out")
33 writer.add_rule(cc)
34 writer.add_newline()
35 build_action = BuildAction(output="foo.o", rule="cc", inputs=["foo.c"])
36 writer.add_build_action(build_action)
37 writer.write()
38 self.assertEqual('''cflags = -Wall
39
40rule cc
41 command = gcc $cflags -c $in -o $out
42
43build foo.o: cc foo.c
44''', f.getvalue())
45
46 def test_comment(self):
47 with StringIO() as f:
48 writer = Writer(f)
49 writer.add_comment("This is a comment in a ninja file")
50 writer.write()
51 self.assertEqual("# This is a comment in a ninja file\n", f.getvalue())
52
53if __name__ == "__main__":
54 unittest.main()