-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSearch: SearchQueryParenthesisedParser rewrite
* New SQPP supports parenthetic subexpressions nested to arbitrary depth. * Unit tests corrected to reflect both the new support for parentheses and because some of them should not have been correct with the old parser, either. (fixes #131) (fixes #67) (fixes #181) (fixes #189) (fixes #190) (fixes #191) * Introduction of logicutils unit tests.
- Loading branch information
1 parent
4722b8a
commit 1fc28f3
Showing
6 changed files
with
393 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Unit tests for logic library.""" | ||
|
||
import unittest | ||
|
||
from invenio.logic import expr, Expr, to_cnf, pl_true | ||
from invenio.testutils import make_test_suite, run_test_suite | ||
|
||
class exprExprOpsTest(unittest.TestCase): | ||
"""Testing expr and Expr against one another.""" | ||
|
||
def test_trivial_expr(self): | ||
"""logicutils - create trivial Expr with expr()""" | ||
self.assertEqual(expr('a | b'), Expr('|', 'a', 'b')) | ||
|
||
def test_deep_expr(self): | ||
"""logicutils - create deep Expr with expr()""" | ||
self.assertEqual(expr('a | b | c | d | e'), | ||
Expr('|', Expr('|', Expr('|', Expr('|', 'a', 'b'), 'c'), 'd'), 'e')) | ||
|
||
|
||
class toCNFTest(unittest.TestCase): | ||
"""Testing conversion to conjunctive normal form""" | ||
|
||
def test_singleton(self): | ||
"""logicutils - singletons are already in CNF""" | ||
self.assertEqual(to_cnf(expr('a')), | ||
Expr('a')) | ||
|
||
def test_complex_example_Norvig(self): | ||
"""logicutils - (P&Q) | (~P & ~Q) in CNF""" | ||
self.assertEqual(str(to_cnf('(P&Q) | (~P & ~Q)')), | ||
str('((~P | P) & (~Q | P) & (~P | Q) & (~Q | Q))')) | ||
|
||
def test_ORed_pair(self): | ||
"""logicutils - ORed pair should be in CNF""" | ||
self.assertEqual(to_cnf(expr('a | b')), | ||
Expr('|', 'a', 'b')) | ||
|
||
def test_ANDed_pair(self): | ||
"""logicutils - ANDed pair should be in CNF""" | ||
self.assertEqual(to_cnf(expr('a & b')), | ||
Expr('&', 'a', 'b')) | ||
|
||
|
||
class prop_logicTest(unittest.TestCase): | ||
"""Testing basic propositional logic functionality""" | ||
P = Expr('P') | ||
|
||
def test_pl_true_P_true(self): | ||
"""logicutils - True thing is evaluated as such""" | ||
self.assertEqual(pl_true(self.P, {self.P: True}), | ||
True) | ||
|
||
def test_pl_true_P_false(self): | ||
"""logicutils - False thing is evaluated as such""" | ||
self.assertEqual(pl_true(self.P, {self.P: False}), | ||
False) | ||
|
||
|
||
TEST_SUITE = make_test_suite(exprExprOpsTest, toCNFTest, prop_logicTest) | ||
|
||
if __name__ == "__main__": | ||
run_test_suite(TEST_SUITE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.