Commit 546e8fcb authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

more tests (and bug fix)

parent 773a394f
......@@ -28,8 +28,7 @@ import regex
from . import latexfuntypes
from . import parser
class LatexSyntaxError(SyntaxError):
pass
from .latexfuntypes import LatexSyntaxError
class converter:
"""flatlatex converter class
......
......@@ -27,4 +27,6 @@ class latexfun:
self.fun = fun
self.nargs = nargs
class LatexSyntaxError(SyntaxError):
pass
......@@ -23,10 +23,13 @@
# POSSIBILITY OF SUCH DAMAGE.
import regex
from .latexfuntypes import LatexSyntaxError
def parse_one_element(s):
R = r'((?>\\(?:[^A-Za-z]|[A-Za-z]+))|(?>[^\{\}\\])|\{(?1)*\})'
r = regex.match(R,s)
if not r:
raise LatexSyntaxError
s = s[r.span()[1]:]
c = r.captures()[0]
if c[0] == '\\':
......
from .. import converter
def test_conv1():
c = converter()
r = c.convert(
r'\forall \eta>0\, \exists n\in\mathbb{N}\, \forall i>n\, |u_i-\mathcal{l}|<\eta')
assert r=='∀η>0 ∃n∊ℕ ∀i>n |uᵢ-𝓵|<η'
def test_conv2():
c = converter()
c.add_newcommand(r'\newcommand\prob{\mathbb{P}}')
c.add_newcommand(r'\newcommand\binom[2]{\frac{#2!}{#1!(#2-#1)!}}')
r = c.convert(r'\prob(X=k)\,=\,\binom{k}{n}\times p^k(1-p)^{n-k}')
assert r == 'ℙ(X=k) = (n!)/(k!(n-k)!)×pᵏ(1-p)ⁿ⁻ᵏ'
def test_conv3():
c = converter()
c.allow_zw = True
r = c.convert(r'\frac{8}{9}')
assert r == '⁸⁄₉'
c.allow_zw = False
r = c.convert(r'\frac{8}{9}')
assert r == '8/9'
def test_conv4():
c = converter()
c.allow_combinings = True
r = c.convert(r'\hat\alpha')
assert r == '\u03B1\u0302'
c.allow_combinings = False
r = c.convert(r'\hat\alpha')
assert r == 'hat(\u03B1)'
from .. import parser
def test_parser():
expected = {
r'\\': [('cmd', r'\\')],
r'\cd': [('cmd', r'\cd')],
r'ab': [('char','a'),('char','b')],
r'e{to{t}o}e': [('char','e'),('subexpr','to{t}o'),('char','e')],
r'g_f': [('char','g'),('oper','_'),('char','f')],
r'h^i': [('char','h'),('oper','^'),('char','i')],
}
for k,v in expected.items():
assert all([pki==vi for pki,vi in zip(parser.parse(k),v)]), "parser '%s'" % k
for k1,v1 in expected.items():
for k2,v2 in expected.items():
assert all([pki==vi for pki,vi in zip(parser.parse(k1+' '+k2),v1+v2)]), "parser '%s'+'%s'" % (k1,k2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment