test_b1_ch10.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import unittest
  2. from src.parser import Parser
  3. from src.node import ExpressionNode as N, ExpressionLeaf as L
  4. from tests.parser import run_expressions
  5. class TestB1Ch10(unittest.TestCase):
  6. def test_diagnostic_test(self):
  7. run_expressions(Parser, [
  8. ('5(a-2b)', L(5) * (L('a') + -(L(2) * 'b'))),
  9. ('-(3a+6b)', -(L(3) * L('a') + L(6) * 'b')),
  10. ('18-(a-12)', L(18) + -(L('a') + -L(12))),
  11. ('-p-q+5(p-q)-3q-2(p-q)',
  12. -L('p') + -L('q') + L(5) * (L('p') + -L('q')) + -(L(3) * 'q') \
  13. + -(L(2) * (L('p') + -L('q')))
  14. ),
  15. ('(2+3/7)^4',
  16. N('^', N('+', L(2), N('/', L(3), L(7))), L(4))
  17. ),
  18. ('x^3*x^2*x',
  19. N('*',
  20. N('*',
  21. N('^', L('x'), L(3)),
  22. N('^', L('x'), L(2))),
  23. L('x')
  24. )
  25. ),
  26. ('-x^3*-2x^5',
  27. -(L('x') ** L(3) * -(L(2) * L('x') ** L(5)))
  28. ),
  29. ('(7x^2y^3)^2/(7x^2y^3)',
  30. N('/',
  31. N('^',
  32. N('*',
  33. N('*', L(7), N('^', L('x'), L(2))),
  34. N('^', L('y'), L(3))
  35. ),
  36. L(2)),
  37. N('*',
  38. N('*', L(7), N('^', L('x'), L(2))),
  39. N('^', L('y'), L(3))
  40. )
  41. )
  42. ),
  43. ])