| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using System;
- using System.Collections.Generic;
- using Avalonia.Utilities;
- using Xunit;
- namespace Avalonia.Base.UnitTests.Utilities
- {
- public class AvaloniaPropertyDictionaryTests
- {
- private static AvaloniaProperty[] TestProperties;
- static AvaloniaPropertyDictionaryTests()
- {
- TestProperties = new AvaloniaProperty[100];
- for (var i = 0; i < 100; ++i)
- {
- TestProperties[i] = new StyledProperty<string>(
- $"Test{i}",
- typeof(AvaloniaPropertyDictionaryTests),
- new StyledPropertyMetadata<string>());
- }
- Shuffle(TestProperties, 42);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Property_Indexer_Finds_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- var result = target[property];
- Assert.Equal($"Value{index}", result);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Property_Indexer_Throws_If_Value_Not_Found(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- Assert.Throws<KeyNotFoundException>(() => target[property]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Property_Indexer_Adds_New_Value(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- target[property] = "new";
- Assert.Equal("new", target[property]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Property_Indexer_Sets_Existing_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.Equal($"Value{index}", target[property]);
- target[property] = "new";
- Assert.Equal("new", target[property]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Int_Indexer_Finds_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var result = target[index];
- Assert.NotNull(result);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Int_Indexer_Throws_If_Index_Out_Of_Range(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- Assert.Throws<IndexOutOfRangeException>(() => target[index]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Add_Adds_New_Value(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- target.Add(property, "new");
- Assert.Equal("new", target[property]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Add_Throws_If_Key_Exists(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.Throws<ArgumentException>(() => target.Add(property, "new"));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void ContainsKey_Returns_True_If_Value_Exists(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.True(target.ContainsKey(property));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void ContainsKey_Returns_False_If_Value_Does_Not_Exist(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- Assert.False(target.ContainsKey(property));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void GetKeyValue_Finds_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- target.GetKeyValue(index, out var property, out var value);
- Assert.NotNull(property);
- Assert.NotNull(value);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void GetKeyValue_Throws_If_Index_Out_Of_Range(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- Assert.Throws<IndexOutOfRangeException>(() => target.GetKeyValue(index, out var _, out var _));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Remove_Removes_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.True(target.Remove(property));
- Assert.False(target.ContainsKey(property));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Remove_Returns_False_If_Value_Not_Present(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- Assert.False(target.Remove(property));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void Remove_Returns_Existing_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.True(target.Remove(property, out var value));
- Assert.Equal($"Value{index}", value);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void TryAdd_Adds_New_Value(int count)
- {
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- Assert.True(target.TryAdd(property, "new"));
- Assert.Equal("new", target[property]);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void TryAdd_Returns_False_If_Key_Exists(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.False(target.TryAdd(property, "new"));
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void TryGetValue_Finds_Value(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count / 2;
- var property = TestProperties[index];
- Assert.True(target.TryGetValue(property, out var value));
- Assert.Equal($"Value{index}", value);
- }
- [Theory]
- [MemberData(nameof(Counts))]
- public void TryGetValue_Returns_False_If_Key_Does_Not_Exist(int count)
- {
- if (count == 0)
- return;
- var target = CreateTarget(count);
- var index = count;
- var property = TestProperties[index];
- Assert.False(target.TryGetValue(property, out var value));
- Assert.Null(value);
- }
- public static TheoryData<int> Counts()
- {
- var result = new TheoryData<int>();
- result.Add(0);
- result.Add(1);
- result.Add(10);
- result.Add(13);
- result.Add(50);
- result.Add(72);
- return result;
- }
- private static AvaloniaPropertyDictionary<string> CreateTarget(int items)
- {
- var result = new AvaloniaPropertyDictionary<string>();
- for (var i = 0; i < items; ++i)
- result.Add(TestProperties[i], $"Value{i}");
- return result;
- }
- private static void Shuffle<T>(T[] array, int seed)
- {
- var rng = new Random(seed);
- int n = array.Length;
- while (n > 1)
- {
- int k = rng.Next(n--);
- T temp = array[n];
- array[n] = array[k];
- array[k] = temp;
- }
- }
- }
- }
|