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.

83 lines
1.8 KiB

  1. from __future__ import absolute_import
  2. import sys
  3. from unittest import TestCase, main
  4. from lark import Lark, Tree
  5. import lark.lark as lark_module
  6. try:
  7. from StringIO import StringIO
  8. except ImportError:
  9. from io import BytesIO as StringIO
  10. import tempfile, os
  11. class MockFile(StringIO):
  12. def close(self):
  13. pass
  14. def __enter__(self):
  15. return self
  16. def __exit__(self, *args):
  17. pass
  18. class MockFS:
  19. def __init__(self):
  20. self.files = {}
  21. def open(self, name, mode=None):
  22. if name not in self.files:
  23. f = self.files[name] = MockFile()
  24. else:
  25. f = self.files[name]
  26. f.seek(0)
  27. return f
  28. def exists(self, name):
  29. return name in self.files
  30. class TestCache(TestCase):
  31. def setUp(self):
  32. pass
  33. def test_simple(self):
  34. g = '''start: "a"'''
  35. fn = "bla"
  36. fs = lark_module.FS
  37. mock_fs = MockFS()
  38. try:
  39. lark_module.FS = mock_fs
  40. Lark(g, parser='lalr', cache=fn)
  41. assert fn in mock_fs.files
  42. parser = Lark(g, parser='lalr', cache=fn)
  43. assert parser.parse('a') == Tree('start', [])
  44. mock_fs.files = {}
  45. assert len(mock_fs.files) == 0
  46. Lark(g, parser='lalr', cache=True)
  47. assert len(mock_fs.files) == 1
  48. parser = Lark(g, parser='lalr', cache=True)
  49. assert parser.parse('a') == Tree('start', [])
  50. parser = Lark(g+' "b"', parser='lalr', cache=True)
  51. assert len(mock_fs.files) == 2
  52. assert parser.parse('ab') == Tree('start', [])
  53. parser = Lark(g, parser='lalr', cache=True)
  54. assert parser.parse('a') == Tree('start', [])
  55. finally:
  56. lark_module.FS = fs
  57. if __name__ == '__main__':
  58. main()