Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions csharp-linq/ToDictionaryMethodInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ private static readonly Dictionary<string, Country> _expectedCountries
{ "Egypt", new("Egypt", "Cairo", "Africa") },
{ "India", new("India", "New Delhi", "Asia") },
{ "Chile", new("Chile", "Santiago", "South America") },
{ "Australia", new("Australia", "Canberra", "Oceania") }
{ "Australia", new("Australia", "Canberra", "Oceania") },
{ "Kenya", new("Kenya", "Nairobi", "Africa") }
};

private static readonly Dictionary<string, string> _expectedCountriesandCapitals
Expand All @@ -19,7 +20,8 @@ private static readonly Dictionary<string, string> _expectedCountriesandCapitals
{ "Egypt", "Cairo" },
{ "India", "New Delhi" },
{ "Chile", "Santiago" },
{ "Australia", "Canberra" }
{ "Australia", "Canberra" },
{ "Kenya", "Nairobi" }
};

[Fact]
Expand All @@ -37,4 +39,57 @@ public void WhenCallToDictionaryWithKeysAndValuesIsCalled_ThenReturnsDictionary(

Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key)));
}

[Fact]
public void WhenCallToDictionaryOnKeyValuePairsIsCalled_ThenReturnsDictionary()
{
var result = ToDictionaryOperations.CallToDictionaryOnKeyValuePairs();

Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key)));
}

[Fact]
public void WhenCallToDictionaryOnTuplesIsCalled_ThenReturnsDictionary()
{
var result = ToDictionaryOperations.CallToDictionaryOnTuples();

Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key)));
}

[Fact]
public void WhenCallToDictionaryWithComparerIsCalled_ThenLookupIgnoresCase()
{
var result = ToDictionaryOperations.CallToDictionaryWithComparer();

Assert.Same(StringComparer.OrdinalIgnoreCase, result.Comparer);
Assert.Equal("Cairo", result["egypt"].Capital);
}

[Fact]
public void WhenCallToDictionaryWithDuplicateKeysIsCalled_ThenThrowsArgumentException()
{
var exception = Assert.Throws<ArgumentException>(
() => ToDictionaryOperations.CallToDictionaryWithDuplicateKeys());

Assert.Equal("An item with the same key has already been added. Key: Africa", exception.Message);
}

[Fact]
public void WhenCallToDictionaryKeepingFirstPerContinentIsCalled_ThenKeepsFirstCountry()
{
var result = ToDictionaryOperations.CallToDictionaryKeepingFirstPerContinent();

Assert.Equal(4, result.Count);
Assert.Equal("Egypt", result["Africa"].Name);
}

[Fact]
public void WhenCallToLookupOnContinentsIsCalled_ThenGroupsCountriesPerContinent()
{
var result = ToDictionaryOperations.CallToLookupOnContinents();

Assert.Equal(4, result.Count);
Assert.Equal(2, result["Africa"].Count());
Assert.Empty(result["Antarctica"]);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,38 @@ private static readonly List<Country> _countries
new("Egypt", "Cairo", "Africa"),
new("India", "New Delhi", "Asia"),
new("Chile", "Santiago", "South America"),
new("Australia", "Canberra", "Oceania")
new("Australia", "Canberra", "Oceania"),
new("Kenya", "Nairobi", "Africa")
];

public static Dictionary<string, Country> CallToDictionaryWithKeysOnly()
=> _countries.ToDictionary(c => c.Name);

public static Dictionary<string, string> CallToDictionaryWithKeysAndValues()
=> _countries.ToDictionary(c => c.Name, c => c.Capital);

public static Dictionary<string, string> CallToDictionaryOnKeyValuePairs()
=> _countries
.Select(c => new KeyValuePair<string, string>(c.Name, c.Capital))
.ToDictionary();

public static Dictionary<string, string> CallToDictionaryOnTuples()
=> _countries
.Select(c => (c.Name, c.Capital))
.ToDictionary();

public static Dictionary<string, Country> CallToDictionaryWithComparer()
=> _countries.ToDictionary(c => c.Name, StringComparer.OrdinalIgnoreCase);

// Throws: two countries share the continent "Africa"
public static Dictionary<string, Country> CallToDictionaryWithDuplicateKeys()
=> _countries.ToDictionary(c => c.Continent);

public static Dictionary<string, Country> CallToDictionaryKeepingFirstPerContinent()
=> _countries
.DistinctBy(c => c.Continent)
.ToDictionary(c => c.Continent);

public static ILookup<string, Country> CallToLookupOnContinents()
=> _countries.ToLookup(c => c.Continent);
}
Loading