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.
9 from idl_lexer
import IDLLexer
10 from idl_ppapi_lexer
import IDLPPAPILexer
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()
27 # From a source file generate a list of tokens.
29 def TextToTokens(lexer
, text
):
31 return lexer
.GetTokens()
34 class WebIDLLexer(unittest
.TestCase
):
36 self
.lexer
= IDLLexer()
37 cur_dir
= os
.path
.dirname(os
.path
.realpath(__file__
))
39 os
.path
.join(cur_dir
, 'test_lexer/values.in'),
40 os
.path
.join(cur_dir
, 'test_lexer/keywords.in')
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
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
)
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
)
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
)
79 self
.assertTrue(count
> 0)
80 self
.assertFalse(count
& 1)
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
)
89 self
.assertEqual(expect_type
, actual_type
, msg
)
92 class PepperIDLLexer(WebIDLLexer
):
94 self
.lexer
= IDLPPAPILexer()
95 cur_dir
= os
.path
.dirname(os
.path
.realpath(__file__
))
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__':