Skip to content

Commit

Permalink
test: Add tests for #ASK, #ASK_ONLY, #ANS and #ANSWER tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
wxgeo committed Nov 20, 2023
1 parent 5ed724e commit 2d4be53
Show file tree
Hide file tree
Showing 3 changed files with 441 additions and 378 deletions.
56 changes: 56 additions & 0 deletions tests/test_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest

from ptyx.printers import sympy2latex
from ptyx.utilities import find_closing_bracket, round_away_from_zero


def test_find_closing_bracket():
assert find_closing_bracket("{hello{world} !} etc.", 1) == 15
assert find_closing_bracket("{'}'}", 1) == 4
assert find_closing_bracket("{'}'}", 1, detect_strings=False) == 2
# Unbalanced
with pytest.raises(ValueError, match="ERROR: unbalanced brackets"):
find_closing_bracket("{'}' '}")
assert find_closing_bracket("[o[k]]", 1, brackets="[]") == 5


def test_find_closing_bracket_escape_char():
# Support \\ to escape ' or " character
assert find_closing_bracket("{'}\\''}", 1) == 6
assert find_closing_bracket('{"\'}\\""}', 1) == 7
# Unbalanced
with pytest.raises(ValueError, match="ERROR: unbalanced brackets"):
assert find_closing_bracket("{'}\\\\''}", 1) == 6
assert find_closing_bracket("{'}\\\\\\''}", 1) == 8


def test_round():
assert round_away_from_zero(1.775, 2) == 1.78

assert round_away_from_zero(1.454, -2) == 0
assert round_away_from_zero(1.454, -1) == 0
assert round_away_from_zero(1.454) == 1
assert round_away_from_zero(1.454, 1) == 1.5
assert round_away_from_zero(1.454, 2) == 1.45
assert round_away_from_zero(1.454, 3) == 1.454
assert round_away_from_zero(1.454, 4) == 1.454

assert round_away_from_zero(-9.545, -2) == 0
assert round_away_from_zero(-9.545, -1) == -10
assert round_away_from_zero(-9.545) == -10
assert round_away_from_zero(-9.545, 1) == -9.5
assert round_away_from_zero(-9.545, 2) == -9.55
assert round_away_from_zero(-9.545, 3) == -9.545
assert round_away_from_zero(-9.545, 4) == -9.545

assert round_away_from_zero(float("inf"), 4) == float("inf")
assert round_away_from_zero(float("-inf"), 4) == float("-inf")
assert str(round_away_from_zero(float("nan"), 4)) == "nan"


def test_sympy2latex():
assert sympy2latex(0.0) == "0"
assert sympy2latex(-2.0) == "-2"
assert sympy2latex(-0.0) == "0"
# Issue with sympy 1.7+
assert sympy2latex("hello") == "hello"
Loading

0 comments on commit 2d4be53

Please sign in to comment.