Skip to content

Commit 148a1b1

Browse files
committed
Introduced new GetDouble() extension
The new `GetDouble()` extension method allows an `AttributeValue.Value` string to be returned as a `double`. This is convenient for storing doubles as attributes, as it provides the conversion. Includes unit tests to confirm functionality.
1 parent 13cffee commit 148a1b1

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

OnTopic.Tests/AttributeValueCollectionTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ public void GetInteger_IncorrectKey_ReturnsDefault() {
9494

9595
}
9696

97+
/*==========================================================================================================================
98+
| TEST: GET DOUBLE: INCORRECT VALUE: RETURNS DEFAULT
99+
\-------------------------------------------------------------------------------------------------------------------------*/
100+
/// <summary>
101+
/// Ensures that double values return the default.
102+
/// </summary>
103+
[TestMethod]
104+
public void GetDouble_IncorrectValue_ReturnsDefault() {
105+
106+
var topic = TopicFactory.Create("Test", "Container");
107+
108+
topic.Attributes.SetValue("Number3", "Invalid");
109+
110+
Assert.AreEqual<double>(5.0, topic.Attributes.GetDouble("Number3", 5.0));
111+
112+
}
113+
114+
/*==========================================================================================================================
115+
| TEST: GET DOUBLE: INCORRECT KEY: RETURNS DEFAULT
116+
\-------------------------------------------------------------------------------------------------------------------------*/
117+
/// <summary>
118+
/// Ensures that double key names return the default.
119+
/// </summary>
120+
[TestMethod]
121+
public void GetDouble_IncorrectKey_ReturnsDefault() {
122+
123+
var topic = TopicFactory.Create("Test", "Container");
124+
125+
Assert.AreEqual<double>(5.0, topic.Attributes.GetDouble("InvalidKey", 5.0));
126+
127+
}
128+
97129
/*==========================================================================================================================
98130
| TEST: GET DATETIME: CORRECT VALUE: IS RETURNED
99131
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Attributes/AttributeValueCollectionExtensions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,44 @@ out var result
9696
) ? result : defaultValue;
9797
}
9898

99+
/*==========================================================================================================================
100+
| METHOD: GET DOUBLE
101+
\-------------------------------------------------------------------------------------------------------------------------*/
102+
/// <summary>
103+
/// Gets a named attribute from the Attributes dictionary with a specified default value, an optional setting for enabling
104+
/// of inheritance, and an optional setting for searching through derived topics for values. Return as a double.
105+
/// </summary>
106+
/// <param name="attributes">The instance of the <see cref="AttributeValueCollection"/> this extension is bound to.</param>
107+
/// <param name="name">The string identifier for the <see cref="AttributeValue"/>.</param>
108+
/// <param name="defaultValue">A string value to which to fall back in the case the value is not found.</param>
109+
/// <param name="inheritFromParent">
110+
/// Boolean indicator nothing whether to search through the topic's parents in order to get the value.
111+
/// </param>
112+
/// <param name="inheritFromDerived">
113+
/// Boolean indicator nothing whether to search through any of the topic's <see cref="Topic.DerivedTopic"/> topics in
114+
/// order to get the value.
115+
/// </param>
116+
/// <returns>The value for the attribute as a double.</returns>
117+
public static double GetDouble(
118+
this AttributeValueCollection attributes,
119+
string name,
120+
double defaultValue,
121+
bool inheritFromParent = false,
122+
bool inheritFromDerived = true
123+
) {
124+
Contract.Requires(attributes);
125+
Contract.Requires<ArgumentNullException>(!String.IsNullOrWhiteSpace(name));
126+
return Double.TryParse(
127+
attributes.GetValue(
128+
name,
129+
defaultValue.ToString(CultureInfo.InvariantCulture),
130+
inheritFromParent,
131+
inheritFromDerived? 5 : 0
132+
),
133+
out var result
134+
) ? result : defaultValue;
135+
}
136+
99137
/*==========================================================================================================================
100138
| METHOD: GET DATETIME
101139
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)