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 json
      7 import optparse
      8 import os
      9 import sys
     10 import unittest
     11 
     12 from idl_lexer import IDLLexer
     13 from idl_ppapi_lexer import IDLPPAPILexer
     14 
     15 #
     16 # FileToTokens
     17 #
     18 # From a source file generate a list of tokens.
     19 #
     20 def FileToTokens(lexer, filename):
     21   with open(filename, 'rb') as srcfile:
     22     lexer.Tokenize(srcfile.read(), filename)
     23     return lexer.GetTokens()
     24 
     25 
     26 #
     27 # TextToTokens
     28 #
     29 # From a source file generate a list of tokens.
     30 #
     31 def TextToTokens(lexer, text):
     32   lexer.Tokenize(text)
     33   return lexer.GetTokens()
     34 
     35 
     36 class WebIDLLexer(unittest.TestCase):
     37   def setUp(self):
     38     self.lexer = IDLLexer()
     39     self.filenames = [
     40         'test_lexer/values.in',
     41         'test_lexer/keywords.in'
     42     ]
     43 
     44   #
     45   # testRebuildText
     46   #
     47   # From a set of tokens, generate a new source text by joining with a
     48   # single space.  The new source is then tokenized and compared against the
     49   # old set.
     50   #
     51   def testRebuildText(self):
     52     for filename in self.filenames:
     53       tokens1 = FileToTokens(self.lexer, filename)
     54       to_text = '\n'.join(['%s' % t.value for t in tokens1])
     55       tokens2 = TextToTokens(self.lexer, to_text)
     56 
     57       count1 = len(tokens1)
     58       count2 = len(tokens2)
     59       self.assertEqual(count1, count2)
     60 
     61       for i in range(count1):
     62         msg = 'Value %s does not match original %s on line %d of %s.' % (
     63               tokens2[i].value, tokens1[i].value, tokens1[i].lineno, filename)
     64         self.assertEqual(tokens1[i].value, tokens2[i].value, msg)
     65 
     66   #
     67   # testExpectedType
     68   #
     69   # From a set of tokens pairs, verify the type field of the second matches
     70   # the value of the first, so that:
     71   # integer 123 float 1.1 ...
     72   # will generate a passing test, when the first token has both the type and
     73   # value of the keyword integer and the second has the type of integer and
     74   # value of 123 and so on.
     75   #
     76   def testExpectedType(self):
     77     for filename in self.filenames:
     78       tokens = FileToTokens(self.lexer, filename)
     79       count = len(tokens)
     80       self.assertTrue(count > 0)
     81       self.assertFalse(count & 1)
     82 
     83       index = 0
     84       while index < count:
     85         expect_type = tokens[index].value
     86         actual_type = tokens[index + 1].type
     87         msg = 'Type %s does not match expected %s on line %d of %s.' % (
     88               actual_type, expect_type, tokens[index].lineno, filename)
     89         index += 2
     90         self.assertEqual(expect_type, actual_type, msg)
     91 
     92 
     93 class PepperIDLLexer(WebIDLLexer):
     94   def setUp(self):
     95     self.lexer = IDLPPAPILexer()
     96     self.filenames = [
     97         'test_lexer/values_ppapi.in',
     98         'test_lexer/keywords_ppapi.in'
     99     ]
    100 
    101 
    102 if __name__ == '__main__':
    103   unittest.main()