Skip to content

Commit 0ec0545

Browse files
committed
wrap enum to contain choice, address review comments
1 parent bf48ab9 commit 0ec0545

File tree

2 files changed

+103
-89
lines changed

2 files changed

+103
-89
lines changed

src/python_testing/TestConformanceSupport.py

+34-34
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,18 @@ def test_device_type_conformance(self):
751751
asserts.assert_equal(str(xml_callable), 'CD, testy', msg)
752752
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.OPTIONAL)
753753

754+
def check_good_choice(self, xml: str, conformance_str: str) -> Conformance:
755+
et = ElementTree.fromstring(xml)
756+
xml_callable = parse_callable_from_xml(et, self.params)
757+
asserts.assert_equal(str(xml_callable), conformance_str, 'Bad choice conformance string')
758+
return xml_callable
759+
760+
def check_decision(self, more_expected: bool, conformance: Conformance, feature_map: uint, attr_list: list[uint], cmd_list: list[uint]):
761+
decision = conformance(feature_map, attr_list, cmd_list)
762+
asserts.assert_true(decision.choice, 'Expected choice conformance on decision, but did not get one')
763+
asserts.assert_equal(decision.choice.marker, 'a', 'Unexpected conformance string returned')
764+
asserts.assert_equal(decision.choice.more, more_expected, "Unexpected more on choice")
765+
754766
def test_choice_conformance(self):
755767
# Choice conformances can appear on:
756768
# - base optional O.a
@@ -776,67 +788,55 @@ def test_choice_conformance(self):
776788

777789
choices = [('a+', 'choice="a" more="true"', True), ('a', 'choice="a"', False)]
778790
for suffix, xml_attrs, more in choices:
779-
def check_good_choice(xml: str, conformance_str: str) -> Conformance:
780-
msg = 'Bad choice conformance string'
781-
et = ElementTree.fromstring(xml)
782-
xml_callable = parse_callable_from_xml(et, self.params)
783-
asserts.assert_equal(str(xml_callable), conformance_str, msg)
784-
return xml_callable
785-
786-
def check_decision(conformance: Conformance, feature_map: uint, attr_list: list[uint], cmd_list: list[uint]):
787-
decision = conformance(feature_map, attr_list, cmd_list)
788-
asserts.assert_true(decision.get_choice(), 'Expected choice conformance on decision, but did not get one')
789-
asserts.assert_equal(decision.get_choice().choice, 'a', 'Unexpected conformance string returned')
790-
asserts.assert_equal(decision.get_choice().more, more, "Unexpected more on choice")
791791

792792
AB = self.feature_names_to_bits['AB']
793793
attr1 = [self.attribute_names_to_values['attr1']]
794794
cmd1 = [self.command_names_to_values['cmd1']]
795795

796796
msg_not_applicable = "Expected NOT_APPLICABLE conformance"
797797
xml = f'<optionalConform {xml_attrs} />'
798-
conformance = check_good_choice(xml, f'O.{suffix}')
799-
check_decision(conformance, 0, [], [])
798+
conformance = self.check_good_choice(xml, f'O.{suffix}')
799+
self.check_decision(more, conformance, 0, [], [])
800800

801801
xml = (f'<optionalConform {xml_attrs}>'
802802
'<feature name="AB" />'
803803
'</optionalConform>')
804-
conformance = check_good_choice(xml, f'[AB].{suffix}')
804+
conformance = self.check_good_choice(xml, f'[AB].{suffix}')
805805
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
806-
check_decision(conformance, AB, [], [])
806+
self.check_decision(more, conformance, AB, [], [])
807807

808808
xml = (f'<optionalConform {xml_attrs}>'
809809
'<attribute name="attr1" />'
810810
'</optionalConform>')
811-
conformance = check_good_choice(xml, f'[attr1].{suffix}')
811+
conformance = self.check_good_choice(xml, f'[attr1].{suffix}')
812812
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
813-
check_decision(conformance, 0, attr1, [])
813+
self.check_decision(more, conformance, 0, attr1, [])
814814

815815
xml = (f'<optionalConform {xml_attrs}>'
816816
'<command name="cmd1" />'
817817
'</optionalConform>')
818-
conformance = check_good_choice(xml, f'[cmd1].{suffix}')
818+
conformance = self.check_good_choice(xml, f'[cmd1].{suffix}')
819819
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
820-
check_decision(conformance, 0, [], cmd1)
820+
self.check_decision(more, conformance, 0, [], cmd1)
821821

822822
xml = (f'<optionalConform {xml_attrs}>'
823823
'<orTerm>'
824824
'<feature name="AB" />'
825825
'<feature name="CD" />'
826826
'</orTerm>'
827827
'</optionalConform>')
828-
conformance = check_good_choice(xml, f'[AB | CD].{suffix}')
828+
conformance = self.check_good_choice(xml, f'[AB | CD].{suffix}')
829829
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
830-
check_decision(conformance, AB, [], [])
830+
self.check_decision(more, conformance, AB, [], [])
831831

832832
xml = (f'<optionalConform {xml_attrs}>'
833833
'<notTerm>'
834834
'<attribute name="attr1" />'
835835
'</notTerm>'
836836
'</optionalConform>')
837-
conformance = check_good_choice(xml, f'[!attr1].{suffix}')
837+
conformance = self.check_good_choice(xml, f'[!attr1].{suffix}')
838838
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
839-
check_decision(conformance, 0, [], [])
839+
self.check_decision(more, conformance, 0, [], [])
840840

841841
xml = ('<otherwiseConform>'
842842
'<attribute name="attr1" />'
@@ -845,15 +845,15 @@ def check_decision(conformance: Conformance, feature_map: uint, attr_list: list[
845845
'</optionalConform>'
846846
f'<optionalConform {xml_attrs} />'
847847
'</otherwiseConform>')
848-
conformance = check_good_choice(xml, f'attr1, [AB], O.{suffix}')
848+
conformance = self.check_good_choice(xml, f'attr1, [AB], O.{suffix}')
849849
# with no features or attributes, this should end up as O.a, so there should be a choice
850-
check_decision(conformance, 0, [], [])
850+
self.check_decision(more, conformance, 0, [], [])
851851
# when we have this attribute, we should not have a choice
852852
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
853-
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
853+
asserts.assert_equal(conformance(0, attr1, []).choice, None, 'Unexpected choice in conformance')
854854
# when we have only this feature, we should not have a choice
855855
asserts.assert_equal(conformance(AB, [], []), ConformanceDecision.OPTIONAL, 'Unexpected conformance')
856-
asserts.assert_equal(conformance(AB, [], []).get_choice(), None, 'Unexpected choice in conformance')
856+
asserts.assert_equal(conformance(AB, [], []).choice, None, 'Unexpected choice in conformance')
857857

858858
# - multiple in otherwise [AB].a, [CD].b
859859
xml = ('<otherwiseConform>'
@@ -865,23 +865,23 @@ def check_decision(conformance: Conformance, feature_map: uint, attr_list: list[
865865
'<feature name="CD" />'
866866
'</optionalConform>'
867867
'</otherwiseConform>')
868-
conformance = check_good_choice(xml, f'attr1, [AB].{suffix}, [CD].b')
868+
conformance = self.check_good_choice(xml, f'attr1, [AB].{suffix}, [CD].b')
869869
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
870870
# when we have this attribute, we should not have a choice
871871
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
872-
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
872+
asserts.assert_equal(conformance(0, attr1, []).choice, None, 'Unexpected choice in conformance')
873873
# When it's just AB, we should have a choice
874-
check_decision(conformance, AB, [], [])
874+
self.check_decision(more, conformance, AB, [], [])
875875
# When we have both the attribute and AB, we should not have a choice
876876
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
877-
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
877+
asserts.assert_equal(conformance(0, attr1, []).choice, None, 'Unexpected choice in conformance')
878878
# When we have AB and CD, we should be using the AB choice
879879
CD = self.feature_names_to_bits['CD']
880880
ABCD = AB | CD
881-
check_decision(conformance, ABCD, [], [])
881+
self.check_decision(more, conformance, ABCD, [], [])
882882
# When we just have CD, we still have a choice, but the string should be b
883883
asserts.assert_equal(conformance(CD, [], []), ConformanceDecision.OPTIONAL, 'Unexpected conformance')
884-
asserts.assert_equal(conformance(CD, [], []).get_choice(), Choice('b', False), 'Unexpected choice in conformance')
884+
asserts.assert_equal(conformance(CD, [], []).choice, Choice('b', False), 'Unexpected choice in conformance')
885885

886886
# Ones that should throw exceptions
887887

0 commit comments

Comments
 (0)