|
16 | 16 | import pathlib |
17 | 17 | import unittest |
18 | 18 | from pathlib import Path |
| 19 | +from typing import Final |
19 | 20 |
|
20 | | -logger = logging.getLogger() |
21 | | - |
22 | | - |
23 | | -class TestExamples(unittest.TestCase): |
24 | | - """Test the examples.""" |
| 21 | +import pytest |
25 | 22 |
|
26 | | - def test_imports(self) -> None: |
27 | | - """Tests the examples can be imported.""" |
28 | | - from fuzzylite import examples # noqa |
29 | | - from fuzzylite.examples import hybrid, mamdani, takagi_sugeno # noqa |
30 | | - from fuzzylite.examples.mamdani import simple_dimmer # noqa |
31 | | - from fuzzylite.examples.mamdani.simple_dimmer import SimpleDimmer # noqa |
32 | | - |
33 | | - def test_absolute_imports(self) -> None: |
34 | | - """Tests the examples can be imported with absolute imports.""" |
35 | | - import fuzzylite # noqa |
36 | | - import fuzzylite.examples # noqa |
37 | | - import fuzzylite.examples.mamdani # noqa |
38 | | - import fuzzylite.examples.mamdani.simple_dimmer # noqa |
39 | | - from fuzzylite.examples.mamdani.simple_dimmer import SimpleDimmer # noqa |
40 | | - |
41 | | - def test_examples(self) -> None: |
42 | | - """Test all the examples are included and can be imported.""" |
43 | | - import fuzzylite as fl |
44 | | - |
45 | | - examples = pathlib.Path(*fl.examples.__path__) |
46 | | - self.assertTrue(examples.exists() and examples.is_dir()) |
| 23 | +logger = logging.getLogger() |
47 | 24 |
|
48 | | - expected = set( |
49 | | - """\ |
| 25 | +EXAMPLES: Final = ( |
| 26 | + """\ |
50 | 27 | fuzzylite.examples.hybrid.obstacle_avoidance |
51 | 28 | fuzzylite.examples.hybrid.tipper |
52 | 29 | fuzzylite.examples.mamdani.all_terms |
@@ -107,39 +84,68 @@ def test_examples(self) -> None: |
107 | 84 | fuzzylite.examples.terms.trapezoid |
108 | 85 | fuzzylite.examples.terms.triangle |
109 | 86 | fuzzylite.examples.terms.zs_shape |
110 | | -fuzzylite.examples.tsukamoto.tsukamoto |
111 | | -""".split() |
112 | | - ) |
| 87 | +fuzzylite.examples.tsukamoto.tsukamoto""".split() |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +class TestExamples(unittest.TestCase): |
| 92 | + """Test the examples.""" |
| 93 | + |
| 94 | + def test_imports(self) -> None: |
| 95 | + """Tests the examples can be imported.""" |
| 96 | + from fuzzylite import examples # noqa |
| 97 | + from fuzzylite.examples import hybrid, mamdani, takagi_sugeno # noqa |
| 98 | + from fuzzylite.examples.mamdani import simple_dimmer # noqa |
| 99 | + from fuzzylite.examples.mamdani.simple_dimmer import SimpleDimmer # noqa |
| 100 | + |
| 101 | + def test_absolute_imports(self) -> None: |
| 102 | + """Tests the examples can be imported with absolute imports.""" |
| 103 | + import fuzzylite # noqa |
| 104 | + import fuzzylite.examples # noqa |
| 105 | + import fuzzylite.examples.mamdani # noqa |
| 106 | + import fuzzylite.examples.mamdani.simple_dimmer # noqa |
| 107 | + from fuzzylite.examples.mamdani.simple_dimmer import SimpleDimmer # noqa |
| 108 | + |
| 109 | + def test_examples(self) -> None: |
| 110 | + """Test all the examples are included and can be imported.""" |
| 111 | + import fuzzylite as fl |
| 112 | + |
| 113 | + examples = pathlib.Path(*fl.examples.__path__) |
| 114 | + self.assertTrue(examples.exists() and examples.is_dir()) |
| 115 | + |
| 116 | + expected = EXAMPLES |
113 | 117 |
|
114 | 118 | obtained = {module for module in fl.Op.glob_examples("module")} |
115 | | - self.assertSetEqual(expected, {module.__name__ for module in obtained}) |
| 119 | + self.assertSetEqual(set(expected), {module.__name__ for module in obtained}) |
116 | 120 |
|
117 | 121 | engines = [engine for engine in fl.Op.glob_examples("engine")] |
118 | 122 | self.assertEqual(len(engines), len(obtained)) |
119 | 123 |
|
120 | | - def test_examples_remain_the_same(self) -> None: |
121 | | - """Test that all the examples remain the same when exported in Python, FLL and FLD.""" |
122 | | - import fuzzylite as fl |
123 | 124 |
|
124 | | - examples = set(fl.Op.glob_examples("module")) |
125 | | - self.assertTrue(bool(examples), msg="examples not found") |
| 125 | +@pytest.mark.parametrize("example", EXAMPLES) |
| 126 | +def test_examples_remain_the_same(example: str) -> None: |
| 127 | + """Test that all the examples remain the same when exported in Python, FLL and FLD.""" |
| 128 | + import fuzzylite as fl |
| 129 | + |
| 130 | + import importlib |
| 131 | + |
| 132 | + example_module = importlib.import_module(example) |
126 | 133 |
|
127 | | - for example_module in examples: |
128 | | - example_class, *_ = inspect.getmembers(example_module, predicate=inspect.isclass) |
129 | | - engine = example_class[1]().engine |
| 134 | + example_class, *_ = inspect.getmembers(example_module, predicate=inspect.isclass) |
| 135 | + engine = example_class[1]().engine |
130 | 136 |
|
131 | | - expected_python = Path(str(example_module.__file__)).read_text() |
132 | | - obtained_python = fl.PythonExporter(formatted=True, encapsulated=True).to_string(engine) |
133 | | - self.assertEqual(expected_python, obtained_python, msg=example_module.__name__) |
| 137 | + expected_python = Path(str(example_module.__file__)).read_text() |
| 138 | + obtained_python = fl.PythonExporter(formatted=True, encapsulated=True).to_string(engine) |
| 139 | + assert obtained_python == expected_python |
134 | 140 |
|
135 | | - expected_fll = Path(str(example_module.__file__)).with_suffix(".fll").read_text() |
136 | | - obtained_fll = fl.FllExporter().to_string(engine) |
137 | | - self.assertEqual(expected_fll, obtained_fll, msg=example_module.__name__) |
| 141 | + expected_fll = Path(str(example_module.__file__)).with_suffix(".fll").read_text() |
| 142 | + obtained_fll = fl.FllExporter().to_string(engine) |
| 143 | + assert obtained_fll == expected_fll |
138 | 144 |
|
139 | | - expected_fld = Path(str(example_module.__file__)).with_suffix(".fld").read_text() |
140 | | - with fl.settings.context(decimals=9): |
141 | | - obtained_fld = fl.FldExporter().to_string(engine) |
142 | | - self.assertEqual(expected_fld, obtained_fld, msg=example_module.__name__) |
| 145 | + expected_fld = Path(str(example_module.__file__)).with_suffix(".fld").read_text() |
| 146 | + with fl.settings.context(decimals=9): |
| 147 | + obtained_fld = fl.FldExporter().to_string(engine) |
| 148 | + assert obtained_fld == expected_fld |
143 | 149 |
|
144 | 150 |
|
145 | 151 | if __name__ == "__main__": |
|
0 commit comments