Skip to content

Commit ec18bd6

Browse files
committed
Added tests for CustomDataMap, HexadecimalFormatter, PathUtil and XElementSerializer.
1 parent f024d1a commit ec18bd6

File tree

9 files changed

+393
-42
lines changed

9 files changed

+393
-42
lines changed

ReClass.NET/Util/CustomDataMap.cs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,30 @@ public string this[string key]
4747
}
4848

4949
/// <summary>
50-
/// Sets a configuration item.
50+
/// Removes an item.
5151
/// </summary>
5252
/// <param name="key">The key of the item.</param>
53-
/// <param name="value">
54-
/// The value of the item.
55-
/// If the value is null the item gets removed.
56-
/// </param>
53+
public void RemoveValue(string key)
54+
{
55+
ValidateKey(key);
56+
57+
data.Remove(key);
58+
}
59+
60+
/// <summary>
61+
/// Sets the string value of an item.
62+
/// </summary>
63+
/// <param name="key">The key of the item.</param>
64+
/// <param name="value">The value of the item.</param>
5765
public void SetString(string key, string value)
5866
{
59-
if (key == null)
60-
{
61-
throw new ArgumentNullException(nameof(key));
62-
}
63-
if (key.Length == 0)
64-
{
65-
throw new ArgumentException();
66-
}
67+
ValidateKey(key);
6768

68-
if (value == null)
69-
{
70-
data.Remove(key);
71-
}
72-
else
73-
{
74-
data[key] = value;
75-
}
69+
data[key] = value;
7670
}
7771

7872
/// <summary>
79-
/// Sets a configuration item.
73+
/// Sets the boolean value of an item.
8074
/// </summary>
8175
/// <param name="key">The key of the item.</param>
8276
/// <param name="value">The value of the item.</param>
@@ -86,7 +80,7 @@ public void SetBool(string key, bool value)
8680
}
8781

8882
/// <summary>
89-
/// Sets a configuration item.
83+
/// Sets the long value of an item.
9084
/// </summary>
9185
/// <param name="key">The key of the item.</param>
9286
/// <param name="value">The value of the item.</param>
@@ -96,7 +90,7 @@ public void SetLong(string key, long value)
9690
}
9791

9892
/// <summary>
99-
/// Sets a configuration item.
93+
/// Sets the ulong value of an item.
10094
/// </summary>
10195
/// <param name="key">The key of the item.</param>
10296
/// <param name="value">The value of the item.</param>
@@ -105,13 +99,18 @@ public void SetULong(string key, ulong value)
10599
SetString(key, value.ToString(NumberFormatInfo.InvariantInfo));
106100
}
107101

102+
/// <summary>
103+
/// Sets the XElement value of an item.
104+
/// </summary>
105+
/// <param name="key">The key of the item.</param>
106+
/// <param name="value">The value of the item.</param>
108107
public void SetXElement(string key, XElement value)
109108
{
110-
SetString(key, value.ToString());
109+
SetString(key, value?.ToString());
111110
}
112111

113112
/// <summary>
114-
/// Gets the value of the config item.
113+
/// Gets the string value of the item.
115114
/// </summary>
116115
/// <param name="key">The key of the item.</param>
117116
/// <returns>The value of the config item or null if the key does not exists.</returns>
@@ -121,21 +120,14 @@ public string GetString(string key)
121120
}
122121

123122
/// <summary>
124-
/// Gets the value of the config item.
123+
/// Gets the string value of the item.
125124
/// </summary>
126125
/// <param name="key">The key of the item.</param>
127126
/// <param name="def">The default value if the key does not exists.</param>
128127
/// <returns>The value of the config item or <paramref name="def"/> if the key does not exists.</returns>
129128
public string GetString(string key, string def)
130129
{
131-
if (key == null)
132-
{
133-
throw new ArgumentNullException(nameof(key));
134-
}
135-
if (key.Length == 0)
136-
{
137-
throw new ArgumentException();
138-
}
130+
ValidateKey(key);
139131

140132
if (data.TryGetValue(key, out var value))
141133
{
@@ -146,7 +138,7 @@ public string GetString(string key, string def)
146138
}
147139

148140
/// <summary>
149-
/// Gets the value of the config item.
141+
/// Gets the boolean value of the item.
150142
/// </summary>
151143
/// <param name="key">The key of the item.</param>
152144
/// <param name="def">The default value if the key does not exists.</param>
@@ -163,7 +155,7 @@ public bool GetBool(string key, bool def)
163155
}
164156

165157
/// <summary>
166-
/// Gets the value of the config item.
158+
/// Gets the long value of the item.
167159
/// </summary>
168160
/// <param name="key">The key of the item.</param>
169161
/// <param name="def">The default value if the key does not exists.</param>
@@ -185,7 +177,7 @@ public long GetLong(string key, long def)
185177
}
186178

187179
/// <summary>
188-
/// Gets the value of the config item.
180+
/// Gets the ulong value of the item.
189181
/// </summary>
190182
/// <param name="key">The key of the item.</param>
191183
/// <param name="def">The default value if the key does not exists.</param>
@@ -207,7 +199,7 @@ public ulong GetULong(string key, ulong def)
207199
}
208200

209201
/// <summary>
210-
/// Gets the value of the config item.
202+
/// Gets the XElement value of the item.
211203
/// </summary>
212204
/// <param name="key">The key of the item.</param>
213205
/// <param name="def">The default value if the key does not exists.</param>
@@ -222,5 +214,17 @@ public XElement GetXElement(string key, XElement def)
222214

223215
return XElement.Parse(str);
224216
}
217+
218+
/// <summary>
219+
/// Validates the given key.
220+
/// </summary>
221+
/// <param name="key">The key of an item.</param>
222+
private static void ValidateKey(string key)
223+
{
224+
if (key == null)
225+
{
226+
throw new ArgumentNullException(nameof(key));
227+
}
228+
}
225229
}
226230
}

ReClass.NET/Util/NumberFormat.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33

44
namespace ReClassNET.Util
55
{
6-
public class NumberFormat
6+
public static class NumberFormat
77
{
88
public static NumberFormatInfo GuessNumberFormat(string input)
99
{
1010
Contract.Requires(input != null);
1111
Contract.Ensures(Contract.Result<NumberFormatInfo>() != null);
1212

13-
if (input.Contains(",") && !input.Contains("."))
13+
var commaIndex = input.IndexOf(',');
14+
var dotIndex = input.IndexOf('.');
15+
16+
if (commaIndex > dotIndex)
1417
{
1518
return new NumberFormatInfo
1619
{
1720
NumberDecimalSeparator = ",",
1821
NumberGroupSeparator = "."
1922
};
2023
}
24+
2125
return new NumberFormatInfo
2226
{
2327
NumberDecimalSeparator = ".",

ReClass.NET/Util/PathUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static string FileUrlToPath(string url)
8989

9090
if (url.StartsWith("file:///", StringComparison.OrdinalIgnoreCase))
9191
{
92-
url = url.Substring(8, url.Length - 8);
92+
url = url.Substring(8);
9393
}
9494

9595
url = url.Replace('/', Path.DirectorySeparatorChar);

ReClass.NET_Tests/ReClass.NET_Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
<ItemGroup>
5959
<Reference Include="System" />
6060
<Reference Include="System.Core" />
61+
<Reference Include="System.Drawing" />
62+
<Reference Include="System.Xml.Linq" />
6163
</ItemGroup>
6264
<ItemGroup>
6365
<Compile Include="AddressParser\DynamicCompilerTest.cs" />
@@ -68,8 +70,13 @@
6870
<Compile Include="Properties\AssemblyInfo.cs" />
6971
<Compile Include="Util\CircularBufferTest.cs" />
7072
<Compile Include="Util\CommandLineArgsTest.cs" />
73+
<Compile Include="Util\CustomDataMapTest.cs" />
7174
<Compile Include="Util\DirectedGraphTest.cs" />
7275
<Compile Include="Util\GrowingListTest.cs" />
76+
<Compile Include="Util\HexadecimalFormatterTest.cs" />
77+
<Compile Include="Util\NumberFormatTest.cs" />
78+
<Compile Include="Util\PathUtilTest.cs" />
79+
<Compile Include="Util\XElementSerializerTest.cs" />
7380
</ItemGroup>
7481
<ItemGroup>
7582
<ProjectReference Include="..\ReClass.NET\ReClass.NET.csproj">
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Xml.Linq;
5+
using NFluent;
6+
using ReClassNET.Util;
7+
using Xunit;
8+
9+
namespace ReClass.NET_Tests.Util
10+
{
11+
public class CustomDataMapTest
12+
{
13+
[Fact]
14+
public void TestNullKeyNotAllowed()
15+
{
16+
var sut = new CustomDataMap();
17+
18+
Check.ThatCode(() => sut.SetString(null, default)).Throws<ArgumentNullException>();
19+
Check.ThatCode(() => sut.SetBool(null, default)).Throws<ArgumentNullException>();
20+
Check.ThatCode(() => sut.SetLong(null, default)).Throws<ArgumentNullException>();
21+
Check.ThatCode(() => sut.SetULong(null, default)).Throws<ArgumentNullException>();
22+
Check.ThatCode(() => sut.SetXElement(null, default)).Throws<ArgumentNullException>();
23+
Check.ThatCode(() => sut.GetString(null)).Throws<ArgumentNullException>();
24+
Check.ThatCode(() => sut.GetBool(null, default)).Throws<ArgumentNullException>();
25+
Check.ThatCode(() => sut.GetLong(null, default)).Throws<ArgumentNullException>();
26+
Check.ThatCode(() => sut.GetULong(null, default)).Throws<ArgumentNullException>();
27+
Check.ThatCode(() => sut.GetXElement(null, default)).Throws<ArgumentNullException>();
28+
}
29+
30+
[Theory]
31+
[InlineData("key", "")]
32+
[InlineData("key", "value")]
33+
public void TestSetGetString(string key, string value)
34+
{
35+
var sut = new CustomDataMap();
36+
37+
sut.SetString(key, value);
38+
39+
Check.That(sut.GetString(key)).IsEqualTo(value);
40+
}
41+
42+
[Theory]
43+
[InlineData("key", "")]
44+
[InlineData("key", "value")]
45+
public void TestIndexString(string key, string value)
46+
{
47+
var sut = new CustomDataMap();
48+
49+
sut.SetString(key, value);
50+
51+
Check.That(sut[key]).IsEqualTo(value);
52+
}
53+
54+
[Fact]
55+
public void TestItemEnumeration()
56+
{
57+
var data = new Dictionary<string, string>
58+
{
59+
{ "key1", "value1" },
60+
{ "key2", "value2" },
61+
{ "key3", "value3" }
62+
};
63+
64+
var sut = new CustomDataMap();
65+
66+
foreach (var kv in data)
67+
{
68+
sut.SetString(kv.Key, kv.Value);
69+
}
70+
71+
Check.That(sut).IsEquivalentTo(data);
72+
}
73+
74+
[Fact]
75+
public void TestRemoveItem()
76+
{
77+
const string KeyToRemove = "key2";
78+
79+
var data = new Dictionary<string, string>
80+
{
81+
{ "key1", "value1" },
82+
{ KeyToRemove, "value2" },
83+
{ "key3", "value3" }
84+
};
85+
86+
var sut = new CustomDataMap();
87+
88+
foreach (var kv in data)
89+
{
90+
sut.SetString(kv.Key, kv.Value);
91+
}
92+
93+
sut.RemoveValue(KeyToRemove);
94+
95+
Check.That(sut).IsEquivalentTo(data.Where(kv => kv.Key != KeyToRemove));
96+
}
97+
98+
[Theory]
99+
[InlineData("key", true)]
100+
[InlineData("key", false)]
101+
public void TestSetGetBool(string key, bool value)
102+
{
103+
var sut = new CustomDataMap();
104+
105+
sut.SetBool(key, value);
106+
107+
Check.That(sut.GetBool(key, !value)).IsEqualTo(value);
108+
}
109+
110+
[Theory]
111+
[InlineData("key", -1)]
112+
[InlineData("key", 0)]
113+
[InlineData("key", 1)]
114+
[InlineData("key", long.MaxValue)]
115+
[InlineData("key", long.MinValue)]
116+
public void TestSetGetLong(string key, long value)
117+
{
118+
var sut = new CustomDataMap();
119+
120+
sut.SetLong(key, value);
121+
122+
Check.That(sut.GetLong(key, 0)).IsEqualTo(value);
123+
}
124+
125+
[Theory]
126+
[InlineData("key", 0)]
127+
[InlineData("key", 1)]
128+
[InlineData("key", ulong.MaxValue)]
129+
[InlineData("key", ulong.MinValue)]
130+
public void TestSetGetULong(string key, ulong value)
131+
{
132+
var sut = new CustomDataMap();
133+
134+
sut.SetULong(key, value);
135+
136+
Check.That(sut.GetULong(key, 0)).IsEqualTo(value);
137+
}
138+
139+
public static IEnumerable<object[]> GetTestSetGetXElementData() => new List<object[]>
140+
{
141+
new object[] { "key", null },
142+
new object[] { "key", new XElement("name") },
143+
new object[] { "key", new XElement("name", new XAttribute("attr", "test")) },
144+
new object[] { "key", new XElement("name", new XElement("value", "test")) }
145+
};
146+
147+
[Theory]
148+
[MemberData(nameof(GetTestSetGetXElementData))]
149+
public void TestSetGetXElement(string key, XElement value)
150+
{
151+
var sut = new CustomDataMap();
152+
153+
sut.SetXElement(key, value);
154+
155+
Check.That(XNode.DeepEquals(sut.GetXElement(key, null), value)).IsTrue();
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)