-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicExtensionsTests.cs
More file actions
125 lines (111 loc) · 4.43 KB
/
DynamicExtensionsTests.cs
File metadata and controls
125 lines (111 loc) · 4.43 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using Xunit;
namespace Platform.Reflection.Tests
{
public class DynamicExtensionsTests
{
private class TestClass
{
public int IntProperty { get; set; }
public string StringProperty { get; set; } = "test";
public bool BoolProperty { get; set; }
}
[Fact]
public void HasPropertyExistingPropertyTest()
{
var obj = new TestClass();
Assert.True(obj.HasProperty("IntProperty"));
Assert.True(obj.HasProperty("StringProperty"));
Assert.True(obj.HasProperty("BoolProperty"));
}
[Fact]
public void HasPropertyNonExistingPropertyTest()
{
var obj = new TestClass();
Assert.False(obj.HasProperty("NonExistentProperty"));
Assert.False(obj.HasProperty("AnotherMissingProperty"));
}
[Fact]
public void HasPropertyCaseSensitiveTest()
{
var obj = new TestClass();
Assert.True(obj.HasProperty("IntProperty"));
Assert.False(obj.HasProperty("intproperty"));
Assert.False(obj.HasProperty("INTPROPERTY"));
Assert.False(obj.HasProperty("intProperty"));
}
[Fact]
public void HasPropertyWithDictionaryTest()
{
var dictionary = new Dictionary<string, object>
{
{ "Key1", "Value1" },
{ "Key2", 42 },
{ "Key3", true }
};
// The current implementation doesn't actually check if the object is a dictionary
// It checks if the type "is" IDictionary<string, object> which fails for Dictionary<string, object>
// So it will fall back to checking properties on the Dictionary type itself
object obj = dictionary;
// Dictionary<string, object> has properties like Keys, Values, Count, etc.
Assert.True(obj.HasProperty("Keys"));
Assert.True(obj.HasProperty("Values"));
Assert.True(obj.HasProperty("Count"));
Assert.False(obj.HasProperty("Key1")); // These are dictionary entries, not properties
}
[Fact]
public void HasPropertyWithEmptyDictionaryTest()
{
var dictionary = new Dictionary<string, object>();
object obj = dictionary;
// Same as above - it checks Dictionary type properties, not dictionary contents
Assert.True(obj.HasProperty("Count")); // Dictionary has Count property
Assert.False(obj.HasProperty("AnyKey")); // But not custom keys
}
[Fact]
public void HasPropertyWithNullPropertyNameTest()
{
var obj = new TestClass();
// The current implementation throws ArgumentNullException when propertyName is null
// because Type.GetProperty(null) throws
Assert.Throws<ArgumentNullException>(() => obj.HasProperty(null));
}
[Fact]
public void HasPropertyWithEmptyPropertyNameTest()
{
var obj = new TestClass();
Assert.False(obj.HasProperty(""));
Assert.False(obj.HasProperty(string.Empty));
}
[Fact]
public void HasPropertyWithSystemObjectTest()
{
var obj = new object();
// Object class has standard properties like GetType, ToString, etc.
Assert.False(obj.HasProperty("GetType")); // GetType is a method, not a property
Assert.False(obj.HasProperty("SomeProperty"));
}
[Fact]
public void HasPropertyWithBuiltInTypesTest()
{
var str = "test string";
object obj = str;
Assert.True(obj.HasProperty("Length")); // String has Length property
Assert.False(obj.HasProperty("Size"));
var array = new int[] { 1, 2, 3 };
obj = array;
Assert.True(obj.HasProperty("Length")); // Array has Length property
Assert.False(obj.HasProperty("Count"));
}
[Fact]
public void HasPropertyWithAnonymousObjectTest()
{
var obj = new { Name = "Test", Age = 25, IsActive = true };
Assert.True(obj.HasProperty("Name"));
Assert.True(obj.HasProperty("Age"));
Assert.True(obj.HasProperty("IsActive"));
Assert.False(obj.HasProperty("Height"));
}
}
}