Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Tests/Tests.Asn1SourceGenerator/EmitterTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<MyState>()"),
"ENUMERATED with @cs-enum should use ReadEnumeratedValue<TEnum>().");

// 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]
Expand Down
14 changes: 7 additions & 7 deletions Tools/Asn1SourceGenerator/Emit/CSharpEmitter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down