Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to match-case (switch) with dict without conditionals #1245

Merged
merged 1 commit into from
May 2, 2024

Conversation

luc10921
Copy link
Collaborator

@luc10921 luc10921 commented May 2, 2024

Summary or solution description
Now it's possible to use dictionaries on the match case

How to Reproduce

from boa3.builtin.compile_time import public


@public
def main(dict_: dict) -> str:
    match dict_:
        case {
            'ccccc': None,
            'ab': 'cd',
            '12': '34',
            'xy': 'zy',
            '00': '55',
        }:
            return "big dictionary"
        case {'key': 'value'}:
            return "key and value"
        case {}:
            return "empty dict"
        case _:
            return "default return"

Tests

    async def test_dict_type_match_case(self):
        await self.set_up_contract('DictTypeMatchCase.py')

        def match_case(dict_: dict) -> str:
            match dict_:
                case {
                    'ccccc': None,
                    'ab': 'cd',
                    '12': '34',
                    'xy': 'zy',
                    '00': '55',
                }:
                    return "big dictionary"
                case {'key': 'value'}:
                    return "key and value"
                case {}:
                    return "empty dict"
                case _:
                    return "default return"

        arg = {}
        result, _ = await self.call('main', [arg], return_type=str)
        self.assertEqual(match_case(arg), result)

        arg = {'key': 'value'}
        result, _ = await self.call('main', [arg], return_type=str)
        self.assertEqual(match_case(arg), result)

        arg = {'key': 'value', 'unit': 'test'}
        result, _ = await self.call('main', [arg], return_type=str)
        self.assertEqual(match_case(arg), result)

        arg = {'another': 'pair'}
        result, _ = await self.call('main', [arg], return_type=str)
        self.assertEqual(match_case(arg), result)

        arg = {
            'ccccc': None,
            'ab': 'cd',
            '12': '34',
            'xy': 'zy',
            '00': '55',
        }
        result, _ = await self.call('main', [arg], return_type=str)
        self.assertEqual(match_case(arg), result)

Platform:

  • OS: Windows 11 x64
  • Python version: Python 3.11

@luc10921 luc10921 requested a review from meevee98 May 2, 2024 15:12
@luc10921 luc10921 self-assigned this May 2, 2024
@melanke
Copy link
Collaborator

melanke commented May 2, 2024

@meevee98 meevee98 merged commit a5627c2 into development May 2, 2024
4 checks passed
@meevee98 meevee98 deleted the CU-86drpncqy branch May 2, 2024 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants