| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
"""Test the version comparison functions.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from typing import Final |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from feature_check import defs |
| 13 |
from feature_check import expr as fexpr |
| 14 |
from feature_check import parser as fparser |
| 15 |
from feature_check import version as fver |
| 16 |
|
| 17 |
from . import data |
| 18 |
|
| 19 |
|
| 20 |
def parsed_features() -> dict[str, fver.Version]: |
| 21 |
"""Parse the versions of the features used in the test.""" |
| 22 |
return {name: fparser.parse_version(value) for name, value in data.FEATURES.items()} |
| 23 |
|
| 24 |
|
| 25 |
def do_test_compare(var: str, op_name: str, right: str, *, expected: bool) -> None: |
| 26 |
"""Test the comparison functions.""" |
| 27 |
feature: Final = f"{var} {op_name} {right}" |
| 28 |
mode: Final = fparser.parse_expr(feature) |
| 29 |
assert isinstance(mode, defs.ModeSimple) |
| 30 |
expr: Final = mode.ast |
| 31 |
assert isinstance(expr, fexpr.ExprOp) |
| 32 |
assert len(expr.args) == 2 # noqa: PLR2004 |
| 33 |
assert isinstance(expr.args[0], fexpr.ExprFeature) |
| 34 |
assert isinstance(expr.args[1], fexpr.ExprVersion) |
| 35 |
|
| 36 |
res: Final = expr.evaluate(parsed_features()) |
| 37 |
assert isinstance(res, fexpr.ResultBool) |
| 38 |
assert res.value == expected |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize(("var", "op_name", "right", "expected"), data.COMPARE) |
| 42 |
def test_compare(*, var: str, op_name: str, right: str, expected: bool) -> None: |
| 43 |
"""Test the comparison functions with word operands.""" |
| 44 |
return do_test_compare(var, op_name, right, expected=expected) |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.parametrize(("var", "op_name", "right", "expected"), data.COMPARE) |
| 48 |
def test_synonyms(*, var: str, op_name: str, right: str, expected: bool) -> None: |
| 49 |
"""Test the comparison functions with word operands.""" |
| 50 |
return do_test_compare(var, data.SYNONYMS[op_name], right, expected=expected) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize("var", sorted({item[0] for item in data.COMPARE})) |
| 54 |
def test_single(*, var: str) -> None: |
| 55 |
"""Test obtaining the version of a single feature.""" |
| 56 |
mode: Final = fparser.parse_expr(var) |
| 57 |
assert isinstance(mode, defs.ModeSingle) |
| 58 |
assert mode.feature == var |
| 59 |
expr: Final = mode.ast |
| 60 |
assert isinstance(expr, fexpr.ExprFeature) |
| 61 |
assert expr.name == var |
| 62 |
|
| 63 |
res: Final = expr.evaluate(parsed_features()) |
| 64 |
assert isinstance(res, fexpr.ResultVersion) |
| 65 |
ver: Final = res.value |
| 66 |
assert ver.value |
| 67 |
assert ver.components |