This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import unittest
  4. import logging
  5. import os
  6. import codecs
  7. logging.basicConfig(level=logging.INFO)
  8. from lark.tools.nearley import create_code_for_nearley_grammar, main as nearley_tool_main
  9. TEST_PATH = os.path.abspath(os.path.dirname(__file__))
  10. NEARLEY_PATH = os.path.join(TEST_PATH, 'nearley')
  11. BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin')
  12. if not os.path.exists(NEARLEY_PATH):
  13. logging.warn("Nearley not installed. Skipping Nearley tests!")
  14. raise ImportError("Skipping Nearley tests!")
  15. import js2py # Ensures that js2py exists, to avoid failing tests
  16. class TestNearley(unittest.TestCase):
  17. def test_css(self):
  18. fn = os.path.join(NEARLEY_PATH, 'examples/csscolor.ne')
  19. with open(fn) as f:
  20. grammar = f.read()
  21. code = create_code_for_nearley_grammar(grammar, 'csscolor', BUILTIN_PATH, os.path.dirname(fn))
  22. d = {}
  23. exec (code, d)
  24. parse = d['parse']
  25. c = parse('#a199ff')
  26. assert c['r'] == 161
  27. assert c['g'] == 153
  28. assert c['b'] == 255
  29. c = parse('rgb(255, 70%, 3)')
  30. assert c['r'] == 255
  31. assert c['g'] == 178
  32. assert c['b'] == 3
  33. def test_include(self):
  34. fn = os.path.join(NEARLEY_PATH, 'test/grammars/folder-test.ne')
  35. with open(fn) as f:
  36. grammar = f.read()
  37. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  38. d = {}
  39. exec (code, d)
  40. parse = d['parse']
  41. parse('a')
  42. parse('b')
  43. def test_multi_include(self):
  44. fn = os.path.join(NEARLEY_PATH, 'test/grammars/multi-include-test.ne')
  45. with open(fn) as f:
  46. grammar = f.read()
  47. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  48. d = {}
  49. exec (code, d)
  50. parse = d['parse']
  51. parse('a')
  52. parse('b')
  53. parse('c')
  54. def test_utf8(self):
  55. grammar = u'main -> "±a"'
  56. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  57. d = {}
  58. exec (code, d)
  59. parse = d['parse']
  60. parse(u'±a')
  61. def test_backslash(self):
  62. grammar = r'main -> "\""'
  63. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  64. d = {}
  65. exec (code, d)
  66. parse = d['parse']
  67. parse(u'"')
  68. def test_null(self):
  69. grammar = r'main -> "a" | null'
  70. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  71. d = {}
  72. exec (code, d)
  73. parse = d['parse']
  74. parse('a')
  75. parse('')
  76. def test_utf8_2(self):
  77. fn = os.path.join(TEST_PATH, 'grammars/unicode.ne')
  78. nearley_tool_main(fn, 'x', NEARLEY_PATH)
  79. def test_include_utf8(self):
  80. fn = os.path.join(TEST_PATH, 'grammars/include_unicode.ne')
  81. nearley_tool_main(fn, 'main', NEARLEY_PATH)
  82. if __name__ == '__main__':
  83. unittest.main()