Skip to content

Commit c8237de

Browse files
committed
Fixed issue with "GetDouble" method trying to parse to "long" instead of "double"
+ added some unit tests
1 parent b18f7ab commit c8237de

2 files changed

Lines changed: 113 additions & 1 deletion

File tree

src/Skybrud.Essentials.AspNetCore/QueryStringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public static List<float> GetFloatList(this IQueryCollection? query, string key)
414414
/// <returns>The converted <see cref="double"/> value if a matching query string component is found and the
415415
/// conversion is successful; otherwise <c>0</c>.</returns>
416416
public static double GetDouble(this IQueryCollection? query, string key) {
417-
return query == null ? 0 : query[key].ToInt64();
417+
return query == null ? 0 : query[key].ToDouble();
418418
}
419419

420420
/// <summary>

src/TestProject1/QueryStringTests.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ public class QueryStringTests {
1111

1212
#region Int32
1313

14+
[TestMethod]
15+
public void GetInt32() {
16+
17+
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
18+
{"id", new StringValues("1")},
19+
{"ids", new StringValues(new []{"1", "2", "3"})},
20+
{"moreIds", new StringValues(new []{"5,6", "7", "8"})}
21+
});
22+
23+
int id = query.GetInt32("id");
24+
Assert.AreEqual(1, id, "#1");
25+
26+
int id2 = query.GetInt32("id2");
27+
Assert.AreEqual(0, id2, "#2");
28+
29+
int id3 = query.GetInt32("id2", 2);
30+
Assert.AreEqual(2, id3, "#3");
31+
32+
int? id4 = query.GetInt32OrNull("id3");
33+
Assert.IsNull(id4, "#4");
34+
35+
int id5 = query.GetInt32("ids");
36+
Assert.AreEqual(1, id5, "#5");
37+
38+
int id6 = query.GetInt32("moreIds");
39+
Assert.AreEqual(0, id6, "#6");
40+
41+
}
42+
1443
[TestMethod]
1544
public void GetInt32Array() {
1645

@@ -49,6 +78,35 @@ public void GetInt32List() {
4978

5079
#region Int64
5180

81+
[TestMethod]
82+
public void GetInt64() {
83+
84+
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
85+
{"id", new StringValues("1")},
86+
{"ids", new StringValues(new []{"1", "2", "3"})},
87+
{"moreIds", new StringValues(new []{"5,6", "7", "8"})}
88+
});
89+
90+
long id = query.GetInt64("id");
91+
Assert.AreEqual(1, id, "#1");
92+
93+
long id2 = query.GetInt64("id2");
94+
Assert.AreEqual(0, id2, "#2");
95+
96+
long id3 = query.GetInt64("id2", 2);
97+
Assert.AreEqual(2, id3, "#3");
98+
99+
long? id4 = query.GetInt64OrNull("id3");
100+
Assert.IsNull(id4, "#4");
101+
102+
long id5 = query.GetInt64("ids");
103+
Assert.AreEqual(1, id5, "#5");
104+
105+
long id6 = query.GetInt64("moreIds");
106+
Assert.AreEqual(0, id6, "#6");
107+
108+
}
109+
52110
[TestMethod]
53111
public void GetInt64Array() {
54112

@@ -87,6 +145,33 @@ public void GetInt64List() {
87145

88146
#region Float / Single
89147

148+
[TestMethod]
149+
public void GetFloat() {
150+
151+
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
152+
{"pi", new StringValues("3.14")},
153+
{"values", new StringValues(new []{"3.14", "6.28", "9.42"})},
154+
{"otherValues", new StringValues(new []{"3.14,6.28", "9.42"})}
155+
});
156+
157+
// Returns "3.14"
158+
float pi = query.GetFloat("pi");
159+
Assert.AreEqual("3.14", pi.ToString("F2", CultureInfo.InvariantCulture), "#1");
160+
161+
// Returns "0.00" (implicit fallback)
162+
float meh = query.GetFloat("meh");
163+
Assert.AreEqual("0.00", meh.ToString("F2", CultureInfo.InvariantCulture), "#2");
164+
165+
// Returns "1.23" (explicit fallback)
166+
float meh2 = query.GetFloat("meh", 1.23f);
167+
Assert.AreEqual("1.23", meh2.ToString("F2", CultureInfo.InvariantCulture), "#3");
168+
169+
// Returns "null"
170+
float? meh3 = query.GetFloatOrNull("meh");
171+
Assert.IsNull(meh3, "#4");
172+
173+
}
174+
90175
[TestMethod]
91176
public void GetFloatArray() {
92177

@@ -125,6 +210,33 @@ public void GetFLoatList() {
125210

126211
#region Double
127212

213+
[TestMethod]
214+
public void GetDouble() {
215+
216+
IQueryCollection query = new QueryCollection(new Dictionary<string, StringValues> {
217+
{"pi", new StringValues("3.1415926535")},
218+
{"values", new StringValues(new []{"3.1415926535", "6.283185307", "9.4247779605"})},
219+
{"otherValues", new StringValues(new []{"3.1415926535,6.283185307", "9.4247779605"})}
220+
});
221+
222+
// Returns "3.1415926535"
223+
double pi = query.GetDouble("pi");
224+
Assert.AreEqual("3.1415926535", pi.ToString("F10", CultureInfo.InvariantCulture), "#1");
225+
226+
// Returns "0.000" (implicit fallback)
227+
double meh = query.GetDouble("meh");
228+
Assert.AreEqual("0.000", meh.ToString("F3", CultureInfo.InvariantCulture), "#2");
229+
230+
// Returns "1.230" (explicit fallback)
231+
double meh2 = query.GetDouble("meh", 1.23f);
232+
Assert.AreEqual("1.230", meh2.ToString("F3", CultureInfo.InvariantCulture), "#3");
233+
234+
// Returns "null"
235+
double? meh3 = query.GetDoubleOrNull("meh");
236+
Assert.IsNull(meh3, "#4");
237+
238+
}
239+
128240
[TestMethod]
129241
public void GetDoubleArray() {
130242

0 commit comments

Comments
 (0)