[test] Init

This commit is contained in:
OleehyO
2025-04-19 16:31:37 +00:00
parent d4cef5135f
commit 59df576c85
2 changed files with 76 additions and 0 deletions

68
tests/test_globals.py Normal file
View File

@@ -0,0 +1,68 @@
import pytest
import logging
from texteller.globals import Globals
def test_singleton_pattern():
"""Test that Globals uses the singleton pattern correctly."""
# Create two instances
globals1 = Globals()
globals2 = Globals()
# Both variables should reference the same object
assert globals1 is globals2
# Modifying one should affect the other
globals1.test_attr = "test_value"
assert globals2.test_attr == "test_value"
# Clean up after test
delattr(globals1, "test_attr")
def test_predefined_attributes():
"""Test predefined attributes have correct default values."""
globals_instance = Globals()
assert globals_instance.repo_name == "OleehyO/TexTeller"
assert globals_instance.logging_level == logging.INFO
def test_attribute_modification():
"""Test that attributes can be modified."""
globals_instance = Globals()
# Modify existing attribute
original_repo_name = globals_instance.repo_name
globals_instance.repo_name = "NewRepo/NewName"
assert globals_instance.repo_name == "NewRepo/NewName"
assert Globals().logging_level == logging.INFO
Globals().logging_level = logging.DEBUG
assert Globals().logging_level == logging.DEBUG
# Reset for other tests
globals_instance.repo_name = original_repo_name
globals_instance.logging_level = logging.INFO
def test_dynamic_attributes():
"""Test that new attributes can be added dynamically."""
globals_instance = Globals()
# Add new attribute
globals_instance.new_attribute = "new_value"
assert globals_instance.new_attribute == "new_value"
# Clean up after test
delattr(globals_instance, "new_attribute")
def test_representation():
"""Test the string representation of Globals."""
globals_instance = Globals()
repr_string = repr(globals_instance)
# Check that repr contains class name and is formatted as expected
assert repr_string.startswith("<Globals:")
assert "repo_name" in repr_string
assert "logging_level" in repr_string

8
tests/test_to_katex.py Normal file
View File

@@ -0,0 +1,8 @@
from texteller import to_katex
def test_to_katex():
# Test complex mathematical equations with vectors and symbols
complex_input = "\\[\\begin{split}&\\mathbb{E}_{\\bm{\\omega}}[h(\\mathbf{x})h(\\mathbf{y})^{ *}]\\\\ &=\\mathbb{E}_{\\bm{\\omega}}[\\exp(i\\bm{\\omega}^{\\top}\\mathbf{x})\\exp (-i\\bm{\\omega}^{\\top}\\mathbf{y}))]\\\\ &=\\mathbb{E}_{\\bm{\\omega}}[\\exp(i\\bm{\\omega}^{\\top}\\bm{\\delta})] \\\\ &=\\int_{\\mathbb{R}^{D}}p(\\bm{\\omega})\\exp(i\\bm{\\omega}^{\\top}\\bm{ \\delta})\\mathrm{d}\\bm{\\omega}\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp\\!\\big{(}-\\frac{1}{2}\\bm{ \\omega}^{\\top}\\bm{\\omega}\\big{)}\\exp(i\\bm{\\omega}^{\\top}\\bm{\\delta})\\mathrm{d} \\bm{\\omega}\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp\\!\\big{(}-\\frac{1}{2}\\bm{ \\omega}^{\\top}\\bm{\\omega}-i\\bm{\\omega}^{\\top}\\bm{\\delta}\\big{)}\\mathrm{d}\\bm{ \\omega}\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp\\!\\big{(}-\\frac{1}{2}\\big{(} \\bm{\\omega}^{\\top}\\bm{\\omega}-2i\\bm{\\omega}^{\\top}\\bm{\\delta}-\\bm{\\delta}^{ \\top}\\bm{\\delta}\\big{)}-\\frac{1}{2}\\bm{\\delta}^{\\top}\\bm{\\delta}\\big{)} \\mathrm{d}\\bm{\\omega}\\\\ &=(2\\pi)^{-D/2}\\exp\\!\\big{(}-\\frac{1}{2}\\bm{\\delta}^{\\top}\\bm{ \\delta}\\big{)}\\!\\underbrace{\\int_{\\mathbb{R}^{D}}\\exp\\!\\big{(}-\\frac{1}{2}\\big{(} \\bm{\\omega}-i\\bm{\\delta}\\big{)}^{\\top}\\big{(}\\bm{\\omega}-i\\bm{\\delta}\\big{)} \\big{)}\\mathrm{d}\\bm{\\omega}}_{(2\\pi)^{D/2}}\\\\ &=\\exp\\!\\big{(}-\\frac{1}{2}\\bm{\\delta}^{\\top}\\bm{\\delta}\\big{)} \\\\ &=k(\\bm{\\delta}).\\end{split}\\]"
expected_output = "\\begin{split}&\\mathbb{E}_{ \\omega}[h( x)h( y)^{ *}]\\\\ &=\\mathbb{E}_{ \\omega}[\\exp(i \\omega^{\\top} x)\\exp (-i \\omega^{\\top} y))]\\\\ &=\\mathbb{E}_{ \\omega}[\\exp(i \\omega^{\\top} \\delta)] \\\\ &=\\int_{\\mathbb{R}^{D}}p( \\omega)\\exp(i \\omega^{\\top} \\delta)\\mathrm{d} \\omega\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp \\big(-\\frac{1}{2} \\omega^{\\top} \\omega\\big)\\exp(i \\omega^{\\top} \\delta)\\mathrm{d} \\omega\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp \\big(-\\frac{1}{2} \\omega^{\\top} \\omega-i \\omega^{\\top} \\delta\\big)\\mathrm{d} \\omega\\\\ &=(2\\pi)^{-D/2}\\int_{\\mathbb{R}^{D}}\\exp \\big(-\\frac{1}{2}\\big( \\omega^{\\top} \\omega-2i \\omega^{\\top} \\delta- \\delta^{ \\top} \\delta\\big)-\\frac{1}{2} \\delta^{\\top} \\delta\\big) \\mathrm{d} \\omega\\\\ &=(2\\pi)^{-D/2}\\exp \\big(-\\frac{1}{2} \\delta^{\\top} \\delta\\big) \\underbrace{\\int_{\\mathbb{R}^{D}}\\exp \\big(-\\frac{1}{2}\\big( \\omega-i \\delta\\big)^{\\top}\\big( \\omega-i \\delta\\big) \\big)\\mathrm{d} \\omega}_{(2\\pi)^{D/2}}\\\\ &=\\exp \\big(-\\frac{1}{2} \\delta^{\\top} \\delta\\big) \\\\ &=k( \\delta).\n\\end{split}\n"
assert to_katex(complex_input).strip() == expected_output.strip()