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 imp | 5 import imp |
6 import os.path | 6 import os.path |
7 import sys | 7 import sys |
8 import unittest | 8 import unittest |
9 | 9 |
10 # Disable lint check for finding modules: | 10 # Disable lint check for finding modules: |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 source = """\ | 368 source = """\ |
369 module my_module { | 369 module my_module { |
370 | 370 |
371 enum MyEnum { | 371 enum MyEnum { |
372 MY_ENUM_1 = 1 ? 2 : 3 | 372 MY_ENUM_1 = 1 ? 2 : 3 |
373 }; | 373 }; |
374 | 374 |
375 } // my_module | 375 } // my_module |
376 """ | 376 """ |
377 with self.assertRaisesRegexp( | 377 with self.assertRaisesRegexp( |
378 lexer.LexError, | 378 parser.ParseError, |
379 r"^my_file\.mojom:4: Error: Illegal character '\?'$"): | 379 r"^my_file\.mojom:4: Error: Unexpected '\?':\n" |
| 380 r" *MY_ENUM_1 = 1 \? 2 : 3$"): |
380 parser.Parse(source, "my_file.mojom") | 381 parser.Parse(source, "my_file.mojom") |
381 | 382 |
382 def testSimpleOrdinals(self): | 383 def testSimpleOrdinals(self): |
383 """Tests that (valid) ordinal values are scanned correctly.""" | 384 """Tests that (valid) ordinal values are scanned correctly.""" |
384 | 385 |
385 source = """\ | 386 source = """\ |
386 module my_module { | 387 module my_module { |
387 | 388 |
388 // This isn't actually valid .mojom, but the problem (missing ordinals) | 389 // This isn't actually valid .mojom, but the problem (missing ordinals) |
389 // should be handled at a different level. | 390 // should be handled at a different level. |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 source2 = """\ | 968 source2 = """\ |
968 import // Missing string. | 969 import // Missing string. |
969 module {} | 970 module {} |
970 """ | 971 """ |
971 with self.assertRaisesRegexp( | 972 with self.assertRaisesRegexp( |
972 parser.ParseError, | 973 parser.ParseError, |
973 r"^my_file\.mojom:2: Error: Unexpected 'module':\n" | 974 r"^my_file\.mojom:2: Error: Unexpected 'module':\n" |
974 r" *module {}$"): | 975 r" *module {}$"): |
975 parser.Parse(source2, "my_file.mojom") | 976 parser.Parse(source2, "my_file.mojom") |
976 | 977 |
| 978 def testValidNullableTypes(self): |
| 979 """Tests parsing nullable types.""" |
| 980 |
| 981 source = """\ |
| 982 struct MyStruct { |
| 983 int32? a; // This is actually invalid, but handled at a different |
| 984 // level. |
| 985 string? b; |
| 986 int32[] ? c; |
| 987 string ? [] ? d; |
| 988 int32[]?[]? e; |
| 989 int32[1]? f; |
| 990 string?[1]? g; |
| 991 some_struct? h; |
| 992 handle? i; |
| 993 handle<data_pipe_consumer>? j; |
| 994 handle<data_pipe_producer>? k; |
| 995 handle<message_pipe>? l; |
| 996 handle<shared_buffer>? m; |
| 997 some_interface&? n; |
| 998 }; |
| 999 """ |
| 1000 expected = ast.Mojom( |
| 1001 None, |
| 1002 ast.ImportList(), |
| 1003 [ast.Struct( |
| 1004 'MyStruct', |
| 1005 None, |
| 1006 ast.StructBody( |
| 1007 [ast.StructField('a', None, 'int32?', None), |
| 1008 ast.StructField('b', None, 'string?', None), |
| 1009 ast.StructField('c', None, 'int32[]?', None), |
| 1010 ast.StructField('d', None, 'string?[]?', None), |
| 1011 ast.StructField('e', None, 'int32[]?[]?', None), |
| 1012 ast.StructField('f', None, 'int32[1]?', None), |
| 1013 ast.StructField('g', None, 'string?[1]?', None), |
| 1014 ast.StructField('h', None, 'some_struct?', None), |
| 1015 ast.StructField('i', None, 'handle?', None), |
| 1016 ast.StructField('j', None, 'handle<data_pipe_consumer>?', |
| 1017 None), |
| 1018 ast.StructField('k', None, 'handle<data_pipe_producer>?', |
| 1019 None), |
| 1020 ast.StructField('l', None, 'handle<message_pipe>?', None), |
| 1021 ast.StructField('m', None, 'handle<shared_buffer>?', None), |
| 1022 ast.StructField('n', None, 'some_interface&?', None)]))]) |
| 1023 self.assertEquals(parser.Parse(source, "my_file.mojom"), expected) |
| 1024 |
| 1025 def testInvalidNullableTypes(self): |
| 1026 """Tests that invalid nullable types are correctly detected.""" |
| 1027 source1 = """\ |
| 1028 struct MyStruct { |
| 1029 string?? a; |
| 1030 }; |
| 1031 """ |
| 1032 with self.assertRaisesRegexp( |
| 1033 parser.ParseError, |
| 1034 r"^my_file\.mojom:2: Error: Unexpected '\?':\n" |
| 1035 r" *string\?\? a;$"): |
| 1036 parser.Parse(source1, "my_file.mojom") |
| 1037 |
| 1038 source2 = """\ |
| 1039 struct MyStruct { |
| 1040 handle?<data_pipe_consumer> a; |
| 1041 }; |
| 1042 """ |
| 1043 with self.assertRaisesRegexp( |
| 1044 parser.ParseError, |
| 1045 r"^my_file\.mojom:2: Error: Unexpected '<':\n" |
| 1046 r" *handle\?<data_pipe_consumer> a;$"): |
| 1047 parser.Parse(source2, "my_file.mojom") |
| 1048 |
| 1049 source3 = """\ |
| 1050 struct MyStruct { |
| 1051 some_interface?& a; |
| 1052 }; |
| 1053 """ |
| 1054 with self.assertRaisesRegexp( |
| 1055 parser.ParseError, |
| 1056 r"^my_file\.mojom:2: Error: Unexpected '&':\n" |
| 1057 r" *some_interface\?& a;$"): |
| 1058 parser.Parse(source3, "my_file.mojom") |
977 | 1059 |
978 if __name__ == "__main__": | 1060 if __name__ == "__main__": |
979 unittest.main() | 1061 unittest.main() |
OLD | NEW |