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_parser
import IDLParser
, ParseFile
12 def ParseCommentTest(comment
):
13 comment
= comment
.strip()
14 comments
= comment
.split(None, 1)
15 return comments
[0], comments
[1]
18 class WebIDLParser(unittest
.TestCase
):
20 self
.parser
= IDLParser(IDLLexer(), mute_error
=True)
21 self
.filenames
= glob
.glob('test_parser/*.idl')
23 def _TestNode(self
, node
):
24 comments
= node
.GetListOf('Comment')
25 for comment
in comments
:
26 check
, value
= ParseCommentTest(comment
.GetName())
28 msg
= 'Expecting %s, but found %s.\n' % (value
, str(node
))
29 self
.assertEqual(value
, str(node
), msg
)
32 msg
= node
.GetLogLine('Expecting\n\t%s\nbut found \n\t%s\n' % (
34 self
.assertEqual(value
, node
.GetName(), msg
)
37 key
, expect
= value
.split('=')
38 actual
= str(node
.GetProperty(key
))
39 msg
= 'Mismatched property %s: %s vs %s.\n' % (key
, expect
, actual
)
40 self
.assertEqual(expect
, actual
, msg
)
43 quick
= '\n'.join(node
.Tree())
44 lineno
= node
.GetProperty('LINENO')
45 msg
= 'Mismatched tree at line %d:\n%sVS\n%s' % (lineno
, value
, quick
)
46 self
.assertEqual(value
, quick
, msg
)
48 def testExpectedNodes(self
):
49 for filename
in self
.filenames
:
50 filenode
= ParseFile(self
.parser
, filename
)
51 children
= filenode
.GetChildren()
52 self
.assertTrue(len(children
) > 2, 'Expecting children in %s.' %
55 for node
in filenode
.GetChildren()[2:]:
59 if __name__
== '__main__':
60 unittest
.main(verbosity
=2)