Skip to content
Snippets Groups Projects
Commit f4054794 authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

Write some test cases

More will follow
parent e568d84b
No related branches found
No related tags found
No related merge requests found
iniconfig==2.0.0
packaging==23.1
pluggy==1.3.0
pytest==7.4.1
import pytest
from dsiParser import dsiTree
def test_baseCase():
# Most basic case: one unit without prefix or exponent
tree = dsiTree(r'\metre')
assert tree.tree == [[['','metre','']]]
assert tree.toLatex() == r'\mathrm{m}'
assert tree.valid
assert tree.warnings == []
def test_robustness():
# Unknown unit
with pytest.warns(RuntimeWarning, match='The identifier \"foo\" does not match any D-SI units!'):
tree = dsiTree(r'\foo')
assert tree.toLatex() == r'{\color{red}\mathrm{foo}}'
assert not tree.valid
assert len(tree.warnings) == 1
assert tree.warnings == ['The identifier \"foo\" does not match any D-SI units!']
# Unknown string in the middle of input
with pytest.warns(RuntimeWarning, match='The identifier \"mini\" does not match any D-SI units!'):
tree = dsiTree(r'\kilo\metre\per\mini\second')
print(tree.toLatex())
assert tree.toLatex() == r'\frac{\mathrm{k}\mathrm{m}}{{\color{red}\mathrm{mini}}\,\mathrm{s}}'
assert not tree.valid
assert len(tree.warnings) == 1
assert tree.warnings == ['The identifier \"mini\" does not match any D-SI units!']
def test_power():
# power
powerTree = dsiTree(r'\metre\tothe{2}')
assert powerTree.tree == [[['','metre','2']]]
assert powerTree.toLatex() == r'\mathrm{m}^{2}'
assert powerTree.valid
assert powerTree.warnings == []
assert dsiTree(r'\metre\tothe{0.5}').toLatex() == r'\sqrt{\mathrm{m}}'
assert dsiTree(r'\metre\tothe{-2}').toLatex() == r'\mathrm{m}^{-2}'
assert dsiTree(r'\metre\tothe{1337}').toLatex() == r'\mathrm{m}^{1337}'
def test_prefix():
# prefix
prefixTree = dsiTree(r'\kilo\metre')
assert prefixTree.tree == [[['kilo','metre','']]]
assert prefixTree.toLatex() == r'\mathrm{k}\mathrm{m}'
assert prefixTree.valid
def test_node():
# full node
fullNodeTree = dsiTree(r'\kilo\metre\tothe{2}')
assert fullNodeTree.tree == [[['kilo','metre','2']]]
assert fullNodeTree.toLatex() == r'\mathrm{k}\mathrm{m}^{2}'
assert fullNodeTree.valid
def test_fraction():
fractionTree = dsiTree(r'\mega\metre\per\second\tothe{2}')
assert fractionTree.tree == [[['mega','metre','']],[['','second','2']]]
assert fractionTree.toLatex() == r'\frac{\mathrm{M}\mathrm{m}}{\mathrm{s}^{2}}'
assert fractionTree.valid
def test_empty():
with pytest.warns(RuntimeWarning, match='Given D-SI string is empty!'):
assert dsiTree('').toLatex() == ''
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment