A semantic search engine for source code https://bitshift.benkurtovic.com/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

77 satır
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import unittest
  4. from bitshift.query import parse_query
  5. TESTS = [
  6. # Text
  7. ("test", "Tree(Text(String(u'test')))"),
  8. ("re:test", "Tree(Text(Regex(u'test')))"),
  9. # Language
  10. ("language:python", "Tree(Language(Python))"),
  11. ("language:py", "Tree(Language(Python))"),
  12. ("l:r:r..y", "Tree(Language(Ruby))"),
  13. (r'"lang:re:python|^c$"',
  14. "Tree(BinaryOp(Language(C), OR, Language(Python)))"),
  15. # Author
  16. ('"author:Ben Kurtovic"', "Tree(Author(String(u'Ben Kurtovic')))"),
  17. (r"'a:re:b.*?\sk.*?'", r"Tree(Author(Regex(u'b.*?\\sk.*?')))"),
  18. # Date
  19. ("'create:before:Jan 1, 2014'",
  20. "Tree(Date(CREATE, BEFORE, 2014-01-01 00:00:00))"),
  21. ("'modify:after:2010-05-09 10:11:12'",
  22. "Tree(Date(MODIFY, AFTER, 2010-05-09 10:11:12))"),
  23. # Symbol
  24. ("sym:foobar", "Tree(Symbol(ALL, ALL, String(u'foobar')))"),
  25. ("func:foo_bar", "Tree(Symbol(ALL, FUNCTION, String(u'foo_bar')))"),
  26. ("func:foo_bar()", "Tree(Symbol(ALL, FUNCTION, String(u'foo_bar')))"),
  27. ("class:FooBar", "Tree(Symbol(ALL, CLASS, String(u'FooBar')))"),
  28. ("var:foobar", "Tree(Symbol(ALL, VARIABLE, String(u'foobar')))"),
  29. ("var:r:foobar", "Tree(Symbol(ALL, VARIABLE, Regex(u'foobar')))"),
  30. # Composition
  31. ("(a and b) or (c and d)", ", ".join([
  32. "Tree(BinaryOp(BinaryOp(Text(String(u'a'))", "AND",
  33. "Text(String(u'b')))", "OR", "BinaryOp(Text(String(u'c'))", "AND",
  34. "Text(String(u'd')))))"])),
  35. ("a and b or c and d", ", ".join([
  36. "Tree(BinaryOp(BinaryOp(Text(String(u'a'))", "AND",
  37. "Text(String(u'b')))", "OR", "BinaryOp(Text(String(u'c'))", "AND",
  38. "Text(String(u'd')))))"])),
  39. ("a and b or c or d", ", ".join([
  40. "Tree(BinaryOp(BinaryOp(Text(String(u'a'))", "AND",
  41. "Text(String(u'b')))", "OR", "BinaryOp(Text(String(u'c'))", "OR",
  42. "Text(String(u'd')))))"])),
  43. ("a and (b or c or d)", ", ".join([
  44. "Tree(BinaryOp(Text(String(u'a'))", "AND",
  45. "BinaryOp(Text(String(u'b'))", "OR", "BinaryOp(Text(String(u'c'))", "OR",
  46. "Text(String(u'd'))))))"])),
  47. ("a not b", ", ".join([
  48. "Tree(BinaryOp(Text(String(u'a'))", "AND", "UnaryOp(NOT",
  49. "Text(String(u'b')))))"])),
  50. # Unicode, Escaping
  51. (r'lang:py "author:fo\\o \"bar\" baz\\"', ", ".join([
  52. "Tree(BinaryOp(Language(Python)", "AND",
  53. "Author(String(u'fo\\\\o \"bar\" baz\\\\'))))"])),
  54. ('"author:Ben Kurtović"', "Tree(Author(String(u'Ben Kurtovi\\u0107')))")
  55. ]
  56. class TestQueryParser(unittest.TestCase):
  57. """Unit tests for the query parser in :py:mod:`bitshift.query`."""
  58. def test_parse(self):
  59. """test full query parsing"""
  60. for test, expected in TESTS:
  61. self.assertEqual(expected, parse_query(test).serialize())
  62. if __name__ == "__main__":
  63. unittest.main(verbosity=2)