added UT sample

pull/1/head
Marc CHEVALDONNE 2 years ago
parent a938b840cc
commit cc7605b105

@ -579,6 +579,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_042_015_OneToMany_conven
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_042_016_OneToMany_FluentAPI", "p08_BDD_EntityFramework\ex_042_016_OneToMany_FluentAPI\ex_042_016_OneToMany_FluentAPI.csproj", "{9C2D4443-635F-4BA7-9870-9705BAD534EF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "chap026_UnitTests", "chap026_UnitTests", "{53D9CC81-18E0-482C-953D-F2ED8B0C472D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ex_026_000_ClassLibrary", "p05_More_dotNet\ex_026_000_ClassLibrary\ex_026_000_ClassLibrary.csproj", "{A7B99201-30D6-498A-AC40-43662373D457}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ex_026_001_xUnit_Fact", "p05_More_dotNet\ex_026_001_xUnit_Fact\ex_026_001_xUnit_Fact.csproj", "{6BDA111C-227C-4F61-A466-EE7D5901B1BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -4775,6 +4781,46 @@ Global
{9C2D4443-635F-4BA7-9870-9705BAD534EF}.Release|x64.Build.0 = Release|Any CPU
{9C2D4443-635F-4BA7-9870-9705BAD534EF}.Release|x86.ActiveCfg = Release|Any CPU
{9C2D4443-635F-4BA7-9870-9705BAD534EF}.Release|x86.Build.0 = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|ARM.ActiveCfg = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|ARM.Build.0 = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|ARM64.Build.0 = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|x64.ActiveCfg = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|x64.Build.0 = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|x86.ActiveCfg = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Debug|x86.Build.0 = Debug|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|Any CPU.Build.0 = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|ARM.ActiveCfg = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|ARM.Build.0 = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|ARM64.ActiveCfg = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|ARM64.Build.0 = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|x64.ActiveCfg = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|x64.Build.0 = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|x86.ActiveCfg = Release|Any CPU
{A7B99201-30D6-498A-AC40-43662373D457}.Release|x86.Build.0 = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|ARM.ActiveCfg = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|ARM.Build.0 = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|ARM64.Build.0 = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|x64.ActiveCfg = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|x64.Build.0 = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|x86.ActiveCfg = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Debug|x86.Build.0 = Debug|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|Any CPU.Build.0 = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|ARM.ActiveCfg = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|ARM.Build.0 = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|ARM64.ActiveCfg = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|ARM64.Build.0 = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|x64.ActiveCfg = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|x64.Build.0 = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|x86.ActiveCfg = Release|Any CPU
{6BDA111C-227C-4F61-A466-EE7D5901B1BF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -5047,6 +5093,9 @@ Global
{BE5CC150-CBCA-4E5F-BC7B-4F4A8E93380C} = {C7672736-AA73-4B7E-B5C6-A7A984216372}
{C85BE497-5BA9-4D7E-93EF-8F44E06DFDBC} = {C7672736-AA73-4B7E-B5C6-A7A984216372}
{9C2D4443-635F-4BA7-9870-9705BAD534EF} = {C7672736-AA73-4B7E-B5C6-A7A984216372}
{53D9CC81-18E0-482C-953D-F2ED8B0C472D} = {FBF49939-ACAD-4B71-AC29-14102ED6DB45}
{A7B99201-30D6-498A-AC40-43662373D457} = {53D9CC81-18E0-482C-953D-F2ED8B0C472D}
{6BDA111C-227C-4F61-A466-EE7D5901B1BF} = {53D9CC81-18E0-482C-953D-F2ED8B0C472D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D31C3AE-36FF-4667-A2A7-0E670245A59E}

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace ex_026_000_ClassLibrary
{
public class Album
{
public long Id { get; private set; }
public string Title { get; private set; }
public DateTime Date { get; private set; }
public ReadOnlyCollection<Artist> Artists { get; private set; }
private List<Artist> artists = new List<Artist>();
public Album(long id, string title, DateTime date, params Artist[] artists)
{
Id = id;
Title = title;
Date = date;
Artists = new ReadOnlyCollection<Artist>(this.artists);
AddArtists(artists);
}
public Album(string title, DateTime date, params Artist[] artists)
: this(0, title, date, artists) {}
public Album(string title, params Artist[] artists)
: this(0, title, DateTime.Now, artists) {}
public bool AddArtist(Artist artist)
{
if (artists.Contains(artist))
{
return false;
}
artists.Add(artist);
return true;
}
public IEnumerable<Artist> AddArtists(params Artist[] artists)
{
List<Artist> result = new();
foreach(var a in artists)
{
if(AddArtist(a))
{
result.Add(a);
}
}
return result;
}
public bool RemoveArtist(Artist artist)
=> artists.Remove(artist);
public void ClearArtists()
=> artists.Clear();
}
}

@ -0,0 +1,64 @@
using System;
namespace ex_026_000_ClassLibrary
{
public class Artist : IEquatable<Artist>
{
public string FirstName
{
get => firstname;
private set
{
firstname = value;
if(string.IsNullOrWhiteSpace(firstname))
{
firstname = "";
}
}
}
private string firstname;
public string LastName
{
get => lastname;
private set
{
lastname = value;
if(string.IsNullOrWhiteSpace(lastname))
{
lastname = "";
}
}
}
private string lastname;
public Artist(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
if(string.IsNullOrEmpty(FirstName) && string.IsNullOrEmpty(LastName))
{
throw new ArgumentException("Artist should have at least a first name or a last name");
}
}
public bool Equals(Artist other)
{
return FirstName.Equals(other.FirstName)
&& LastName.Equals(other.LastName);
}
public override bool Equals(object obj)
{
if(ReferenceEquals(obj, null)) return false;
if(ReferenceEquals(obj, this)) return true;
if(GetType() != obj.GetType()) return false;
return Equals(obj as Artist);
}
public override int GetHashCode()
{
return LastName.GetHashCode();
}
}
}

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="ex_026_001_xUnit_Fact\UnitTest1.cs" />
<Compile Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.GlobalUsings.g.cs" />
<Compile Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\.NETCoreApp,Version=v6.0.AssemblyAttributes.cs" />
<Compile Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.csproj.AssemblyReference.cache" />
<None Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.GeneratedMSBuildEditorConfig.editorconfig" />
<None Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.AssemblyInfoInputs.cache" />
<None Remove="ex_026_001_xUnit_Fact\obj\ex_026_001_xUnit_Fact.csproj.nuget.g.props" />
<None Remove="ex_026_001_xUnit_Fact\obj\ex_026_001_xUnit_Fact.csproj.nuget.g.targets" />
<None Remove="ex_026_001_xUnit_Fact\obj\project.assets.json" />
<None Remove="ex_026_001_xUnit_Fact\obj\project.nuget.cache" />
<None Remove="ex_026_001_xUnit_Fact\obj\ex_026_001_xUnit_Fact.csproj.nuget.dgspec.json" />
<None Remove="ex_026_001_xUnit_Fact\obj\Debug\net6.0\ex_026_001_xUnit_Fact.assets.cache" />
</ItemGroup>
</Project>

@ -0,0 +1,61 @@
using System;
using ex_026_000_ClassLibrary;
using Xunit;
namespace ex_026_001_xUnit_Fact
{
public class Album_UT
{
public static IEnumerable<object[]> Data_AddArtistToAlbum()
{
yield return new object[]
{
true,
new Artist[]
{
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter"),
new Artist("Tony", "Williams")
},
new Album("Miles Smiles", new DateTime(1967, 1, 1),
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter")),
new Artist("Tony", "Williams")
};
}
[Theory]
[MemberData(nameof(Data_AddArtistToAlbum))]
public void Test_AddArtistToAlbum(bool expectedResult,
IEnumerable<Artist> expectedArtists,
Album album,
Artist artistToAdd)
{
bool result = album.AddArtist(artistToAdd);
Assert.Equal(expectedResult, result);
Assert.Equal(expectedArtists.Count(), album.Artists.Count());
Assert.All(expectedArtists, a => album.Artists.Contains(a));
}
[Theory]
[MemberData(nameof(TestData.Data_AddArtistsToAlbum), MemberType=typeof(TestData))]
public void Test_AddArtistsToAlbum(int expectedResult,
IEnumerable<Artist> expectedArtists,
IEnumerable<Artist> expectedAddedArtists,
Album album,
params Artist[] artistsToAdd)
{
var addedArtists = album.AddArtists(artistsToAdd);
Assert.Equal(expectedResult, addedArtists.Count());
Assert.All(expectedAddedArtists, a => addedArtists.Contains(a));
Assert.Equal(expectedArtists.Count(), album.Artists.Count());
Assert.All(expectedArtists, a => album.Artists.Contains(a));
}
}
}

@ -0,0 +1,38 @@
using System;
using ex_026_000_ClassLibrary;
using Xunit;
namespace ex_026_001_xUnit_Fact
{
public class Artist_InlineData_UT
{
[Theory]
[InlineData(true, "Neil", "Young", "Neil", "Young")]
[InlineData(false, "", "", "", "")]
[InlineData(false, "", "", "", " ")]
[InlineData(false, "", "", " ", "")]
[InlineData(false, "", "", "", null)]
[InlineData(false, "", "", null, null)]
[InlineData(false, "", "", " ", null)]
[InlineData(true, "", "Young", " ", "Young")]
[InlineData(true, "", "Young", "", "Young")]
[InlineData(true, "", "Young", null, "Young")]
[InlineData(true, "Neil", "", "Neil", "")]
[InlineData(true, "Neil", "", "Neil", null)]
[InlineData(true, "Neil", "", "Neil", " ")]
public void TestConstructor(bool isValid, string expectedFirstName, string expectedLastName,
string firstname, string lastname)
{
if(!isValid)
{
Assert.Throws<ArgumentException>(
() => new Artist(firstname, lastname));
return;
}
Artist a = new Artist(firstname, lastname);
Assert.Equal(expectedFirstName, a.FirstName);
Assert.Equal(expectedLastName, a.LastName);
}
}
}

@ -0,0 +1,24 @@
using Xunit;
using System;
using ex_026_000_ClassLibrary;
namespace ex_026_001_xUnit_Fact
{
public class Artist_UT
{
[Fact]
public void TestConstructorWithValidNames()
{
Artist a = new Artist("Neil", "Young");
Assert.NotNull(a);
Assert.Equal("Neil", a.FirstName);
Assert.Equal("Young", a.LastName);
}
[Fact]
public void TestConstructorWithInvalidNames()
{
Assert.Throws<ArgumentException>(() => new Artist(null, null));
}
}
}

@ -0,0 +1,84 @@
using System;
using ex_026_000_ClassLibrary;
namespace ex_026_001_xUnit_Fact
{
public static class TestData
{
public static IEnumerable<object[]> Data_AddArtistsToAlbum()
{
yield return new object[]
{
1,
new Artist[]
{
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter"),
new Artist("Tony", "Williams")
},
new Artist[]
{
new Artist("Tony", "Williams")
},
new Album("Miles Smiles", new DateTime(1967, 1, 1),
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter")),
new Artist("Tony", "Williams")
};
yield return new object[]
{
2,
new Artist[]
{
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter"),
new Artist("Tony", "Williams")
},
new Artist[]
{
new Artist("Ron", "Carter"),
new Artist("Tony", "Williams")
},
new Album("Miles Smiles", new DateTime(1967, 1, 1),
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock")),
new Artist("Ron", "Carter"),
new Artist("Miles", "Davis"),
new Artist("Tony", "Williams")
};
yield return new object[]
{
1,
new Artist[]
{
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter"),
new Artist("Tony", "Williams")
},
new Artist[]
{
new Artist("Tony", "Williams")
},
new Album("Miles Smiles", new DateTime(1967, 1, 1),
new Artist("Miles", "Davis"),
new Artist("Wayne", "Shorter"),
new Artist("Herbie", "Hancock"),
new Artist("Ron", "Carter")),
new Artist("Tony", "Williams"),
new Artist("Tony", "Williams")
};
}
}
}

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ex_026_000_ClassLibrary\ex_026_000_ClassLibrary.csproj" />
</ItemGroup>
</Project>
Loading…
Cancel
Save