1111from unittest import mock
1212from unittest .mock import Mock
1313
14+ from parameterized import parameterized
15+
1416import can
1517import can .player
1618
@@ -38,7 +40,7 @@ def assertSuccessfulCleanup(self):
3840 self .mock_virtual_bus .__exit__ .assert_called_once ()
3941
4042 def test_play_virtual (self ):
41- sys .argv = self .baseargs + [ self .logfile ]
43+ sys .argv = [ * self .baseargs , self .logfile ]
4244 can .player .main ()
4345 msg1 = can .Message (
4446 timestamp = 2.501 ,
@@ -65,8 +67,8 @@ def test_play_virtual(self):
6567 self .assertSuccessfulCleanup ()
6668
6769 def test_play_virtual_verbose (self ):
68- sys .argv = self .baseargs + [ "-v" , self .logfile ]
69- with unittest . mock .patch ("sys.stdout" , new_callable = io .StringIO ) as mock_stdout :
70+ sys .argv = [ * self .baseargs , "-v" , self .logfile ]
71+ with mock .patch ("sys.stdout" , new_callable = io .StringIO ) as mock_stdout :
7072 can .player .main ()
7173 self .assertIn ("09 08 07 06 05 04 03 02" , mock_stdout .getvalue ())
7274 self .assertIn ("05 0c 00 00 00 00 00 00" , mock_stdout .getvalue ())
@@ -76,7 +78,7 @@ def test_play_virtual_verbose(self):
7678 def test_play_virtual_exit (self ):
7779 self .MockSleep .side_effect = [None , KeyboardInterrupt ]
7880
79- sys .argv = self .baseargs + [ self .logfile ]
81+ sys .argv = [ * self .baseargs , self .logfile ]
8082 can .player .main ()
8183 assert self .mock_virtual_bus .send .call_count <= 2
8284 self .assertSuccessfulCleanup ()
@@ -85,7 +87,7 @@ def test_play_skip_error_frame(self):
8587 logfile = os .path .join (
8688 os .path .dirname (__file__ ), "data" , "logfile_errorframes.asc"
8789 )
88- sys .argv = self .baseargs + [ "-v" , logfile ]
90+ sys .argv = [ * self .baseargs , "-v" , logfile ]
8991 can .player .main ()
9092 self .assertEqual (self .mock_virtual_bus .send .call_count , 9 )
9193 self .assertSuccessfulCleanup ()
@@ -94,11 +96,52 @@ def test_play_error_frame(self):
9496 logfile = os .path .join (
9597 os .path .dirname (__file__ ), "data" , "logfile_errorframes.asc"
9698 )
97- sys .argv = self .baseargs + [ "-v" , "--error-frames" , logfile ]
99+ sys .argv = [ * self .baseargs , "-v" , "--error-frames" , logfile ]
98100 can .player .main ()
99101 self .assertEqual (self .mock_virtual_bus .send .call_count , 12 )
100102 self .assertSuccessfulCleanup ()
101103
104+ @parameterized .expand ([0 , 1 , 2 , 3 ])
105+ def test_play_loop (self , loop_val ):
106+ sys .argv = [* self .baseargs , "--loop" , str (loop_val ), self .logfile ]
107+ can .player .main ()
108+ msg1 = can .Message (
109+ timestamp = 2.501 ,
110+ arbitration_id = 0xC8 ,
111+ is_extended_id = False ,
112+ is_fd = False ,
113+ is_rx = False ,
114+ channel = 1 ,
115+ dlc = 8 ,
116+ data = [0x9 , 0x8 , 0x7 , 0x6 , 0x5 , 0x4 , 0x3 , 0x2 ],
117+ )
118+ msg2 = can .Message (
119+ timestamp = 17.876708 ,
120+ arbitration_id = 0x6F9 ,
121+ is_extended_id = False ,
122+ is_fd = False ,
123+ is_rx = True ,
124+ channel = 0 ,
125+ dlc = 8 ,
126+ data = [0x5 , 0xC , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 ],
127+ )
128+ for i in range (loop_val ):
129+ self .assertTrue (
130+ msg1 .equals (self .mock_virtual_bus .send .mock_calls [2 * i + 0 ].args [0 ])
131+ )
132+ self .assertTrue (
133+ msg2 .equals (self .mock_virtual_bus .send .mock_calls [2 * i + 1 ].args [0 ])
134+ )
135+ self .assertEqual (self .mock_virtual_bus .send .call_count , 2 * loop_val )
136+ self .assertSuccessfulCleanup ()
137+
138+ def test_play_loop_infinite (self ):
139+ self .mock_virtual_bus .send .side_effect = [None ] * 99 + [KeyboardInterrupt ]
140+ sys .argv = [* self .baseargs , "-l" , "i" , self .logfile ]
141+ can .player .main ()
142+ self .assertEqual (self .mock_virtual_bus .send .call_count , 100 )
143+ self .assertSuccessfulCleanup ()
144+
102145
103146class TestPlayerCompressedFile (TestPlayerScriptModule ):
104147 """
0 commit comments