Adapt unit test to Operator. Still have to includ fractions
This commit is contained in:
parent
6e99dadd39
commit
9b1ad5c14a
@ -123,9 +123,7 @@ class Expression(object):
|
|||||||
if len(steps[:-1]) > 0:
|
if len(steps[:-1]) > 0:
|
||||||
self.steps += [flatten_list(s) for s in steps[:-1]]
|
self.steps += [flatten_list(s) for s in steps[:-1]]
|
||||||
|
|
||||||
print("self.steps -> ", self.steps)
|
|
||||||
self.child = Expression(steps[-1])
|
self.child = Expression(steps[-1])
|
||||||
print("self.child -> ", self.child)
|
|
||||||
|
|
||||||
## ---------------------
|
## ---------------------
|
||||||
## String parsing
|
## String parsing
|
||||||
@ -359,8 +357,7 @@ def test(exp):
|
|||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
#Expression.STR_RENDER = txt
|
Expression.STR_RENDER = txt
|
||||||
Expression.STR_RENDER = lambda x: str(x)
|
|
||||||
exp = "2 ^ 3 * 5"
|
exp = "2 ^ 3 * 5"
|
||||||
test(exp)
|
test(exp)
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
from .generic import Stack,flatten_list
|
from .generic import Stack,flatten_list
|
||||||
from .fraction import Fraction
|
from .fraction import Fraction
|
||||||
|
from .operator import Operator
|
||||||
|
|
||||||
__all__ = ['Render']
|
__all__ = ['Render']
|
||||||
|
|
||||||
@ -195,7 +196,7 @@ class Render(object):
|
|||||||
:returns: boolean
|
:returns: boolean
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return (type(exp) == str and exp in self.operators)
|
return (type(exp) == Operator and str(exp) in self.operators)
|
||||||
|
|
||||||
class flist(list):
|
class flist(list):
|
||||||
"""Fake list- they are used to stock the main operation of an rendered expression"""
|
"""Fake list- they are used to stock the main operation of an rendered expression"""
|
||||||
|
@ -6,6 +6,7 @@ import unittest
|
|||||||
|
|
||||||
from pymath.renders import tex, txt
|
from pymath.renders import tex, txt
|
||||||
from pymath.fraction import Fraction
|
from pymath.fraction import Fraction
|
||||||
|
from pymath.operator import Operator
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -22,21 +23,21 @@ class TestTexRender(unittest.TestCase):
|
|||||||
self.assertEqual(tex([Fraction(1,2)]), "\\frac{ 1 }{ 2 }")
|
self.assertEqual(tex([Fraction(1,2)]), "\\frac{ 1 }{ 2 }")
|
||||||
|
|
||||||
def test_mult_interger(self):
|
def test_mult_interger(self):
|
||||||
exps = [ [2, 3, "*"], [2, -3, "*"], [-2, 3, "*"]]
|
exps = [ [2, 3, Operator("*", 2)], [2, -3, Operator("*", 2)], [-2, 3, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 \\times 3", "2 \\times ( -3 )", "-2 \\times 3"]
|
wanted_render = [ "2 \\times 3", "2 \\times ( -3 )", "-2 \\times 3"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = tex(e)
|
rend = tex(e)
|
||||||
self.assertEqual(rend, wanted_render[i])
|
self.assertEqual(rend, wanted_render[i])
|
||||||
|
|
||||||
def test_mult_letter(self):
|
def test_mult_letter(self):
|
||||||
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]]
|
exps = [ [2, "a", Operator("*", 2)], ["a", 3, Operator("*", 2)], [-2, "a", Operator("*", 2)], ["a", -2, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
|
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = tex(e)
|
rend = tex(e)
|
||||||
self.assertEqual(rend, wanted_render[i])
|
self.assertEqual(rend, wanted_render[i])
|
||||||
|
|
||||||
def test_mult_fraction(self):
|
def test_mult_fraction(self):
|
||||||
exps = [ [2, Fraction(1,2), "*"], [Fraction(1,2), 3, "*"]]
|
exps = [ [2, Fraction(1,2), Operator("*", 2)], [Fraction(1,2), 3, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 2 } \\times 3"]
|
wanted_render = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 2 } \\times 3"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = tex(e)
|
rend = tex(e)
|
||||||
@ -64,21 +65,27 @@ class TesttxtRender(unittest.TestCase):
|
|||||||
self.assertEqual(txt([Fraction(1,2)]), "1 / 2")
|
self.assertEqual(txt([Fraction(1,2)]), "1 / 2")
|
||||||
|
|
||||||
def test_mult_interger(self):
|
def test_mult_interger(self):
|
||||||
exps = [ [2, 3, "*"], [2, -3, "*"], [-2, 3, "*"]]
|
exps = [ [2, 3, Operator("*", 2)], \
|
||||||
|
[2, -3, Operator("*", 2)], \
|
||||||
|
[-2, 3, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 * 3", "2 * ( -3 )", "-2 * 3"]
|
wanted_render = [ "2 * 3", "2 * ( -3 )", "-2 * 3"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = txt(e)
|
rend = txt(e)
|
||||||
self.assertEqual(rend, wanted_render[i])
|
self.assertEqual(rend, wanted_render[i])
|
||||||
|
|
||||||
def test_mult_letter(self):
|
def test_mult_letter(self):
|
||||||
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]]
|
exps = [ [2, "a", Operator("*", 2)], \
|
||||||
|
["a", 3, Operator("*", 2)], \
|
||||||
|
[-2, "a", Operator("*", 2)], \
|
||||||
|
["a", -2, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 a", "a * 3", "-2 a", "a * ( -2 )"]
|
wanted_render = [ "2 a", "a * 3", "-2 a", "a * ( -2 )"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = txt(e)
|
rend = txt(e)
|
||||||
self.assertEqual(rend, wanted_render[i])
|
self.assertEqual(rend, wanted_render[i])
|
||||||
|
|
||||||
def test_mult_fraction(self):
|
def test_mult_fraction(self):
|
||||||
exps = [ [2, Fraction(1,2), "*"], [Fraction(1,2), 3, "*"]]
|
exps = [ [2, Fraction(1,2), Operator("*", 2)], \
|
||||||
|
[Fraction(1,2), 3, Operator("*", 2)]]
|
||||||
wanted_render = [ "2 * 1 / 2", "1 / 2 * 3"]
|
wanted_render = [ "2 * 1 / 2", "1 / 2 * 3"]
|
||||||
for (i,e) in enumerate(exps):
|
for (i,e) in enumerate(exps):
|
||||||
rend = txt(e)
|
rend = txt(e)
|
||||||
|
Loading…
Reference in New Issue
Block a user