| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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() |