private Message Init(string input) { var message = new Message(input); Assert.That( message.ParseMessage(), Is.True ); Assert.That( message.SegmentCount, Is.GreaterThan(0) ); return message; } [Test] public void DOES_NOT_WORK_BUT_I_WOULD_HAVE_EXPECTED_IT_TO() { var msg = Init( @"MSH|^~\&|SomeSystem|Wubba|Mustersystem||200001211610||BAR^P01|MSG161271|P|2.2 EVN|P01|200001211614 PID|1||1000040|1000040|Musterfrau^Eva^^^||196106280000|M|||Some Street 12^^Somewhere^^12345^D||0651/8247105|||||||||||||D| PV1|1|O|^^^CACH^CAFR^|01^Normalfall^301||||^^^^^|^^^^^|N||||||N|||0119171||K||||||||||| ||||||||||||200106200800|||||| DG1|110|I9|602.9||200001211610|AD|||||||||N PR1|5000|OPS-20|5-822.7||200001211800||10|||||||H ZOP||5093|200001212100|60||||| DG1|200|I9|602.9||200001212130|PO|||||||||H PR1|6000|OPS-20|5-822.7||200001212130||10|||||||H ZOP||5094|200001222100|60||||| DG1|201|I9|602.9||200001212130|PO|||||||||H DG1|202|I9|602.9||200001212145|PO|||||||||N PR1|6001|OPS-20|5||200001212130||10|||||||H||200001222130|PO|||||||||N PR1|6001|OPS-20|5-822.7||200001222130||10|||||||H"); Assert.That(msg.SegmentCount, Is.EqualTo(14)); var dg1Segs = msg.Segments("DG1"); var third = dg1Segs[2].GetAllFields(); var fourth = dg1Segs[3].GetAllFields(); Assert.That(third.Count, Is.EqualTo(15)); Assert.That(fourth.Count, Is.EqualTo(15)); for (int i = 0; i < 15; i++) { switch (i) { case 1: Assert.That(third[i].Value, Is.EqualTo("201"), string.Format("Invalid DG1 field at position {0} in third detected Segment", i)); Assert.That(fourth[i].Value, Is.EqualTo("202"), string.Format("Invalid DG1 field at position {0} in fourth detected Segment", i)); break; case 5: Assert.That(third[i].Value, Is.EqualTo("200001212130"), string.Format("Invalid DG1 field at position {0} in third detected Segment", i)); Assert.That(fourth[i].Value, Is.EqualTo("200001212145"), string.Format("Invalid DG1 field at position {0} in fourth detected Segment", i)); break; case 14: Assert.That(third[i].Value, Is.EqualTo("H"), string.Format("Invalid DG1 field at position {0} in third detected Segment", i)); Assert.That(fourth[i].Value, Is.EqualTo("N"), string.Format("Invalid DG1 field at position {0} in fourth detected Segment", i)); break; default: Assert.That(third[i].Value, Is.EqualTo(fourth[i].Value), string.Format("DG1-fields differed at position {0}", i)); break; } } } [Test] public void DOES_WORK_SINCE_THE_ORDERING_CONDITION_WAS_REMOVED() { var msg = Init( @"MSH|^~\&|SomeSystem|Wubba|Mustersystem||200001211610||BAR^P01|MSG161271|P|2.2 EVN|P01|200001211614 PID|1||1000040|1000040|Musterfrau^Eva^^^||196106280000|M|||Some Street 12^^Somewhere^^12345^D||0651/8247105|||||||||||||D| PV1|1|O|^^^CACH^CAFR^|01^Normalfall^301||||^^^^^|^^^^^|N||||||N|||0119171||K||||||||||| ||||||||||||200106200800|||||| DG1|110|I9|602.9||200001211610|AD|||||||||N PR1|5000|OPS-20|5-822.7||200001211800||10|||||||H ZOP||5093|200001212100|60||||| DG1|200|I9|602.9||200001212130|PO|||||||||H PR1|6000|OPS-20|5-822.7||200001212130||10|||||||H ZOP||5094|200001222100|60||||| DG1|201|I9|602.9||200001212130|PO|||||||||H DG1|202|I9|602.9||200001212145|PO|||||||||N PR1|6001|OPS-20|5||200001212130||10|||||||H||200001222130|PO|||||||||N PR1|6001|OPS-20|5-822.7||200001222130||10|||||||H"); Assert.That(msg.SegmentCount, Is.EqualTo(14)); var dg1Segs = msg.Segments("DG1"); var third = dg1Segs[2].GetAllFields(); var fourth = dg1Segs[3].GetAllFields(); Assert.That(third.Count, Is.EqualTo(15)); Assert.That(fourth.Count, Is.EqualTo(15)); // Note: Apparently, 'GetAllFields()' provides *ABSOLUTELY NO GUARANTEE ON THE ORDERING* of the elements // Internally, it is implemented as a List in that library but its not filled in order of input. // The the ordering is at least identical between these two segments, otherwise the code would not be // working deterministically. Therefore, this assumption has been encoded into this test, aka the for-loop. for (int i = 0; i < 15; i++) { if ( third[i].Value.Equals("200001212130") ) { Assert.That(fourth[i].Value, Is.EqualTo("200001212145"), string.Format("Invalid DG1 field in fourth detected Segment (FieldList-Position {0})!", i)); } else if ( third[i].Value.Equals("201") ) { Assert.That(fourth[i].Value, Is.EqualTo("202"), string.Format("Invalid DG1 field in fourth detected Segment (FieldList-Position {0})!", i)); } else if ( third[i].Value.Equals("H") ) { Assert.That(fourth[i].Value, Is.EqualTo("N"), string.Format("Invalid DG1 field in fourth detected Segment (FieldList-Position {0})!", i)); } else Assert.That(third[i].Value, Is.EqualTo(fourth[i].Value), string.Format("DG1-fields differed at position {0}", i)); } }