2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
9 class CodeTest(unittest
.TestCase
):
13 self
.assertEquals('line', c
.Render())
51 self
.assertMultiLineEqual(
62 self
.assertEquals('', a
.Render())
73 def testConcatErrors(self
):
77 self
.assertRaises(TypeError, c
.Concat
, d
)
79 d
.Append('%(classname)s')
80 self
.assertRaises(TypeError, c
.Concat
, d
)
82 self
.assertRaises(TypeError, c
.Concat
, d
)
84 def testSubstitute(self
):
86 c
.Append('%(var1)s %(var2)s %(var1)s')
87 c
.Substitute({'var1': 'one', 'var2': 'two'})
88 self
.assertEquals('one two one', c
.Render())
89 c
.Append('%(var1)s %(var2)s %(var3)s')
90 c
.Append('%(var2)s %(var1)s %(var3)s')
91 c
.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
98 def testSubstituteErrors(self
):
99 # No unnamed placeholders allowed when substitute is run
102 self
.assertRaises(TypeError, c
.Substitute
, ('var1', 'one'))
104 c
.Append('%s %(var1)s')
105 self
.assertRaises(TypeError, c
.Substitute
, {'var1': 'one'})
107 c
.Append('%s %(var1)s')
108 self
.assertRaises(TypeError, c
.Substitute
, {'var1': 'one'})
111 self
.assertRaises(KeyError, c
.Substitute
, {'clearlynotvar1': 'one'})
113 def testIsEmpty(self
):
115 self
.assertTrue(c
.IsEmpty())
117 self
.assertFalse(c
.IsEmpty())
119 def testComment(self
):
120 long_comment
= ('This comment is ninety one characters in longness, '
121 'that is, using a different word, length.')
123 c
.Comment(long_comment
)
125 '// This comment is ninety one characters '
126 'in longness, that is, using a different\n'
131 c
.Comment(long_comment
)
133 c
.Comment(long_comment
)
136 ' // This comment is ninety one characters '
137 'in longness, that is, using a\n'
138 ' // different word, length.\n'
140 '// This comment is ninety one characters in '
141 'longness, that is, using a different\n'
144 long_word
= 'x' * 100
148 '// ' + 'x' * 77 + '\n'
151 c
= Code(indent_size
=2, comment_length
=40)
152 c
.Comment('Pretend this is a Closure Compiler style comment, which should '
153 'both wrap and indent', comment_prefix
=' * ', wrap_indent
=4)
155 ' * Pretend this is a Closure Compiler\n'
156 ' * style comment, which should both\n'
157 ' * wrap and indent',
160 def testCommentWithSpecialCharacters(self
):
162 c
.Comment('20% of 80%s')
164 self
.assertEquals('// 20% of 80%s', c
.Render())
168 self
.assertEquals('90\n'
172 def testLinePrefixes(self
):
174 c
.Sblock(line
='/**', line_prefix
=' * ')
175 c
.Sblock('@typedef {{')
176 c
.Append('foo: bar,')
183 self
.assertMultiLineEqual(
194 def testSameLineAppendConcatComment(self
):
196 c
.Append('This is a line.')
197 c
.Append('This too.', new_line
=False)
199 d
.Append('And this.')
200 c
.Concat(d
, new_line
=False)
201 self
.assertEquals('This is a line.This too.And this.', c
.Render())
203 c
.Append('This is a')
204 c
.Comment(' spectacular 80-character line thingy ' +
205 'that fits wonderfully everywhere.',
208 self
.assertEquals('This is a spectacular 80-character line thingy that ' +
209 'fits wonderfully everywhere.',
212 if __name__
== '__main__':