1695b41eeSopenharmony_ci#!/usr/bin/env python3 2695b41eeSopenharmony_ci 3695b41eeSopenharmony_ci# Copyright 2011 Google Inc. All Rights Reserved. 4695b41eeSopenharmony_ci# 5695b41eeSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 6695b41eeSopenharmony_ci# you may not use this file except in compliance with the License. 7695b41eeSopenharmony_ci# You may obtain a copy of the License at 8695b41eeSopenharmony_ci# 9695b41eeSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 10695b41eeSopenharmony_ci# 11695b41eeSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 12695b41eeSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 13695b41eeSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14695b41eeSopenharmony_ci# See the License for the specific language governing permissions and 15695b41eeSopenharmony_ci# limitations under the License. 16695b41eeSopenharmony_ci 17695b41eeSopenharmony_ciimport unittest 18695b41eeSopenharmony_ci 19695b41eeSopenharmony_citry: 20695b41eeSopenharmony_ci from StringIO import StringIO 21695b41eeSopenharmony_ciexcept ImportError: 22695b41eeSopenharmony_ci from io import StringIO 23695b41eeSopenharmony_ci 24695b41eeSopenharmony_ciimport ninja_syntax 25695b41eeSopenharmony_ci 26695b41eeSopenharmony_ciLONGWORD = 'a' * 10 27695b41eeSopenharmony_ciLONGWORDWITHSPACES = 'a'*5 + '$ ' + 'a'*5 28695b41eeSopenharmony_ciINDENT = ' ' 29695b41eeSopenharmony_ci 30695b41eeSopenharmony_ciclass TestLineWordWrap(unittest.TestCase): 31695b41eeSopenharmony_ci def setUp(self): 32695b41eeSopenharmony_ci self.out = StringIO() 33695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=8) 34695b41eeSopenharmony_ci 35695b41eeSopenharmony_ci def test_single_long_word(self): 36695b41eeSopenharmony_ci # We shouldn't wrap a single long word. 37695b41eeSopenharmony_ci self.n._line(LONGWORD) 38695b41eeSopenharmony_ci self.assertEqual(LONGWORD + '\n', self.out.getvalue()) 39695b41eeSopenharmony_ci 40695b41eeSopenharmony_ci def test_few_long_words(self): 41695b41eeSopenharmony_ci # We should wrap a line where the second word is overlong. 42695b41eeSopenharmony_ci self.n._line(' '.join(['x', LONGWORD, 'y'])) 43695b41eeSopenharmony_ci self.assertEqual(' $\n'.join(['x', 44695b41eeSopenharmony_ci INDENT + LONGWORD, 45695b41eeSopenharmony_ci INDENT + 'y']) + '\n', 46695b41eeSopenharmony_ci self.out.getvalue()) 47695b41eeSopenharmony_ci 48695b41eeSopenharmony_ci def test_comment_wrap(self): 49695b41eeSopenharmony_ci # Filenames should not be wrapped 50695b41eeSopenharmony_ci self.n.comment('Hello /usr/local/build-tools/bin') 51695b41eeSopenharmony_ci self.assertEqual('# Hello\n# /usr/local/build-tools/bin\n', 52695b41eeSopenharmony_ci self.out.getvalue()) 53695b41eeSopenharmony_ci 54695b41eeSopenharmony_ci def test_short_words_indented(self): 55695b41eeSopenharmony_ci # Test that indent is taking into account when breaking subsequent lines. 56695b41eeSopenharmony_ci # The second line should not be ' to tree', as that's longer than the 57695b41eeSopenharmony_ci # test layout width of 8. 58695b41eeSopenharmony_ci self.n._line('line_one to tree') 59695b41eeSopenharmony_ci self.assertEqual('''\ 60695b41eeSopenharmony_ciline_one $ 61695b41eeSopenharmony_ci to $ 62695b41eeSopenharmony_ci tree 63695b41eeSopenharmony_ci''', 64695b41eeSopenharmony_ci self.out.getvalue()) 65695b41eeSopenharmony_ci 66695b41eeSopenharmony_ci def test_few_long_words_indented(self): 67695b41eeSopenharmony_ci # Check wrapping in the presence of indenting. 68695b41eeSopenharmony_ci self.n._line(' '.join(['x', LONGWORD, 'y']), indent=1) 69695b41eeSopenharmony_ci self.assertEqual(' $\n'.join([' ' + 'x', 70695b41eeSopenharmony_ci ' ' + INDENT + LONGWORD, 71695b41eeSopenharmony_ci ' ' + INDENT + 'y']) + '\n', 72695b41eeSopenharmony_ci self.out.getvalue()) 73695b41eeSopenharmony_ci 74695b41eeSopenharmony_ci def test_escaped_spaces(self): 75695b41eeSopenharmony_ci self.n._line(' '.join(['x', LONGWORDWITHSPACES, 'y'])) 76695b41eeSopenharmony_ci self.assertEqual(' $\n'.join(['x', 77695b41eeSopenharmony_ci INDENT + LONGWORDWITHSPACES, 78695b41eeSopenharmony_ci INDENT + 'y']) + '\n', 79695b41eeSopenharmony_ci self.out.getvalue()) 80695b41eeSopenharmony_ci 81695b41eeSopenharmony_ci def test_fit_many_words(self): 82695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=78) 83695b41eeSopenharmony_ci 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) 84695b41eeSopenharmony_ci self.assertEqual('''\ 85695b41eeSopenharmony_ci command = cd ../../chrome; python ../tools/grit/grit/format/repack.py $ 86695b41eeSopenharmony_ci ../out/Debug/obj/chrome/chrome_dll.gen/repack/theme_resources_large.pak $ 87695b41eeSopenharmony_ci ../out/Debug/gen/chrome/theme_resources_large.pak 88695b41eeSopenharmony_ci''', 89695b41eeSopenharmony_ci self.out.getvalue()) 90695b41eeSopenharmony_ci 91695b41eeSopenharmony_ci def test_leading_space(self): 92695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=14) # force wrapping 93695b41eeSopenharmony_ci self.n.variable('foo', ['', '-bar', '-somethinglong'], 0) 94695b41eeSopenharmony_ci self.assertEqual('''\ 95695b41eeSopenharmony_cifoo = -bar $ 96695b41eeSopenharmony_ci -somethinglong 97695b41eeSopenharmony_ci''', 98695b41eeSopenharmony_ci self.out.getvalue()) 99695b41eeSopenharmony_ci 100695b41eeSopenharmony_ci def test_embedded_dollar_dollar(self): 101695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=15) # force wrapping 102695b41eeSopenharmony_ci self.n.variable('foo', ['a$$b', '-somethinglong'], 0) 103695b41eeSopenharmony_ci self.assertEqual('''\ 104695b41eeSopenharmony_cifoo = a$$b $ 105695b41eeSopenharmony_ci -somethinglong 106695b41eeSopenharmony_ci''', 107695b41eeSopenharmony_ci self.out.getvalue()) 108695b41eeSopenharmony_ci 109695b41eeSopenharmony_ci def test_two_embedded_dollar_dollars(self): 110695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=17) # force wrapping 111695b41eeSopenharmony_ci self.n.variable('foo', ['a$$b', '-somethinglong'], 0) 112695b41eeSopenharmony_ci self.assertEqual('''\ 113695b41eeSopenharmony_cifoo = a$$b $ 114695b41eeSopenharmony_ci -somethinglong 115695b41eeSopenharmony_ci''', 116695b41eeSopenharmony_ci self.out.getvalue()) 117695b41eeSopenharmony_ci 118695b41eeSopenharmony_ci def test_leading_dollar_dollar(self): 119695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=14) # force wrapping 120695b41eeSopenharmony_ci self.n.variable('foo', ['$$b', '-somethinglong'], 0) 121695b41eeSopenharmony_ci self.assertEqual('''\ 122695b41eeSopenharmony_cifoo = $$b $ 123695b41eeSopenharmony_ci -somethinglong 124695b41eeSopenharmony_ci''', 125695b41eeSopenharmony_ci self.out.getvalue()) 126695b41eeSopenharmony_ci 127695b41eeSopenharmony_ci def test_trailing_dollar_dollar(self): 128695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out, width=14) # force wrapping 129695b41eeSopenharmony_ci self.n.variable('foo', ['a$$', '-somethinglong'], 0) 130695b41eeSopenharmony_ci self.assertEqual('''\ 131695b41eeSopenharmony_cifoo = a$$ $ 132695b41eeSopenharmony_ci -somethinglong 133695b41eeSopenharmony_ci''', 134695b41eeSopenharmony_ci self.out.getvalue()) 135695b41eeSopenharmony_ci 136695b41eeSopenharmony_ciclass TestBuild(unittest.TestCase): 137695b41eeSopenharmony_ci def setUp(self): 138695b41eeSopenharmony_ci self.out = StringIO() 139695b41eeSopenharmony_ci self.n = ninja_syntax.Writer(self.out) 140695b41eeSopenharmony_ci 141695b41eeSopenharmony_ci def test_variables_dict(self): 142695b41eeSopenharmony_ci self.n.build('out', 'cc', 'in', variables={'name': 'value'}) 143695b41eeSopenharmony_ci self.assertEqual('''\ 144695b41eeSopenharmony_cibuild out: cc in 145695b41eeSopenharmony_ci name = value 146695b41eeSopenharmony_ci''', 147695b41eeSopenharmony_ci self.out.getvalue()) 148695b41eeSopenharmony_ci 149695b41eeSopenharmony_ci def test_variables_list(self): 150695b41eeSopenharmony_ci self.n.build('out', 'cc', 'in', variables=[('name', 'value')]) 151695b41eeSopenharmony_ci self.assertEqual('''\ 152695b41eeSopenharmony_cibuild out: cc in 153695b41eeSopenharmony_ci name = value 154695b41eeSopenharmony_ci''', 155695b41eeSopenharmony_ci self.out.getvalue()) 156695b41eeSopenharmony_ci 157695b41eeSopenharmony_ci def test_implicit_outputs(self): 158695b41eeSopenharmony_ci self.n.build('o', 'cc', 'i', implicit_outputs='io') 159695b41eeSopenharmony_ci self.assertEqual('''\ 160695b41eeSopenharmony_cibuild o | io: cc i 161695b41eeSopenharmony_ci''', 162695b41eeSopenharmony_ci self.out.getvalue()) 163695b41eeSopenharmony_ci 164695b41eeSopenharmony_ciclass TestExpand(unittest.TestCase): 165695b41eeSopenharmony_ci def test_basic(self): 166695b41eeSopenharmony_ci vars = {'x': 'X'} 167695b41eeSopenharmony_ci self.assertEqual('foo', ninja_syntax.expand('foo', vars)) 168695b41eeSopenharmony_ci 169695b41eeSopenharmony_ci def test_var(self): 170695b41eeSopenharmony_ci vars = {'xyz': 'XYZ'} 171695b41eeSopenharmony_ci self.assertEqual('fooXYZ', ninja_syntax.expand('foo$xyz', vars)) 172695b41eeSopenharmony_ci 173695b41eeSopenharmony_ci def test_vars(self): 174695b41eeSopenharmony_ci vars = {'x': 'X', 'y': 'YYY'} 175695b41eeSopenharmony_ci self.assertEqual('XYYY', ninja_syntax.expand('$x$y', vars)) 176695b41eeSopenharmony_ci 177695b41eeSopenharmony_ci def test_space(self): 178695b41eeSopenharmony_ci vars = {} 179695b41eeSopenharmony_ci self.assertEqual('x y z', ninja_syntax.expand('x$ y$ z', vars)) 180695b41eeSopenharmony_ci 181695b41eeSopenharmony_ci def test_locals(self): 182695b41eeSopenharmony_ci vars = {'x': 'a'} 183695b41eeSopenharmony_ci local_vars = {'x': 'b'} 184695b41eeSopenharmony_ci self.assertEqual('a', ninja_syntax.expand('$x', vars)) 185695b41eeSopenharmony_ci self.assertEqual('b', ninja_syntax.expand('$x', vars, local_vars)) 186695b41eeSopenharmony_ci 187695b41eeSopenharmony_ci def test_double(self): 188695b41eeSopenharmony_ci self.assertEqual('a b$c', ninja_syntax.expand('a$ b$$c', {})) 189695b41eeSopenharmony_ci 190695b41eeSopenharmony_ciif __name__ == '__main__': 191695b41eeSopenharmony_ci unittest.main() 192