]>
git.proxmox.com Git - ceph.git/blob - ceph/src/googletest/googlemock/test/pump_test.py
3 # Copyright 2010, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
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.
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.
32 """Tests for the Pump meta-programming tool."""
34 from google3
.testing
.pybase
import googletest
35 import google3
.third_party
.googletest
.googlemock
.scripts
.pump
37 pump
= google3
.third_party
.googletest
.googlemock
.scripts
.pump
38 Convert
= pump
.ConvertFromPumpSource
39 StripMetaComments
= pump
.StripMetaComments
42 class PumpTest(googletest
.TestCase
):
44 def testConvertsEmptyToEmpty(self
):
45 self
.assertEquals('', Convert('').strip())
47 def testConvertsPlainCodeToSame(self
):
48 self
.assertEquals('#include <stdio.h>\n',
49 Convert('#include <stdio.h>\n'))
51 def testConvertsLongIWYUPragmaToSame(self
):
52 long_line
= '// IWYU pragma: private, include "' + (80*'a') + '.h"\n'
53 self
.assertEquals(long_line
, Convert(long_line
))
55 def testConvertsIWYUPragmaWithLeadingSpaceToSame(self
):
56 long_line
= ' // IWYU pragma: private, include "' + (80*'a') + '.h"\n'
57 self
.assertEquals(long_line
, Convert(long_line
))
59 def testConvertsIWYUPragmaWithSlashStarLeaderToSame(self
):
60 long_line
= '/* IWYU pragma: private, include "' + (80*'a') + '.h"\n'
61 self
.assertEquals(long_line
, Convert(long_line
))
63 def testConvertsIWYUPragmaWithSlashStarAndSpacesToSame(self
):
64 long_line
= ' /* IWYU pragma: private, include "' + (80*'a') + '.h"\n'
65 self
.assertEquals(long_line
, Convert(long_line
))
67 def testIgnoresMetaComment(self
):
69 Convert('$$ This is a Pump meta comment.\n').strip())
71 def testSimpleVarDeclarationWorks(self
):
72 self
.assertEquals('3\n',
73 Convert('$var m = 3\n'
76 def testVarDeclarationCanReferenceEarlierVar(self
):
77 self
.assertEquals('43 != 3;\n',
78 Convert('$var a = 42\n'
80 '$var c = (b - a)*3\n'
83 def testSimpleLoopWorks(self
):
84 self
.assertEquals('1, 2, 3, 4, 5\n',
85 Convert('$var n = 5\n'
89 def testSimpleLoopWithCommentWorks(self
):
90 self
.assertEquals('1, 2, 3, 4, 5\n',
91 Convert('$var n = 5 $$ This is comment 1.\n'
92 '$range i 1..n $$ This is comment 2.\n'
95 def testNonTrivialRangeExpressionsWork(self
):
96 self
.assertEquals('1, 2, 3, 4\n',
97 Convert('$var n = 5\n'
98 '$range i (n/n)..(n - 1)\n'
101 def testLoopWithoutSeparatorWorks(self
):
102 self
.assertEquals('a + 1 + 2 + 3;\n',
103 Convert('$range i 1..3\n'
104 'a$for i [[ + $i]];\n'))
106 def testCanGenerateDollarSign(self
):
107 self
.assertEquals('$\n', Convert('$($)\n'))
109 def testCanIterpolateExpressions(self
):
110 self
.assertEquals('a[2] = 3;\n',
111 Convert('$var i = 1\n'
112 'a[$(i + 1)] = $(i*4 - 1);\n'))
114 def testConditionalWithoutElseBranchWorks(self
):
115 self
.assertEquals('true\n',
116 Convert('$var n = 5\n'
117 '$if n > 0 [[true]]\n'))
119 def testConditionalWithElseBranchWorks(self
):
120 self
.assertEquals('true -- really false\n',
121 Convert('$var n = 5\n'
122 '$if n > 0 [[true]]\n'
123 '$else [[false]] -- \n'
124 '$if n > 10 [[really true]]\n'
125 '$else [[really false]]\n'))
127 def testConditionalWithCascadingElseBranchWorks(self
):
128 self
.assertEquals('a\n',
129 Convert('$var n = 5\n'
131 '$elif n > 10 [[b]]\n'
133 self
.assertEquals('b\n',
134 Convert('$var n = 5\n'
136 '$elif n > 0 [[b]]\n'
138 self
.assertEquals('c\n',
139 Convert('$var n = 5\n'
141 '$elif n > 8 [[b]]\n'
144 def testNestedLexicalBlocksWork(self
):
145 self
.assertEquals('a = 5;\n',
146 Convert('$var n = 5\n'
147 'a = [[$if n > 0 [[$n]]]];\n'))
150 class StripMetaCommentsTest(googletest
.TestCase
):
152 def testReturnsSameStringIfItContainsNoComment(self
):
153 self
.assertEquals('', StripMetaComments(''))
154 self
.assertEquals(' blah ', StripMetaComments(' blah '))
155 self
.assertEquals('A single $ is fine.',
156 StripMetaComments('A single $ is fine.'))
157 self
.assertEquals('multiple\nlines',
158 StripMetaComments('multiple\nlines'))
160 def testStripsSimpleComment(self
):
161 self
.assertEquals('yes\n', StripMetaComments('yes $$ or no?\n'))
163 def testStripsSimpleCommentWithMissingNewline(self
):
164 self
.assertEquals('yes', StripMetaComments('yes $$ or no?'))
166 def testStripsPureCommentLinesEntirely(self
):
167 self
.assertEquals('yes\n',
168 StripMetaComments('$$ a pure comment line.\n'
170 ' $$ another comment line.\n'))
172 def testStripsCommentsFromMultiLineText(self
):
173 self
.assertEquals('multi-\n'
176 StripMetaComments('multi- $$ comment 1\n'
178 'text is fine. $$ comment 2'))
181 if __name__
== '__main__':