idl_lexer_test.py 2.6 KB

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