Matt Chaput avatar Matt Chaput committed f16a314

- Changed operator regexes to use look-ahead/-behind instead of matching whitespace. Fixes issue #253.
WhitespacePlugin could "steal" all whitespace before an operator so the op pattern (which
included leading WS) wouldn't match.
- Moved test_not_order to a more appropriate test module.

Comments (0)

Files changed (3)

src/whoosh/qparser/plugins.py

             self.memo = memo
 
         def __repr__(self):
-            return "<%s %r (%s)>" % (self.__class__.__name__, self.expr,
-                                     self.memo)
+            return "<%s %r (%s)>" % (self.__class__.__name__,
+                                     self.expr.pattern, self.memo)
 
         def create(self, parser, match):
             return self.optype(match.group(0), self.grouptype, self.leftassoc)
 
-    def __init__(self, ops=None, clean=False, And=r"\sAND\s", Or=r"\sOR\s",
-                 AndNot=r"\sANDNOT\s", AndMaybe=r"\sANDMAYBE\s",
-                 Not=r"(^|(?<=[ \r\n\t()]))NOT\s",
-                 Require=r"(^|(?<= ))REQUIRE\s"):
+    def __init__(self, ops=None, clean=False,
+                 And=r"(?<= )AND(?= )",
+                 Or=r"(?<= )OR(?= )",
+                 AndNot=r"(?<= )ANDNOT(?= )",
+                 AndMaybe=r"(?<= )ANDMAYBE(?= )",
+                 Not=r"(^|(?<=[ \r\n\t()]))NOT(?= )",
+                 Require=r"(^|(?<= ))REQUIRE(?= )"):
         if ops:
             ops = list(ops)
         else:

tests/test_parsing.py

     assert_equal(q.text, "t")
 
 
+def test_not_order():
+    schema = fields.Schema(id=fields.STORED,
+                           count=fields.KEYWORD(lowercase=True),
+                           cats=fields.KEYWORD(lowercase=True))
+    qp = default.QueryParser("count", schema)
 
+    q1 = qp.parse(u("(NOT (count:0) AND cats:1)"))
+    assert_equal(q1.__class__, query.And)
+    assert_equal(q1[0].__class__, query.Not)
+    assert_equal(q1[1].__class__, query.Term)
+    assert_equal(q1.__unicode__(), '(NOT count:0 AND cats:1)')
 
+    q2 = qp.parse(u("(cats:1 AND NOT (count:0))"))
+    assert_equal(q2.__class__, query.And)
+    assert_equal(q2[0].__class__, query.Term)
+    assert_equal(q2[1].__class__, query.Not)
+    assert_equal(q2.__unicode__(), '(cats:1 AND NOT count:0)')
 
 
+def test_spacespace_and():
+    qp = default.QueryParser("f", None)
+    # one blank before/after AND
+    q = qp.parse("A AND B")
+    assert_equal(q.__class__, query.And)
+    assert_equal(len(q), 2)
+    assert_equal(q[0], query.Term("f", "A"))
+    assert_equal(q[1], query.Term("f", "B"))
 
+    # two blanks before AND
+    q = qp.parse("A  AND B")
+    assert_equal(q.__class__, query.And)
+    assert_equal(len(q), 2)
+    assert_equal(q[0], query.Term("f", "A"))
+    assert_equal(q[1], query.Term("f", "B"))
+

tests/test_queries.py

         assert_equal(q._find_prefix(q.text), "a")
 
 
-def test_not_order():
-    domain = [(1, 5, 1), (2, 4, 1), (3, 3, 1), (4, 20, None), (5, 0, 1)]
-    schema = fields.Schema(id=fields.STORED,
-                           count=fields.KEYWORD(lowercase=True),
-                           cats=fields.KEYWORD(lowercase=True))
-    ix = RamStorage().create_index(schema)
-    with ix.writer() as w:
-        for idnum, count, cats in domain:
-            count = u(str(count))
-            cats = u("1") if cats else None
-            w.add_document(id=idnum, count=count, cats=cats)
 
-    with ix.searcher() as s:
-        qp = qparser.QueryParser("count", ix.schema)
 
-        q1 = qp.parse(u("(NOT (count:0) AND cats:1)"))
-        assert_equal(q1.__class__, query.And)
-        assert_equal(q1[0].__class__, query.Not)
-        assert_equal(q1[1].__class__, query.Term)
-        assert_equal(q1.__unicode__(), '(NOT count:0 AND cats:1)')
 
-        q2 = qp.parse(u("(cats:1 AND NOT (count:0))"))
-        assert_equal(q2.__class__, query.And)
-        assert_equal(q2[0].__class__, query.Term)
-        assert_equal(q2[1].__class__, query.Not)
-        assert_equal(q2.__unicode__(), '(cats:1 AND NOT count:0)')
 
 
 
 
 
 
-
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.