@@ -1521,5 +1521,41 @@ def test_tapify_has_kwargs_replace_default(self) -> None:
1521
1521
self .assertEqual (output , "1_5_c=3-d=4" )
1522
1522
1523
1523
1524
+ class TestTapifyUnderscoresToDashes (unittest .TestCase ):
1525
+ def setUp (self ) -> None :
1526
+ class MyClass :
1527
+ def __init__ (self , my_arg : str ):
1528
+ self .my_arg = my_arg
1529
+
1530
+ def __eq__ (self , other : str ) -> bool :
1531
+ return self .my_arg == other
1532
+
1533
+ @dataclass
1534
+ class DataClassTarget :
1535
+ my_arg : str
1536
+
1537
+ def __eq__ (self , other : str ) -> bool :
1538
+ return self .my_arg == other
1539
+
1540
+ def my_function (my_arg : str ) -> str :
1541
+ return my_arg
1542
+
1543
+ self .class_or_functions = [my_function , MyClass , DataClassTarget ]
1544
+
1545
+ def test_underscores_to_dashes (self ) -> None :
1546
+ for target in self .class_or_functions :
1547
+ # With underscores_to_dashes True and using dashes in the args.
1548
+ instance = tapify (target , command_line_args = ["--my-arg" , "value" ], underscores_to_dashes = True )
1549
+ self .assertEqual (instance , "value" )
1550
+
1551
+ # With underscores_to_dashes False and using underscore in the args.
1552
+ instance = tapify (target , command_line_args = ["--my_arg" , "value" ], underscores_to_dashes = False )
1553
+ self .assertEqual (instance , "value" )
1554
+
1555
+ # Using underscore when dashes are expected causes a parse error.
1556
+ with self .assertRaises (SystemExit ):
1557
+ tapify (target , command_line_args = ["--my_arg" , "value" ], underscores_to_dashes = True )
1558
+
1559
+
1524
1560
if __name__ == "__main__" :
1525
1561
unittest .main ()
0 commit comments