Skip to content

Commit

Permalink
new instance of the AvaloniaDictionary trough IEnumerable<KeyValuePai…
Browse files Browse the repository at this point in the history
…r<TKey, TValue>>
  • Loading branch information
IoannTerrible committed Jan 21, 2025
1 parent 9d564e4 commit 96ac471
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/Avalonia.Base/Collections/AvaloniaDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public AvaloniaDictionary(int capacity)
/// <summary>
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class.
/// </summary>
public AvaloniaDictionary(Dictionary<TKey, TValue> initialDictionary)
public AvaloniaDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer)
{
if (initialDictionary == null)
if (collection == null)
{
throw new ArgumentNullException(nameof(initialDictionary));
throw new ArgumentNullException(nameof(collection));
}
_inner = new Dictionary<TKey, TValue>(initialDictionary);
}

_inner = new Dictionary<TKey, TValue>(collection, comparer ?? EqualityComparer<TKey>.Default);
}


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,31 @@ 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 List<KeyValuePair<string, string>>
{
{ "key1", "value1" },
{ "key2", "value2" }
new KeyValuePair<string, string>("key1", "value1"),
new KeyValuePair<string, string>("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 96ac471

Please sign in to comment.