|
| 1 | +using System.Reflection; |
| 2 | +using AwesomeAssertions; |
| 3 | +using Spillgebees.NeTEx.Models.V1_3_1; |
| 4 | +using Spillgebees.NeTEx.Models.V1_3_1.NeTEx; |
| 5 | + |
| 6 | +namespace Spillgebees.NeTEx.Models.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Tests verifying that the generated NeTEx v1.3.1 models carry correct |
| 10 | +/// <see cref="XmlChoiceGroupAttribute"/> annotations on properties that |
| 11 | +/// originate from <c>xs:choice</c> groups in the NeTEx XSD schemas. |
| 12 | +/// </summary> |
| 13 | +public class ChoiceGroupAttributeTests |
| 14 | +{ |
| 15 | + // -- helpers ------------------------------------------------------------------ |
| 16 | + |
| 17 | + private static XmlChoiceGroupAttribute? GetChoiceGroupAttribute<T>(string propertyName) => |
| 18 | + typeof(T).GetProperty(propertyName)? |
| 19 | + .GetCustomAttribute<XmlChoiceGroupAttribute>(); |
| 20 | + |
| 21 | + private static XmlChoiceGroupAttribute GetRequiredChoiceGroupAttribute<T>(string propertyName) |
| 22 | + { |
| 23 | + var attr = GetChoiceGroupAttribute<T>(propertyName); |
| 24 | + attr.Should().NotBeNull($"property {typeof(T).Name}.{propertyName} should have [XmlChoiceGroupAttribute]"); |
| 25 | + return attr!; |
| 26 | + } |
| 27 | + |
| 28 | + // -- DistanceMatrixElementDerivedViewStructure: two parallel choice groups ----- |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void Should_have_choice_group_on_start_stop_point_ref() |
| 32 | + { |
| 33 | + var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 34 | + nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef)); |
| 35 | + |
| 36 | + attr.ArmId.Should().Be(0); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void Should_have_choice_group_on_start_tariff_zone_ref() |
| 41 | + { |
| 42 | + var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 43 | + nameof(DistanceMatrixElementDerivedViewStructure.StartTariffZoneRef)); |
| 44 | + |
| 45 | + attr.ArmId.Should().Be(1); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public void Should_have_same_group_id_for_start_choice() |
| 50 | + { |
| 51 | + var stopRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 52 | + nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef)); |
| 53 | + var zoneRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 54 | + nameof(DistanceMatrixElementDerivedViewStructure.StartTariffZoneRef)); |
| 55 | + |
| 56 | + stopRef.GroupId.Should().Be(zoneRef.GroupId); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void Should_have_choice_group_on_end_stop_point_ref() |
| 61 | + { |
| 62 | + var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 63 | + nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef)); |
| 64 | + |
| 65 | + attr.ArmId.Should().Be(0); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void Should_have_choice_group_on_end_tariff_zone_ref() |
| 70 | + { |
| 71 | + var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 72 | + nameof(DistanceMatrixElementDerivedViewStructure.EndTariffZoneRef)); |
| 73 | + |
| 74 | + attr.ArmId.Should().Be(1); |
| 75 | + } |
| 76 | + |
| 77 | + [Test] |
| 78 | + public void Should_have_same_group_id_for_end_choice() |
| 79 | + { |
| 80 | + var stopRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 81 | + nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef)); |
| 82 | + var zoneRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 83 | + nameof(DistanceMatrixElementDerivedViewStructure.EndTariffZoneRef)); |
| 84 | + |
| 85 | + stopRef.GroupId.Should().Be(zoneRef.GroupId); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void Should_have_different_group_ids_for_start_and_end_choices() |
| 90 | + { |
| 91 | + var startAttr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 92 | + nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef)); |
| 93 | + var endAttr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>( |
| 94 | + nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef)); |
| 95 | + |
| 96 | + startAttr.GroupId.Should().NotBe(endAttr.GroupId); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void Should_not_have_choice_group_on_non_choice_property() |
| 101 | + { |
| 102 | + // StartName is not part of any choice group |
| 103 | + var attr = GetChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>("StartName"); |
| 104 | + |
| 105 | + attr.Should().BeNull(); |
| 106 | + } |
| 107 | + |
| 108 | + // -- FareStructureElementVersionStructure: multiple choice groups in NeTEx ----- |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void Should_have_multiple_distinct_choice_groups_on_fare_structure_element() |
| 112 | + { |
| 113 | + var properties = typeof(FareStructureElementVersionStructure).GetProperties(); |
| 114 | + var choiceGroupIds = properties |
| 115 | + .Select(p => p.GetCustomAttribute<XmlChoiceGroupAttribute>()) |
| 116 | + .Where(a => a is not null) |
| 117 | + .Select(a => a!.GroupId) |
| 118 | + .Distinct() |
| 119 | + .ToList(); |
| 120 | + |
| 121 | + choiceGroupIds.Count.Should().BeGreaterThanOrEqualTo(2); |
| 122 | + } |
| 123 | +} |
0 commit comments