Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(637)

Side by Side Diff: mojo/public/bindings/pylib/parse/mojo_parser_unittest.py

Issue 217093002: Mojo: Mojom: Detect (lexically) bad or missing ordinals. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/pylib/parse/mojo_lexer.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import mojo_lexer 5 import mojo_lexer
6 import mojo_parser 6 import mojo_parser
7 import unittest 7 import unittest
8 8
9 9
10 class MojoParserTest(unittest.TestCase): 10 class MojoParserTest(unittest.TestCase):
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 MY_ENUM_6 = -6 / -2, 76 MY_ENUM_6 = -6 / -2,
77 MY_ENUM_7 = 3 | (1 << 2), 77 MY_ENUM_7 = 3 | (1 << 2),
78 MY_ENUM_8 = 16 >> 1, 78 MY_ENUM_8 = 16 >> 1,
79 MY_ENUM_9 = 1 ^ 15 & 8, 79 MY_ENUM_9 = 1 ^ 15 & 8,
80 MY_ENUM_10 = 110 % 100, 80 MY_ENUM_10 = 110 % 100,
81 MY_ENUM_MINUS_1 = ~0 81 MY_ENUM_MINUS_1 = ~0
82 }; 82 };
83 83
84 } // my_module 84 } // my_module
85 """ 85 """
86 self.maxDiff = 2000
87 expected = \ 86 expected = \
88 [('MODULE', 87 [('MODULE',
89 'my_module', 88 'my_module',
90 [('ENUM', 89 [('ENUM',
91 'MyEnum', 90 'MyEnum',
92 [('ENUM_FIELD', 'MY_ENUM_1', ('EXPRESSION', ['1'])), 91 [('ENUM_FIELD', 'MY_ENUM_1', ('EXPRESSION', ['1'])),
93 ('ENUM_FIELD', 'MY_ENUM_2', ('EXPRESSION', ['1', '+', '1'])), 92 ('ENUM_FIELD', 'MY_ENUM_2', ('EXPRESSION', ['1', '+', '1'])),
94 ('ENUM_FIELD', 'MY_ENUM_3', ('EXPRESSION', ['1', '*', '3'])), 93 ('ENUM_FIELD', 'MY_ENUM_3', ('EXPRESSION', ['1', '*', '3'])),
95 ('ENUM_FIELD', 94 ('ENUM_FIELD',
96 'MY_ENUM_4', 95 'MY_ENUM_4',
(...skipping 29 matching lines...) Expand all
126 MY_ENUM_1 = 1 ? 2 : 3 125 MY_ENUM_1 = 1 ? 2 : 3
127 }; 126 };
128 127
129 } // my_module 128 } // my_module
130 """ 129 """
131 with self.assertRaisesRegexp( 130 with self.assertRaisesRegexp(
132 mojo_lexer.LexError, 131 mojo_lexer.LexError,
133 r"^my_file\.mojom:4: Error: Illegal character '\?'$"): 132 r"^my_file\.mojom:4: Error: Illegal character '\?'$"):
134 mojo_parser.Parse(source, "my_file.mojom") 133 mojo_parser.Parse(source, "my_file.mojom")
135 134
135 def testSimpleOrdinals(self):
136 """Tests that (valid) ordinal values are scanned correctly."""
137 source = """\
138 module my_module {
139
140 // This isn't actually valid .mojom, but the problem (missing ordinals) should
141 // be handled at a different level.
142 struct MyStruct {
143 int32 a0 @0;
144 int32 a1 @1;
145 int32 a2 @2;
146 int32 a9 @9;
147 int32 a10 @10;
148 int32 a11 @11;
149 int32 a29 @29;
150 int32 a1234567890 @1234567890;
151 };
152
153 } // module my_module
154 """
155 expected = \
156 [('MODULE',
157 'my_module',
158 [('STRUCT',
159 'MyStruct',
160 None,
161 [('FIELD', 'int32', 'a0', '@0', None),
162 ('FIELD', 'int32', 'a1', '@1', None),
163 ('FIELD', 'int32', 'a2', '@2', None),
164 ('FIELD', 'int32', 'a9', '@9', None),
165 ('FIELD', 'int32', 'a10', '@10', None),
166 ('FIELD', 'int32', 'a11', '@11', None),
167 ('FIELD', 'int32', 'a29', '@29', None),
168 ('FIELD', 'int32', 'a1234567890', '@1234567890', None)])])]
169 self.assertEquals(mojo_parser.Parse(source, "my_file.mojom"), expected)
170
171 def testInvalidOrdinals(self):
172 """Tests that (lexically) invalid ordinals are correctly detected."""
173 source1 = """\
174 module my_module {
175
176 struct MyStruct {
177 int32 a_missing @;
178 };
179
180 } // module my_module
181 """
182 with self.assertRaisesRegexp(
183 mojo_lexer.LexError,
184 r"^my_file\.mojom:4: Error: Missing ordinal value$"):
185 mojo_parser.Parse(source1, "my_file.mojom")
186
187 source2 = """\
188 module my_module {
189
190 struct MyStruct {
191 int32 a_octal @01;
192 };
193
194 } // module my_module
195 """
196 with self.assertRaisesRegexp(
197 mojo_lexer.LexError,
198 r"^my_file\.mojom:4: Error: "
199 r"Octal and hexadecimal ordinal values not allowed$"):
200 mojo_parser.Parse(source2, "my_file.mojom")
201
202 source3 = """\
203 module my_module { struct MyStruct { int32 a_invalid_octal @08; }; }
204 """
205 with self.assertRaisesRegexp(
206 mojo_lexer.LexError,
207 r"^my_file\.mojom:1: Error: "
208 r"Octal and hexadecimal ordinal values not allowed$"):
209 mojo_parser.Parse(source3, "my_file.mojom")
210
211 source4 = """\
212 module my_module { struct MyStruct { int32 a_hex @0x1aB9; }; }
213 """
214 with self.assertRaisesRegexp(
215 mojo_lexer.LexError,
216 r"^my_file\.mojom:1: Error: "
217 r"Octal and hexadecimal ordinal values not allowed$"):
218 mojo_parser.Parse(source4, "my_file.mojom")
219
220 source5 = """\
221 module my_module { struct MyStruct { int32 a_hex @0X0; }; }
222 """
223 with self.assertRaisesRegexp(
224 mojo_lexer.LexError,
225 r"^my_file\.mojom:1: Error: "
226 r"Octal and hexadecimal ordinal values not allowed$"):
227 mojo_parser.Parse(source5, "my_file.mojom")
136 228
137 if __name__ == "__main__": 229 if __name__ == "__main__":
138 unittest.main() 230 unittest.main()
OLDNEW
« no previous file with comments | « mojo/public/bindings/pylib/parse/mojo_lexer.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698