question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OpenXmlSimpleType children do not implement equality

See original GitHub issue

Description

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasBarnekowcommented, Apr 18, 2018

Yes, can do.

0reactions
twsouthwickcommented, Apr 27, 2018

@ThomasBarnekow thanks for the contribution!

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found