コード例 #1
0
def test_latex_newcommand():
    # \newcommand parameters #1, #2... are not tags.
    test = r"""\newcommand{\rep}[1]{\ding{114}\,\,#1\hfill}"""
    result = test
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #2
0
def test_CASE():
    test = (
        "#CASE{0}first case#CASE{1}second case#CASE{2}third one#END#CASE{1} bonus#END this is something else."
    )
    result = "second case bonus this is something else."
    c = Compiler()
    latex = c.parse(test, PTYX_NUM=1)
    assert latex == result
コード例 #3
0
def test_INCLUDE():
    os.chdir(TEST_DIR)
    test = "#SEED{99}First, $a=#{a=7}$#INCLUDE{include_example.txt}Last, $a=#a$ still."
    result = """First, $a=7$
This is an example of a file
that can be included using INCLUDE tag.
It may contain some pTyX code...
Note that if $a=1$, then $a+1=2$.
Last, $a=7$ still."""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #4
0
def test_ADD():
    test = r"""
#PYTHON
a = 2
b = 3
#END
$#a#+\dfrac{#b}{x}$"""
    result = r"""
$2+\dfrac{3}{x}$"""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #5
0
def test_MUL_SUB_ADD_expr():
    test = r"""
#PYTHON
a = 6
b = 6
c = 5
d = -4
#END
$#a#*#{b*x+c}$
$#a#-#{b*x+c}$
$#a#+#d$"""
    result = r"""
$6\times \left(6 x + 5\right)$
$6-\left(6 x + 5\right)$
$6-4$"""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #6
0
def test_comments():
    test = """# Let's test comments.
First, $a=#{a=7}$ # This is a comment
# This is another comment
# Comment are introduced using an hashtag followed by a space : # comment
# The hashtag must also be preceded by a space or be the first caracter of the line.
Last, $a=#a$ still.
#a#a#a#a # comment must support things like #a of course (there are remove
# before parsing).
## is just displayed as a hash, it is not a comment.
# # is comment though."""
    result = """First, $a=7$
Last, $a=7$ still.
7777
# is just displayed as a hash, it is not a comment.
#"""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #7
0
def test_TABVAR():
    test = (
        "$#{a=2;}\\alpha=#{alpha=3},\\beta=#{beta=5}\n\n"
        "#TABVAR[limites=False,derivee=False]f(x)=#a*(x-#alpha)^2+#beta#END_TABVAR$"
    )
    result = """$\\alpha=3,\\beta=5
\\setlength{\\TVextraheight}{\\baselineskip}
\\[\\begin{tabvar}{|C|CCCCC|}
\\hline
\\,\\,x\\,\\,                            &-\\infty      &        &3&      &+\\infty\\\\
\\hline
\\niveau{1}{2}\\raisebox{0.5em}{$f(x)$}&\\niveau{2}{2}&\\decroit&5&\\croit&\\\\
\\hline
\\end{tabvar}\\]
% x;f(x):(-oo;) >> (3;5) << (+oo;)
% f(x)=2*(x-3)^2+5\n$"""
    c = Compiler()
    latex = c.parse("#LOAD{geophyx}" + test)
    assert latex == result
コード例 #8
0
def test_SEED_SHUFFLE():
    test = '''#SEED{16}Who said "Having nothing, nothing can he lose" ?

\\begin{enumerate}
#SHUFFLE
#ITEM \\item W. Shakespeare
#ITEM \\item R. Wallace
#ITEM \\item C. Doyle
#ITEM \\item R. Bradsbury
#END
\\end{enumerate}

"The game is up."'''
    result = '''Who said "Having nothing, nothing can he lose" ?

\\begin{enumerate} \\item W. Shakespeare \\item R. Bradsbury \\item R. Wallace \\item C. Doyle
\\end{enumerate}

"The game is up."'''
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #9
0
def test_SEED_SHUFFLE_2():
    tests = []
    tests.append("""#SEED{153}%
#SHUFFLE
#ITEM
a
#ITEM
b
#ITEM
c
#END""")
    tests.append("""#SEED{153}%
#SHUFFLE

#ITEM
a
#ITEM
b
#ITEM
c
#END""")
    tests.append("""#SEED{153}%
#SHUFFLE
 #ITEM
a
#ITEM
b
#ITEM
c
#END""")
    c = Compiler()
    results = []
    for test in tests:
        results.append(c.parse(test))
    assert results[0] == "%\nc\na\nb"
    assert results[1] == "%\n\nc\na\nb"
    assert results[2] == "%\nc\na\nb"
コード例 #10
0
def test_MUL():
    # Test 1
    test = r"""
#PYTHON
a=-8
b=4
c=5
d=8
#END
$\dfrac{#a#*(#{c*x+d})#-#{a*x+b}#*#c}{(#{c*x+d})^2}$"""
    result = r"""
$\dfrac{-8\times (5 x + 8)-\left(- 8 x + 4\right)\times 5}{(5 x + 8)^2}$"""
    alternative_result = r"""
$\dfrac{-8\times (5 x + 8)-\left(4 - 8 x\right)\times 5}{(5 x + 8)^2}$"""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result or latex == alternative_result

    # Test 2
    test = "2#*3"
    result = r"2\times 3"
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #11
0
def test_EVAL_abs():
    test = r"$#{abs(-5)}$"
    result = r"$5$"
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #12
0
def test_MACRO():
    test = r"#MACRO{a0}#IF{a==0}$a=0$#ELSE$a\neq 0$#END#END_MACRO#{a=0;}Initially #CALL{a0}#{a=2;}, but now #CALL{a0}."
    result = r"Initially $a=0$, but now $a\neq 0$."
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #13
0
def test_IF_ELIF_ELSE():
    test = r"#{a=1;}#IF{a==0}0#ELIF{a==1}1#ELSE{}2#END#{a=0;}#IF{a==0}0#ELIF{a==1}1#ELSE{}2#END#{a=2;}#IF{a==0}0#ELIF{a==1}1#ELSE{}2#END."
    result = r"10{}2."
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #14
0
def test_PICK():
    test = """
    And the winner is:
    #PICK
    #ITEM
    1
    #ITEM
    2
    #ITEM
    3
    #ITEM
    4
    #END_PICK"""
    c = Compiler()
    c.read_code(test)
    c.preparse()
    # Tweak seed.
    c._state["seed"] = 1
    c.generate_syntax_tree()
    g = c.latex_generator
    assert g.NUM == 0

    # Tweak seed again.
    c._state["seed"] = 5
    assert g.NUM == 0
    latex = c.get_latex()
    latex = re.sub(r"\s+", " ", latex).strip()
    assert latex == "And the winner is: 3"
コード例 #15
0
def test_TEST():
    test = r"""#{hxA=2;}#{yA=3;}\fbox{#TEST{hxA==yA}{is in}{isn't in}}"""
    result = r"""\fbox{isn't in}"""
    c = Compiler()
    latex = c.parse(test)
    assert latex == result
コード例 #16
0
def test_CALC():
    test = r"$#CALC{\dfrac{2}{3}+1}=#RESULT$ et $#CALC[a]{\dfrac{2}{3}-1}=#a$"
    c = Compiler()
    latex = c.parse("#LOAD{geophyx}" + test)
    print(latex)
    assert latex == r"$\dfrac{2}{3}+1=\frac{5}{3}$ et $\dfrac{2}{3}-1=- \frac{1}{3}$"
コード例 #17
0
def test_latex_code_generator():
    test = "#{variable=3;b=1;}#{a=2}#IF{a>0}some text here#ELIF{b>0}some more text#ELSE variable value is #variable not #{variable+1} !#END ok"
    c = Compiler()
    latex = c.parse(test)
    assert latex == "2some text here ok"