From b0f4e1af4b7b5ee2f82f58410ba7072fd80a124b Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Sun, 3 Jun 2018 19:14:19 +0200 Subject: [PATCH] Allow null default values in fluent API --- src/Cake.Issues.Tests/IssueBuilderTests.cs | 79 ++++++++++++---------- src/Cake.Issues/IssueBuilder.cs | 37 +++++----- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/Cake.Issues.Tests/IssueBuilderTests.cs b/src/Cake.Issues.Tests/IssueBuilderTests.cs index 040d3f803..fa9d87af2 100644 --- a/src/Cake.Issues.Tests/IssueBuilderTests.cs +++ b/src/Cake.Issues.Tests/IssueBuilderTests.cs @@ -221,45 +221,45 @@ public void Should_Throw_If_IssueProvider_Is_Null() public sealed class TheInProjectMethod { [Fact] - public void Should_Throw_If_Name_Is_Null() + public void Should_Handle_Projects_Which_Are_Null() { // Given var fixture = new IssueBuilderFixture(); + string project = null; // When - var result = Record.Exception(() => - fixture.IssueBuilder.InProject(null)); + var issue = fixture.IssueBuilder.InProject(project).Create(); // Then - result.IsArgumentNullException("name"); + issue.Project.ShouldBe(project); } [Fact] - public void Should_Throw_If_Name_Is_Empty() + public void Should_Handle_Projects_Which_Are_Empty() { // Given var fixture = new IssueBuilderFixture(); + var project = string.Empty; // When - var result = Record.Exception(() => - fixture.IssueBuilder.InProject(string.Empty)); + var issue = fixture.IssueBuilder.InProject(project).Create(); // Then - result.IsArgumentOutOfRangeException("name"); + issue.Project.ShouldBe(project); } [Fact] - public void Should_Throw_If_Name_Is_WhiteSpace() + public void Should_Handle_Projects_Which_Are_WhiteSpace() { // Given var fixture = new IssueBuilderFixture(); + var project = " "; // When - var result = Record.Exception(() => - fixture.IssueBuilder.InProject(" ")); + var issue = fixture.IssueBuilder.InProject(project).Create(); // Then - result.IsArgumentOutOfRangeException("name"); + issue.Project.ShouldBe(project); } [Theory] @@ -280,45 +280,42 @@ public void Should_Set_Project(string project) public sealed class TheInFileMethod { [Fact] - public void Should_Throw_If_FilePath_Is_Null() + public void Should_Handle_File_Paths_Which_Are_Null() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(null)); + var issue = fixture.IssueBuilder.InFile(null).Create(); // Then - result.IsArgumentNullException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Fact] - public void Should_Throw_If_FilePath_Is_Empty() + public void Should_Handle_File_Paths_Which_Are_Empty() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(string.Empty)); + var issue = fixture.IssueBuilder.InFile(string.Empty).Create(); // Then - result.IsArgumentOutOfRangeException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Fact] - public void Should_Throw_If_FilePath_Is_WhiteSpace() + public void Should_Handle_File_Paths_Which_Are_WhiteSpace() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(" ")); + var issue = fixture.IssueBuilder.InFile(" ").Create(); // Then - result.IsArgumentOutOfRangeException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Theory] @@ -348,45 +345,42 @@ public void Should_Set_FilePath(string filePath, string expectedFilePath) public sealed class TheInFileLineMethod { [Fact] - public void Should_Throw_If_FilePath_Is_Null() + public void Should_Handle_File_Paths_Which_Are_Null() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(null, 10)); + var issue = fixture.IssueBuilder.InFile(null).Create(); // Then - result.IsArgumentNullException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Fact] - public void Should_Throw_If_FilePath_Is_Empty() + public void Should_Handle_File_Paths_Which_Are_Empty() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(string.Empty, 10)); + var issue = fixture.IssueBuilder.InFile(string.Empty).Create(); // Then - result.IsArgumentOutOfRangeException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Fact] - public void Should_Throw_If_FilePath_Is_WhiteSpace() + public void Should_Handle_File_Paths_Which_Are_WhiteSpace() { // Given var fixture = new IssueBuilderFixture(); // When - var result = Record.Exception(() => - fixture.IssueBuilder.InFile(" ", 10)); + var issue = fixture.IssueBuilder.InFile(" ").Create(); // Then - result.IsArgumentOutOfRangeException("filePath"); + issue.AffectedFileRelativePath.ShouldBe(null); } [Fact] @@ -417,6 +411,19 @@ public void Should_Throw_If_Line_Is_Zero() result.IsArgumentOutOfRangeException("line"); } + [Fact] + public void Should_Handle_Line_Which_Is_Null() + { + // Given + var fixture = new IssueBuilderFixture(); + + // When + var issue = fixture.IssueBuilder.InFile("foo", null).Create(); + + // Then + issue.Line.ShouldBe(null); + } + [Theory] [InlineData(@"foo", @"foo")] [InlineData(@"foo\bar", @"foo/bar")] diff --git a/src/Cake.Issues/IssueBuilder.cs b/src/Cake.Issues/IssueBuilder.cs index 5a76fbad0..4023a3bb8 100644 --- a/src/Cake.Issues/IssueBuilder.cs +++ b/src/Cake.Issues/IssueBuilder.cs @@ -82,13 +82,13 @@ public static IssueBuilder NewIssue( /// /// Sets the name of the project to which the file affected by the issue belongs. /// - /// Name of the project to which the file affected by the issue belongs. + /// Name of the project to which the file affected by the issue belongs. + /// null or if issue is not related to a project. /// Issue Builder instance. public IssueBuilder InProject(string name) { - name.NotNullOrWhiteSpace(nameof(name)); - this.project = name; + return this; } @@ -96,12 +96,11 @@ public IssueBuilder InProject(string name) /// Sets the path to the file affected by the issue. /// /// The path to the file affacted by the issue. - /// The path needs to be relative to the repository root. + /// The path needs to be relative to the repository root. + /// null or if issue is not related to a change in a file. /// Issue Builder instance. public IssueBuilder InFile(string filePath) { - filePath.NotNullOrWhiteSpace(nameof(filePath)); - this.filePath = filePath; return this; @@ -111,13 +110,14 @@ public IssueBuilder InFile(string filePath) /// Sets the path to the file affected by the issue and the line in the file where the issues has occurred. /// /// The path to the file affacted by the issue. - /// The path needs to be relative to the repository root. - /// The line in the file where the issues has occurred. + /// The path needs to be relative to the repository root. + /// null or if issue is not related to a change in a file. + /// The line in the file where the issues has occurred. + /// null if the issue affects the whole file or an asssembly. /// Issue Builder instance. - public IssueBuilder InFile(string filePath, int line) + public IssueBuilder InFile(string filePath, int? line) { - filePath.NotNullOrWhiteSpace(nameof(filePath)); - line.NotNegativeOrZero(nameof(line)); + line?.NotNegativeOrZero(nameof(line)); this.filePath = filePath; this.line = line; @@ -138,8 +138,10 @@ public IssueBuilder WithPriority(IssuePriority priority) /// /// Sets the priority of the message. /// - /// The priority of the message. - /// The human friendly name of the priority. + /// The priority of the message. + /// null if no priority should be assigned. + /// The human friendly name of the priority. + /// null or if no priority should be assigned. /// Issue Builder instance. public IssueBuilder WithPriority(int value, string name) { @@ -154,7 +156,8 @@ public IssueBuilder WithPriority(int value, string name) /// /// Sets the rule of the issue. /// - /// The rule of the issue. + /// The rule of the issue. + /// null or if issue has no specific rule ID. /// Issue Builder instance. public IssueBuilder OfRule(string name) { @@ -168,8 +171,10 @@ public IssueBuilder OfRule(string name) /// /// Sets the rule of the issue. /// - /// The rule of the issue. - /// The URL containing information about the failing rule. + /// The rule of the issue. + /// null or if issue has no specific rule ID. + /// The URL containing information about the failing rule. + /// null if no URL is available. /// Issue Builder instance. public IssueBuilder OfRule(string name, Uri uri) {