diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2021-08-27 11:32:42 -0400 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2021-10-09 18:13:34 -0400 |
commit | 709d151eb944d764b35008ca3275b02bd16a765d (patch) | |
tree | ed731fc59be4131eb3b8f2cc9e5951b4899f07a6 /unittests/internaltests.py | |
parent | 329d111709ab5c5140f75f29c7176c9546de5770 (diff) | |
download | meson-709d151eb944d764b35008ca3275b02bd16a765d.zip meson-709d151eb944d764b35008ca3275b02bd16a765d.tar.gz meson-709d151eb944d764b35008ca3275b02bd16a765d.tar.bz2 |
typed_kwargs: Fix when ContainerTypeInfo is used in a tuple
info.types could be a tuple like (str, ContainerTypeInfo()). That means
we have to check types one by one and only print error if none of them
matched.
Also fix the case when default value is None for a container type, it
should leave the value to None to be able to distinguish between unset
and empty list.
Diffstat (limited to 'unittests/internaltests.py')
-rw-r--r-- | unittests/internaltests.py | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/unittests/internaltests.py b/unittests/internaltests.py index 957f180..cb50f37 100644 --- a/unittests/internaltests.py +++ b/unittests/internaltests.py @@ -42,7 +42,7 @@ from mesonbuild.mesonlib import ( LibType, MachineChoice, PerMachine, Version, is_windows, is_osx, is_cygwin, is_openbsd, search_version, MesonException, OptionKey, ) -from mesonbuild.interpreter.type_checking import in_set_validator +from mesonbuild.interpreter.type_checking import in_set_validator, NoneType from mesonbuild.dependencies import PkgConfigDependency from mesonbuild.programs import ExternalProgram import mesonbuild.modules.pkgconfig @@ -1266,7 +1266,7 @@ class InternalTests(unittest.TestCase): with self.assertRaises(InvalidArguments) as cm: _(None, mock.Mock(), [], {'input': {}}) - self.assertEqual(str(cm.exception), 'testfunc keyword argument "input" container type was "dict", but should have been "list"') + self.assertEqual(str(cm.exception), "testfunc keyword argument 'input' was of type 'dict' but should have been list[str]") def test_typed_kwarg_contained_invalid(self) -> None: @typed_kwargs( @@ -1278,7 +1278,7 @@ class InternalTests(unittest.TestCase): with self.assertRaises(InvalidArguments) as cm: _(None, mock.Mock(), [], {'input': {'key': 1}}) - self.assertEqual(str(cm.exception), 'testfunc keyword argument "input" contained a value of type "int" but should have been "str"') + self.assertEqual(str(cm.exception), "testfunc keyword argument 'input' was of type 'dict' but should have been dict[str]") def test_typed_kwarg_container_listify(self) -> None: @typed_kwargs( @@ -1313,7 +1313,7 @@ class InternalTests(unittest.TestCase): with self.assertRaises(MesonException) as cm: _(None, mock.Mock(), [], {'input': ['a']}) - self.assertEqual(str(cm.exception), "testfunc keyword argument \"input\" container should be of even length, but is not") + self.assertEqual(str(cm.exception), "testfunc keyword argument 'input' was of type 'list' but should have been list[str] that has even size") @mock.patch.dict(mesonbuild.mesonlib.project_meson_versions, {}) def test_typed_kwarg_since(self) -> None: @@ -1425,6 +1425,39 @@ class InternalTests(unittest.TestCase): self.assertEqual(k.default, 'foo') self.assertEqual(v.default, 'bar') + def test_typed_kwarg_default_type(self) -> None: + @typed_kwargs( + 'testfunc', + KwargInfo('no_default', (str, ContainerTypeInfo(list, str), NoneType)), + KwargInfo('str_default', (str, ContainerTypeInfo(list, str)), default=''), + KwargInfo('list_default', (str, ContainerTypeInfo(list, str)), default=['']), + ) + def _(obj, node, args: T.Tuple, kwargs: T.Dict[str, str]) -> None: + self.assertEqual(kwargs['no_default'], None) + self.assertEqual(kwargs['str_default'], '') + self.assertEqual(kwargs['list_default'], ['']) + _(None, mock.Mock(), [], {}) + + def test_typed_kwarg_invalid_default_type(self) -> None: + @typed_kwargs( + 'testfunc', + KwargInfo('invalid_default', (str, ContainerTypeInfo(list, str), NoneType), default=42), + ) + def _(obj, node, args: T.Tuple, kwargs: T.Dict[str, str]) -> None: + pass + self.assertRaises(AssertionError, _, None, mock.Mock(), [], {}) + + def test_typed_kwarg_container_in_tuple(self) -> None: + @typed_kwargs( + 'testfunc', + KwargInfo('input', (str, ContainerTypeInfo(list, str))), + ) + def _(obj, node, args: T.Tuple, kwargs: T.Dict[str, str]) -> None: + self.assertEqual(kwargs['input'], args[0]) + _(None, mock.Mock(), [''], {'input': ''}) + _(None, mock.Mock(), [['']], {'input': ['']}) + self.assertRaises(InvalidArguments, _, None, mock.Mock(), [], {'input': 42}) + def test_detect_cpu_family(self) -> None: """Test the various cpu familes that we detect and normalize. |