| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- # This file is part of TRS (http://math.kompiler.org)
- #
- # TRS is free software: you can redistribute it and/or modify it under the
- # terms of the GNU Affero General Public License as published by the Free
- # Software Foundation, either version 3 of the License, or (at your option) any
- # later version.
- #
- # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
- # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
- # details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with TRS. If not, see <http://www.gnu.org/licenses/>.
- import unittest
- from src.possibilities import MESSAGES, Possibility as P
- from tests.rulestestcase import tree
- from src.parser import Parser
- from tests.parser import ParserWrapper
- def dummy_handler(root, args): # pragma: nocover
- pass
- def dummy_handler_msg(root, args): # pragma: nocover
- pass
- MESSAGES[dummy_handler_msg] = 'foo {1} + {2} bar'
- class TestPossibilities(unittest.TestCase):
- def setUp(self):
- self.l1, self.l2 = self.n = tree('1 + 2')
- self.p0 = P(self.n, dummy_handler, (self.l1, self.l2))
- self.p1 = P(self.n, dummy_handler_msg, (self.l1, self.l2))
- def test___str__(self):
- self.assertEqual(str(self.p0),
- '<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
- self.assertEqual(str(self.p1), 'foo `1` + `2` bar')
- def test___repr__(self):
- self.assertEqual(repr(self.p0),
- '<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
- def test___eq__(self):
- assert self.p0 == P(self.n, dummy_handler, (self.l1, self.l2))
- assert self.p0 != self.p1
- def test_multiple_input(self):
- parser = ParserWrapper(Parser)
- parser.run(['1+2', '?', '3+4', '?'])
- possibilities = parser.parser.possibilities
- self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
- '<Possibility root="3 + 4" handler=add_numerics' \
- ' args=(<Scope of "3 + 4">, 3, 4)>')
- def test_multiple_runs(self):
- parser = ParserWrapper(Parser)
- parser.run(['1+2', '?'])
- possibilities = parser.parser.possibilities
- self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
- '<Possibility root="1 + 2" handler=add_numerics' \
- ' args=(<Scope of "1 + 2">, 1, 2)>')
- # Remove previous possibilities after second run() call.
- parser.run(['', ' '])
- possibilities = parser.parser.possibilities
- self.assertEqual(possibilities, None)
- # Overwrite previous possibilities with new ones
- parser.run(['3+4', '?'])
- possibilities = parser.parser.possibilities
- self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
- '<Possibility root="3 + 4" handler=add_numerics' \
- ' args=(<Scope of "3 + 4">, 3, 4)>')
- #def test_filter_duplicates(self):
- # a, b = ab = tree('a + b')
- # p0 = P(a, dummy_handler, (1, 2))
- # p1 = P(ab, dummy_handler, (1, 2))
- # p2 = P(ab, dummy_handler, (1, 2, 3))
- # p3 = P(ab, dummy_handler_msg, (1, 2))
- # self.assertEqual(filter_duplicates([]), [])
- # self.assertEqual(filter_duplicates([p0, p1]), [p1])
- # self.assertEqual(filter_duplicates([p1, p2]), [p1, p2])
- # self.assertEqual(filter_duplicates([p1, p3]), [p1, p3])
- # self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])
- # # Docstrings example
- # (l1, l2), l3 = left, l3 = right = tree('1 + 2 + 3')
- # p0 = P(left, add_numerics, (1, 2, 1, 2))
- # p1 = P(right, add_numerics, (1, 2, 1, 2))
- # p2 = P(right, add_numerics, (1, 3, 1, 3))
- # p3 = P(right, add_numerics, (2, 3, 2, 3))
- # self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])
|