| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 | 57 |
| 58 class LexerTest(unittest.TestCase): | 58 class LexerTest(unittest.TestCase): |
| 59 """Tests |mojom.parse.lexer.Lexer|.""" | 59 """Tests |mojom.parse.lexer.Lexer|.""" |
| 60 | 60 |
| 61 def __init__(self, *args, **kwargs): | 61 def __init__(self, *args, **kwargs): |
| 62 unittest.TestCase.__init__(self, *args, **kwargs) | 62 unittest.TestCase.__init__(self, *args, **kwargs) |
| 63 # Clone all lexer instances from this one, since making a lexer is slow. | 63 # Clone all lexer instances from this one, since making a lexer is slow. |
| 64 self._zygote_lexer = lex.lex(mojom.parse.lexer.Lexer("my_file.mojom")) | 64 self._zygote_lexer = lex.lex(mojom.parse.lexer.Lexer("my_file.mojom")) |
| 65 | 65 |
| 66 def testValidIdentifiers(self): | 66 def testValidKeywords(self): |
| 67 """Tests identifiers.""" | 67 """Tests valid keywords.""" |
| 68 self.assertEquals(self._SingleTokenForInput("abcd"), | |
| 69 _MakeLexToken("NAME", "abcd")) | |
| 70 self.assertEquals(self._SingleTokenForInput("AbC_d012_"), | |
| 71 _MakeLexToken("NAME", "AbC_d012_")) | |
| 72 self.assertEquals(self._SingleTokenForInput("_0123"), | |
| 73 _MakeLexToken("NAME", "_0123")) | |
| 74 | |
| 75 def testInvalidIdentifiers(self): | |
| 76 with self.assertRaisesRegexp( | |
| 77 mojom.parse.lexer.LexError, | |
| 78 r"^my_file\.mojom:1: Error: Illegal character '\$'$"): | |
| 79 self._TokensForInput("$abc") | |
| 80 with self.assertRaisesRegexp( | |
| 81 mojom.parse.lexer.LexError, | |
| 82 r"^my_file\.mojom:1: Error: Illegal character '\$'$"): | |
| 83 self._TokensForInput("a$bc") | |
| 84 | |
| 85 def testValidSingleKeywords(self): | |
| 86 """Tests valid, single keywords.""" | |
| 87 self.assertEquals(self._SingleTokenForInput("handle"), | 68 self.assertEquals(self._SingleTokenForInput("handle"), |
| 88 _MakeLexTokenForKeyword("handle")) | 69 _MakeLexTokenForKeyword("handle")) |
| 89 self.assertEquals(self._SingleTokenForInput("data_pipe_consumer"), | 70 self.assertEquals(self._SingleTokenForInput("data_pipe_consumer"), |
| 90 _MakeLexTokenForKeyword("data_pipe_consumer")) | 71 _MakeLexTokenForKeyword("data_pipe_consumer")) |
| 91 self.assertEquals(self._SingleTokenForInput("data_pipe_producer"), | 72 self.assertEquals(self._SingleTokenForInput("data_pipe_producer"), |
| 92 _MakeLexTokenForKeyword("data_pipe_producer")) | 73 _MakeLexTokenForKeyword("data_pipe_producer")) |
| 93 self.assertEquals(self._SingleTokenForInput("message_pipe"), | 74 self.assertEquals(self._SingleTokenForInput("message_pipe"), |
| 94 _MakeLexTokenForKeyword("message_pipe")) | 75 _MakeLexTokenForKeyword("message_pipe")) |
| 95 self.assertEquals(self._SingleTokenForInput("import"), | 76 self.assertEquals(self._SingleTokenForInput("import"), |
| 96 _MakeLexTokenForKeyword("import")) | 77 _MakeLexTokenForKeyword("import")) |
| 97 self.assertEquals(self._SingleTokenForInput("module"), | 78 self.assertEquals(self._SingleTokenForInput("module"), |
| 98 _MakeLexTokenForKeyword("module")) | 79 _MakeLexTokenForKeyword("module")) |
| 99 self.assertEquals(self._SingleTokenForInput("struct"), | 80 self.assertEquals(self._SingleTokenForInput("struct"), |
| 100 _MakeLexTokenForKeyword("struct")) | 81 _MakeLexTokenForKeyword("struct")) |
| 101 self.assertEquals(self._SingleTokenForInput("interface"), | 82 self.assertEquals(self._SingleTokenForInput("interface"), |
| 102 _MakeLexTokenForKeyword("interface")) | 83 _MakeLexTokenForKeyword("interface")) |
| 103 self.assertEquals(self._SingleTokenForInput("enum"), | 84 self.assertEquals(self._SingleTokenForInput("enum"), |
| 104 _MakeLexTokenForKeyword("enum")) | 85 _MakeLexTokenForKeyword("enum")) |
| 105 | 86 |
| 106 def testValidSingleTokens(self): | 87 def testValidIdentifiers(self): |
| 107 """Tests valid, single (non-keyword) tokens.""" | 88 """Tests identifiers.""" |
| 89 self.assertEquals(self._SingleTokenForInput("abcd"), |
| 90 _MakeLexToken("NAME", "abcd")) |
| 91 self.assertEquals(self._SingleTokenForInput("AbC_d012_"), |
| 92 _MakeLexToken("NAME", "AbC_d012_")) |
| 93 self.assertEquals(self._SingleTokenForInput("_0123"), |
| 94 _MakeLexToken("NAME", "_0123")) |
| 95 |
| 96 def testInvalidIdentifiers(self): |
| 97 with self.assertRaisesRegexp( |
| 98 mojom.parse.lexer.LexError, |
| 99 r"^my_file\.mojom:1: Error: Illegal character '\$'$"): |
| 100 self._TokensForInput("$abc") |
| 101 with self.assertRaisesRegexp( |
| 102 mojom.parse.lexer.LexError, |
| 103 r"^my_file\.mojom:1: Error: Illegal character '\$'$"): |
| 104 self._TokensForInput("a$bc") |
| 105 |
| 106 def testDecimalIntegerConstants(self): |
| 107 self.assertEquals(self._SingleTokenForInput("0"), |
| 108 _MakeLexToken("INT_CONST_DEC", "0")) |
| 109 self.assertEquals(self._SingleTokenForInput("1"), |
| 110 _MakeLexToken("INT_CONST_DEC", "1")) |
| 111 self.assertEquals(self._SingleTokenForInput("123"), |
| 112 _MakeLexToken("INT_CONST_DEC", "123")) |
| 113 self.assertEquals(self._SingleTokenForInput("10"), |
| 114 _MakeLexToken("INT_CONST_DEC", "10")) |
| 115 |
| 116 def testValidTokens(self): |
| 117 """Tests valid tokens (which aren't tested elsewhere).""" |
| 118 # Keywords tested in |testValidKeywords|. |
| 108 # NAME tested in |testValidIdentifiers|. | 119 # NAME tested in |testValidIdentifiers|. |
| 109 self.assertEquals(self._SingleTokenForInput("@123"), | 120 self.assertEquals(self._SingleTokenForInput("@123"), |
| 110 _MakeLexToken("ORDINAL", "@123")) | 121 _MakeLexToken("ORDINAL", "@123")) |
| 111 self.assertEquals(self._SingleTokenForInput("456"), | 122 self.assertEquals(self._SingleTokenForInput("456"), |
| 112 _MakeLexToken("INT_CONST_DEC", "456")) | 123 _MakeLexToken("INT_CONST_DEC", "456")) |
| 113 self.assertEquals(self._SingleTokenForInput("0765"), | 124 self.assertEquals(self._SingleTokenForInput("0765"), |
| 114 _MakeLexToken("INT_CONST_OCT", "0765")) | 125 _MakeLexToken("INT_CONST_OCT", "0765")) |
| 115 self.assertEquals(self._SingleTokenForInput("0x01aB2eF3"), | 126 self.assertEquals(self._SingleTokenForInput("0x01aB2eF3"), |
| 116 _MakeLexToken("INT_CONST_HEX", "0x01aB2eF3")) | 127 _MakeLexToken("INT_CONST_HEX", "0x01aB2eF3")) |
| 117 self.assertEquals(self._SingleTokenForInput("123.456"), | 128 self.assertEquals(self._SingleTokenForInput("123.456"), |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 def _SingleTokenForInput(self, input_string): | 192 def _SingleTokenForInput(self, input_string): |
| 182 """Gets the single token for the given input string. (Raises an exception if | 193 """Gets the single token for the given input string. (Raises an exception if |
| 183 the input string does not result in exactly one token.)""" | 194 the input string does not result in exactly one token.)""" |
| 184 toks = self._TokensForInput(input_string) | 195 toks = self._TokensForInput(input_string) |
| 185 assert len(toks) == 1 | 196 assert len(toks) == 1 |
| 186 return toks[0] | 197 return toks[0] |
| 187 | 198 |
| 188 | 199 |
| 189 if __name__ == "__main__": | 200 if __name__ == "__main__": |
| 190 unittest.main() | 201 unittest.main() |
| OLD | NEW |