test_possibilities.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  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_filter_duplicates(self):
  65. # a, b = ab = tree('a + b')
  66. # p0 = P(a, dummy_handler, (1, 2))
  67. # p1 = P(ab, dummy_handler, (1, 2))
  68. # p2 = P(ab, dummy_handler, (1, 2, 3))
  69. # p3 = P(ab, dummy_handler_msg, (1, 2))
  70. # self.assertEqual(filter_duplicates([]), [])
  71. # self.assertEqual(filter_duplicates([p0, p1]), [p1])
  72. # self.assertEqual(filter_duplicates([p1, p2]), [p1, p2])
  73. # self.assertEqual(filter_duplicates([p1, p3]), [p1, p3])
  74. # self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])
  75. # # Docstrings example
  76. # (l1, l2), l3 = left, l3 = right = tree('1 + 2 + 3')
  77. # p0 = P(left, add_numerics, (1, 2, 1, 2))
  78. # p1 = P(right, add_numerics, (1, 2, 1, 2))
  79. # p2 = P(right, add_numerics, (1, 3, 1, 3))
  80. # p3 = P(right, add_numerics, (2, 3, 2, 3))
  81. # self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])