From d503db6cefb430ab087a06c32cc794d3557c2d68 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Sat, 7 Mar 2015 20:04:16 +0100 Subject: [PATCH] add doctest and unittest for render --- pymath/operator.py | 38 ++++++++++++++++++++++---------------- test/test_render.py | 21 +++++++++++++++------ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/pymath/operator.py b/pymath/operator.py index 3dab91a..9706a96 100644 --- a/pymath/operator.py +++ b/pymath/operator.py @@ -386,8 +386,14 @@ class op(object): 2 >>> mul.__tex__('1','2') '1 \\times 2' + >>> mul.__tex__('2','a') + '2 a' >>> mul.__txt__('1','2') '1 * 2' + >>> mul.__txt__('2','a') + '2 a' + >>> mul.__txt__('a','2') + 'a * 2' >>> mul.__tex__('1','-2') '1 \\times (-2)' """ @@ -514,24 +520,24 @@ class op(object): return caract if __name__ == '__main__': - print(op.add.__tex__('1','2')) - print(op.mul.__tex__('1','2')) - print(op.sub.__tex__('1','2')) - f = save_mainOp('2 + 3',op.add) - print(op.mul.__txt__(f, '4')) - f = save_mainOp('-3',op.sub1) - print(op.sub1.__txt__(f)) - print(op.sub1.__txt__('-3')) - f = save_mainOp('2 + 3',op.add) - print(op.sub1.__txt__(f)) + #print(op.add.__tex__('1','2')) + #print(op.mul.__tex__('1','2')) + #print(op.sub.__tex__('1','2')) + #f = save_mainOp('2 + 3',op.add) + #print(op.mul.__txt__(f, '4')) + #f = save_mainOp('-3',op.sub1) + #print(op.sub1.__txt__(f)) + #print(op.sub1.__txt__('-3')) + #f = save_mainOp('2 + 3',op.add) + #print(op.sub1.__txt__(f)) - from .fraction import Fraction - f = Fraction(1, 2) - print(op.add.__txt__(f.__txt__(),'2')) - print(op.add.__tex__(f.__tex__(),'2')) + #from .fraction import Fraction + #f = Fraction(1, 2) + #print(op.add.__txt__(f.__txt__(),'2')) + #print(op.add.__tex__(f.__tex__(),'2')) - print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+'))) - print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t'))) + #print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+'))) + #print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t'))) import doctest diff --git a/test/test_render.py b/test/test_render.py index bb42311..4b58b55 100644 --- a/test/test_render.py +++ b/test/test_render.py @@ -55,7 +55,7 @@ class TestTexRender(unittest.TestCase): ] wanted_render = [ "2 ( 3 x^{ 2 } + 2 x + 1 )", "( 3 x^{ 2 } + 2 x + 1 ) \\times 2", - "( 3 x^{ 2 } + 2 x + 1 ) ( 3 x^{ 2 } + 2 x + 1 )", + "( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )", ] for (i,e) in enumerate(exps): rend = tex(e) @@ -129,11 +129,20 @@ class TesttxtRender(unittest.TestCase): self.assertEqual(rend, wanted_render[i]) def test_mult_letter(self): - exps = [ [2, "a", op.get_op("*", 2)], \ - ["a", 3, op.get_op("*", 2)], \ - [-2, "a", op.get_op("*", 2)], \ - ["a", -2, op.get_op("*", 2)]] - wanted_render = [ "2 a", "a * 3", "-2 a", "a * ( -2 )"] + exps = [ [2, "a", op.mul], \ + ["a", 3, op.mul], \ + [-2, "a", op.mul], \ + ["a", -2, op.mul], + ["a", -2, op.mul, -2, op.mul], + ["a", -2, op.mul, "a", op.mul], + ] + wanted_render = [ "2 a", + "a * 3", + "-2 a", + "a * ( -2 )", + "a * ( -2 ) * ( -2 )", + "a * ( -2 ) a", + ] for (i,e) in enumerate(exps): rend = txt(e) self.assertEqual(rend, wanted_render[i])