Skip to content
Merged
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
12 changes: 9 additions & 3 deletions src/CodoMetis.ValueRanges/Internals/RangeOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ internal static int CompareObject<TSelf>(TSelf self, object? other, Func<TSelf,
};

private static string TypeName(Type type)
=> type.IsGenericType
? $"{type.Name[..type.Name.IndexOf('`')]}<{string.Join(", ", type.GetGenericArguments().Select(TypeName))}>"
: type.Name;
{
if (!type.IsGenericType) return type.Name;

// A type nested in a generic one is generic too, but its own name has no arity suffix:
// List<int>.Enumerator is named "Enumerator" (#258).
var tick = type.Name.IndexOf('`');
var name = tick < 0 ? type.Name : type.Name[..tick];
return $"{name}<{string.Join(", ", type.GetGenericArguments().Select(TypeName))}>";
}

private readonly record struct Bound<T>(T Value, bool Inclusive, bool Absent)
where T : struct, IComparable<T>, IEquatable<T>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ .. RangeProbes.Shapes<TRange, T>(),

// A foreign argument throws: another range type, the element itself, and a range where a set is expected.
object other = typeof(TRange) == typeof(Int32Range) ? DecimalRange.Empty : Int32Range.Empty;
foreach (var foreign in new[] { other, low, "[1,2)" })
// A nested type inside a generic one (List<int>.Enumerator) is generic without a backtick in its
// own name, which broke the message (#258).
foreach (var foreign in new[] { other, low, "[1,2)", new List<int>().GetEnumerator() })
{
var thrown = Assert.ThrowsExactly<ArgumentException>(() => NonGeneric(ranges[0]).CompareTo(foreign),
$"{name}: CompareTo({foreign.GetType().Name})");
StringAssert.Contains(thrown.Message, name);
StringAssert.Contains(thrown.Message, foreign.GetType().Name.Split('`')[0]);
}

Assert.ThrowsExactly<ArgumentException>(() => NonGeneric(sets[0]).CompareTo(ranges[0]), $"RangeSet<{name}>: CompareTo({name})");
Expand Down