Skip to content

Commit 5085a9a

Browse files
glados-vermacopybara-github
authored andcommitted
In unit test helper, flatten nested sequences that OpenHTF can handle in core testing.
PiperOrigin-RevId: 759755874
1 parent f3fadf9 commit 5085a9a

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

openhtf/util/test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,13 @@ def get_profile_filepath(cls) -> Optional[pathlib.Path]:
10161016

10171017

10181018
def get_flattened_phases(
1019-
phases_or_phase_groups: Sequence[Union[
1020-
phase_nodes.PhaseNode, phase_collections.PhaseCollectionNode]]
1019+
node_collections: Sequence[
1020+
Union[phase_nodes.PhaseNode, phase_collections.PhaseCollectionNode]
1021+
],
10211022
) -> Sequence[phase_nodes.PhaseNode]:
1022-
"""Flattens a sequence of phase nodes or phase collection nodes into nodes."""
1023+
"""Flattens nested sequences of nodes into phase descriptors."""
10231024
phases = []
1025+
phases_or_phase_groups = phase_collections.flatten(node_collections)
10241026
for phase_or_phase_group in phases_or_phase_groups:
10251027
if isinstance(phase_or_phase_group, phase_collections.PhaseCollectionNode):
10261028
phases.extend(phase_or_phase_group.all_phases())

test/util/test_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,57 @@ def test_profile_test(self):
356356
self.assertIn(
357357
test_phase_with_shameless_plug.func.__module__.replace(
358358
'.', os.path.sep), output)
359+
360+
361+
class GetFlattenedPhasesTest(unittest.TestCase):
362+
363+
def test_unflattens_nested_mixed_nodes(self):
364+
365+
def no_op_phase():
366+
"""No-op phase."""
367+
368+
def make_phase(name: str):
369+
return openhtf.PhaseOptions(name=name)(no_op_phase)
370+
371+
nested_nodes = [
372+
make_phase('TopLevelPhase'),
373+
[make_phase('NestedPhase1'), make_phase('NestedPhase2')],
374+
openhtf.PhaseGroup(
375+
setup=make_phase('SetupPhase1a'),
376+
main=[make_phase('MainPhase1a'), make_phase('MainPhase1b')],
377+
teardown=make_phase('TeardownPhase1a'),
378+
),
379+
[
380+
openhtf.PhaseGroup(
381+
setup=[make_phase('SetupPhase2a'), make_phase('SetupPhase2b')],
382+
main=[make_phase('MainPhase2a'), make_phase('MainPhase2b')],
383+
teardown=[
384+
make_phase('TeardownPhase2a'),
385+
make_phase('TeardownPhase2b'),
386+
],
387+
),
388+
make_phase('NestedPhase3'),
389+
],
390+
]
391+
node_names = []
392+
for node in test.get_flattened_phases(nested_nodes):
393+
node_names.append(node.name)
394+
self.assertEqual(
395+
node_names,
396+
[
397+
'TopLevelPhase',
398+
'NestedPhase1',
399+
'NestedPhase2',
400+
'SetupPhase1a',
401+
'MainPhase1a',
402+
'MainPhase1b',
403+
'TeardownPhase1a',
404+
'SetupPhase2a',
405+
'SetupPhase2b',
406+
'MainPhase2a',
407+
'MainPhase2b',
408+
'TeardownPhase2a',
409+
'TeardownPhase2b',
410+
'NestedPhase3',
411+
],
412+
)

0 commit comments

Comments
 (0)