mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-08-18 11:31:45 +02:00
.github
CommunityToolkit.Common
CommunityToolkit.Diagnostics
CommunityToolkit.HighPerformance
CommunityToolkit.Mvvm
CommunityToolkit.Mvvm.SourceGenerators
build
tests
CommunityToolkit.Common.UnitTests
CommunityToolkit.Diagnostics.UnitTests
CommunityToolkit.HighPerformance.UnitTests
CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests
CommunityToolkit.Mvvm.ExternalAssembly
CommunityToolkit.Mvvm.Internals.UnitTests
CommunityToolkit.Mvvm.SourceGenerators.UnitTests
CommunityToolkit.Mvvm.UnitTests
Collections
IntGroup.cs
Test_ObservableGroup.cs
Test_ObservableGroupedCollection.cs
Test_ObservableGroupedCollectionExtensions.cs
Test_ReadOnlyObservableGroup.cs
Test_ReadOnlyObservableGroupedCollection.cs
Helpers
CommunityToolkit.Mvvm.UnitTests.csproj
Test_ArgumentNullException.ComponentModel.cs
Test_ArgumentNullException.DependencyInjection.cs
Test_ArgumentNullException.Input.cs
Test_ArgumentNullException.Messaging.cs
Test_AsyncRelayCommand.cs
Test_AsyncRelayCommand{T}.cs
Test_INotifyPropertyChangedAttribute.cs
Test_IRecipientGenerator.cs
Test_Messenger.Request.cs
Test_Messenger.cs
Test_ObservableObject.cs
Test_ObservableObjectAttribute.cs
Test_ObservablePropertyAttribute.RootNamespace.cs
Test_ObservablePropertyAttribute.cs
Test_ObservableRecipient.cs
Test_ObservableRecipientAttribute.cs
Test_ObservableValidator.cs
Test_RelayCommand.cs
Test_RelayCommandAttribute.cs
Test_RelayCommand{T}.cs
Test_SourceGenerators.cs
.editorconfig
.git-blame-ignore-revs
.gitattributes
.gitignore
.runsettings
CODE_OF_CONDUCT.md
Contributing.md
Directory.Build.props
Directory.Build.targets
License.md
README.md
ThirdPartyNotices.txt
azure-pipelines.yml
dotnet Community Toolkit.sln
toolkit.snk
version.json
157 lines
4.8 KiB
C#
157 lines
4.8 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using CommunityToolkit.Mvvm.Collections;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace CommunityToolkit.Mvvm.UnitTests.Collections;
|
|
|
|
[TestClass]
|
|
public class Test_ObservableGroup
|
|
{
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Ctor_ShouldHaveExpectedState()
|
|
{
|
|
ObservableGroup<string, int> group = new("key");
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
Assert.AreEqual(group.Count, 0);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Ctor_WithGrouping_ShouldHaveExpectedState()
|
|
{
|
|
IntGroup source = new("key", new[] { 1, 2, 3 });
|
|
ObservableGroup<string, int> group = new(source);
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
CollectionAssert.AreEqual(group, new[] { 1, 2, 3 });
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Ctor_WithCollection_ShouldHaveExpectedState()
|
|
{
|
|
int[] source = new[] { 1, 2, 3 };
|
|
ObservableGroup<string, int> group = new("key", source);
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
CollectionAssert.AreEqual(group, new[] { 1, 2, 3 });
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Add_ShouldRaiseEvent()
|
|
{
|
|
bool collectionChangedEventRaised = false;
|
|
int[] source = new[] { 1, 2, 3 };
|
|
ObservableGroup<string, int> group = new("key", source);
|
|
|
|
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
|
|
|
|
group.Add(4);
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
CollectionAssert.AreEqual(group, new[] { 1, 2, 3, 4 });
|
|
Assert.IsTrue(collectionChangedEventRaised);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Update_ShouldRaiseEvent()
|
|
{
|
|
bool collectionChangedEventRaised = false;
|
|
int[] source = new[] { 1, 2, 3 };
|
|
ObservableGroup<string, int> group = new("key", source);
|
|
|
|
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
|
|
|
|
group[1] = 4;
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
CollectionAssert.AreEqual(group, new[] { 1, 4, 3 });
|
|
Assert.IsTrue(collectionChangedEventRaised);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Remove_ShouldRaiseEvent()
|
|
{
|
|
bool collectionChangedEventRaised = false;
|
|
int[] source = new[] { 1, 2, 3 };
|
|
ObservableGroup<string, int>? group = new("key", source);
|
|
|
|
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
|
|
|
|
_ = group.Remove(1);
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
CollectionAssert.AreEqual(group, new[] { 2, 3 });
|
|
Assert.IsTrue(collectionChangedEventRaised);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ObservableGroup_Clear_ShouldRaiseEvent()
|
|
{
|
|
bool collectionChangedEventRaised = false;
|
|
int[] source = new[] { 1, 2, 3 };
|
|
ObservableGroup<string, int>? group = new("key", source);
|
|
|
|
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
|
|
|
|
group.Clear();
|
|
|
|
Assert.AreEqual(group.Key, "key");
|
|
Assert.AreEqual(group.Count, 0);
|
|
Assert.IsTrue(collectionChangedEventRaised);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(0)]
|
|
[DataRow(3)]
|
|
public void Test_ObservableGroup_IReadOnlyObservableGroup_ShouldReturnExpectedValues(int count)
|
|
{
|
|
ObservableGroup<string, int> group = new("key", Enumerable.Range(0, count));
|
|
IReadOnlyObservableGroup iReadOnlyObservableGroup = group;
|
|
|
|
Assert.AreEqual(iReadOnlyObservableGroup.Key, "key");
|
|
Assert.AreEqual(iReadOnlyObservableGroup.Count, count);
|
|
}
|
|
|
|
[TestMethod]
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
public void Test_ObservableGroup_Ctor_NullKey()
|
|
{
|
|
_ = new ObservableGroup<string, int>((string)null!);
|
|
}
|
|
|
|
[TestMethod]
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
public void Test_ObservableGroup_Ctor_NullGroup()
|
|
{
|
|
_ = new ObservableGroup<string, int>((IGrouping<string, int>)null!);
|
|
}
|
|
|
|
[TestMethod]
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
public void Test_ObservableGroup_Ctor_NullKeyWithNotNullElements()
|
|
{
|
|
_ = new ObservableGroup<string, int>(null!, new int[0]);
|
|
}
|
|
|
|
[TestMethod]
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
public void Test_ObservableGroup_Ctor_NotNullKeyWithNullElements()
|
|
{
|
|
_ = new ObservableGroup<string, int>("A", null!);
|
|
}
|
|
|
|
[TestMethod]
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
public void Test_ObservableGroup_Ctor_NullKeySetter()
|
|
{
|
|
ObservableGroup<string, int> group = new("A");
|
|
|
|
group.Key = null!;
|
|
}
|
|
}
|