test_possibilities.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. import unittest
  16. from src.possibilities import MESSAGES, Possibility as P, flatten_mult
  17. from tests.rulestestcase import tree
  18. from src.parser import Parser
  19. from tests.parser import ParserWrapper
  20. def dummy_handler(root, args): # pragma: nocover
  21. pass
  22. def dummy_handler_msg(root, args): # pragma: nocover
  23. pass
  24. MESSAGES[dummy_handler_msg] = 'foo {1} + {2} bar'
  25. class TestPossibilities(unittest.TestCase):
  26. def setUp(self):
  27. self.l1, self.l2 = self.n = tree('1 + 2')
  28. self.p0 = P(self.n, dummy_handler, (self.l1, self.l2))
  29. self.p1 = P(self.n, dummy_handler_msg, (self.l1, self.l2))
  30. def test___str__(self):
  31. self.assertEqual(str(self.p0),
  32. '<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
  33. self.assertEqual(str(self.p1), 'foo `1` + `2` bar')
  34. def test___repr__(self):
  35. self.assertEqual(repr(self.p0),
  36. '<Possibility root="1 + 2" handler=dummy_handler args=(1, 2)>')
  37. def test___eq__(self):
  38. assert self.p0 == P(self.n, dummy_handler, (self.l1, self.l2))
  39. assert self.p0 != self.p1
  40. def test_multiple_input(self):
  41. parser = ParserWrapper(Parser)
  42. parser.run(['1+2', '?', '3+4', '?'])
  43. possibilities = parser.parser.possibilities
  44. self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
  45. '<Possibility root="3 + 4" handler=add_numerics' \
  46. ' args=(<Scope of "3 + 4">, 3, 4)>')
  47. def test_multiple_runs(self):
  48. parser = ParserWrapper(Parser)
  49. parser.run(['1+2', '?'])
  50. possibilities = parser.parser.possibilities
  51. self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
  52. '<Possibility root="1 + 2" handler=add_numerics' \
  53. ' args=(<Scope of "1 + 2">, 1, 2)>')
  54. # Remove previous possibilities after second run() call.
  55. parser.run(['', ' '])
  56. possibilities = parser.parser.possibilities
  57. self.assertEqual(possibilities, None)
  58. # Overwrite previous possibilities with new ones
  59. parser.run(['3+4', '?'])
  60. possibilities = parser.parser.possibilities
  61. self.assertEqual('\n'.join([repr(pos) for pos in possibilities]),
  62. '<Possibility root="3 + 4" handler=add_numerics' \
  63. ' args=(<Scope of "3 + 4">, 3, 4)>')
  64. def test_flatten_mult(self):
  65. self.assertEqual(flatten_mult(tree('2(xx)')), tree('2xx'))
  66. self.assertEqual(flatten_mult(tree('2(xx) + 1')), tree('2xx + 1'))