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.

102 lines
2.5 KiB

  1. from __future__ import absolute_import
  2. import sys
  3. from unittest import TestCase, main
  4. from lark import Lark, Tree
  5. from lark.lexer import Lexer, Token
  6. import lark.lark as lark_module
  7. try:
  8. from StringIO import StringIO
  9. except ImportError:
  10. from io import BytesIO as StringIO
  11. import tempfile, os
  12. class MockFile(StringIO):
  13. def close(self):
  14. pass
  15. def __enter__(self):
  16. return self
  17. def __exit__(self, *args):
  18. pass
  19. class MockFS:
  20. def __init__(self):
  21. self.files = {}
  22. def open(self, name, mode=None):
  23. if name not in self.files:
  24. f = self.files[name] = MockFile()
  25. else:
  26. f = self.files[name]
  27. f.seek(0)
  28. return f
  29. def exists(self, name):
  30. return name in self.files
  31. class CustomLexer(Lexer):
  32. def __init__(self, lexer_conf):
  33. pass
  34. def lex(self, data):
  35. for obj in data:
  36. yield Token('A', obj)
  37. class TestCache(TestCase):
  38. def setUp(self):
  39. pass
  40. def test_simple(self):
  41. g = '''start: "a"'''
  42. fn = "bla"
  43. fs = lark_module.FS
  44. mock_fs = MockFS()
  45. try:
  46. lark_module.FS = mock_fs
  47. Lark(g, parser='lalr', cache=fn)
  48. assert fn in mock_fs.files
  49. parser = Lark(g, parser='lalr', cache=fn)
  50. assert parser.parse('a') == Tree('start', [])
  51. mock_fs.files = {}
  52. assert len(mock_fs.files) == 0
  53. Lark(g, parser='lalr', cache=True)
  54. assert len(mock_fs.files) == 1
  55. parser = Lark(g, parser='lalr', cache=True)
  56. assert parser.parse('a') == Tree('start', [])
  57. parser = Lark(g+' "b"', parser='lalr', cache=True)
  58. assert len(mock_fs.files) == 2
  59. assert parser.parse('ab') == Tree('start', [])
  60. parser = Lark(g, parser='lalr', cache=True)
  61. assert parser.parse('a') == Tree('start', [])
  62. # Test with custom lexer
  63. mock_fs.files = {}
  64. parser = Lark(g, parser='lalr', lexer=CustomLexer, cache=True)
  65. parser = Lark(g, parser='lalr', lexer=CustomLexer, cache=True)
  66. assert len(mock_fs.files) == 1
  67. assert parser.parse('a') == Tree('start', [])
  68. # Test options persistence
  69. mock_fs.files = {}
  70. Lark(g, parser="lalr", debug=True, cache=True)
  71. parser = Lark(g, parser="lalr", debug=True, cache=True)
  72. assert parser.options.options['debug']
  73. finally:
  74. lark_module.FS = fs
  75. if __name__ == '__main__':
  76. main()