Skip to content

Commit

Permalink
#Add - Writes Enum as Text when Alphanumeric Picture is Provided
Browse files Browse the repository at this point in the history
When setting an alphanumeric picture to X(n), then automatically its gonna
translate to Enum Name, not the integer.
Also, two test cases were provided in EdiTextWriterTests.cs.
This Implementation need to be analyzed because of changes in the ConvertUtils.cs, maybe we need to change de approach to some code
that Add Function not change the function.
This can be addressed to Issue indice-co#183
  • Loading branch information
eddyrodrigues committed Mar 2, 2024
1 parent f1b44a1 commit d7146da
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/indice.Edi/EdiTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public override bool EscapeDecimalMarkInText {
}



/// <summary>
/// Creates an instance of the <c>EdiWriter</c> class using the specified <see cref="TextWriter"/>.
/// </summary>
/// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
/// <param name="grammar">The <see cref="IEdiGrammar"/> to use for structure and dilimiters</param>
/// </summary>
public EdiTextWriter(TextWriter textWriter, IEdiGrammar grammar)
: base(grammar) {
if (textWriter == null) {
Expand Down Expand Up @@ -206,7 +206,7 @@ private void WriteEscapedString(string value, Picture? picture) {
int valueLength = value.Length;

if (StrictAlphanumericCharLimit) {
var validPicture = picture != null && picture.Value.Kind == PictureKind.Alphanumeric && picture.Value.Scale > 0;
var validPicture = picture != null && picture.Value.Kind == PictureKind.Alphanumeric && picture.Value.IsValid;
var limitMaximumLength = validPicture && valueLength > 0 && valueLength > picture.Value.Scale;
if (limitMaximumLength) {
valueLength = picture.Value.Scale;
Expand Down
8 changes: 8 additions & 0 deletions src/indice.Edi/EdiWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ public virtual void WriteValue(TimeSpan? value) {
/// Writes a <see cref="Uri"/> value.
/// </summary>
/// <param name="value">The <see cref="Uri"/> value to write.</param>
/// <param name="picture"></param>
public virtual void WriteValue(Uri value, Picture? picture = null) {
if (value == null) {
WriteNull();
Expand Down Expand Up @@ -1173,6 +1174,13 @@ internal static void WriteValue(EdiWriter writer, PrimitiveTypeCode typeCode, ob
case PrimitiveTypeCode.Bytes:
writer.WriteValue((byte[])value);
break;
case PrimitiveTypeCode.Enum:
if (picture.HasValue && picture.Value.IsValid && picture.Value.Kind == PictureKind.Alphanumeric) {
writer.WriteValue(Enum.GetName(value.GetType(), value), picture);
} else {
writer.WriteValue((int)value, picture);
}
break;
#if !(PORTABLE || NETSTANDARD10)
case PrimitiveTypeCode.DBNull:
writer.WriteNull();
Expand Down
5 changes: 3 additions & 2 deletions src/indice.Edi/Utilities/ConvertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ internal enum PrimitiveTypeCode
Uri = 38,
String = 39,
Bytes = 40,
DBNull = 41
DBNull = 41,
Enum = 42
}

internal class TypeInformation
Expand Down Expand Up @@ -190,7 +191,7 @@ public static PrimitiveTypeCode GetTypeCode(Type t, out bool isEnum) {

if (t.IsEnum()) {
isEnum = true;
return GetTypeCode(Enum.GetUnderlyingType(t));
return PrimitiveTypeCode.Enum;
}

// performance?
Expand Down
35 changes: 35 additions & 0 deletions test/indice.Edi.Tests/EdiTextWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using indice.Edi.Tests.Models;
using Xunit;

namespace indice.Edi.Tests
Expand Down Expand Up @@ -144,5 +145,39 @@ public void WriterLimits_Maximum_When_Alphanumeric(bool escapeDecimalMarkInText,
Assert.Equal(expected.ToString(), output.ToString());
}


[Theory, Trait(Traits.Tag, "Writer"), Trait(Traits.Issue, "#198")]
[InlineData(TestEnumWriter.AAA, 1)]
[InlineData(TestEnumWriter.AAA, 2)]
[InlineData(TestEnumWriter.AAA, 3)]
public void WriterEnumsAsString_And_Limits_Its_value_When_Alphanumeric_Picture_StrictAlphanumericCharLimited(TestEnumWriter enumratorWrite, int pictureScale) {
var grammar = EdiGrammar.NewEdiFact();
grammar.SetAdvice('+', '+', ':', '\'', '?', null, ',');
var expected = new StringBuilder().Append($"AAA+{Enum.GetName(enumratorWrite).Substring(0, pictureScale)}'{Environment.NewLine}");
var output = new StringBuilder();
using (var writer = new EdiTextWriter(new StringWriter(output), grammar) {
StrictAlphanumericCharLimit = true
}) {
writer.WriteToken(EdiToken.SegmentName, "AAA"); Assert.Equal("AAA", writer.Path);
writer.WriteValue(enumratorWrite, (Picture)$"X({pictureScale})", null); Assert.Equal("AAA[0][0]", writer.Path);
}
Assert.Equal(expected.ToString(), output.ToString());
}

[Theory, Trait(Traits.Tag, "Writer"), Trait(Traits.Issue, "#198")]
[InlineData(TestEnumWriter.AAA)]
public void WriterEnumsAsString_When_Alphanumeric_Picture(TestEnumWriter enumratorWrite) {
var grammar = EdiGrammar.NewEdiFact();
grammar.SetAdvice('+', '+', ':', '\'', '?', null, ',');
var expected = new StringBuilder().Append($"AAA+{Enum.GetName(enumratorWrite)}'{Environment.NewLine}");
var output = new StringBuilder();
using (var writer = new EdiTextWriter(new StringWriter(output), grammar) {
StrictAlphanumericCharLimit = false
}) {
writer.WriteToken(EdiToken.SegmentName, "AAA"); Assert.Equal("AAA", writer.Path);
writer.WriteValue(enumratorWrite, (Picture)$"X(5)", null); Assert.Equal("AAA[0][0]", writer.Path);
}
Assert.Equal(expected.ToString(), output.ToString());
}
}
}
7 changes: 7 additions & 0 deletions test/indice.Edi.Tests/Models/TestEnumWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace indice.Edi.Tests.Models
{
public enum TestEnumWriter
{
AAA = 1, BBB = 2
}
}

0 comments on commit d7146da

Please sign in to comment.