OpenXmlSimpleType children do not implement equality
See original GitHub issueDescription
Classes like StringValue
and HexBinaryValue
do not implement equality and you must use their Value
properties to compare them. This is not handy and can lead to unexpected results.
Information
- .NET Target: Any
- DocumentFormat.OpenXml Version: 2.8.1
Repro
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml;
using Xunit;
namespace DocumantFormat.OpenXml.Tests
{
public class OpenXmlSimpleValueTests
{
private const string HexBinary = "12345678";
private const string Value = "Hello World";
[Fact]
public void HexBinaryValue_ValuesAreEqual_HexBinaryValuesAreNotEqual()
{
var hexBinaryValue1 = new HexBinaryValue(HexBinary);
var hexBinaryValue2 = new HexBinaryValue(HexBinary);
Assert.Equal(hexBinaryValue1.Value, hexBinaryValue2.Value);
// Those two HexBinaryValue instances should be deemed equal.
Assert.NotEqual(hexBinaryValue1, hexBinaryValue2);
}
[Fact]
public void HexBinaryValue_ValueIsContainedInList_ListDoesNotContainHexBinaryValue()
{
var hexBinaryValues = new List<HexBinaryValue>
{
new HexBinaryValue(HexBinary),
new HexBinaryValue("ABCDEF00")
};
var hexBinaryValue = new HexBinaryValue(HexBinary);
Assert.Contains(hexBinaryValue.Value, hexBinaryValues.Select(hbv => hbv.Value));
// The HexBinaryValue should be deemed contained in the list.
// The fact that this wasn't the case was a gotcha.
Assert.DoesNotContain(hexBinaryValue, hexBinaryValues);
}
[Fact]
public void StringValue_ValuesAreEqual_StringValuesAreNotEqual()
{
var stringValue1 = new StringValue(Value);
var stringValue2 = new StringValue(Value);
Assert.Equal(stringValue1.Value, stringValue2.Value);
// The two StringValue instances should be deemed equal.
Assert.NotEqual(stringValue1, stringValue2);
}
}
}
Observed
Two distinct instances of HexBinaryValue
or StringValue
, for example, will not be deemed equal even if their values are equal.
Expected
Given two distinct instances of HexBinaryValue
, when the Value
properties of those instances are equal, then those HexBinaryValue
instances should be deemed equal as well.
The same should apply to other child classes of OpenXmlSimpleType
such as StringValue
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
OpenXmlSimpleType children do not implement equality
Classes like StringValue and HexBinaryValue do not implement equality and you must use their Value properties to compare them. This is not handy ......
Read more >How to define value equality for a class or struct - C# ...
Equals to examine fields that are in the base class. (Don't call base.Equals if the type inherits directly from Object, because the Object ......
Read more >Anti-equality: Social comparison in young children - PMC
Young children dislike getting less than others, which might suggest a general preference for equal outcomes. However, young children are ...
Read more >Equality Act Harms American Freedom
The Equality Act promises to promote tolerance, respect and — of course — equality. Instead, it would promote inequality by undermining ...
Read more >Equality & Diversity in the Classroom
As we have seen, promoting equality, diversity and inclusion should be a clear priority within schools and should be something that all children...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, can do.
@ThomasBarnekow thanks for the contribution!