Skip to content

Commit 2a765aa

Browse files
committed
Pin Windows-1252 against the platform table and value equality; compare mapped numbers by value
1 parent c0455cc commit 2a765aa

3 files changed

Lines changed: 139 additions & 2 deletions

File tree

‎src/TriasDev.Tabular/Mapping/MappedValue.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,22 @@ public static MappedValue FromBoolean(bool value) =>
7878
public bool Boolean => _number != 0;
7979

8080
/// <inheritdoc />
81+
/// <remarks>
82+
/// By value. The text of a number, date or boolean is only its rendering — 1.0 and 1.00 are one
83+
/// amount — so it takes part only for a text value, where it is the value.
84+
/// </remarks>
8185
public bool Equals(MappedValue other) =>
8286
Type == other.Type
8387
&& IsPresent == other.IsPresent
8488
&& _number == other._number
85-
&& string.Equals(Text, other.Text, StringComparison.Ordinal);
89+
&& (Type != ColumnType.Text || string.Equals(Text, other.Text, StringComparison.Ordinal));
8690

8791
/// <inheritdoc />
8892
public override bool Equals(object? obj) => obj is MappedValue other && Equals(other);
8993

9094
/// <inheritdoc />
91-
public override int GetHashCode() => HashCode.Combine(Type, IsPresent, Text, _number);
95+
public override int GetHashCode() =>
96+
HashCode.Combine(Type, IsPresent, Type == ColumnType.Text ? Text : null, _number);
9297

9398
/// <inheritdoc />
9499
public override string ToString() => IsPresent ? Text ?? string.Empty : "(absent)";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Text;
2+
3+
using TriasDev.Tabular.Csv;
4+
5+
using Xunit;
6+
7+
namespace TriasDev.Tabular.Tests.Csv;
8+
9+
/// <summary>
10+
/// The fallback decoder agrees with the platform's code page 1252 on every assigned byte, and says
11+
/// "unassigned" where the specification does.
12+
/// </summary>
13+
public sealed class Windows1252EncodingTests
14+
{
15+
private static readonly byte[] Unassigned = [0x81, 0x8D, 0x8F, 0x90, 0x9D];
16+
17+
[Fact]
18+
public void DecodesEveryAssignedByteAsThePlatformDoes()
19+
{
20+
// The platform's table is the oracle. It is not the library's decoder because it lives behind
21+
// an encoding provider a caller must register — see the class's own remarks.
22+
Encoding oracle = CodePagesEncodingProvider.Instance.GetEncoding(1252)!;
23+
24+
for (int b = 0; b <= 0xFF; b++)
25+
{
26+
if (Unassigned.Contains((byte)b))
27+
{
28+
continue;
29+
}
30+
31+
byte[] one = [(byte)b];
32+
33+
Assert.True(
34+
oracle.GetString(one) == Windows1252Encoding.Instance.GetString(one),
35+
$"byte 0x{b:X2}: expected U+{(int)oracle.GetString(one)[0]:X4}, got U+{(int)Windows1252Encoding.Instance.GetString(one)[0]:X4}");
36+
}
37+
}
38+
39+
[Fact]
40+
public void DecodesTheUnassignedBytesAsTheReplacementCharacter()
41+
{
42+
Assert.All(Unassigned, b => Assert.Equal("�", Windows1252Encoding.Instance.GetString([b])));
43+
}
44+
45+
[Fact]
46+
public void ReadsTheHighRangeThroughTheCsvFallback()
47+
{
48+
// € Š œ Ÿ, then ü — which makes the file invalid UTF-8 and forces the fallback.
49+
byte[] content = [0x80, 0x8A, 0x9C, 0x9F, 0xFC, (byte)';', (byte)'x', (byte)'\n'];
50+
51+
using CsvCursor cursor = new(new MemoryStream(content, writable: false), "t.csv");
52+
53+
Assert.True(cursor.ReadRow(TestContext.Current.CancellationToken));
54+
Assert.Equal("windows-1252", cursor.Dialect.Encoding.WebName);
55+
Assert.Equal("€ŠœŸü", cursor.CurrentRow[0].AsText());
56+
}
57+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Xunit;
2+
3+
namespace TriasDev.Tabular.Tests;
4+
5+
/// <summary>
6+
/// The two value structs compare by what they hold: a raw cell by its kind and contents, a mapped
7+
/// value by its type and value.
8+
/// </summary>
9+
public sealed class ValueEqualityTests
10+
{
11+
[Fact]
12+
public void ARawCellOfOneKindIsNotEqualToTheSameTextOfAnother()
13+
{
14+
Assert.NotEqual(RawCell.FromText("1"), RawCell.FromNumber(1));
15+
Assert.NotEqual(RawCell.FromNumber(1), RawCell.FromBoolean(true));
16+
}
17+
18+
[Fact]
19+
public void RawCellsHoldingTheSameAreEqualAndHashAlike()
20+
{
21+
Assert.True(RawCell.FromNumber(2.5) == RawCell.FromNumber(2.5));
22+
Assert.Equal(RawCell.FromNumber(2.5).GetHashCode(), RawCell.FromNumber(2.5).GetHashCode());
23+
24+
// Text is trimmed on the way in, and whitespace alone is nothing.
25+
Assert.Equal(RawCell.FromText("a"), RawCell.FromText(" a "));
26+
Assert.Equal(RawCell.Empty, RawCell.FromText(" "));
27+
Assert.Equal(RawCell.Empty, default);
28+
}
29+
30+
[Fact]
31+
public void ARawCellHoldingNotANumberEqualsItself()
32+
{
33+
// Equality has to be reflexive for a cell to be found in a set at all.
34+
RawCell nan = RawCell.FromNumber(double.NaN);
35+
36+
Assert.True(nan.Equals(nan));
37+
Assert.Contains(nan, new HashSet<RawCell> { nan });
38+
}
39+
40+
[Fact]
41+
public void MappedNumbersCompareByValueNotByHowTheyWereWritten()
42+
{
43+
// 1.0 and 1.00 are one amount. Comparing the rendered text as well made them two, and a
44+
// caller de-duplicating mapped values kept both.
45+
MappedValue one = MappedValue.FromDecimal(1.0m);
46+
MappedValue sameAmount = MappedValue.FromDecimal(1.00m);
47+
48+
Assert.Equal(one, sameAmount);
49+
Assert.Equal(one.GetHashCode(), sameAmount.GetHashCode());
50+
}
51+
52+
[Fact]
53+
public void MappedValuesOfDifferentTypesAreNotEqual()
54+
{
55+
Assert.NotEqual(MappedValue.FromInteger(1), MappedValue.FromDecimal(1m));
56+
Assert.NotEqual(MappedValue.FromText("1"), MappedValue.FromInteger(1));
57+
Assert.NotEqual(MappedValue.FromBoolean(false), MappedValue.Absent);
58+
}
59+
60+
[Fact]
61+
public void MappedTextComparesOrdinally()
62+
{
63+
Assert.NotEqual(MappedValue.FromText("a"), MappedValue.FromText("A"));
64+
Assert.Equal(MappedValue.FromText("ä"), MappedValue.FromText("ä"));
65+
}
66+
67+
[Fact]
68+
public void MappedDatesCompareByTheInstantTheyHold()
69+
{
70+
DateTime morning = new(2026, 9, 25, 8, 0, 0, DateTimeKind.Unspecified);
71+
72+
Assert.Equal(MappedValue.FromDate(morning), MappedValue.FromDate(morning));
73+
Assert.NotEqual(MappedValue.FromDate(morning), MappedValue.FromDate(morning.AddHours(1)));
74+
}
75+
}

0 commit comments

Comments
 (0)