11import os
22import json
33import yaml
4- from unittest import TestSuite , TestCase , main
4+ from unittest import TestSuite , TestCase
55import io
6+ import re
67from xmlrunner import XMLTestRunner
78from xmlrunner .extra .xunit_plugin import transform
89
@@ -37,6 +38,8 @@ def tearDown(self):
3738 self .wrapper .ccos .unload ()
3839
3940 def runTest (self ):
41+ print (f"{ BOLD } Running test: { self .id ()} { RESET } " )
42+
4043 assert isinstance (self .test_case ["test" ], list )
4144 for command in self .test_case ["test" ]:
4245 print (f"{ BOLD } ============={ RESET } " )
@@ -58,6 +61,13 @@ def runTest(self):
5861 for chord in command ["verifyChords" ]:
5962 self .wrapper .check_chord (chord ["input" ], chord ["output" ])
6063 print (f"{ BLUE } ...1ms{ RESET } " )
64+ if "settings" in command :
65+ for category_name , category in command ["settings" ].items ():
66+ for setting_name , setting_value in category .items ():
67+ self .wrapper .set_setting (
68+ category_name , setting_name , setting_value
69+ )
70+
6171 if "remap" in command :
6272 print (json .dumps (command ))
6373 for layer , remaps in command ["remap" ].items ():
@@ -90,15 +100,28 @@ def runTest(self):
90100 self .wrapper .millis += command ["step" ]
91101 self .wrapper .ccos .update (self .wrapper .millis )
92102 print (f"{ BLUE } ...{ command ['step' ]} ms{ RESET } " )
93- if "keys" in command :
103+ if "keys" in command or "modifiers" in command :
104+ modifiers = command .get ("modifiers" , {})
105+ keys = command .get ("keys" , [])
94106 print (
95- f"{ YELLOW } ? modifiers=[{ ', ' .join (command . get ( 'modifiers' , [] ))} ] keys=[{ ', ' .join (command [ ' keys' ] )} ]{ RESET } "
107+ f"{ YELLOW } ? modifiers=[{ ', ' .join (modifiers . keys ( ))} ] keys=[{ ', ' .join (keys )} ]{ RESET } "
96108 )
97109 self .assertGreater (len (self .wrapper .reports ), 0 )
98110 report = self .wrapper .reports .pop (0 )
99- keys = [self .wrapper .ccos .toKeycode (key ) for key in command ["keys" ]]
111+ expected_modifiers = (
112+ (modifiers .get ("lctrl" , False ) << 0 )
113+ | (modifiers .get ("lshift" , False ) << 1 )
114+ | (modifiers .get ("lalt" , False ) << 2 )
115+ | (modifiers .get ("lmeta" , False ) << 3 )
116+ | (modifiers .get ("rctrl" , False ) << 4 )
117+ | (modifiers .get ("rshift" , False ) << 5 )
118+ | (modifiers .get ("ralt" , False ) << 6 )
119+ | (modifiers .get ("rmeta" , False ) << 7 )
120+ )
121+ expected_keys = [self .wrapper .ccos .toKeycode (key ) for key in keys ]
100122 report .keys = [key for key in report .keys if key != 0 ]
101- self .assertEqual (report .keys , keys )
123+ self .assertEqual (report .keys , expected_keys )
124+ self .assertEqual (report .modifiers , expected_modifiers )
102125
103126 self .assertEqual (
104127 0 ,
@@ -152,23 +175,34 @@ def runTest(self):
152175 self .wrapper .check_chord_backup ([])
153176
154177
155- def collect_tests (build_dir : str ) -> TestSuite :
156- tests : list [TestCase ] = [FactoryTest (build_dir )]
178+ def collect_tests (build_dir : str , filter : str | None ) -> TestSuite :
179+ filter_match = re .compile (filter ) if filter is not None else None
180+ tests : list [TestCase ] = (
181+ [FactoryTest (build_dir )]
182+ if filter_match is None or filter_match .match ("factory" )
183+ else []
184+ )
157185 for root , _ , files in os .walk (tests_dir ):
186+ dirs = os .path .relpath (root , tests_dir ).split (os .sep )
187+ if dirs == ["." ]:
188+ dirs = []
158189 for file in files :
159190 if not file .endswith (".yml" ):
160191 continue
192+ test_name = "." .join ([* dirs , file .removesuffix (".yml" )])
193+ if filter_match is not None and not filter_match .match (test_name ):
194+ continue
161195 with open (os .path .join (root , file ), "r" ) as f :
162- test = CCOSTest (build_dir , file . removesuffix ( ".yml" ) , yaml .safe_load (f ))
196+ test = CCOSTest (build_dir , test_name , yaml .safe_load (f ))
163197 tests .append (test )
164198 return TestSuite (tests )
165199
166200
167- def run_tests (build_dir : str ):
201+ def run_tests (build_dir : str , filter : str | None = None ):
168202 report_file = os .path .join (e2e_dir , "report.xml" )
169203 if os .path .exists (report_file ):
170204 os .remove (report_file )
171- suite = collect_tests (build_dir )
205+ suite = collect_tests (build_dir , filter )
172206 out = io .BytesIO ()
173207 runner = XMLTestRunner (output = out )
174208 runner .run (suite )
0 commit comments