diff --git a/Tests/Tests.Asn1SourceGenerator/EmitterTests.cs b/Tests/Tests.Asn1SourceGenerator/EmitterTests.cs index e487258..b992de3 100644 --- a/Tests/Tests.Asn1SourceGenerator/EmitterTests.cs +++ b/Tests/Tests.Asn1SourceGenerator/EmitterTests.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; @@ -326,6 +326,34 @@ public void Emit_InlineSequenceOfField_GeneratesArrayProperty() Assert.IsTrue(code.Contains("int[]"), "Inline SEQUENCE OF INTEGER should produce int[]"); } + // ─── Enumerated field ──────────────────────────────── + [TestMethod] + public void Emit_EnumeratedDecode_UsesEnumReaderOrIntFallback() + { + var code = EmitSingle(@" +-- @cs-class: KrbEnumDecode +EnumDecode ::= SEQUENCE { + typed-state [0] ENUMERATED { a(0), b(1) }, -- @cs-name: TypedState @cs-enum: MyState + raw-state [1] ENUMERATED { c(0), d(1) } -- @cs-name: RawState +}"); + + // Branch: EnumType is set + Assert.IsTrue( + code.Contains("ReadEnumeratedValue()"), + "ENUMERATED with @cs-enum should use ReadEnumeratedValue()."); + + // Branch: EnumType is not set + Assert.IsTrue( + code.Contains("TryReadInt32(out int tmpRawState)"), + "ENUMERATED without @cs-enum should use TryReadInt32 fallback."); + Assert.IsTrue( + code.Contains("ThrowIfNotEmpty();"), + "TryReadInt32 fallback should include ThrowIfNotEmpty guard."); + Assert.IsTrue( + code.Contains("decoded.RawState = tmpRawState;"), + "TryReadInt32 fallback should assign temp value to decoded property."); + } + // ─── Full pipeline smoke test with real schema snippet ────── [TestMethod] diff --git a/Tools/Asn1SourceGenerator/Emit/CSharpEmitter.cs b/Tools/Asn1SourceGenerator/Emit/CSharpEmitter.cs index c960a17..2831ca9 100644 --- a/Tools/Asn1SourceGenerator/Emit/CSharpEmitter.cs +++ b/Tools/Asn1SourceGenerator/Emit/CSharpEmitter.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -1138,17 +1138,17 @@ private static void EmitFieldValueDecode(IndentedWriter w, ResolvedField field, case FieldKind.Enumerated: w.WriteLine(); - w.WriteLine($"if (!{readerVar}.TryReadInt32(out int tmp{field.PropertyName}))"); - w.OpenBrace(); - w.WriteLine($"{readerVar}.ThrowIfNotEmpty();"); - w.CloseBrace(); - w.WriteLine(); + if (!string.IsNullOrEmpty(field.Encoding.EnumType)) { - w.WriteLine($"decoded.{field.PropertyName} = ({field.Encoding.EnumType})tmp{field.PropertyName};"); + w.WriteLine($"decoded.{field.PropertyName} = {readerVar}.ReadEnumeratedValue<{field.Encoding.EnumType}>();"); } else { + w.WriteLine($"if (!{readerVar}.TryReadInt32(out int tmp{field.PropertyName}))"); + w.OpenBrace(); + w.WriteLine($"{readerVar}.ThrowIfNotEmpty();"); + w.CloseBrace(); w.WriteLine($"decoded.{field.PropertyName} = tmp{field.PropertyName};"); } break;