[Zope-CVS] CVS: Products/ZCTextIndex/tests - testQueryParser.py:1.1.2.1
Guido van Rossum
guido@python.org
Tue, 30 Apr 2002 16:20:15 -0400
Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv17061/tests
Added Files:
Tag: TextIndexDS9-branch
testQueryParser.py
Log Message:
Query Parser.
=== Added File Products/ZCTextIndex/tests/testQueryParser.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from unittest import TestCase, TestSuite, main, makeSuite
from Products.ZCTextIndex.QueryParser import \
QueryParser, ParseTreeNode, OrNode, AndNode, NotNode, AtomNode
class TestQueryParser(TestCase):
def compareParseTrees(self, got, expected):
self.assertEqual(isinstance(got, ParseTreeNode), 1)
self.assertEqual(got.__class__, expected.__class__)
if isinstance(got, AtomNode):
self.assertEqual(got.nodeType(), "ATOM")
self.assertEqual(got.getValue(), expected.getValue())
elif isinstance(got, NotNode):
self.assertEqual(got.nodeType(), "NOT")
self.compareParseTrees(got.getValue(), expected.getValue())
elif isinstance(got, AndNode) or isinstance(got, OrNode):
self.assertEqual(got.nodeType(),
isinstance(got, AndNode) and "AND" or "OR")
list1 = got.getValue()
list2 = expected.getValue()
self.assertEqual(len(list1), len(list2))
for i in range(len(list1)):
self.compareParseTrees(list1[i], list2[i])
def expect(self, input, output):
tree = self.p.parseQuery(input)
self.compareParseTrees(tree, output)
def setUp(self):
self.p = QueryParser()
def testParseQuery(self):
self.expect("foo", AtomNode("foo"))
self.expect("NOT foo", NotNode(AtomNode("foo")))
self.expect("not", AtomNode("not"))
self.expect("a AND b AND c",
AndNode([AtomNode("a"), AtomNode("b"), AtomNode("c")]))
self.expect("a OR b OR c",
OrNode([AtomNode("a"), AtomNode("b"), AtomNode("c")]))
self.expect("a AND b OR c AND d",
OrNode([AndNode([AtomNode("a"), AtomNode("b")]),
AndNode([AtomNode("c"), AtomNode("d")])]))
self.expect("(a OR b) AND (c OR d)",
AndNode([OrNode([AtomNode("a"), AtomNode("b")]),
OrNode([AtomNode("c"), AtomNode("d")])]))
self.expect("a AND NOT b",
AndNode([AtomNode("a"), NotNode(AtomNode("b"))]))
def test_suite():
return TestSuite((
makeSuite(TestQueryParser),
))
if __name__=='__main__':
main(defaultTest='test_suite')