Skip to content

Commit

Permalink
AvaloniaDictionary accept IDictionary as init collection
Browse files Browse the repository at this point in the history
  • Loading branch information
IoannTerrible committed Jan 21, 2025
1 parent 9d564e4 commit 957f83b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/Avalonia.Base/Collections/AvaloniaDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ public AvaloniaDictionary(int capacity)
}

/// <summary>
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class.
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class using an IDictionary.
/// </summary>
public AvaloniaDictionary(Dictionary<TKey, TValue> initialDictionary)
public AvaloniaDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer = null)
{
if (initialDictionary == null)
if (dictionary != null)
{
throw new ArgumentNullException(nameof(initialDictionary));
_inner = new Dictionary<TKey, TValue>(dictionary, comparer ?? EqualityComparer<TKey>.Default);
}
else
{
throw new ArgumentNullException(nameof(dictionary));
}
_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
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Avalonia.Collections;
using Avalonia.Data.Core;

using Xunit;

namespace Avalonia.Base.UnitTests.Collections
Expand Down Expand Up @@ -158,29 +160,32 @@ public void Clearing_Collection_Should_Raise_PropertyChanged()
}

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


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

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

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



}
}

0 comments on commit 957f83b

Please sign in to comment.