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.
8 from idl_lexer
import IDLLexer
9 from idl_ppapi_lexer
import IDLPPAPILexer
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()
25 # From a source file generate a list of tokens.
27 def TextToTokens(lexer
, text
):
29 return lexer
.GetTokens()
32 class WebIDLLexer(unittest
.TestCase
):
34 self
.lexer
= IDLLexer()
36 'test_lexer/values.in',
37 'test_lexer/keywords.in'
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
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
)
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
)
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
)
76 self
.assertTrue(count
> 0)
77 self
.assertFalse(count
& 1)
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
)
86 self
.assertEqual(expect_type
, actual_type
, msg
)
89 class PepperIDLLexer(WebIDLLexer
):
91 self
.lexer
= IDLPPAPILexer()
93 'test_lexer/values_ppapi.in',
94 'test_lexer/keywords_ppapi.in'
98 if __name__
== '__main__':