forked from YAXLib/YAXLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionUtilsTest.cs
109 lines (99 loc) · 6.62 KB
/
ReflectionUtilsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2009 - 2010 Sina Iravanian - <[email protected]>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//-----------------------------------------------------------------------
using NUnit.Framework;
using YAXLib;
using System;
using System.Collections.Generic;
using System.Collections;
namespace YAXLibTests
{
[TestFixture]
public class ReflectionUtilsTest
{
[Test]
public void IsArrayTest()
{
Assert.That(ReflectionUtils.IsArray(typeof(int[])), Is.True);
Assert.That(ReflectionUtils.IsArray(typeof(int[,])), Is.True);
Assert.That(ReflectionUtils.IsArray(typeof(Array)), Is.True);
Assert.That(ReflectionUtils.IsArray(typeof(List<int>)), Is.False);
Assert.That(ReflectionUtils.IsArray(typeof(List<>)), Is.False);
Assert.That(ReflectionUtils.IsArray(typeof(Dictionary<,>)), Is.False);
Assert.That(ReflectionUtils.IsArray(typeof(Dictionary<int, string>)), Is.False);
Assert.That(ReflectionUtils.IsArray(typeof(string)), Is.False);
}
[Test]
public void IsCollectionTypeTest()
{
Assert.That(ReflectionUtils.IsCollectionType(typeof(int[])), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(Array)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(List<int>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(List<>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(Dictionary<,>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(Dictionary<int,string>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(IEnumerable)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(IEnumerable<>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(IEnumerable<int>)), Is.True);
Assert.That(ReflectionUtils.IsCollectionType(typeof(string)), Is.False);
}
[Test]
public void GetCollectionItemTypeTest()
{
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(IEnumerable<int>)) == typeof(int), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(double[])) == typeof(double), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(float[][])) == typeof(float[]), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(string[,])) == typeof(string), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(List<char>)) == typeof(char), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(Dictionary<int,char>)) == typeof(KeyValuePair<int, char>), Is.True);
Assert.That(ReflectionUtils.GetCollectionItemType(typeof(Dictionary<Dictionary<int, double>, char>)) == typeof(KeyValuePair<Dictionary<int, double>, char>), Is.True);
//Assert.That(ReflectionUtils.GetCollectionItemType(typeof(IEnumerable<>)) == typeof(object), Is.True);
}
[Test]
public void IsTypeEqualOrInheritedFromTypeTest()
{
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof (int), typeof (object)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(string), typeof(object)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Array), typeof(IEnumerable)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string,int>), typeof(Dictionary<,>)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int>), typeof(ICollection)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int>), typeof(IDictionary)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int>), typeof(IDictionary<,>)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int>), typeof(IDictionary<string, int>)), Is.True);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int>), typeof(IDictionary<int, string>)), Is.False);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(Dictionary<string, int[]>), typeof(IDictionary<int, Array>)), Is.False);
Assert.That(ReflectionUtils.IsTypeEqualOrInheritedFromType(typeof(ICollection), typeof(IEnumerable)), Is.True);
}
[Test]
public void EqualsOrIsNullableOfTest()
{
Assert.That(typeof(int).EqualsOrIsNullableOf(typeof(int)), Is.True);
Assert.That(typeof(int?).EqualsOrIsNullableOf(typeof(int)), Is.True);
Assert.That(typeof(int).EqualsOrIsNullableOf(typeof(int?)), Is.True);
Assert.That(typeof(double).EqualsOrIsNullableOf(typeof(Nullable<double>)), Is.True);
Assert.That(typeof(double?).EqualsOrIsNullableOf(typeof(Nullable<>)), Is.False);
Assert.That(typeof(Nullable<double>).EqualsOrIsNullableOf(typeof(double)), Is.True);
Assert.That(typeof(Nullable<char>).EqualsOrIsNullableOf(typeof(char?)), Is.True);
Assert.That(typeof(char?).EqualsOrIsNullableOf(typeof(Nullable<char>)), Is.True);
Assert.That(typeof(int[]).EqualsOrIsNullableOf(typeof(System.Array)), Is.False);
}
[Test]
public void GetTypeByNameTest()
{
var type1 = ReflectionUtils.GetTypeByName("System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
var type2 = ReflectionUtils.GetTypeByName("System.Collections.Generic.List`1[[System.Int32]]");
var type3 = ReflectionUtils.GetTypeByName("System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral]]");
Assert.That(type1, Is.Not.Null);
Assert.That(type2, Is.Not.Null);
Assert.That(type3, Is.Not.Null);
Assert.That(type2, Is.EqualTo(type1));
Assert.That(type3, Is.EqualTo(type2));
}
}
}