Procházet zdrojové kódy

Add unit tests, cleanup.

tags/v1.0^2
Ben Kurtovic před 10 roky
rodič
revize
30406483b9
3 změnil soubory, kde provedl 34 přidání a 6 odebrání
  1. +0
    -1
      bitshift/languages.py
  2. +1
    -1
      bitshift/parser/__init__.py
  3. +33
    -4
      test/test_query_parser.py

+ 0
- 1
bitshift/languages.py Zobrazit soubor

@@ -1,2 +1 @@

LANGS = ["Python", "C", "Java", "Ruby"]

+ 1
- 1
bitshift/parser/__init__.py Zobrazit soubor

@@ -21,7 +21,7 @@ def _lang(codelet):

if codelet.filename is not None:
try:
return pgl.guess_lexer_for_filename(codelet.filename, '').name
return pgl.guess_lexer_for_filename(codelet.filename, codelet.code).name
except:
raise UnsupportedFileError('Could not find a lexer for the codelet\'s filename')



+ 33
- 4
test/test_query_parser.py Zobrazit soubor

@@ -3,14 +3,43 @@ import unittest

from bitshift.query import parse_query

TESTS = [
# Text
("test", "Tree(Text(String(u'test')))"),
("re:test", "Tree(Text(Regex(u'test')))"),

# Language
("language:python", "Tree(Language(Python))"),
("language:py", "Tree(Language(Python))"),
("l:r:r..y", "Tree(Language(Ruby))"),
("lang:re:py|c", "Tree(BinaryOp(Language(C), OR, Language(Python)))"),

# Author
('"author:Ben Kurtovic"', "Tree(Author(String(u'Ben Kurtovic')))"),
(r"'a:re:b.*?\sk.*?'", r"Tree(Author(Regex(u'b.*?\\sk.*?')))"),

# Date
("'create:before:Jan 1, 2014'",
"Tree(Date(CREATE, BEFORE, 2014-01-01 00:00:00))"),
("'modify:after:2010-05-09 10:11:12'",
"Tree(Date(MODIFY, AFTER, 2010-05-09 10:11:12))"),

# Symbol
("sym:foobar", "Tree(Symbol(ALL, String(u'foobar')))"),
("func:foo_bar", "Tree(Symbol(FUNCTION, String(u'foo_bar')))"),
("func:foo_bar()", "Tree(Symbol(FUNCTION, String(u'foo_bar')))"),
("class:FooBar", "Tree(Symbol(CLASS, String(u'FooBar')))"),
("var:foobar", "Tree(Symbol(VARIABLE, String(u'foobar')))"),
("var:r:foobar", "Tree(Symbol(VARIABLE, Regex(u'foobar')))"),
]

class TestQueryParser(unittest.TestCase):
"""Unit tests for the query parser in :py:mod:`bitshift.query`."""

def test_parse(self):
"""test basic query parsing"""
pq = lambda s: parse_query(s).serialize()
self.assertEqual("Tree(Text(String('test')))", pq("test"))
self.assertEqual("Tree(Text(Regex('test')))", pq("re:test"))
"""test full query parsing"""
for test, expected in TESTS:
self.assertEqual(expected, parse_query(test).serialize())


if __name__ == "__main__":


Načítá se…
Zrušit
Uložit