| OLD | NEW |
| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 module my_module { | 35 module my_module { |
| 36 """ | 36 """ |
| 37 with self.assertRaisesRegexp( | 37 with self.assertRaisesRegexp( |
| 38 mojo_parser.ParseError, | 38 mojo_parser.ParseError, |
| 39 r"^my_file\.mojom: Error: Unexpected end of file$"): | 39 r"^my_file\.mojom: Error: Unexpected end of file$"): |
| 40 mojo_parser.Parse(source, "my_file.mojom") | 40 mojo_parser.Parse(source, "my_file.mojom") |
| 41 | 41 |
| 42 def testSimpleStruct(self): | 42 def testSimpleStruct(self): |
| 43 """Tests a simple .mojom source that just defines a struct.""" | 43 """Tests a simple .mojom source that just defines a struct.""" |
| 44 source ="""\ | 44 source = """\ |
| 45 module my_module { | 45 module my_module { |
| 46 | 46 |
| 47 struct MyStruct { | 47 struct MyStruct { |
| 48 int32 a; | 48 int32 a; |
| 49 double b; | 49 double b; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 } // module my_module | 52 } // module my_module |
| 53 """ | 53 """ |
| 54 # Note: Output as pretty-printed on failure by the test harness. | |
| 55 expected = \ | 54 expected = \ |
| 56 [('MODULE', | 55 [('MODULE', |
| 57 'my_module', | 56 'my_module', |
| 58 [('STRUCT', | 57 [('STRUCT', |
| 59 'MyStruct', | 58 'MyStruct', |
| 60 None, | 59 None, |
| 61 [('FIELD', 'int32', 'a', None, None), | 60 [('FIELD', 'int32', 'a', None, None), |
| 62 ('FIELD', 'double', 'b', None, None)])])] | 61 ('FIELD', 'double', 'b', None, None)])])] |
| 63 self.assertEquals(mojo_parser.Parse(source, "my_file.mojom"), expected) | 62 self.assertEquals(mojo_parser.Parse(source, "my_file.mojom"), expected) |
| 64 | 63 |
| 64 def testSimpleStructWithoutModule(self): |
| 65 """Tests a simple struct without an enclosing module.""" |
| 66 source = """\ |
| 67 struct MyStruct { |
| 68 int32 a; |
| 69 double b; |
| 70 }; |
| 71 """ |
| 72 expected = \ |
| 73 [('MODULE', |
| 74 '', |
| 75 [('STRUCT', |
| 76 'MyStruct', |
| 77 None, |
| 78 [('FIELD', 'int32', 'a', None, None), |
| 79 ('FIELD', 'double', 'b', None, None)])])] |
| 80 self.assertEquals(mojo_parser.Parse(source, "my_file.mojom"), expected) |
| 81 |
| 82 def testMissingModuleName(self): |
| 83 """Tests an (invalid) .mojom with a missing module name.""" |
| 84 source1 = """\ |
| 85 // Missing module name. |
| 86 module { |
| 87 struct MyStruct { |
| 88 int32 a; |
| 89 }; |
| 90 } |
| 91 """ |
| 92 with self.assertRaisesRegexp( |
| 93 mojo_parser.ParseError, |
| 94 r"^my_file\.mojom:2: Error: Unexpected '{':\nmodule {$"): |
| 95 mojo_parser.Parse(source1, "my_file.mojom") |
| 96 |
| 97 # Another similar case, but make sure that line-number tracking/reporting |
| 98 # is correct. |
| 99 source2 = """\ |
| 100 module |
| 101 // This line intentionally left unblank. |
| 102 |
| 103 { |
| 104 } |
| 105 """ |
| 106 with self.assertRaisesRegexp( |
| 107 mojo_parser.ParseError, |
| 108 r"^my_file\.mojom:4: Error: Unexpected '{':\n{$"): |
| 109 mojo_parser.Parse(source2, "my_file.mojom") |
| 110 |
| 65 def testEnumExpressions(self): | 111 def testEnumExpressions(self): |
| 66 """Tests an enum with values calculated using simple expressions.""" | 112 """Tests an enum with values calculated using simple expressions.""" |
| 67 source = """\ | 113 source = """\ |
| 68 module my_module { | 114 module my_module { |
| 69 | 115 |
| 70 enum MyEnum { | 116 enum MyEnum { |
| 71 MY_ENUM_1 = 1, | 117 MY_ENUM_1 = 1, |
| 72 MY_ENUM_2 = 1 + 1, | 118 MY_ENUM_2 = 1 + 1, |
| 73 MY_ENUM_3 = 1 * 3, | 119 MY_ENUM_3 = 1 * 3, |
| 74 MY_ENUM_4 = 2 * (1 + 1), | 120 MY_ENUM_4 = 2 * (1 + 1), |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 """ | 268 """ |
| 223 with self.assertRaisesRegexp( | 269 with self.assertRaisesRegexp( |
| 224 mojo_lexer.LexError, | 270 mojo_lexer.LexError, |
| 225 r"^my_file\.mojom:1: Error: " | 271 r"^my_file\.mojom:1: Error: " |
| 226 r"Octal and hexadecimal ordinal values not allowed$"): | 272 r"Octal and hexadecimal ordinal values not allowed$"): |
| 227 mojo_parser.Parse(source5, "my_file.mojom") | 273 mojo_parser.Parse(source5, "my_file.mojom") |
| 228 | 274 |
| 229 | 275 |
| 230 if __name__ == "__main__": | 276 if __name__ == "__main__": |
| 231 unittest.main() | 277 unittest.main() |
| OLD | NEW |