Skip to content

Commit 11894b7

Browse files
committed
Introduced unit tests for the new AttributeDictionary
The new `AttributeDictionary` (72ca032) offers a lightweight container for attribute key/value pairs alongside strongly typed convenience methods for checking if a value exists and, if it does, converting it to a type supported by `AttributeValueConverter`. This commit introduces unit tests for that scenario.
1 parent caddffd commit 11894b7

1 file changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
using OnTopic.Attributes;
7+
using Xunit;
8+
9+
namespace OnTopic.Tests {
10+
11+
/*============================================================================================================================
12+
| CLASS: ATTRIBUTE DICTIONARY TEST
13+
\---------------------------------------------------------------------------------------------------------------------------*/
14+
/// <summary>
15+
/// Provides unit tests for the <see cref="AttributeDictionary"/> class.
16+
/// </summary>
17+
[ExcludeFromCodeCoverage]
18+
public class AttributeDictionaryTest {
19+
20+
/*==========================================================================================================================
21+
| TEST: GET VALUE: RETURNS EXPECTED VALUE
22+
\-------------------------------------------------------------------------------------------------------------------------*/
23+
/// <summary>
24+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
25+
/// "AttributeDictionary.GetValue(String)"/> returns the <paramref name="expected"/> value.
26+
/// </summary>
27+
/// <param name="input">The value to add to the dictionary.</param>
28+
/// <param name="expected">The value expected to be returned.</param>
29+
[Theory]
30+
[InlineData(null, null)]
31+
[InlineData("", null)]
32+
[InlineData(" ", null)]
33+
[InlineData("0", "0")]
34+
[InlineData("True", "True")]
35+
[InlineData("Hello", "Hello")]
36+
public void GetValue_ReturnsExpectedValue(string? input, string? expected) {
37+
38+
var attributes = new AttributeDictionary() {{"Key", input}};
39+
40+
Assert.Equal(expected, attributes.GetValue("Key"));
41+
42+
}
43+
44+
/*==========================================================================================================================
45+
| TEST: GET BOOLEAN: RETURNS EXPECTED VALUE
46+
\-------------------------------------------------------------------------------------------------------------------------*/
47+
/// <summary>
48+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
49+
/// "AttributeDictionary.GetBoolean(String)"/> returns the <paramref name="expected"/> value.
50+
/// </summary>
51+
/// <param name="input">The value to add to the dictionary.</param>
52+
/// <param name="expected">The value expected to be returned.</param>
53+
[Theory]
54+
[InlineData("0", false)]
55+
[InlineData("1", true)]
56+
[InlineData("False", false)]
57+
[InlineData("True", true)]
58+
[InlineData("", null)]
59+
[InlineData("Hello", null)]
60+
public void GetBoolean_ReturnsExpectedValue(string input, bool? expected) {
61+
62+
var attributes = new AttributeDictionary() {{"Key", input}};
63+
64+
Assert.Equal(expected, attributes.GetBoolean("Key"));
65+
66+
}
67+
68+
/*==========================================================================================================================
69+
| TEST: GET INTEGER: RETURNS EXPECTED VALUE
70+
\-------------------------------------------------------------------------------------------------------------------------*/
71+
/// <summary>
72+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
73+
/// "AttributeDictionary.GetInteger(String)"/> returns the <paramref name="expected"/> value.
74+
/// </summary>
75+
/// <param name="input">The value to add to the dictionary.</param>
76+
/// <param name="expected">The value expected to be returned.</param>
77+
[Theory]
78+
[InlineData("0", 0)]
79+
[InlineData("1", 1)]
80+
[InlineData("2.4", null)]
81+
[InlineData("", null)]
82+
[InlineData("Hello", null)]
83+
public void GetInteger_ReturnsExpectedValue(string input, int? expected) {
84+
85+
var attributes = new AttributeDictionary() {{"Key", input}};
86+
87+
Assert.Equal(expected, attributes.GetInteger("Key"));
88+
89+
}
90+
91+
/*==========================================================================================================================
92+
| TEST: GET DOUBLE: RETURNS EXPECTED VALUE
93+
\-------------------------------------------------------------------------------------------------------------------------*/
94+
/// <summary>
95+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
96+
/// "AttributeDictionary.GetDouble(String)"/> returns the <paramref name="expected"/> value.
97+
/// </summary>
98+
/// <param name="input">The value to add to the dictionary.</param>
99+
/// <param name="expected">The value expected to be returned.</param>
100+
[Theory]
101+
[InlineData("0.0", 0.0)]
102+
[InlineData("1.0", 1.0)]
103+
[InlineData("1", 1.0)]
104+
[InlineData("1.4", 1.4)]
105+
[InlineData("", null)]
106+
[InlineData("Hello", null)]
107+
public void GetDouble_ReturnsExpectedValue(string input, double? expected) {
108+
109+
var attributes = new AttributeDictionary() {{"Key", input}};
110+
111+
Assert.Equal(expected, attributes.GetDouble("Key"));
112+
113+
}
114+
115+
/*==========================================================================================================================
116+
| TEST: GET DATE/TIME: RETURNS EXPECTED VALUE
117+
\-------------------------------------------------------------------------------------------------------------------------*/
118+
/// <summary>
119+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
120+
/// "AttributeDictionary.GetDateTime(String)"/> returns the expected value.
121+
/// </summary>
122+
/// <param name="input">The value to add to the dictionary.</param>
123+
/// <param name="isSet">Determines whether a valid <see cref="DateTime"/> is expected in response.</param>
124+
[Theory]
125+
[InlineData("1976-10-15 01:02:03", true)]
126+
[InlineData("October 15, 1976 01:02:03 AM", true)]
127+
[InlineData("15 Oct 1976 01:02:03", true)]
128+
[InlineData("10/15/1976 01:02:03 AM", true)]
129+
[InlineData("", false)]
130+
[InlineData("Hello", false)]
131+
public void GetDate_ReturnsExpectedValue(string input, bool isSet) {
132+
133+
var attributes = new AttributeDictionary() {{"Key", input}};
134+
135+
Assert.Equal(isSet? new DateTime(1976, 10, 15, 1, 2, 3) : null, attributes.GetDateTime("Key"));
136+
137+
}
138+
139+
/*==========================================================================================================================
140+
| TEST: GET URI: RETURNS EXPECTED VALUE
141+
\-------------------------------------------------------------------------------------------------------------------------*/
142+
/// <summary>
143+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
144+
/// "AttributeDictionary.GetUri(String)"/> returns the expected value.
145+
/// </summary>
146+
/// <param name="input">The value to add to the dictionary.</param>
147+
[Theory]
148+
[InlineData("https://www.github.com/OnTopicCMS")]
149+
[InlineData("Some:\\\\URL")]
150+
public void GetUri_ReturnsExpectedValue(string input) {
151+
152+
var attributes = new AttributeDictionary() {{"Key", input}};
153+
154+
Assert.Equal(new Uri(input), attributes.GetUri("Key"));
155+
156+
}
157+
158+
/*==========================================================================================================================
159+
| TEST: GET {TYPE}: INVALID KEY: RETURNS NULL
160+
\-------------------------------------------------------------------------------------------------------------------------*/
161+
/// <summary>
162+
/// Constructs a <see cref="AttributeDictionary"/> and confirms that each of the <see cref="AttributeDictionary.GetValue(
163+
/// String)"/> methods return <c>null</c> if an invalid key is passed.
164+
/// </summary>
165+
[Fact]
166+
public void GetType_InvalidKey_ReturnsNull() {
167+
168+
var attributes = new AttributeDictionary();
169+
170+
Assert.Null(attributes.GetValue("MissingKey"));
171+
Assert.Null(attributes.GetBoolean("MissingKey"));
172+
Assert.Null(attributes.GetInteger("MissingKey"));
173+
Assert.Null(attributes.GetDouble("MissingKey"));
174+
Assert.Null(attributes.GetDateTime("MissingKey"));
175+
Assert.Null(attributes.GetUri("MissingKey"));
176+
177+
}
178+
179+
} //Class
180+
} //Namespace

0 commit comments

Comments
 (0)