|
| 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