fix(commons): codec handles non-finite list floats, ISO-8601 UTC scalar DateTime, JSON null converts to null (CMN-3, CMN-4, CMN-10, WP3 task 3.11)
Wire stays compatible: the scalar DateTime is a string either way and every reader parses with RoundtripKind/invariant, which still accepts the old invariant general form.
This commit is contained in:
@@ -324,4 +324,89 @@ public class AttributeValueCodecTests
|
||||
public void CoerceEnumerable_ThrowsOnUnsupportedElementType() =>
|
||||
Assert.Throws<FormatException>(
|
||||
() => AttributeValueCodec.CoerceEnumerable(new object[] { new byte[] { 1 } }, DataType.Binary));
|
||||
|
||||
// ── CMN-3: non-finite floating-point list elements ──
|
||||
|
||||
/// <summary>A double list holding NaN/±Infinity encodes (no throw) and round-trips.</summary>
|
||||
[Fact]
|
||||
public void RoundTrip_DoubleList_NonFinite()
|
||||
{
|
||||
var json = AttributeValueCodec.Encode(
|
||||
new List<double> { double.NaN, double.PositiveInfinity, double.NegativeInfinity, 1.5 });
|
||||
var back = (IList<double>)AttributeValueCodec.Decode(json, DataType.List, DataType.Double)!;
|
||||
Assert.True(double.IsNaN(back[0]));
|
||||
Assert.Equal(double.PositiveInfinity, back[1]);
|
||||
Assert.Equal(double.NegativeInfinity, back[2]);
|
||||
Assert.Equal(1.5, back[3]);
|
||||
}
|
||||
|
||||
/// <summary>A float list holding NaN/±Infinity encodes (no throw) and round-trips.</summary>
|
||||
[Fact]
|
||||
public void RoundTrip_FloatList_NonFinite()
|
||||
{
|
||||
var json = AttributeValueCodec.Encode(
|
||||
new List<float> { float.NaN, float.PositiveInfinity, float.NegativeInfinity });
|
||||
var back = (IList<float>)AttributeValueCodec.Decode(json, DataType.List, DataType.Float)!;
|
||||
Assert.True(float.IsNaN(back[0]));
|
||||
Assert.Equal(float.PositiveInfinity, back[1]);
|
||||
Assert.Equal(float.NegativeInfinity, back[2]);
|
||||
}
|
||||
|
||||
// ── CMN-4: scalar DateTime wire form ──
|
||||
|
||||
/// <summary>A UTC scalar DateTime encodes as ISO-8601 round-trip and keeps sub-second precision and Kind.</summary>
|
||||
[Fact]
|
||||
public void Encode_ScalarUtcDateTime_RoundTripsWithSubSecondPrecision()
|
||||
{
|
||||
var dt = new DateTime(2026, 6, 15, 13, 45, 30, 123, DateTimeKind.Utc).AddTicks(4567);
|
||||
var encoded = AttributeValueCodec.Encode(dt)!;
|
||||
Assert.Equal("2026-06-15T13:45:30.1234567Z", encoded);
|
||||
|
||||
var back = DateTime.Parse(encoded, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
|
||||
Assert.Equal(dt, back);
|
||||
Assert.Equal(DateTimeKind.Utc, back.Kind);
|
||||
}
|
||||
|
||||
/// <summary>An Unspecified-kind scalar DateTime is taken as UTC (all timestamps are UTC system-wide).</summary>
|
||||
[Fact]
|
||||
public void Encode_ScalarUnspecifiedDateTime_IsStampedUtc() =>
|
||||
Assert.Equal("2026-06-15T13:45:30.0000000Z",
|
||||
AttributeValueCodec.Encode(new DateTime(2026, 6, 15, 13, 45, 30, DateTimeKind.Unspecified)));
|
||||
|
||||
/// <summary>The scalar DateTime encoding is culture-independent.</summary>
|
||||
[Fact]
|
||||
public void Encode_ScalarDateTime_IsInvariant()
|
||||
{
|
||||
var original = CultureInfo.CurrentCulture;
|
||||
try
|
||||
{
|
||||
CultureInfo.CurrentCulture = new CultureInfo("de-DE");
|
||||
Assert.Equal("2026-06-15T13:45:30.5000000Z",
|
||||
AttributeValueCodec.Encode(new DateTime(2026, 6, 15, 13, 45, 30, 500, DateTimeKind.Utc)));
|
||||
}
|
||||
finally
|
||||
{
|
||||
CultureInfo.CurrentCulture = original;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A scalar DateTimeOffset encodes as ISO-8601 round-trip, preserving the offset.</summary>
|
||||
[Fact]
|
||||
public void Encode_ScalarDateTimeOffset_IsIso8601() =>
|
||||
Assert.Equal("2026-06-15T13:45:30.2500000+00:00",
|
||||
AttributeValueCodec.Encode(new DateTimeOffset(2026, 6, 15, 13, 45, 30, 250, TimeSpan.Zero)));
|
||||
|
||||
/// <summary>
|
||||
/// Compatibility read: a List element in the pre-CMN-4 scalar form (invariant general
|
||||
/// format) still decodes next to an element in the new ISO form.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Decode_DateTimeList_AcceptsOldAndNewScalarForms()
|
||||
{
|
||||
var back = (IList<DateTime>)AttributeValueCodec.Decode(
|
||||
"[\"06/15/2026 13:45:30\",\"2026-06-15T13:45:30.1230000Z\"]",
|
||||
DataType.List, DataType.DateTime)!;
|
||||
Assert.Equal(new DateTime(2026, 6, 15, 13, 45, 30), back[0]);
|
||||
Assert.Equal(new DateTime(2026, 6, 15, 13, 45, 30, 123, DateTimeKind.Utc), back[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,4 +205,54 @@ public class DynamicJsonElementTests
|
||||
Assert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(
|
||||
() => { var _ = obj.items[idx]; });
|
||||
}
|
||||
|
||||
// ── CMN-10: JSON null converts to null for nullable targets ──
|
||||
|
||||
private static ConvertBinder ConvertTo(Type t) =>
|
||||
(ConvertBinder)Microsoft.CSharp.RuntimeBinder.Binder.Convert(
|
||||
Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.ConvertExplicit, t, typeof(DynamicJsonElementTests));
|
||||
|
||||
/// <summary>A JSON null converts to a null string (not "" and not a binder failure).</summary>
|
||||
[Fact]
|
||||
public void TryConvert_StringTarget_OnJsonNull_ReturnsNull()
|
||||
{
|
||||
var wrapper = new DynamicJsonElement(JsonDocument.Parse("null").RootElement);
|
||||
Assert.True(wrapper.TryConvert(ConvertTo(typeof(string)), out var result));
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
/// <summary>A JSON null converts to a null <c>int?</c>.</summary>
|
||||
[Fact]
|
||||
public void TryConvert_NullableIntTarget_OnJsonNull_ReturnsNull()
|
||||
{
|
||||
var wrapper = new DynamicJsonElement(JsonDocument.Parse("null").RootElement);
|
||||
Assert.True(wrapper.TryConvert(ConvertTo(typeof(int?)), out var result));
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
/// <summary>A JSON null cannot convert to a non-nullable value type: the binder reports failure.</summary>
|
||||
[Fact]
|
||||
public void TryConvert_IntTarget_OnJsonNull_ReportsFailure()
|
||||
{
|
||||
var wrapper = new DynamicJsonElement(JsonDocument.Parse("null").RootElement);
|
||||
Assert.False(wrapper.TryConvert(ConvertTo(typeof(int)), out _));
|
||||
}
|
||||
|
||||
/// <summary>A JSON number converts through Nullable<T>'s underlying type.</summary>
|
||||
[Fact]
|
||||
public void TryConvert_NullableIntTarget_OnNumber_ReturnsValue()
|
||||
{
|
||||
var wrapper = new DynamicJsonElement(JsonDocument.Parse("42").RootElement);
|
||||
Assert.True(wrapper.TryConvert(ConvertTo(typeof(int?)), out var result));
|
||||
Assert.Equal(42, result);
|
||||
}
|
||||
|
||||
/// <summary>End to end through the dynamic binder: (string) and (int?) casts of a JSON-null wrapper yield null.</summary>
|
||||
[Fact]
|
||||
public void DynamicCast_OnJsonNullWrapper_YieldsNull()
|
||||
{
|
||||
dynamic wrapper = new DynamicJsonElement(JsonDocument.Parse("null").RootElement);
|
||||
Assert.Null((string?)wrapper);
|
||||
Assert.Null((int?)wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user