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.
 
 

66 line
1.6 KiB

  1. import logging
  2. from contextlib import contextmanager
  3. from lark import Lark, logger
  4. from unittest import TestCase, main
  5. try:
  6. from StringIO import StringIO
  7. except ImportError:
  8. from io import StringIO
  9. @contextmanager
  10. def capture_log():
  11. stream = StringIO()
  12. orig_handler = logger.handlers[0]
  13. del logger.handlers[:]
  14. logger.addHandler(logging.StreamHandler(stream))
  15. yield stream
  16. del logger.handlers[:]
  17. logger.addHandler(orig_handler)
  18. class Testlogger(TestCase):
  19. def test_debug(self):
  20. logger.setLevel(logging.DEBUG)
  21. collision_grammar = '''
  22. start: as as
  23. as: a*
  24. a: "a"
  25. '''
  26. with capture_log() as log:
  27. Lark(collision_grammar, parser='lalr', debug=True)
  28. log = log.getvalue()
  29. # since there are conflicts about A
  30. # symbol A should appear in the log message for hint
  31. self.assertIn("A", log)
  32. def test_non_debug(self):
  33. logger.setLevel(logging.DEBUG)
  34. collision_grammar = '''
  35. start: as as
  36. as: a*
  37. a: "a"
  38. '''
  39. with capture_log() as log:
  40. Lark(collision_grammar, parser='lalr', debug=False)
  41. log = log.getvalue()
  42. # no log messge
  43. self.assertEqual(len(log), 0)
  44. def test_loglevel_higher(self):
  45. logger.setLevel(logging.ERROR)
  46. collision_grammar = '''
  47. start: as as
  48. as: a*
  49. a: "a"
  50. '''
  51. with capture_log() as log:
  52. Lark(collision_grammar, parser='lalr', debug=True)
  53. log = log.getvalue()
  54. # no log messge
  55. self.assertEqual(len(log), 0)
  56. if __name__ == '__main__':
  57. main()