• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright 2011 Google Inc. All Rights Reserved.
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
19try:
20    from StringIO import StringIO
21except ImportError:
22    from io import StringIO
23
24import ninja_syntax
25
26LONGWORD = 'a' * 10
27LONGWORDWITHSPACES = 'a'*5 + '$ ' + 'a'*5
28INDENT = '    '
29
30class TestLineWordWrap(unittest.TestCase):
31    def setUp(self):
32        self.out = StringIO()
33        self.n = ninja_syntax.Writer(self.out, width=8)
34
35    def test_single_long_word(self):
36        # We shouldn't wrap a single long word.
37        self.n._line(LONGWORD)
38        self.assertEqual(LONGWORD + '\n', self.out.getvalue())
39
40    def test_few_long_words(self):
41        # We should wrap a line where the second word is overlong.
42        self.n._line(' '.join(['x', LONGWORD, 'y']))
43        self.assertEqual(' $\n'.join(['x',
44                                      INDENT + LONGWORD,
45                                      INDENT + 'y']) + '\n',
46                         self.out.getvalue())
47
48    def test_comment_wrap(self):
49        # Filenames should not be wrapped
50        self.n.comment('Hello /usr/local/build-tools/bin')
51        self.assertEqual('# Hello\n# /usr/local/build-tools/bin\n',
52                         self.out.getvalue())
53
54    def test_short_words_indented(self):
55        # Test that indent is taking into account when breaking subsequent lines.
56        # The second line should not be '    to tree', as that's longer than the
57        # test layout width of 8.
58        self.n._line('line_one to tree')
59        self.assertEqual('''\
60line_one $
61    to $
62    tree
63''',
64                         self.out.getvalue())
65
66    def test_few_long_words_indented(self):
67        # Check wrapping in the presence of indenting.
68        self.n._line(' '.join(['x', LONGWORD, 'y']), indent=1)
69        self.assertEqual(' $\n'.join(['  ' + 'x',
70                                      '  ' + INDENT + LONGWORD,
71                                      '  ' + INDENT + 'y']) + '\n',
72                         self.out.getvalue())
73
74    def test_escaped_spaces(self):
75        self.n._line(' '.join(['x', LONGWORDWITHSPACES, 'y']))
76        self.assertEqual(' $\n'.join(['x',
77                                      INDENT + LONGWORDWITHSPACES,
78                                      INDENT + 'y']) + '\n',
79                         self.out.getvalue())
80
81    def test_fit_many_words(self):
82        self.n = ninja_syntax.Writer(self.out, width=78)
83        self.n._line('command = cd ../../chrome; python ../tools/grit/grit/format/repack.py ../out/Debug/obj/chrome/chrome_dll.gen/repack/theme_resources_large.pak ../out/Debug/gen/chrome/theme_resources_large.pak', 1)
84        self.assertEqual('''\
85  command = cd ../../chrome; python ../tools/grit/grit/format/repack.py $
86      ../out/Debug/obj/chrome/chrome_dll.gen/repack/theme_resources_large.pak $
87      ../out/Debug/gen/chrome/theme_resources_large.pak
88''',
89                         self.out.getvalue())
90
91    def test_leading_space(self):
92        self.n = ninja_syntax.Writer(self.out, width=14)  # force wrapping
93        self.n.variable('foo', ['', '-bar', '-somethinglong'], 0)
94        self.assertEqual('''\
95foo = -bar $
96    -somethinglong
97''',
98                         self.out.getvalue())
99
100    def test_embedded_dollar_dollar(self):
101        self.n = ninja_syntax.Writer(self.out, width=15)  # force wrapping
102        self.n.variable('foo', ['a$$b', '-somethinglong'], 0)
103        self.assertEqual('''\
104foo = a$$b $
105    -somethinglong
106''',
107                         self.out.getvalue())
108
109    def test_two_embedded_dollar_dollars(self):
110        self.n = ninja_syntax.Writer(self.out, width=17)  # force wrapping
111        self.n.variable('foo', ['a$$b', '-somethinglong'], 0)
112        self.assertEqual('''\
113foo = a$$b $
114    -somethinglong
115''',
116                         self.out.getvalue())
117
118    def test_leading_dollar_dollar(self):
119        self.n = ninja_syntax.Writer(self.out, width=14)  # force wrapping
120        self.n.variable('foo', ['$$b', '-somethinglong'], 0)
121        self.assertEqual('''\
122foo = $$b $
123    -somethinglong
124''',
125                         self.out.getvalue())
126
127    def test_trailing_dollar_dollar(self):
128        self.n = ninja_syntax.Writer(self.out, width=14)  # force wrapping
129        self.n.variable('foo', ['a$$', '-somethinglong'], 0)
130        self.assertEqual('''\
131foo = a$$ $
132    -somethinglong
133''',
134                         self.out.getvalue())
135
136class TestBuild(unittest.TestCase):
137    def setUp(self):
138        self.out = StringIO()
139        self.n = ninja_syntax.Writer(self.out)
140
141    def test_variables_dict(self):
142        self.n.build('out', 'cc', 'in', variables={'name': 'value'})
143        self.assertEqual('''\
144build out: cc in
145  name = value
146''',
147                         self.out.getvalue())
148
149    def test_variables_list(self):
150        self.n.build('out', 'cc', 'in', variables=[('name', 'value')])
151        self.assertEqual('''\
152build out: cc in
153  name = value
154''',
155                         self.out.getvalue())
156
157    def test_implicit_outputs(self):
158        self.n.build('o', 'cc', 'i', implicit_outputs='io')
159        self.assertEqual('''\
160build o | io: cc i
161''',
162                         self.out.getvalue())
163
164class TestExpand(unittest.TestCase):
165    def test_basic(self):
166        vars = {'x': 'X'}
167        self.assertEqual('foo', ninja_syntax.expand('foo', vars))
168
169    def test_var(self):
170        vars = {'xyz': 'XYZ'}
171        self.assertEqual('fooXYZ', ninja_syntax.expand('foo$xyz', vars))
172
173    def test_vars(self):
174        vars = {'x': 'X', 'y': 'YYY'}
175        self.assertEqual('XYYY', ninja_syntax.expand('$x$y', vars))
176
177    def test_space(self):
178        vars = {}
179        self.assertEqual('x y z', ninja_syntax.expand('x$ y$ z', vars))
180
181    def test_locals(self):
182        vars = {'x': 'a'}
183        local_vars = {'x': 'b'}
184        self.assertEqual('a', ninja_syntax.expand('$x', vars))
185        self.assertEqual('b', ninja_syntax.expand('$x', vars, local_vars))
186
187    def test_double(self):
188        self.assertEqual('a b$c', ninja_syntax.expand('a$ b$$c', {}))
189
190if __name__ == '__main__':
191    unittest.main()
192