A semantic search engine for source code https://bitshift.benkurtovic.com/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

68 righe
2.5 KiB

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