Debian Codegraph @

ml_collections/config_flags/tests/tuple_parser_test.py

raw symbols SCIP
1 # Copyright 2024 The ML Collections Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Tests for ml_collection.config_flags.tuple_parser."""
16
17 from ml_collections.config_flags import tuple_parser
18 from absl.testing import absltest
19 from absl.testing import parameterized
20
21
22 class TupleParserTest(parameterized.TestCase):
23
24 @parameterized.parameters(
25 {'argument': 1, 'expected': (1,)},
26 {'argument': (1, 2), 'expected': (1, 2)},
27 {'argument': ('abc', 'def'), 'expected': ('abc', 'def')},
28 {'argument': '1', 'expected': (1,)},
29 {'argument': '"abc"', 'expected': ('abc',)},
30 {'argument': '"abc",', 'expected': ('abc',)},
31 {'argument': '1, "a"', 'expected': (1, 'a')},
32 {'argument': '(1, "a")', 'expected': (1, 'a')},
33 {'argument': '(1, "a", (2, 3))', 'expected': (1, 'a', (2, 3))},
34 {'argument': ('abc*', 'def*'), 'expected': ('abc*', 'def*')},
35 {'argument': '("abc*", "def*")', 'expected': ('abc*', 'def*')},
36 {'argument': '("/abc",)', 'expected': ('/abc',)},
37 {'argument': '("/abc*",)', 'expected': ('/abc*',)},
38 {'argument': '("/abc/",)', 'expected': ('/abc/',)},
39 )
40 def test_tuple_parser_parse(self, argument, expected):
41 parser = tuple_parser.TupleParser()
42 self.assertEqual(parser.parse(argument), expected)
43
44 @parameterized.parameters(
45 {'argument': '1', 'expected': (1,)},
46 {'argument': '"abc"', 'expected': ('abc',)},
47 {'argument': '"abc",', 'expected': ('abc',)},
48 {'argument': 'abc', 'expected': ('abc',)},
49 {'argument': '1, "a"', 'expected': (1, 'a')},
50 {'argument': '(1, "a")', 'expected': (1, 'a')},
51 {'argument': '(1, "a", (2, 3))', 'expected': (1, 'a', (2, 3))},
52 {'argument': '("abc*", "def*")', 'expected': ('abc*', 'def*')},
53 {'argument': '"abc*", "def*"', 'expected': ('abc*', 'def*')},
54 {'argument': '"abc*",', 'expected': ('abc*',)},
55 {'argument': 'abc*', 'expected': ('abc*',)},
56 {'argument': '"/abc",', 'expected': ('/abc',)},
57 {'argument': '/abc*', 'expected': ('/abc*',)},
58 {'argument': '/abc/', 'expected': ('/abc/',)},
59 )
60 def test_convert_str_to_tuple(self, argument, expected):
61 self.assertEqual(tuple_parser._convert_str_to_tuple(argument), expected)
62
63 @parameterized.parameters(
64 'a b',
65 'a,b*',
66 '/a,b*',
67 '/a,b',
68 'a b',
69 )
70 def test_convert_str_to_tuple_bad_inputs(self, argument):
71 with self.assertRaises(ValueError):
72 tuple_parser._convert_str_to_tuple(argument)
73
74
75 if __name__ == '__main__':
76 absltest.main()