add some test for operators
This commit is contained in:
parent
15d87cf147
commit
0b50961c84
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
|
|
||||||
from .generic import flatten_list, isNumber
|
from .generic import flatten_list, isNumber
|
||||||
|
from functools import wraps
|
||||||
import types
|
import types
|
||||||
|
|
||||||
class Operator(str):
|
class Operator(str):
|
||||||
|
@ -207,6 +208,7 @@ def operatorize(fun):
|
||||||
* "l_parenthesis": mechanism to add parenthesis for left operande
|
* "l_parenthesis": mechanism to add parenthesis for left operande
|
||||||
* "r_parenthesis": mechanism to add parenthesis for rigth operande
|
* "r_parenthesis": mechanism to add parenthesis for rigth operande
|
||||||
"""
|
"""
|
||||||
|
@wraps(fun)
|
||||||
def mod_fun(self, *args):
|
def mod_fun(self, *args):
|
||||||
ans = fun(self, *args)
|
ans = fun(self, *args)
|
||||||
|
|
||||||
|
@ -271,17 +273,8 @@ class op(object):
|
||||||
def add(cls):
|
def add(cls):
|
||||||
""" The operator +
|
""" The operator +
|
||||||
|
|
||||||
>>> add = op.add
|
For doctest see test/test_operator.py
|
||||||
>>> add
|
|
||||||
'+'
|
|
||||||
>>> add(1, 2)
|
|
||||||
3
|
|
||||||
>>> add.__tex__('1','2')
|
|
||||||
'1 + 2'
|
|
||||||
>>> add.__txt__('1','2')
|
|
||||||
'1 + 2'
|
|
||||||
>>> add.__tex__('1','-2')
|
|
||||||
'1 - 2'
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _render(self, link, *args):
|
def _render(self, link, *args):
|
||||||
|
@ -337,7 +330,7 @@ class op(object):
|
||||||
>>> sub.__tex__('1','-2')
|
>>> sub.__tex__('1','-2')
|
||||||
'1 - (-2)'
|
'1 - (-2)'
|
||||||
>>> sub.__tex__('-1','2')
|
>>> sub.__tex__('-1','2')
|
||||||
'i-1 - 2'
|
'-1 - 2'
|
||||||
"""
|
"""
|
||||||
def l_parenthesis(self, op, str_join=False):
|
def l_parenthesis(self, op, str_join=False):
|
||||||
return op
|
return op
|
||||||
|
|
|
@ -4,6 +4,43 @@
|
||||||
from pymath.operator import op
|
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():
|
def test_mul_is_visible():
|
||||||
assert op.mul.is_visible(2,3) == True
|
assert op.mul.is_visible(2,3) == True
|
||||||
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('(3x - 1)', '(2a + 1)') == False
|
||||||
assert op.mul.is_visible(2, '(-2x + 1)(3x + 2)') == 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 '
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue