Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new constructor to AvaloniaDictionary and include unit tests #17311 #17312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Avalonia.Base/Collections/AvaloniaDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public AvaloniaDictionary(int capacity)
_inner = new Dictionary<TKey, TValue>(capacity);
}

/// <summary>
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class.
/// </summary>
public AvaloniaDictionary(Dictionary<TKey, TValue> initialDictionary)
{
if (initialDictionary == null)
{
throw new ArgumentNullException(nameof(initialDictionary));
}
_inner = new Dictionary<TKey, TValue>(initialDictionary);
}


/// <summary>
/// Occurs when the collection changes.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,31 @@ public void Clearing_Collection_Should_Raise_PropertyChanged()

Assert.Equal(new[] { "Count", CommonPropertyNames.IndexerName }, tracker.Names);
}

[Fact]
public void Constructor_Should_Throw_ArgumentNullException_When_Dictionary_Is_Null()
{
Assert.Throws<ArgumentNullException>(() =>
{
var target = new AvaloniaDictionary<string, string>(null);
});
}

[Fact]
public void Constructor_Should_Initialize_With_Provided_Dictionary()
{
var initialDictionary = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};

var target = new AvaloniaDictionary<string, string>(initialDictionary);

Assert.Equal(2, target.Count);
Assert.Equal("value1", target["key1"]);
Assert.Equal("value2", target["key2"]);
}

}
}
Loading