/
Describe_ValueGetter.cs
198 lines (175 loc) · 6.82 KB
/
Describe_ValueGetter.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using Moq;
using NUnit.Framework;
namespace Nustache.Core.Tests
{
[TestFixture]
public class Describe_ValueGetter
{
[Test]
public void It_returns_NoValue_when_it_cant_get_a_value()
{
Assert.AreSame(ValueGetter.NoValue, ValueGetter.GetValue(this, "x"));
}
[Test]
public void It_gets_field_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntField"));
}
[Test]
public void It_gets_case_insensitive_field_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "intfield"));
}
[Test]
public void It_gets_property_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntProperty"));
}
[Test]
public void It_gets_case_insensitive_property_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "intproperty"));
}
[Test]
public void It_gets_method_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntMethod"));
}
[Test]
public void It_gets_case_insensitive_method_values()
{
ReadWriteInts target = new ReadWriteInts();
target.IntField = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "intmethod"));
}
[Test]
public void It_cant_get_values_from_write_only_properties()
{
WriteOnlyInts target = new WriteOnlyInts();
Assert.AreSame(ValueGetter.NoValue, ValueGetter.GetValue(target, "IntProperty"));
}
[Test]
public void It_cant_get_values_from_write_only_methods()
{
WriteOnlyInts target = new WriteOnlyInts();
Assert.AreSame(ValueGetter.NoValue, ValueGetter.GetValue(target, "IntMethod"));
}
[Test]
public void It_gets_Hashtable_values()
{
Hashtable target = new Hashtable();
target["IntKey"] = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntKey"));
}
[Test]
public void It_gets_Dictionary_values()
{
Dictionary<string, int> target = new Dictionary<string, int>();
target["IntKey"] = 123;
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntKey"));
}
[Test]
public void It_gets_GenericDictionary_values()
{
var mock = new Mock<IDictionary<string, int>>();
mock.Setup(x => x.ContainsKey("Key")).Returns(true);
mock.Setup(x => x["Key"]).Returns(123);
IDictionary<string, int> target = mock.Object;
Assert.AreEqual(123, ValueGetter.GetValue(target, "Key"));
}
[Test]
public void It_gets_DataRowView_values()
{
DataTable dt = new DataTable();
dt.Columns.Add("IntColumn", typeof(int));
dt.Rows.Add(new object[] { 123 });
DataRowView target = dt.DefaultView[0];
Assert.AreEqual(123, ValueGetter.GetValue(target, "IntColumn"));
}
[Test]
public void It_gets_case_insensitive_DataRowView_values()
{
DataTable dt = new DataTable();
dt.Columns.Add("IntColumn", typeof(int));
dt.Rows.Add(new object[] { 123 });
DataRowView target = dt.DefaultView[0];
Assert.AreEqual(123, ValueGetter.GetValue(target, "intcolumn"));
}
[Test]
public void It_gets_XmlNode_attribute_values()
{
XmlDocument target = new XmlDocument();
target.LoadXml("<doc attr='val'></doc>");
Assert.AreEqual("val", ValueGetter.GetValue(target.DocumentElement, "@attr"));
}
[Test]
public void It_gets_XmlNode_single_child_element_value_as_a_string()
{
XmlDocument target = new XmlDocument();
target.LoadXml("<doc attr='val'><child>text</child></doc>");
var value= (string)ValueGetter.GetValue(target.DocumentElement, "child");
Assert.AreEqual("text", value);
}
[Test]
public void It_gets_XmlNode_multiple_child_element_values_as_a_list()
{
XmlDocument target = new XmlDocument();
target.LoadXml("<doc attr='val'><child>text1</child><child>text2</child></doc>");
XmlNodeList elements = (XmlNodeList)ValueGetter.GetValue(target.DocumentElement, "child");
Assert.AreEqual(2, elements.Count);
Assert.AreEqual("text1", elements[0].InnerText);
Assert.AreEqual("text2", elements[1].InnerText);
}
[Test]
public void It_gets_ListValueByIndex_values_from_array()
{
string[] target = new[] { "hello", "world" };
Assert.AreEqual("hello", ValueGetter.GetValue(target, "0"));
}
[Test]
public void It_gets_ListValueByIndex_values_from_List()
{
List<string> target = new List<string>() { "hello", "world" };
Assert.AreEqual("hello", ValueGetter.GetValue(target, "0"));
}
[Test]
public void It_fails_ListValueByIndex_values_from_List()
{
string[] target = new[] { "hello", "world" };
Assert.AreEqual(ValueGetter.NoValue, ValueGetter.GetValue(target, "2"));
}
public class ReadWriteInts
{
public int IntField = -1;
public int IntProperty { get { return IntField; } set { IntField = value; } }
public int IntMethod() { return IntField; }
public void IntMethod(int value) { IntField = value; }
}
public class ReadOnlyInts
{
public readonly int IntField = -1;
public int IntProperty { get { return IntField; } }
public int IntMethod() { return IntField; }
}
public class WriteOnlyInts
{
public int IntField; // Write only?
public int IntProperty { set { IntField = value; } }
public void IntMethod(int value) { IntField = value; }
}
}
}