This commit is contained in:
Chris Cameron
2017-10-05 16:41:57 -04:00
parent eb24759580
commit 3b0bab02de
2 changed files with 197 additions and 169 deletions

View File

@@ -5,18 +5,18 @@ using System.Linq;
namespace ICD.Common.Utils.Tests.Extensions namespace ICD.Common.Utils.Tests.Extensions
{ {
[TestFixture] [TestFixture]
public sealed class DictionaryExtensionsTest public sealed class DictionaryExtensionsTest
{ {
[Test] [Test]
public void RemoveValueTest() public void RemoveValueTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 }, {3, 30},
{ 4, 40 }, {4, 40},
}; };
Assert.IsTrue(dict.RemoveValue(10)); Assert.IsTrue(dict.RemoveValue(10));
@@ -26,15 +26,15 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(3, dict.Count); Assert.AreEqual(3, dict.Count);
} }
[Test] [Test]
public void RemoveAllValuesTest() public void RemoveAllValuesTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
{ 3, 30 }, {3, 30},
{ 4, 40 }, {4, 40},
}; };
dict.RemoveAllValues(10); dict.RemoveAllValues(10);
@@ -44,45 +44,45 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(2, dict.Count); Assert.AreEqual(2, dict.Count);
} }
[Test] [Test]
public void GetDefaultTest() public void GetDefaultTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
{ 3, 30 }, {3, 30},
{ 4, 40 }, {4, 40},
}; };
Assert.AreEqual(10, dict.GetDefault(1)); Assert.AreEqual(10, dict.GetDefault(1));
Assert.AreEqual(0, dict.GetDefault(0)); Assert.AreEqual(0, dict.GetDefault(0));
} }
[Test] [Test]
public void GetDefaultValueTest() public void GetDefaultValueTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
{ 3, 30 }, {3, 30},
{ 4, 40 }, {4, 40},
}; };
Assert.AreEqual(10, dict.GetDefault(1, 1000)); Assert.AreEqual(10, dict.GetDefault(1, 1000));
Assert.AreEqual(1000, dict.GetDefault(0, 1000)); Assert.AreEqual(1000, dict.GetDefault(0, 1000));
} }
[Test] [Test]
public void GetOrAddDefaultTest() public void GetOrAddDefaultTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
{ 3, 30 }, {3, 30},
{ 4, 40 }, {4, 40},
}; };
Assert.AreEqual(10, dict.GetOrAddDefault(1, 1000)); Assert.AreEqual(10, dict.GetOrAddDefault(1, 1000));
@@ -91,81 +91,81 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(5, dict.Count); Assert.AreEqual(5, dict.Count);
} }
[Test] [Test]
public void GetKeyTest() public void GetKeyTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 } {3, 30}
}; };
Assert.AreEqual(1, dict.GetKey(10)); Assert.AreEqual(1, dict.GetKey(10));
Assert.AreEqual(2, dict.GetKey(20)); Assert.AreEqual(2, dict.GetKey(20));
Assert.AreEqual(3, dict.GetKey(30)); Assert.AreEqual(3, dict.GetKey(30));
Assert.Throws<KeyNotFoundException>(() => dict.GetKey(40)); Assert.Throws<KeyNotFoundException>(() => dict.GetKey(40));
} }
[Test] [Test]
public void TryGetKeyTest() public void TryGetKeyTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 } {3, 30}
}; };
int key; int key;
Assert.IsTrue(dict.TryGetKey(10, out key)); Assert.IsTrue(dict.TryGetKey(10, out key));
Assert.AreEqual(1, key); Assert.AreEqual(1, key);
Assert.IsTrue(dict.TryGetKey(20, out key)); Assert.IsTrue(dict.TryGetKey(20, out key));
Assert.AreEqual(2, key); Assert.AreEqual(2, key);
Assert.IsTrue(dict.TryGetKey(30, out key)); Assert.IsTrue(dict.TryGetKey(30, out key));
Assert.AreEqual(3, key); Assert.AreEqual(3, key);
} }
[Test] [Test]
public void GetKeysTest() public void GetKeysTest()
{ {
Dictionary<int, int> dict = new Dictionary<int, int> Dictionary<int, int> dict = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
{ 3, 30 } {3, 30}
}; };
int[] aKeys = dict.GetKeys(10).ToArray(); int[] aKeys = dict.GetKeys(10).ToArray();
int[] bKeys = dict.GetKeys(20).ToArray(); int[] bKeys = dict.GetKeys(20).ToArray();
int[] cKeys = dict.GetKeys(30).ToArray(); int[] cKeys = dict.GetKeys(30).ToArray();
Assert.AreEqual(2, aKeys.Length); Assert.AreEqual(2, aKeys.Length);
Assert.IsTrue(aKeys.Contains(1)); Assert.IsTrue(aKeys.Contains(1));
Assert.IsTrue(aKeys.Contains(2)); Assert.IsTrue(aKeys.Contains(2));
Assert.AreEqual(0, bKeys.Length); Assert.AreEqual(0, bKeys.Length);
Assert.AreEqual(1, cKeys.Length); Assert.AreEqual(1, cKeys.Length);
Assert.IsTrue(cKeys.Contains(3)); Assert.IsTrue(cKeys.Contains(3));
} }
[Test] [Test]
public void UpdateTest() public void UpdateTest()
{ {
Dictionary<int, int> a = new Dictionary<int, int> Dictionary<int, int> a = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 10 }, {2, 10},
}; };
Dictionary<int, int> b = new Dictionary<int, int> Dictionary<int, int> b = new Dictionary<int, int>
{ {
{ 2, 20 }, {2, 20},
{ 3, 30 } {3, 30}
}; };
a.Update(b); a.Update(b);
@@ -181,51 +181,45 @@ namespace ICD.Common.Utils.Tests.Extensions
} }
[Test] [Test]
public void AddRangeValueKeyFuncTest() public void AddRangeValueKeyFuncTest()
{ {
Assert.Inconclusive(); Assert.Inconclusive();
} }
[Test] [Test]
public void AddRangeKeyValueFuncTest() public void AddRangeKeyValueFuncTest()
{ {
Assert.Inconclusive(); Assert.Inconclusive();
} }
[Test] [Test]
public void AddRangeTest() public void AddRangeTest()
{ {
Assert.Inconclusive(); Assert.Inconclusive();
} }
[Test] [Test]
public void DictionaryEqualTest() public void DictionaryEqualTest()
{ {
Assert.Inconclusive();
}
[Test]
public void DictionaryEqualComparerTest()
{
Dictionary<int, int> a = new Dictionary<int, int> Dictionary<int, int> a = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 }, {3, 30}
}; };
Dictionary<int, int> b = new Dictionary<int, int> Dictionary<int, int> b = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 }, {3, 30}
}; };
Dictionary<int, int> c = new Dictionary<int, int> Dictionary<int, int> c = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 } {2, 20}
}; };
Assert.IsTrue(a.DictionaryEqual(b)); Assert.IsTrue(a.DictionaryEqual(b));
@@ -235,20 +229,50 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.IsFalse(c.DictionaryEqual(a)); Assert.IsFalse(c.DictionaryEqual(a));
} }
[Test] [Test]
public void DictionaryEqualFuncComparerTest() public void DictionaryEqualComparerTest()
{ {
Assert.Inconclusive(); Assert.Inconclusive();
} }
[Test] [Test]
public void OrderByKeyTest() public void DictionaryEqualFuncComparerTest()
{ {
Dictionary<int, int> a = new Dictionary<int, int> Dictionary<int, int> a = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 }, {3, 30}
};
Dictionary<int, int> b = new Dictionary<int, int>
{
{1, 10},
{2, 20},
{3, 30}
};
Dictionary<int, int> c = new Dictionary<int, int>
{
{1, 10},
{2, 20}
};
Assert.IsTrue(a.DictionaryEqual(b, (left, right) => left / 10 == right / 10));
Assert.IsTrue(b.DictionaryEqual(a, (left, right) => left / 10 == right / 10));
Assert.IsFalse(a.DictionaryEqual(c, (left, right) => left / 10 == right / 10));
Assert.IsFalse(c.DictionaryEqual(a, (left, right) => left / 10 == right / 10));
}
[Test]
public void OrderByKeyTest()
{
Dictionary<int, int> a = new Dictionary<int, int>
{
{1, 10},
{2, 20},
{3, 30}
}; };
KeyValuePair<int, int>[] ordered = a.OrderByKey().ToArray(); KeyValuePair<int, int>[] ordered = a.OrderByKey().ToArray();
@@ -259,14 +283,14 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(30, ordered[2].Value); Assert.AreEqual(30, ordered[2].Value);
} }
[Test] [Test]
public void OrderValuesByKeyTest() public void OrderValuesByKeyTest()
{ {
Dictionary<int, int> a = new Dictionary<int, int> Dictionary<int, int> a = new Dictionary<int, int>
{ {
{ 1, 10 }, {1, 10},
{ 2, 20 }, {2, 20},
{ 3, 30 }, {3, 30},
}; };
int[] ordered = a.OrderValuesByKey().ToArray(); int[] ordered = a.OrderValuesByKey().ToArray();
@@ -276,5 +300,5 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(20, ordered[1]); Assert.AreEqual(20, ordered[1]);
Assert.AreEqual(30, ordered[2]); Assert.AreEqual(30, ordered[2]);
} }
} }
} }

View File

@@ -1,6 +1,7 @@
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ICD.Common.Properties;
#if SIMPLSHARP #if SIMPLSHARP
using Crestron.SimplSharp.Reflection; using Crestron.SimplSharp.Reflection;
#else #else
@@ -10,18 +11,21 @@ using System.Reflection;
namespace ICD.Common.Utils.Tests namespace ICD.Common.Utils.Tests
{ {
[TestFixture] [TestFixture]
public sealed class ReflectionUtilsTest public sealed class ReflectionUtilsTest
{ {
[UsedImplicitly]
private string TestProperty { get; set; } private string TestProperty { get; set; }
[UsedImplicitly]
private void TestMethod(string param1, int param2) private void TestMethod(string param1, int param2)
{ {
} }
private class TestClass [UsedImplicitly]
private sealed class TestClass
{ {
public string Param1 { get; set; } public string Param1 { get; }
public int Param2 { get; set; } public int Param2 { get; }
public TestClass(string param1, int param2) public TestClass(string param1, int param2)
{ {
@@ -46,31 +50,31 @@ namespace ICD.Common.Utils.Tests
{ {
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesConstructorParameters(null, new object[] { "test", 10 })); Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesConstructorParameters(null, new object[] { "test", 10 }));
ConstructorInfo constructor = typeof(TestClass).GetConstructor(new Type[] { typeof(string), typeof(int) }); ConstructorInfo constructor = typeof(TestClass).GetConstructor(new Type[] {typeof(string), typeof(int)});
Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", 10 })); Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {"test", 10}));
Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { null, 10 })); Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {null, 10}));
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", "test" })); Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {"test", "test"}));
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test" })); Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {"test"}));
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", "test", "test" })); Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {"test", "test", "test"}));
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { 10, 10 })); Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] {10, 10}));
} }
[Test] [Test]
public void MatchesMethodParametersTest() public void MatchesMethodParametersTest()
{ {
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesMethodParameters(null, new object[] { "test", 10 })); Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesMethodParameters(null, new object[] {"test", 10}));
MethodInfo method = GetType().GetMethod("TestMethod", MethodInfo method = GetType().GetMethod("TestMethod",
BindingFlags.NonPublic | BindingFlags.NonPublic |
BindingFlags.Instance); BindingFlags.Instance);
Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", 10 })); Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] {"test", 10}));
Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] { null, 10 })); Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] {null, 10}));
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", "test" })); Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] {"test", "test"}));
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test" })); Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] {"test"}));
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", "test", "test" })); Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] {"test", "test", "test"}));
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { 10, 10 })); Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] {10, 10}));
} }
[Test] [Test]
@@ -79,9 +83,9 @@ namespace ICD.Common.Utils.Tests
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesPropertyParameter(null, 10)); Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesPropertyParameter(null, 10));
PropertyInfo prop = GetType().GetProperty("TestProperty", PropertyInfo prop = GetType().GetProperty("TestProperty",
BindingFlags.NonPublic | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Instance |
BindingFlags.GetProperty); BindingFlags.GetProperty);
Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, "test")); Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, "test"));
Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, null)); Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, null));