Skip to content

Commit

Permalink
bug fix: Serilisation fix when using ement range notation in path sho…
Browse files Browse the repository at this point in the history
…uld take into account the min bound. #170
  • Loading branch information
cleftheris committed Oct 8, 2020
1 parent 8899926 commit a0bb927
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/indice.Edi/EdiSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ private static void SerializeStructure(EdiWriter writer, Stack<EdiStructure> sta
} else {
itemType = property.Info.PropertyType.GetGenericArguments().First();
}
int range_offset = 0;
if (container == EdiStructureType.Element && property.PathInfo.PathInternal.Element.IsRange) {
range_offset = property.PathInfo.PathInternal.Element.Min;
}
for (var i = 0; i < collection.Count; i++) {
var item = collection[i];
if (stack.Count == 0) {
Expand All @@ -819,7 +823,7 @@ private static void SerializeStructure(EdiWriter writer, Stack<EdiStructure> sta
while (stack.Peek().StructureType >= container) {
var previous = stack.Pop();
}
stack.Push(new EdiStructure(container, stack.Peek(), property, item, i, null));
stack.Push(new EdiStructure(container, stack.Peek(), property, item, range_offset + i, null));
SerializeStructure(writer, stack, structuralComparer);
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions test/indice.Edi.Tests/SerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@ public void Serialize_ElementList() {
Assert.Equal(expected, output);
}

[Fact, Trait(Traits.Tag, "EDIFact"), Trait(Traits.Issue, "#170")]
public void Serialize_ElementList_WithRanged_Path() {
var grammar = EdiGrammar.NewEdiFact();
var interchange = default(EdiFact_Issue170_ElementList);
using (var stream = Helpers.GetResourceStream("edifact.Issue170.ElementList.edi")) {
interchange = new EdiSerializer().Deserialize<EdiFact_Issue170_ElementList>(new StreamReader(stream), grammar);
}

var expected = new StringBuilder()
.AppendLine("UNA:+.? '")
.AppendLine("IFT+1:1+This+is+the first'")
.AppendLine("IFT+1:2+And+this is+the seccond'")
.AppendLine("IFT+1:3+while+this+should come third'").ToString();
string output = null;
using (var writer = new StringWriter()) {
new EdiSerializer().Serialize(writer, grammar, interchange);
output = writer.ToString();
}

Assert.Equal(expected, output);
}

[Fact(Skip = "Not implemented yet"), Trait(Traits.Tag, "Tradacoms"), Trait(Traits.Issue, "#17")]
public void Should_Serialize_Autogenerated_Counts() {
var grammar = EdiGrammar.NewTradacoms();
Expand Down

0 comments on commit a0bb927

Please sign in to comment.