]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
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 | ||
32 | """Tests for the Pump meta-programming tool.""" | |
33 | ||
34 | from google3.testing.pybase import googletest | |
35 | import google3.third_party.googletest.googlemock.scripts.pump | |
36 | ||
37 | pump = google3.third_party.googletest.googlemock.scripts.pump | |
38 | Convert = pump.ConvertFromPumpSource | |
39 | StripMetaComments = pump.StripMetaComments | |
40 | ||
41 | ||
42 | class PumpTest(googletest.TestCase): | |
43 | ||
44 | def testConvertsEmptyToEmpty(self): | |
45 | self.assertEquals('', Convert('').strip()) | |
46 | ||
47 | def testConvertsPlainCodeToSame(self): | |
48 | self.assertEquals('#include <stdio.h>\n', | |
49 | Convert('#include <stdio.h>\n')) | |
50 | ||
51 | def testConvertsLongIWYUPragmaToSame(self): | |
52 | long_line = '// IWYU pragma: private, include "' + (80*'a') + '.h"\n' | |
53 | self.assertEquals(long_line, Convert(long_line)) | |
54 | ||
55 | def testConvertsIWYUPragmaWithLeadingSpaceToSame(self): | |
56 | long_line = ' // IWYU pragma: private, include "' + (80*'a') + '.h"\n' | |
57 | self.assertEquals(long_line, Convert(long_line)) | |
58 | ||
59 | def testConvertsIWYUPragmaWithSlashStarLeaderToSame(self): | |
60 | long_line = '/* IWYU pragma: private, include "' + (80*'a') + '.h"\n' | |
61 | self.assertEquals(long_line, Convert(long_line)) | |
62 | ||
63 | def testConvertsIWYUPragmaWithSlashStarAndSpacesToSame(self): | |
64 | long_line = ' /* IWYU pragma: private, include "' + (80*'a') + '.h"\n' | |
65 | self.assertEquals(long_line, Convert(long_line)) | |
66 | ||
67 | def testIgnoresMetaComment(self): | |
68 | self.assertEquals('', | |
69 | Convert('$$ This is a Pump meta comment.\n').strip()) | |
70 | ||
71 | def testSimpleVarDeclarationWorks(self): | |
72 | self.assertEquals('3\n', | |
73 | Convert('$var m = 3\n' | |
74 | '$m\n')) | |
75 | ||
76 | def testVarDeclarationCanReferenceEarlierVar(self): | |
77 | self.assertEquals('43 != 3;\n', | |
78 | Convert('$var a = 42\n' | |
79 | '$var b = a + 1\n' | |
80 | '$var c = (b - a)*3\n' | |
81 | '$b != $c;\n')) | |
82 | ||
83 | def testSimpleLoopWorks(self): | |
84 | self.assertEquals('1, 2, 3, 4, 5\n', | |
85 | Convert('$var n = 5\n' | |
86 | '$range i 1..n\n' | |
87 | '$for i, [[$i]]\n')) | |
88 | ||
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' | |
93 | '$for i, [[$i]]\n')) | |
94 | ||
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' | |
99 | '$for i, [[$i]]\n')) | |
100 | ||
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')) | |
105 | ||
106 | def testCanGenerateDollarSign(self): | |
107 | self.assertEquals('$\n', Convert('$($)\n')) | |
108 | ||
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')) | |
113 | ||
114 | def testConditionalWithoutElseBranchWorks(self): | |
115 | self.assertEquals('true\n', | |
116 | Convert('$var n = 5\n' | |
117 | '$if n > 0 [[true]]\n')) | |
118 | ||
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')) | |
126 | ||
127 | def testConditionalWithCascadingElseBranchWorks(self): | |
128 | self.assertEquals('a\n', | |
129 | Convert('$var n = 5\n' | |
130 | '$if n > 0 [[a]]\n' | |
131 | '$elif n > 10 [[b]]\n' | |
132 | '$else [[c]]\n')) | |
133 | self.assertEquals('b\n', | |
134 | Convert('$var n = 5\n' | |
135 | '$if n > 10 [[a]]\n' | |
136 | '$elif n > 0 [[b]]\n' | |
137 | '$else [[c]]\n')) | |
138 | self.assertEquals('c\n', | |
139 | Convert('$var n = 5\n' | |
140 | '$if n > 10 [[a]]\n' | |
141 | '$elif n > 8 [[b]]\n' | |
142 | '$else [[c]]\n')) | |
143 | ||
144 | def testNestedLexicalBlocksWork(self): | |
145 | self.assertEquals('a = 5;\n', | |
146 | Convert('$var n = 5\n' | |
147 | 'a = [[$if n > 0 [[$n]]]];\n')) | |
148 | ||
149 | ||
150 | class StripMetaCommentsTest(googletest.TestCase): | |
151 | ||
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')) | |
159 | ||
160 | def testStripsSimpleComment(self): | |
161 | self.assertEquals('yes\n', StripMetaComments('yes $$ or no?\n')) | |
162 | ||
163 | def testStripsSimpleCommentWithMissingNewline(self): | |
164 | self.assertEquals('yes', StripMetaComments('yes $$ or no?')) | |
165 | ||
166 | def testStripsPureCommentLinesEntirely(self): | |
167 | self.assertEquals('yes\n', | |
168 | StripMetaComments('$$ a pure comment line.\n' | |
169 | 'yes $$ or no?\n' | |
170 | ' $$ another comment line.\n')) | |
171 | ||
172 | def testStripsCommentsFromMultiLineText(self): | |
173 | self.assertEquals('multi-\n' | |
174 | 'line\n' | |
175 | 'text is fine.', | |
176 | StripMetaComments('multi- $$ comment 1\n' | |
177 | 'line\n' | |
178 | 'text is fine. $$ comment 2')) | |
179 | ||
180 | ||
181 | if __name__ == '__main__': | |
182 | googletest.main() |