3 # Copyright 2011 Google Inc. All Rights Reserved.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from StringIO import StringIO
22 from io import StringIO
27 LONGWORDWITHSPACES = 'a'*5 + '$ ' + 'a'*5
30 class TestLineWordWrap(unittest.TestCase):
33 self.n = ninja_syntax.Writer(self.out, width=8)
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())
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',
45 INDENT + 'y']) + '\n',
48 def test_comment_wrap(self):
49 # Filenames shoud not be wrapped
50 self.n.comment('Hello /usr/local/build-tools/bin')
51 self.assertEqual('# Hello\n# /usr/local/build-tools/bin\n',
54 def test_short_words_indented(self):
55 # Test that indent is taking into acount 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')
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',
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',
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)
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
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)
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('''\
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('''\
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('''\
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('''\
136 class TestBuild(unittest.TestCase):
138 self.out = StringIO()
139 self.n = ninja_syntax.Writer(self.out)
141 def test_variables_dict(self):
142 self.n.build('out', 'cc', 'in', variables={'name': 'value'})
143 self.assertEqual('''\
149 def test_variables_list(self):
150 self.n.build('out', 'cc', 'in', variables=[('name', 'value')])
151 self.assertEqual('''\
157 def test_implicit_outputs(self):
158 self.n.build('o', 'cc', 'i', implicit_outputs='io')
159 self.assertEqual('''\
164 class TestExpand(unittest.TestCase):
165 def test_basic(self):
167 self.assertEqual('foo', ninja_syntax.expand('foo', vars))
170 vars = {'xyz': 'XYZ'}
171 self.assertEqual('fooXYZ', ninja_syntax.expand('foo$xyz', vars))
174 vars = {'x': 'X', 'y': 'YYY'}
175 self.assertEqual('XYYY', ninja_syntax.expand('$x$y', vars))
177 def test_space(self):
179 self.assertEqual('x y z', ninja_syntax.expand('x$ y$ z', vars))
181 def test_locals(self):
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))
187 def test_double(self):
188 self.assertEqual('a b$c', ninja_syntax.expand('a$ b$$c', {}))
190 if __name__ == '__main__':