Remove APIPermission::kFileSystemWriteDirectory
[chromium-blink-merge.git] / tools / idl_parser / idl_lexer_test.py
blobf8d8bb9a26c531c6de24b4bbd3a12137cbff1237
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 os
7 import unittest
9 from idl_lexer import IDLLexer
10 from idl_ppapi_lexer import IDLPPAPILexer
14 # FileToTokens
16 # From a source file generate a list of tokens.
18 def FileToTokens(lexer, filename):
19 with open(filename, 'rb') as srcfile:
20 lexer.Tokenize(srcfile.read(), filename)
21 return lexer.GetTokens()
25 # TextToTokens
27 # From a source file generate a list of tokens.
29 def TextToTokens(lexer, text):
30 lexer.Tokenize(text)
31 return lexer.GetTokens()
34 class WebIDLLexer(unittest.TestCase):
35 def setUp(self):
36 self.lexer = IDLLexer()
37 cur_dir = os.path.dirname(os.path.realpath(__file__))
38 self.filenames = [
39 os.path.join(cur_dir, 'test_lexer/values.in'),
40 os.path.join(cur_dir, 'test_lexer/keywords.in')
44 # testRebuildText
46 # From a set of tokens, generate a new source text by joining with a
47 # single space. The new source is then tokenized and compared against the
48 # old set.
50 def testRebuildText(self):
51 for filename in self.filenames:
52 tokens1 = FileToTokens(self.lexer, filename)
53 to_text = '\n'.join(['%s' % t.value for t in tokens1])
54 tokens2 = TextToTokens(self.lexer, to_text)
56 count1 = len(tokens1)
57 count2 = len(tokens2)
58 self.assertEqual(count1, count2)
60 for i in range(count1):
61 msg = 'Value %s does not match original %s on line %d of %s.' % (
62 tokens2[i].value, tokens1[i].value, tokens1[i].lineno, filename)
63 self.assertEqual(tokens1[i].value, tokens2[i].value, msg)
66 # testExpectedType
68 # From a set of tokens pairs, verify the type field of the second matches
69 # the value of the first, so that:
70 # integer 123 float 1.1 ...
71 # will generate a passing test, when the first token has both the type and
72 # value of the keyword integer and the second has the type of integer and
73 # value of 123 and so on.
75 def testExpectedType(self):
76 for filename in self.filenames:
77 tokens = FileToTokens(self.lexer, filename)
78 count = len(tokens)
79 self.assertTrue(count > 0)
80 self.assertFalse(count & 1)
82 index = 0
83 while index < count:
84 expect_type = tokens[index].value
85 actual_type = tokens[index + 1].type
86 msg = 'Type %s does not match expected %s on line %d of %s.' % (
87 actual_type, expect_type, tokens[index].lineno, filename)
88 index += 2
89 self.assertEqual(expect_type, actual_type, msg)
92 class PepperIDLLexer(WebIDLLexer):
93 def setUp(self):
94 self.lexer = IDLPPAPILexer()
95 cur_dir = os.path.dirname(os.path.realpath(__file__))
96 self.filenames = [
97 os.path.join(cur_dir, 'test_lexer/values_ppapi.in'),
98 os.path.join(cur_dir, 'test_lexer/keywords_ppapi.in')
102 if __name__ == '__main__':
103 unittest.main()