Skip to content

Commit

Permalink
Nullable annotations for unit test project
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Sep 6, 2024
1 parent 914e492 commit 4fa74a9
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/LinkDotNet.Blog.Domain/Enumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ protected Enumeration(string key)

public string Key { get; }

public static bool operator ==(Enumeration<TEnumeration> a, Enumeration<TEnumeration> b)
public static bool operator ==(Enumeration<TEnumeration>? a, Enumeration<TEnumeration>? b)
=> a is not null && b is not null && a.Key.Equals(b.Key, StringComparison.Ordinal);

public static bool operator !=(Enumeration<TEnumeration> a, Enumeration<TEnumeration> b) => !(a == b);
public static bool operator !=(Enumeration<TEnumeration>? a, Enumeration<TEnumeration>? b) => !(a == b);

public static TEnumeration Create(string key)
=> All.SingleOrDefault(p => p.Key == key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public ApplicationConfigurationBuilder WithIsDisqusEnabled(bool isDisqusEnabled)
return this;
}

public ApplicationConfigurationBuilder WithKofiToken(string kofiToken)
public ApplicationConfigurationBuilder WithKofiToken(string? kofiToken)
{
this.kofiToken = kofiToken;
return this;
}

public ApplicationConfigurationBuilder WithGithubSponsorName(string githubSponsorName)
public ApplicationConfigurationBuilder WithGithubSponsorName(string? githubSponsorName)
{
this.githubSponsorName = githubSponsorName;
return this;
Expand All @@ -85,7 +85,7 @@ public ApplicationConfigurationBuilder WithShowReadingIndicator(bool showReading
return this;
}

public ApplicationConfigurationBuilder WithPatreonName(string patreonName)
public ApplicationConfigurationBuilder WithPatreonName(string? patreonName)
{
this.patreonName = patreonName;
return this;
Expand Down
2 changes: 1 addition & 1 deletion tests/LinkDotNet.Blog.TestUtilities/BlogPostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public BlogPostBuilder WithPreviewImageUrl(string url)
return this;
}

public BlogPostBuilder WithPreviewImageUrlFallback(string url)
public BlogPostBuilder WithPreviewImageUrlFallback(string? url)
{
previewImageUrlFallback = url;
return this;
Expand Down
2 changes: 1 addition & 1 deletion tests/LinkDotNet.Blog.TestUtilities/IntroductionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IntroductionBuilder
private string profilePictureUrl = "ProfilePictureUrl";
private string description = "Description";

public IntroductionBuilder WithBackgroundUrl(string backgroundUrl)
public IntroductionBuilder WithBackgroundUrl(string? backgroundUrl)
{
this.backgroundUrl = backgroundUrl;
return this;
Expand Down
2 changes: 1 addition & 1 deletion tests/LinkDotNet.Blog.TestUtilities/SkillBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public SkillBuilder WithSkillName(string skill)
return this;
}

public SkillBuilder WithIconUrl(string iconUrl)
public SkillBuilder WithIconUrl(string? iconUrl)
{
this.iconUrl = iconUrl;
return this;
Expand Down
6 changes: 3 additions & 3 deletions tests/LinkDotNet.Blog.UnitTests/Domain/EnumerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void GivenInvalidKey_WhenCreatingEnumeration_ThenErrorResult()
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void GivenNullOrEmptyKey_WhenCreating_ThenException(string key)
public void GivenNullOrEmptyKey_WhenCreating_ThenException(string? key)
{
Action result = () => new TestEnumeration(key);
Action result = () => new TestEnumeration(key!);

// Assert
result.ShouldThrow<ArgumentException>();
Expand All @@ -66,7 +66,7 @@ public void GivenNullOrEmptyKey_WhenCreating_ThenException(string key)
[Fact]
public void NullEnumerationNeverEqual()
{
var isEqual = null == (TestEnumeration)null;
var isEqual = null! == (TestEnumeration?)null;

isEqual.ShouldBeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void ShouldCreateValidLevels(string key)
[InlineData("NotALevel")]
[InlineData("")]
[InlineData(null)]
public void ShouldNotCreateInvalidLevels(string key)
public void ShouldNotCreateInvalidLevels(string? key)
{
Action act = () => ProficiencyLevel.Create(key);
Action act = () => ProficiencyLevel.Create(key!);

act.ShouldThrow<Exception>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void ShouldCreateObject()
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void ShouldThrowExceptionWhenEmptyKeyOrValue(string content)
public void ShouldThrowExceptionWhenEmptyKeyOrValue(string? content)
{
Action act = () => ProfileInformationEntry.Create(content, 0);
Action act = () => ProfileInformationEntry.Create(content!, 0);

act.ShouldThrow<ArgumentException>();
}
Expand Down
12 changes: 6 additions & 6 deletions tests/LinkDotNet.Blog.UnitTests/Domain/SkillTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void ShouldCreateSkillTrimmedWhitespaces()
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ShouldThrowWhenWhitespaceName(string name)
public void ShouldThrowWhenWhitespaceName(string? name)
{
Action result = () => Skill.Create(name, "url", "backend", ProficiencyLevel.Expert.Key);
Action result = () => Skill.Create(name!, "url", "backend", ProficiencyLevel.Expert.Key);

result.ShouldThrow<ArgumentException>();
}
Expand All @@ -31,9 +31,9 @@ public void ShouldThrowWhenWhitespaceName(string name)
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ShouldThrowWhenWhitespaceCapability(string capability)
public void ShouldThrowWhenWhitespaceCapability(string? capability)
{
Action result = () => Skill.Create("name", "url", capability, ProficiencyLevel.Expert.Key);
Action result = () => Skill.Create("name", "url", capability!, ProficiencyLevel.Expert.Key);

result.ShouldThrow<ArgumentException>();
}
Expand All @@ -49,7 +49,7 @@ public void ShouldThrowWhenInvalidProficiencyLevel()
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ShouldSetUrlToNullWhenWhitespace(string url)
public void ShouldSetUrlToNullWhenWhitespace(string? url)
{
var skill = Skill.Create("name", url, "cap", ProficiencyLevel.Expert.Key);

Expand All @@ -71,7 +71,7 @@ public void ShouldThrowWhenProficiencyIsNull()
{
var skill = Skill.Create("name", null, "cap", ProficiencyLevel.Familiar.Key);

Action result = () => skill.SetProficiencyLevel(null);
Action result = () => skill.SetProficiencyLevel(null!);

result.ShouldThrow<ArgumentNullException>();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/LinkDotNet.Blog.UnitTests/Domain/TalkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void CreateTalkWithTrimmedWhiteSpaces()
[InlineData("title", "place", null)]
[InlineData("title", "place", "")]
[InlineData("title", "place", " ")]
public void TalkWithInvalidInvariantShouldNotBeCreated(string title, string place, string desc)
public void TalkWithInvalidInvariantShouldNotBeCreated(string? title, string? place, string? desc)
{
Action act = () => Talk.Create(title, place, desc, DateTime.MinValue);
Action act = () => Talk.Create(title!, place!, desc!, DateTime.MinValue);

act.ShouldThrow<Exception>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public async Task ShouldUpdateCacheOnStore()

var latest = await sut.GetByIdAsync("id");

latest.ShouldNotBeNull();
latest.Title.ShouldBe("new");
}

Expand Down Expand Up @@ -109,10 +110,10 @@ await repositoryMock.Received(2).GetAllAsync(
[Theory]
[InlineData(null)]
[InlineData("some_id")]
public async Task ShouldNotThrowExceptionWhenCallingStoreWithoutRetrievingKeyFirst(string id)
public async Task ShouldNotThrowExceptionWhenCallingStoreWithoutRetrievingKeyFirst(string? id)
{
var blogPost = new BlogPostBuilder().Build();
blogPost.Id = id;
blogPost.Id = id!;

await sut.StoreAsync(blogPost);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task ShouldStoreAndRetrieveBlogPost()

var blogPostFromRepo = await sut.GetByIdAsync(blogPost.Id);

blogPostFromRepo.ShouldNotBeNull();
blogPostFromRepo.Title.ShouldBe("My Title");
}

Expand All @@ -38,6 +39,7 @@ public async Task ShouldHandleUpdates()
await sut.StoreAsync(blogPost);

var blogPostFromRepository = await sut.GetByIdAsync(blogPost.Id);
blogPostFromRepository.ShouldNotBeNull();
blogPostFromRepository.Title.ShouldBe("My new Title");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>

<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ApplicationConfigurationTests
[Fact]
public void ShouldMapFromAppConfiguration()
{
var inMemorySettings = new Dictionary<string, string>
var inMemorySettings = new Dictionary<string, string?>
{
{ "BlogName", "UnitTest" },
{ "BlogBrandUrl", "http://localhost" },
Expand Down Expand Up @@ -111,14 +111,14 @@ public void ShouldMapFromAppConfiguration()
[InlineData("github", null, null, true, false, false)]
[InlineData(null, null, "twitter", false, false, true)]
public void ShouldSetGithubLinkedAccountAccordingToValueSet(
string githubUrl,
string linkedInUrl,
string twitterUrl,
string? githubUrl,
string? linkedInUrl,
string? twitterUrl,
bool githubAvailable,
bool linkedInAvailable,
bool twitterAvailable)
{
var inMemorySettings = new Dictionary<string, string>
var inMemorySettings = new Dictionary<string, string?>
{
{ "Introduction:BackgroundUrl", "someurl" },
{ "Introduction:ProfilePictureUrl", "anotherurl" },
Expand All @@ -142,7 +142,7 @@ public void ShouldSetGithubLinkedAccountAccordingToValueSet(
[Fact]
public void ShouldSetIsAboutMeEnabledToFalseWhenNoInformation()
{
var inMemorySettings = new Dictionary<string, string>
var inMemorySettings = new Dictionary<string, string?>
{
{ "Introduction:BackgroundUrl", "someurl" },
{ "Introduction:ProfilePictureUrl", "anotherurl" },
Expand All @@ -161,7 +161,7 @@ public void ShouldSetIsAboutMeEnabledToFalseWhenNoInformation()
[Fact]
public void ShouldSetCommentPluginsToFalseWhenNoInformation()
{
var inMemorySettings = new Dictionary<string, string>
var inMemorySettings = new Dictionary<string, string?>
{
{ "Introduction:BackgroundUrl", "someurl" },
{ "Introduction:ProfilePictureUrl", "anotherurl" },
Expand All @@ -182,7 +182,7 @@ public void ShouldSetCommentPluginsToFalseWhenNoInformation()
public void ShouldSetDefaultBlogPostPerPageIfNotSet()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>())
.AddInMemoryCollection(new Dictionary<string, string?>())
.Build();

var appConfiguration = new ApplicationConfigurationBuilder().Build();
Expand All @@ -194,7 +194,7 @@ public void ShouldSetDefaultBlogPostPerPageIfNotSet()
[Fact]
public void ShouldSetLogoutUriIfNotGiven()
{
var inMemorySettings = new Dictionary<string, string>
var inMemorySettings = new Dictionary<string, string?>
{
{ "Authentication:AuthenticationProvider", "Auth0" }, { "Authentication:Domain", "domain" }, { "Authentication:ClientId", "clientid" },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public void ShouldSetOgData()

var ogData = cut.FindComponent<OgData>().Instance;
ogData.AbsolutePreviewImageUrl.ShouldBe("http://localhost/someurl");
ogData.Keywords.ShouldNotBeNull();
ogData.Keywords.ShouldContain("My Name");
ogData.Title.ShouldContain("About Me - My Name");
ogData.Description.ShouldNotBeNull();
ogData.Description.ShouldContain("About Me,My Name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AddProfileShortItemTests : BunitContext
[Fact]
public void ShouldAddShortItem()
{
string addedItem = null;
string? addedItem = null;
var cut = Render<AddProfileShortItem>(
p => p.Add(s => s.ValueAdded, c => addedItem = c));
cut.Find("input").Change("Key");
Expand All @@ -21,7 +21,7 @@ public void ShouldAddShortItem()
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void ShouldNotAddItemWhenKeyOrValueIsEmpty(string content)
public void ShouldNotAddItemWhenKeyOrValueIsEmpty(string? content)
{
var wasInvoked = false;
var cut = Render<AddProfileShortItem>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AddSkillDialogTests : BunitContext
[Fact]
public void ShouldCreateSkill()
{
Skill addedSkill = null;
Skill? addedSkill = null;
var toastServiceMock = Substitute.For<IToastService>();
Services.AddScoped(_ => toastServiceMock);
var cut = Render<AddSkillDialog>(p =>
Expand Down
Loading

0 comments on commit 4fa74a9

Please sign in to comment.