Skip to content

Commit 4671451

Browse files
BorisDogrstam
authored andcommitted
CSHARP-4652: TagSet.Equals should ignore tags order (#1084)
* CSHARP-4652: TagSet.Equals should ignore tags order
1 parent fecef65 commit 4671451

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/MongoDB.Driver.Core/TagSet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq;
19-
using System.Text;
20-
using System.Threading.Tasks;
2119
using MongoDB.Driver.Core.Misc;
2220
using MongoDB.Shared;
2321

@@ -46,7 +44,7 @@ public TagSet()
4644
/// <param name="tags">The tags.</param>
4745
public TagSet(IEnumerable<Tag> tags)
4846
{
49-
_tags = Ensure.IsNotNull(tags, nameof(tags)).ToList();
47+
_tags = Ensure.IsNotNull(tags, nameof(tags)).OrderBy(t => t.Name).ThenBy(t => t.Value).ToArray();
5048
}
5149

5250
// properties

tests/MongoDB.Driver.Core.Tests/TagSetTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
using System.Threading.Tasks;
2118
using FluentAssertions;
2219
using Xunit;
2320

@@ -87,6 +84,38 @@ public void ContainsAll_should_return_true_if_all_required_tags_are_present()
8784
tagSet.ContainsAll(required).Should().BeTrue();
8885
}
8986

87+
public static IEnumerable<object[]> Equals_should_ignore_order_memberdata()
88+
{
89+
var data = new TagSet[][]
90+
{
91+
new TagSet[]
92+
{
93+
new (new Tag[] { new ("name1", "value1"), new ("name2", "value2") }),
94+
new (new Tag[] { new ("name2", "value2"), new ("name1", "value1") })
95+
},
96+
new TagSet[]
97+
{
98+
new (new Tag[] { new ("name1", "value1"), new ("name1", "value2") }),
99+
new (new Tag[] { new ("name1", "value2"), new ("name1", "value1") })
100+
},
101+
new TagSet[]
102+
{
103+
new (new Tag[] { new ("name1", "value1"), new ("name2", "value2"), new ("name2", "value1"), new ("name1", "value2") }),
104+
new (new Tag[] { new ("name1", "value2"), new ("name2", "value1"), new ("name1", "value1"), new ("name2", "value2") })
105+
}
106+
};
107+
108+
return data;
109+
}
110+
111+
[Theory]
112+
[MemberData(nameof(Equals_should_ignore_order_memberdata))]
113+
public void Equals_should_ignore_order(TagSet tagSet1, TagSet tagSet2)
114+
{
115+
tagSet1.Equals(tagSet2).Should().BeTrue();
116+
tagSet1.Equals((object)tagSet2).Should().BeTrue();
117+
}
118+
90119
[Fact]
91120
public void Equals_should_return_false_if_any_field_is_not_the_same()
92121
{

0 commit comments

Comments
 (0)