• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6import unittest
7
8from idl_lexer import IDLLexer
9from idl_ppapi_lexer import IDLPPAPILexer
10
11#
12# FileToTokens
13#
14# From a source file generate a list of tokens.
15#
16def FileToTokens(lexer, filename):
17  with open(filename, 'rb') as srcfile:
18    lexer.Tokenize(srcfile.read(), filename)
19    return lexer.GetTokens()
20
21
22#
23# TextToTokens
24#
25# From a source file generate a list of tokens.
26#
27def TextToTokens(lexer, text):
28  lexer.Tokenize(text)
29  return lexer.GetTokens()
30
31
32class WebIDLLexer(unittest.TestCase):
33  def setUp(self):
34    self.lexer = IDLLexer()
35    self.filenames = [
36        'test_lexer/values.in',
37        'test_lexer/keywords.in'
38    ]
39
40  #
41  # testRebuildText
42  #
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
45  # old set.
46  #
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)
52
53      count1 = len(tokens1)
54      count2 = len(tokens2)
55      self.assertEqual(count1, count2)
56
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)
61
62  #
63  # testExpectedType
64  #
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.
71  #
72  def testExpectedType(self):
73    for filename in self.filenames:
74      tokens = FileToTokens(self.lexer, filename)
75      count = len(tokens)
76      self.assertTrue(count > 0)
77      self.assertFalse(count & 1)
78
79      index = 0
80      while index < count:
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)
85        index += 2
86        self.assertEqual(expect_type, actual_type, msg)
87
88
89class PepperIDLLexer(WebIDLLexer):
90  def setUp(self):
91    self.lexer = IDLPPAPILexer()
92    self.filenames = [
93        'test_lexer/values_ppapi.in',
94        'test_lexer/keywords_ppapi.in'
95    ]
96
97
98if __name__ == '__main__':
99  unittest.main()
100