File size: 1,294 Bytes
fe0c99f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import annotations

import pytest

hypothesis = pytest.importorskip("hypothesis")
st = hypothesis.strategies
given = hypothesis.given
settings = hypothesis.settings
HealthCheck = hypothesis.HealthCheck

from core.extractor import ExpressionExtractor  # noqa: E402
from core.parser import ExpressionParser  # noqa: E402

SAFE_ALPHABET = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+-*/^(){}[]\\ ,.=→∞")


@pytest.mark.fuzz
@settings(max_examples=120, suppress_health_check=[HealthCheck.too_slow], deadline=None)
@given(st.text(alphabet=SAFE_ALPHABET, min_size=0, max_size=120))
def test_parser_never_raises_on_fuzzy_math_like_input(text):
    parser = ExpressionParser()
    out = parser.parse(text)
    assert isinstance(out, dict)
    assert "success" in out
    if out["success"]:
        assert "sympy_expr" in out
    else:
        assert "error" in out


@pytest.mark.fuzz
@settings(max_examples=120, suppress_health_check=[HealthCheck.too_slow], deadline=None)
@given(st.text(alphabet=SAFE_ALPHABET, min_size=0, max_size=160))
def test_extractor_never_raises_on_fuzzy_math_like_input(text):
    extractor = ExpressionExtractor()
    inner, params = extractor.extract(text)
    assert isinstance(inner, str)
    assert isinstance(params, dict)