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
11 from idl_ppapi_lexer
import IDLPPAPILexer
12 from idl_ppapi_parser
import IDLPPAPIParser
14 def ParseCommentTest(comment
):
15 comment
= comment
.strip()
16 comments
= comment
.split(None, 1)
17 return comments
[0], comments
[1]
20 class WebIDLParser(unittest
.TestCase
):
22 self
.parser
= IDLParser(IDLLexer(), mute_error
=True)
23 self
.filenames
= glob
.glob('test_parser/*_web.idl')
25 def _TestNode(self
, node
):
26 comments
= node
.GetListOf('Comment')
27 for comment
in comments
:
28 check
, value
= ParseCommentTest(comment
.GetName())
30 msg
= 'Expecting %s, but found %s.\n' % (value
, str(node
))
31 self
.assertEqual(value
, str(node
), msg
)
34 msg
= node
.GetLogLine('Expecting\n\t%s\nbut found \n\t%s\n' % (
36 self
.assertEqual(value
, node
.GetName(), msg
)
39 key
, expect
= value
.split('=')
40 actual
= str(node
.GetProperty(key
))
41 msg
= 'Mismatched property %s: %s vs %s.\n' % (key
, expect
, actual
)
42 self
.assertEqual(expect
, actual
, msg
)
45 quick
= '\n'.join(node
.Tree())
46 lineno
= node
.GetProperty('LINENO')
47 msg
= 'Mismatched tree at line %d:\n%sVS\n%s' % (lineno
, value
, quick
)
48 self
.assertEqual(value
, quick
, msg
)
50 def testExpectedNodes(self
):
51 for filename
in self
.filenames
:
52 filenode
= ParseFile(self
.parser
, filename
)
53 children
= filenode
.GetChildren()
54 self
.assertTrue(len(children
) > 2, 'Expecting children in %s.' %
57 for node
in filenode
.GetChildren()[2:]:
61 class PepperIDLParser(unittest
.TestCase
):
63 self
.parser
= IDLPPAPIParser(IDLPPAPILexer(), mute_error
=True)
64 self
.filenames
= glob
.glob('test_parser/*_ppapi.idl')
66 def _TestNode(self
, filename
, node
):
67 comments
= node
.GetListOf('Comment')
68 for comment
in comments
:
69 check
, value
= ParseCommentTest(comment
.GetName())
71 msg
= '%s - Expecting %s, but found %s.\n' % (
72 filename
, value
, str(node
))
73 self
.assertEqual(value
, str(node
), msg
)
76 msg
= node
.GetLogLine('%s - Expecting\n\t%s\nbut found \n\t%s\n' % (
77 filename
, value
, str(node
)))
78 self
.assertEqual(value
, node
.GetName(), msg
)
81 key
, expect
= value
.split('=')
82 actual
= str(node
.GetProperty(key
))
83 msg
= '%s - Mismatched property %s: %s vs %s.\n' % (
84 filename
, key
, expect
, actual
)
85 self
.assertEqual(expect
, actual
, msg
)
88 quick
= '\n'.join(node
.Tree())
89 lineno
= node
.GetProperty('LINENO')
90 msg
= '%s - Mismatched tree at line %d:\n%sVS\n%s' % (
91 filename
, lineno
, value
, quick
)
92 self
.assertEqual(value
, quick
, msg
)
94 def testExpectedNodes(self
):
95 for filename
in self
.filenames
:
96 filenode
= ParseFile(self
.parser
, filename
)
97 children
= filenode
.GetChildren()
98 self
.assertTrue(len(children
) > 2, 'Expecting children in %s.' %
101 for node
in filenode
.GetChildren()[2:]:
102 self
._TestNode
(filename
, node
)
104 if __name__
== '__main__':
105 unittest
.main(verbosity
=2)