code_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env python3
  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.
  5. from code import Code
  6. import unittest
  7. class CodeTest(unittest.TestCase):
  8. def testAppend(self):
  9. c = Code()
  10. c.Append('line')
  11. self.assertEqual('line', c.Render())
  12. def testBlock(self):
  13. c = Code()
  14. (c.Append('line')
  15. .Sblock('sblock')
  16. .Append('inner')
  17. .Append('moreinner')
  18. .Sblock('moresblock')
  19. .Append('inner')
  20. .Eblock('out')
  21. .Append('inner')
  22. .Eblock('out')
  23. )
  24. self.assertEqual(
  25. 'line\n'
  26. 'sblock\n'
  27. ' inner\n'
  28. ' moreinner\n'
  29. ' moresblock\n'
  30. ' inner\n'
  31. ' out\n'
  32. ' inner\n'
  33. 'out',
  34. c.Render())
  35. def testConcat(self):
  36. b = Code()
  37. (b.Sblock('2')
  38. .Append('2')
  39. .Eblock('2')
  40. )
  41. c = Code()
  42. (c.Sblock('1')
  43. .Concat(b)
  44. .Append('1')
  45. .Eblock('1')
  46. )
  47. self.assertMultiLineEqual(
  48. '1\n'
  49. ' 2\n'
  50. ' 2\n'
  51. ' 2\n'
  52. ' 1\n'
  53. '1',
  54. c.Render())
  55. d = Code()
  56. a = Code()
  57. a.Concat(d)
  58. self.assertEqual('', a.Render())
  59. a.Concat(c)
  60. self.assertEqual(
  61. '1\n'
  62. ' 2\n'
  63. ' 2\n'
  64. ' 2\n'
  65. ' 1\n'
  66. '1',
  67. a.Render())
  68. def testConcatErrors(self):
  69. c = Code()
  70. d = Code()
  71. d.Append('%s')
  72. self.assertRaises(TypeError, c.Concat, d)
  73. d = Code()
  74. d.Append('%(classname)s')
  75. self.assertRaises(TypeError, c.Concat, d)
  76. d = 'line of code'
  77. self.assertRaises(TypeError, c.Concat, d)
  78. def testSubstitute(self):
  79. c = Code()
  80. c.Append('%(var1)s %(var2)s %(var1)s')
  81. c.Substitute({'var1': 'one', 'var2': 'two'})
  82. self.assertEqual('one two one', c.Render())
  83. c.Append('%(var1)s %(var2)s %(var3)s')
  84. c.Append('%(var2)s %(var1)s %(var3)s')
  85. c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
  86. self.assertEqual(
  87. 'one two one\n'
  88. 'one two three\n'
  89. 'two one three',
  90. c.Render())
  91. def testSubstituteErrors(self):
  92. # No unnamed placeholders allowed when substitute is run
  93. c = Code()
  94. c.Append('%s %s')
  95. self.assertRaises(TypeError, c.Substitute, ('var1', 'one'))
  96. c = Code()
  97. c.Append('%s %(var1)s')
  98. self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
  99. c = Code()
  100. c.Append('%s %(var1)s')
  101. self.assertRaises(TypeError, c.Substitute, {'var1': 'one'})
  102. c = Code()
  103. c.Append('%(var1)s')
  104. self.assertRaises(KeyError, c.Substitute, {'clearlynotvar1': 'one'})
  105. def testIsEmpty(self):
  106. c = Code()
  107. self.assertTrue(c.IsEmpty())
  108. c.Append('asdf')
  109. self.assertFalse(c.IsEmpty())
  110. def testComment(self):
  111. long_comment = ('This comment is ninety one characters in longness, '
  112. 'that is, using a different word, length.')
  113. c = Code()
  114. c.Comment(long_comment)
  115. self.assertEqual(
  116. '// This comment is ninety one characters '
  117. 'in longness, that is, using a different\n'
  118. '// word, length.',
  119. c.Render())
  120. c = Code()
  121. c.Sblock('sblock')
  122. c.Comment(long_comment)
  123. c.Eblock('eblock')
  124. c.Comment(long_comment)
  125. self.assertEqual(
  126. 'sblock\n'
  127. ' // This comment is ninety one characters '
  128. 'in longness, that is, using a\n'
  129. ' // different word, length.\n'
  130. 'eblock\n'
  131. '// This comment is ninety one characters in '
  132. 'longness, that is, using a different\n'
  133. '// word, length.',
  134. c.Render())
  135. # Words that cannot be broken up are left as too long.
  136. long_word = 'x' * 100
  137. c = Code()
  138. c.Comment('xxx')
  139. c.Comment(long_word)
  140. c.Comment('xxx')
  141. self.assertEqual(
  142. '// xxx\n'
  143. '// ' + 'x' * 100 + '\n'
  144. '// xxx',
  145. c.Render())
  146. c = Code(indent_size=2, comment_length=40)
  147. c.Comment('Pretend this is a Closure Compiler style comment, which should '
  148. 'both wrap and indent', comment_prefix=' * ', wrap_indent=4)
  149. self.assertEqual(
  150. ' * Pretend this is a Closure Compiler\n'
  151. ' * style comment, which should both\n'
  152. ' * wrap and indent',
  153. c.Render())
  154. def testCommentWithSpecialCharacters(self):
  155. c = Code()
  156. c.Comment('20% of 80%s')
  157. c.Substitute({})
  158. self.assertEqual('// 20% of 80%s', c.Render())
  159. d = Code()
  160. d.Append('90')
  161. d.Concat(c)
  162. self.assertEqual('90\n'
  163. '// 20% of 80%s',
  164. d.Render())
  165. def testLinePrefixes(self):
  166. c = Code()
  167. c.Sblock(line='/**', line_prefix=' * ')
  168. c.Sblock('@typedef {{')
  169. c.Append('foo: bar,')
  170. c.Sblock('baz: {')
  171. c.Append('x: y')
  172. c.Eblock('}')
  173. c.Eblock('}}')
  174. c.Eblock(line=' */')
  175. output = c.Render()
  176. self.assertMultiLineEqual(
  177. '/**\n'
  178. ' * @typedef {{\n'
  179. ' * foo: bar,\n'
  180. ' * baz: {\n'
  181. ' * x: y\n'
  182. ' * }\n'
  183. ' * }}\n'
  184. ' */',
  185. output)
  186. def testSameLineAppendConcatComment(self):
  187. c = Code()
  188. c.Append('This is a line.')
  189. c.Append('This too.', new_line=False)
  190. d = Code()
  191. d.Append('And this.')
  192. c.Concat(d, new_line=False)
  193. self.assertEqual('This is a line.This too.And this.', c.Render())
  194. c = Code()
  195. c.Append('This is a')
  196. c.Comment(' spectacular 80-character line thingy ' +
  197. 'that fits wonderfully everywhere.',
  198. comment_prefix='',
  199. new_line=False)
  200. self.assertEqual('This is a spectacular 80-character line thingy that ' +
  201. 'fits wonderfully everywhere.',
  202. c.Render())
  203. if __name__ == '__main__':
  204. unittest.main()