Home | History | Annotate | Download | only in idl_parser
      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.
      5 
      6 import glob
      7 import unittest
      8 
      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
     13 
     14 def ParseCommentTest(comment):
     15   comment = comment.strip()
     16   comments = comment.split(None, 1)
     17   return comments[0], comments[1]
     18 
     19 
     20 class WebIDLParser(unittest.TestCase):
     21   def setUp(self):
     22     self.parser = IDLParser(IDLLexer(), mute_error=True)
     23     self.filenames = glob.glob('test_parser/*_web.idl')
     24 
     25   def _TestNode(self, node):
     26     comments = node.GetListOf('Comment')
     27     for comment in comments:
     28       check, value = ParseCommentTest(comment.GetName())
     29       if check == 'BUILD':
     30         msg = 'Expecting %s, but found %s.\n' % (value, str(node))
     31         self.assertEqual(value, str(node), msg)
     32 
     33       if check == 'ERROR':
     34         msg = node.GetLogLine('Expecting\n\t%s\nbut found \n\t%s\n' % (
     35                               value, str(node)))
     36         self.assertEqual(value, node.GetName(), msg)
     37 
     38       if check == 'PROP':
     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)
     43 
     44       if check == 'TREE':
     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)
     49 
     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.' %
     55                       filename)
     56 
     57       for node in filenode.GetChildren()[2:]:
     58         self._TestNode(node)
     59 
     60 
     61 class PepperIDLParser(unittest.TestCase):
     62   def setUp(self):
     63     self.parser = IDLPPAPIParser(IDLPPAPILexer(), mute_error=True)
     64     self.filenames = glob.glob('test_parser/*_ppapi.idl')
     65 
     66   def _TestNode(self, filename, node):
     67     comments = node.GetListOf('Comment')
     68     for comment in comments:
     69       check, value = ParseCommentTest(comment.GetName())
     70       if check == 'BUILD':
     71         msg = '%s - Expecting %s, but found %s.\n' % (
     72             filename, value, str(node))
     73         self.assertEqual(value, str(node), msg)
     74 
     75       if check == 'ERROR':
     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)
     79 
     80       if check == 'PROP':
     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)
     86 
     87       if check == 'TREE':
     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)
     93 
     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.' %
     99                       filename)
    100 
    101       for node in filenode.GetChildren()[2:]:
    102         self._TestNode(filename, node)
    103 
    104 if __name__ == '__main__':
    105   unittest.main(verbosity=2)
    106 
    107