[GCM] Persistence of account mappings.
[chromium-blink-merge.git] / tools / idl_parser / idl_lexer_test.py
blob8b20da85fbd647c92020a38ae54861fdd92cd8c6
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import unittest
8 from idl_lexer import IDLLexer
9 from idl_ppapi_lexer import IDLPPAPILexer
12 # FileToTokens
14 # From a source file generate a list of tokens.
16 def FileToTokens(lexer, filename):
17 with open(filename, 'rb') as srcfile:
18 lexer.Tokenize(srcfile.read(), filename)
19 return lexer.GetTokens()
23 # TextToTokens
25 # From a source file generate a list of tokens.
27 def TextToTokens(lexer, text):
28 lexer.Tokenize(text)
29 return lexer.GetTokens()
32 class WebIDLLexer(unittest.TestCase):
33 def setUp(self):
34 self.lexer = IDLLexer()
35 self.filenames = [
36 'test_lexer/values.in',
37 'test_lexer/keywords.in'
41 # testRebuildText
43 # From a set of tokens, generate a new source text by joining with a
44 # single space. The new source is then tokenized and compared against the
45 # old set.
47 def testRebuildText(self):
48 for filename in self.filenames:
49 tokens1 = FileToTokens(self.lexer, filename)
50 to_text = '\n'.join(['%s' % t.value for t in tokens1])
51 tokens2 = TextToTokens(self.lexer, to_text)
53 count1 = len(tokens1)
54 count2 = len(tokens2)
55 self.assertEqual(count1, count2)
57 for i in range(count1):
58 msg = 'Value %s does not match original %s on line %d of %s.' % (
59 tokens2[i].value, tokens1[i].value, tokens1[i].lineno, filename)
60 self.assertEqual(tokens1[i].value, tokens2[i].value, msg)
63 # testExpectedType
65 # From a set of tokens pairs, verify the type field of the second matches
66 # the value of the first, so that:
67 # integer 123 float 1.1 ...
68 # will generate a passing test, when the first token has both the type and
69 # value of the keyword integer and the second has the type of integer and
70 # value of 123 and so on.
72 def testExpectedType(self):
73 for filename in self.filenames:
74 tokens = FileToTokens(self.lexer, filename)
75 count = len(tokens)
76 self.assertTrue(count > 0)
77 self.assertFalse(count & 1)
79 index = 0
80 while index < count:
81 expect_type = tokens[index].value
82 actual_type = tokens[index + 1].type
83 msg = 'Type %s does not match expected %s on line %d of %s.' % (
84 actual_type, expect_type, tokens[index].lineno, filename)
85 index += 2
86 self.assertEqual(expect_type, actual_type, msg)
89 class PepperIDLLexer(WebIDLLexer):
90 def setUp(self):
91 self.lexer = IDLPPAPILexer()
92 self.filenames = [
93 'test_lexer/values_ppapi.in',
94 'test_lexer/keywords_ppapi.in'
98 if __name__ == '__main__':
99 unittest.main()