pump_test.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010, Google Inc.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following disclaimer
  14. # in the documentation and/or other materials provided with the
  15. # distribution.
  16. # * Neither the name of Google Inc. nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """Tests for the Pump meta-programming tool."""
  32. from google3.testing.pybase import googletest
  33. import google3.third_party.googletest.googlemock.scripts.pump
  34. pump = google3.third_party.googletest.googlemock.scripts.pump
  35. Convert = pump.ConvertFromPumpSource
  36. StripMetaComments = pump.StripMetaComments
  37. class PumpTest(googletest.TestCase):
  38. def testConvertsEmptyToEmpty(self):
  39. self.assertEquals('', Convert('').strip())
  40. def testConvertsPlainCodeToSame(self):
  41. self.assertEquals('#include <stdio.h>\n',
  42. Convert('#include <stdio.h>\n'))
  43. def testConvertsLongIWYUPragmaToSame(self):
  44. long_line = '// IWYU pragma: private, include "' + (80*'a') + '.h"\n'
  45. self.assertEquals(long_line, Convert(long_line))
  46. def testConvertsIWYUPragmaWithLeadingSpaceToSame(self):
  47. long_line = ' // IWYU pragma: private, include "' + (80*'a') + '.h"\n'
  48. self.assertEquals(long_line, Convert(long_line))
  49. def testConvertsIWYUPragmaWithSlashStarLeaderToSame(self):
  50. long_line = '/* IWYU pragma: private, include "' + (80*'a') + '.h"\n'
  51. self.assertEquals(long_line, Convert(long_line))
  52. def testConvertsIWYUPragmaWithSlashStarAndSpacesToSame(self):
  53. long_line = ' /* IWYU pragma: private, include "' + (80*'a') + '.h"\n'
  54. self.assertEquals(long_line, Convert(long_line))
  55. def testIgnoresMetaComment(self):
  56. self.assertEquals('',
  57. Convert('$$ This is a Pump meta comment.\n').strip())
  58. def testSimpleVarDeclarationWorks(self):
  59. self.assertEquals('3\n',
  60. Convert('$var m = 3\n'
  61. '$m\n'))
  62. def testVarDeclarationCanReferenceEarlierVar(self):
  63. self.assertEquals('43 != 3;\n',
  64. Convert('$var a = 42\n'
  65. '$var b = a + 1\n'
  66. '$var c = (b - a)*3\n'
  67. '$b != $c;\n'))
  68. def testSimpleLoopWorks(self):
  69. self.assertEquals('1, 2, 3, 4, 5\n',
  70. Convert('$var n = 5\n'
  71. '$range i 1..n\n'
  72. '$for i, [[$i]]\n'))
  73. def testSimpleLoopWithCommentWorks(self):
  74. self.assertEquals('1, 2, 3, 4, 5\n',
  75. Convert('$var n = 5 $$ This is comment 1.\n'
  76. '$range i 1..n $$ This is comment 2.\n'
  77. '$for i, [[$i]]\n'))
  78. def testNonTrivialRangeExpressionsWork(self):
  79. self.assertEquals('1, 2, 3, 4\n',
  80. Convert('$var n = 5\n'
  81. '$range i (n/n)..(n - 1)\n'
  82. '$for i, [[$i]]\n'))
  83. def testLoopWithoutSeparatorWorks(self):
  84. self.assertEquals('a + 1 + 2 + 3;\n',
  85. Convert('$range i 1..3\n'
  86. 'a$for i [[ + $i]];\n'))
  87. def testCanGenerateDollarSign(self):
  88. self.assertEquals('$\n', Convert('$($)\n'))
  89. def testCanIterpolateExpressions(self):
  90. self.assertEquals('a[2] = 3;\n',
  91. Convert('$var i = 1\n'
  92. 'a[$(i + 1)] = $(i*4 - 1);\n'))
  93. def testConditionalWithoutElseBranchWorks(self):
  94. self.assertEquals('true\n',
  95. Convert('$var n = 5\n'
  96. '$if n > 0 [[true]]\n'))
  97. def testConditionalWithElseBranchWorks(self):
  98. self.assertEquals('true -- really false\n',
  99. Convert('$var n = 5\n'
  100. '$if n > 0 [[true]]\n'
  101. '$else [[false]] -- \n'
  102. '$if n > 10 [[really true]]\n'
  103. '$else [[really false]]\n'))
  104. def testConditionalWithCascadingElseBranchWorks(self):
  105. self.assertEquals('a\n',
  106. Convert('$var n = 5\n'
  107. '$if n > 0 [[a]]\n'
  108. '$elif n > 10 [[b]]\n'
  109. '$else [[c]]\n'))
  110. self.assertEquals('b\n',
  111. Convert('$var n = 5\n'
  112. '$if n > 10 [[a]]\n'
  113. '$elif n > 0 [[b]]\n'
  114. '$else [[c]]\n'))
  115. self.assertEquals('c\n',
  116. Convert('$var n = 5\n'
  117. '$if n > 10 [[a]]\n'
  118. '$elif n > 8 [[b]]\n'
  119. '$else [[c]]\n'))
  120. def testNestedLexicalBlocksWork(self):
  121. self.assertEquals('a = 5;\n',
  122. Convert('$var n = 5\n'
  123. 'a = [[$if n > 0 [[$n]]]];\n'))
  124. class StripMetaCommentsTest(googletest.TestCase):
  125. def testReturnsSameStringIfItContainsNoComment(self):
  126. self.assertEquals('', StripMetaComments(''))
  127. self.assertEquals(' blah ', StripMetaComments(' blah '))
  128. self.assertEquals('A single $ is fine.',
  129. StripMetaComments('A single $ is fine.'))
  130. self.assertEquals('multiple\nlines',
  131. StripMetaComments('multiple\nlines'))
  132. def testStripsSimpleComment(self):
  133. self.assertEquals('yes\n', StripMetaComments('yes $$ or no?\n'))
  134. def testStripsSimpleCommentWithMissingNewline(self):
  135. self.assertEquals('yes', StripMetaComments('yes $$ or no?'))
  136. def testStripsPureCommentLinesEntirely(self):
  137. self.assertEquals('yes\n',
  138. StripMetaComments('$$ a pure comment line.\n'
  139. 'yes $$ or no?\n'
  140. ' $$ another comment line.\n'))
  141. def testStripsCommentsFromMultiLineText(self):
  142. self.assertEquals('multi-\n'
  143. 'line\n'
  144. 'text is fine.',
  145. StripMetaComments('multi- $$ comment 1\n'
  146. 'line\n'
  147. 'text is fine. $$ comment 2'))
  148. if __name__ == '__main__':
  149. googletest.main()