Skip to content

StringListTestCaseMixin

Mixin

py
class InOutInputTestCaseMixin:
    """Mixin class providing a cleaner way to create input test cases."""

    def iterate_in_out(self, conform_testcase_inputs: List[Any]):
        """Generator that yields input and expected output pairs.
        Ex usage:
            inputs = [
                {
                    "in" : [
                        "<some_input>",
                        "<some_input>",
                        "<some_input>",
                        ...
                    ]
                    "out" : "<some_output>" # can be a single string or a list
                    # "out" : [
                        "<some_output_that_will_match_first_input>",
                        "<some_output_that_will_match_second_input>",
                        "<some_output_that_will_match_third_input>",
                        ...
                    ]
                }
            ]

        """

        for conform_testcase_input in conform_testcase_inputs:
            _in = conform_testcase_input["in"]
            _out = conform_testcase_input["out"]

            if isinstance(_in, list) and isinstance(_out, str):
                # Multiple inputs, single expected output
                for sub_in in _in:
                    yield sub_in, _out
                continue

            if isinstance(_out, list):
                # Multiple inputs, multiple expected outputs
                for sub_in, sub_out in zip(_in, _out):
                    yield sub_in, sub_out
                continue
            yield _in, _out

Usage

Example with string comparison:

py
class TestMTCPath(TestCase, InOutInputTestCaseMixin):
    def test_conform_path(self):
        """Test that MTCPath conforms paths correctly."""
        test_cases = [
            {
                "in": "//stock-01.mtc.wtf/cache",
                "out": "//stock-01.mtc.wtf/cache",
            },
            {
                "in": "//stock-01.mtc.wtf/cache/",
                "out": "//stock-01.mtc.wtf/cache",
            },
            {
                "in": "//stock-01.mtc.wtf\\cache\\",
                "out": "//stock-01.mtc.wtf/cache",
            },
            {
                "in": "\\stock-01.mtc.wtf\\cache\\",
                "out": "//stock-01.mtc.wtf/cache",
            },
            {
                "in": [
                    r"\\stock-01\cache", 
                    "\\stock-01\\cache", 
                    "/stock-01/cache", 
                ],
                "out": "//stock-01/cache",
            },
        ]

    for _in, _out in self.iterate_in_out(test_cases):
        conform_in = MakeSomeStringManipulation(_in)
        self.assertEqual(str(conform_in), _out)