Skip to content

Commit 817f723

Browse files
Dictionary default value: retarget net10.0, fix mis-wired tests, drop dynamic
- All three projects retargeted net7.0 -> net10.0. - Tests: four of six tests were named for MethodTryGetValue and MethodContainsKey but all six called MethodGetValueOrDefault, so two of the three methods had zero coverage while the suite reported 6/6 green. Each test now calls the method its name claims, and the reversed Assert.AreEqual(actual, expected) arguments are swapped to AreEqual(expected, actual) so a failure message tells the truth. - Methods: dynamic / dynamic? return types replaced with T?; MethodGetValueOrDefault takes string instead of string? (clears CS8620 and CS8603 on net10.0). - New MethodGetValueOrDefaultWithFallback covering the two-argument GetValueOrDefault(key, fallback) overload, with tests proving the fallback is returned for a missing key and that a stored value equal to the fallback is indistinguishable from a missing one. - New test for FirstOrDefault on a dictionary: the default KeyValuePair is a struct, not null. - Packages: BenchmarkDotNet 0.15.8; Microsoft.NET.Test.Sdk 18.9.0, MSTest 4.3.3, coverlet.collector 10.0.1. Removed the unused BenchmarkDotNet reference and the System.Collections 4.3.0 compatibility shim from the app project.
1 parent a59a6f6 commit 817f723

9 files changed

Lines changed: 72 additions & 27 deletions

File tree

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.Benchmark/DefaultValueFromDictionaryInCSharp.Benchmark.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
</Project>

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.csproj‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10-
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
12-
<PackageReference Include="System.Collections" Version="4.3.0" />
13-
</ItemGroup>
14-
1510
</Project>

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodContainsKey.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public static class MethodContainsKey
44
{
5-
public static dynamic GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
5+
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
66
{
77
return dictionary.ContainsKey(key) ? dictionary[key] : default;
88
}

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefault.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public static class MethodGetValueOrDefault
44
{
5-
public static dynamic GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string? key)
5+
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
66
{
77
return dictionary.GetValueOrDefault(key);
88
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback
2+
{
3+
public static class MethodGetValueOrDefaultWithFallback
4+
{
5+
public static T GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key, T fallback)
6+
{
7+
return dictionary.GetValueOrDefault(key, fallback);
8+
}
9+
}
10+
}

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodTryGetValue.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public static class MethodTryGetValue
44
{
5-
public static dynamic? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
5+
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
66
{
77
return dictionary.TryGetValue(key, out var value) ? value : default;
88
}

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Program.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
22
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
3+
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
34
using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;
45

56
var myDictionary = new Dictionary<string, int>
@@ -16,4 +17,6 @@
1617

1718
Console.WriteLine($"{MethodGetValueOrDefault.GetValueFromDictionary(myDictionary, key)}");
1819

20+
Console.WriteLine($"{MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(myDictionary, key, -1)}");
21+
1922
Console.ReadKey();

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/DefaultValueFromDictionaryUnitTests.cs‎

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
12
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
3+
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
4+
using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;
25

36
namespace Tests
47
{
@@ -17,7 +20,7 @@ public void GivenMethodGetValueOrDefault_WhenKeyNotExisting_ThenDefaultValueZero
1720
var key = "sam";
1821
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
1922

20-
Assert.AreEqual(value, 0);
23+
Assert.AreEqual(0, value);
2124
}
2225

2326
[TestMethod]
@@ -26,43 +29,77 @@ public void GivenMethodGetValueOrDefault_WhenKeyBob_ThenValueTwo()
2629
var key = "bob";
2730
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
2831

29-
Assert.AreEqual(value, 2);
32+
Assert.AreEqual(2, value);
3033
}
3134

3235
[TestMethod]
3336
public void GivenMethodTryGetValue_WhenKeyNotExisting_ThenDefaultValueZero()
3437
{
3538
var key = "sam";
36-
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
39+
var value = MethodTryGetValue.GetValueFromDictionary(_myDictionary, key);
3740

38-
Assert.AreEqual(value, 0);
41+
Assert.AreEqual(0, value);
3942
}
4043

4144
[TestMethod]
4245
public void GivenMethodTryGetValue_WhenKeyBob_ThenValueTwo()
4346
{
4447
var key = "bob";
45-
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
48+
var value = MethodTryGetValue.GetValueFromDictionary(_myDictionary, key);
4649

47-
Assert.AreEqual(value, 2);
50+
Assert.AreEqual(2, value);
4851
}
4952

5053
[TestMethod]
5154
public void GivenMethodContainsKey_WhenKeyNotExisting_ThenDefaultValueZero()
5255
{
5356
var key = "sam";
54-
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
57+
var value = MethodContainsKey.GetValueFromDictionary(_myDictionary, key);
5558

56-
Assert.AreEqual(value, 0);
59+
Assert.AreEqual(0, value);
5760
}
5861

5962
[TestMethod]
6063
public void GivenMethodContainsKey_WhenKeyBob_ThenValueTwo()
6164
{
6265
var key = "bob";
63-
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
66+
var value = MethodContainsKey.GetValueFromDictionary(_myDictionary, key);
67+
68+
Assert.AreEqual(2, value);
69+
}
70+
71+
[TestMethod]
72+
public void GivenMethodGetValueOrDefaultWithFallback_WhenKeyNotExisting_ThenFallbackValue()
73+
{
74+
var key = "sam";
75+
var value = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(_myDictionary, key, -1);
76+
77+
Assert.AreEqual(-1, value);
78+
}
79+
80+
[TestMethod]
81+
public void GivenMethodGetValueOrDefaultWithFallback_WhenStoredValueEqualsFallback_ThenSameResultAsMissingKey()
82+
{
83+
var dictionary = new Dictionary<string, int> { { "neg", -1 } };
84+
85+
var stored = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(dictionary, "neg", -1);
86+
var missing = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(dictionary, "gone", -1);
87+
88+
Assert.AreEqual(-1, stored);
89+
Assert.AreEqual(-1, missing);
90+
Assert.AreEqual(stored, missing);
91+
}
92+
93+
[TestMethod]
94+
public void GivenFirstOrDefault_WhenKeyNotExisting_ThenDefaultKeyValuePairNotNull()
95+
{
96+
var searchKey = "sam";
97+
98+
var pair = _myDictionary.FirstOrDefault(p => p.Key == searchKey);
6499

65-
Assert.AreEqual(value, 2);
100+
Assert.AreEqual(default(KeyValuePair<string, int>), pair);
101+
Assert.IsNull(pair.Key);
102+
Assert.AreEqual(0, pair.Value);
66103
}
67104
}
68-
}
105+
}

‎collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/Tests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
13-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
14-
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
15-
<PackageReference Include="coverlet.collector" Version="3.1.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
13+
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
14+
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
15+
<PackageReference Include="coverlet.collector" Version="10.0.1" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)