From 0b50961c841fb60060e3f0e930080d577840f4c3 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 3 Apr 2015 14:49:55 +0200 Subject: [PATCH] add some test for operators --- pymath/operator.py | 17 ++++--------- test/test_operator.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/pymath/operator.py b/pymath/operator.py index 4d171c2..555953b 100644 --- a/pymath/operator.py +++ b/pymath/operator.py @@ -3,6 +3,7 @@ from .generic import flatten_list, isNumber +from functools import wraps import types class Operator(str): @@ -207,6 +208,7 @@ def operatorize(fun): * "l_parenthesis": mechanism to add parenthesis for left operande * "r_parenthesis": mechanism to add parenthesis for rigth operande """ + @wraps(fun) def mod_fun(self, *args): ans = fun(self, *args) @@ -271,17 +273,8 @@ class op(object): def add(cls): """ The operator + - >>> add = op.add - >>> add - '+' - >>> add(1, 2) - 3 - >>> add.__tex__('1','2') - '1 + 2' - >>> add.__txt__('1','2') - '1 + 2' - >>> add.__tex__('1','-2') - '1 - 2' + For doctest see test/test_operator.py + """ def _render(self, link, *args): @@ -337,7 +330,7 @@ class op(object): >>> sub.__tex__('1','-2') '1 - (-2)' >>> sub.__tex__('-1','2') - 'i-1 - 2' + '-1 - 2' """ def l_parenthesis(self, op, str_join=False): return op diff --git a/test/test_operator.py b/test/test_operator.py index c89f9fd..5fdfd6f 100644 --- a/test/test_operator.py +++ b/test/test_operator.py @@ -4,6 +4,43 @@ from pymath.operator import op +# Test de op.add + +def test_add_render_tex(): + assert op.add.__tex__('1','2') == '1 + 2' + assert op.add.__tex__('1','-2') == '1 - 2' + +def test_add_render_txt(): + assert op.add.__txt__('1','2') == '1 + 2' + assert op.add.__txt__('1','-2') == '1 - 2' + +# Test de op.sub + +def test_sub_render_tex(): + assert op.sub.__tex__('1','2') == '1 - 2' + assert op.sub.__tex__('1','-2') == '1 - ( -2 )' + +def test_sub_render_txt(): + assert op.sub.__txt__('1','2') == '1 - 2' + assert op.sub.__txt__('1','-2') == '1 - ( -2 )' + +# Test de op.sub1 + +def test_sub1_render(): + assert op.sub1.__tex__('1') == '- 1' + assert op.sub1.__tex__('-1') == '- ( -1 )' + assert op.sub1.__txt__('1') == '- 1' + assert op.sub1.__txt__('-1') == '- ( -1 )' + +# Test de op.mul +def test_mul_render_tex(): + assert op.mul.__tex__('1','2') == '1 \\times 2' + assert op.mul.__tex__('1','-2') == '1 \\times ( -2 )' + +def test_mul_render_txt(): + assert op.mul.__txt__('1','2') == '1 * 2' + assert op.mul.__txt__('1','-2') == '1 * ( -2 )' + def test_mul_is_visible(): assert op.mul.is_visible(2,3) == True assert op.mul.is_visible(2,-3) == True @@ -20,7 +57,25 @@ def test_mul_is_visible(): assert op.mul.is_visible('(3x - 1)', '(2a + 1)') == False assert op.mul.is_visible(2, '(-2x + 1)(3x + 2)') == False +# Test de op.div +def test_div_render_tex(): + assert op.div.__tex__('1','2') == '\\frac{ 1 }{ 2 }' + assert op.div.__tex__('1','-2') == '\\frac{ 1 }{ -2 }' +def test_div_render_txt(): + assert op.div.__txt__('1','2') == '1 / 2' + assert op.div.__txt__('1','-2') == '1 / ( -2 )' + +# Test de op.pw +def test_pw_render_tex(): + assert op.pw.__tex__('1','2') == '1^{ 2 }' + assert op.pw.__tex__('1','-2') == '1^{-2}' + assert op.pw.__tex__('-1','2') == '( -1 )^{ 2 }' + +def test_pw_render_txt(): + assert op.pw.__txt__('1','2') == '1 ^ 2' + assert op.pw.__txt__('1','-2') == '1 ^ ( -2 )' + assert op.pw.__txt__('-1','2') == '( -1 ) ^ 2 '