Skip to content

Commit

Permalink
Allow null default values in fluent API
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Jun 3, 2018
1 parent f4ee3bd commit b0f4e1a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 52 deletions.
79 changes: 43 additions & 36 deletions src/Cake.Issues.Tests/IssueBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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")]
Expand Down
37 changes: 21 additions & 16 deletions src/Cake.Issues/IssueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,25 @@ public static IssueBuilder NewIssue<T>(
/// <summary>
/// Sets the name of the project to which the file affected by the issue belongs.
/// </summary>
/// <param name="name">Name of the project to which the file affected by the issue belongs.</param>
/// <param name="name">Name of the project to which the file affected by the issue belongs.
/// <c>null</c> or <see cref="string.Empty"/> if issue is not related to a project.</param>
/// <returns>Issue Builder instance.</returns>
public IssueBuilder InProject(string name)
{
name.NotNullOrWhiteSpace(nameof(name));

this.project = name;

return this;
}

/// <summary>
/// Sets the path to the file affected by the issue.
/// </summary>
/// <param name="filePath">The path to the file affacted by the issue.
/// The path needs to be relative to the repository root.</param>
/// The path needs to be relative to the repository root.
/// <c>null</c> or <see cref="string.Empty"/> if issue is not related to a change in a file.</param>
/// <returns>Issue Builder instance.</returns>
public IssueBuilder InFile(string filePath)
{
filePath.NotNullOrWhiteSpace(nameof(filePath));

this.filePath = filePath;

return this;
Expand All @@ -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.
/// </summary>
/// <param name="filePath">The path to the file affacted by the issue.
/// The path needs to be relative to the repository root.</param>
/// <param name="line">The line in the file where the issues has occurred.</param>
/// The path needs to be relative to the repository root.
/// <c>null</c> or <see cref="string.Empty"/> if issue is not related to a change in a file.</param>
/// <param name="line">The line in the file where the issues has occurred.
/// <c>null</c> if the issue affects the whole file or an asssembly.</param>
/// <returns>Issue Builder instance.</returns>
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;
Expand All @@ -138,8 +138,10 @@ public IssueBuilder WithPriority(IssuePriority priority)
/// <summary>
/// Sets the priority of the message.
/// </summary>
/// <param name="value">The priority of the message.</param>
/// <param name="name">The human friendly name of the priority.</param>
/// <param name="value">The priority of the message.
/// <c>null</c> if no priority should be assigned.</param>
/// <param name="name">The human friendly name of the priority.
/// <c>null</c> or <see cref="string.Empty"/> if no priority should be assigned.</param>
/// <returns>Issue Builder instance.</returns>
public IssueBuilder WithPriority(int value, string name)
{
Expand All @@ -154,7 +156,8 @@ public IssueBuilder WithPriority(int value, string name)
/// <summary>
/// Sets the rule of the issue.
/// </summary>
/// <param name="name">The rule of the issue.</param>
/// <param name="name">The rule of the issue.
/// <c>null</c> or <see cref="string.Empty"/> if issue has no specific rule ID.</param>
/// <returns>Issue Builder instance.</returns>
public IssueBuilder OfRule(string name)
{
Expand All @@ -168,8 +171,10 @@ public IssueBuilder OfRule(string name)
/// <summary>
/// Sets the rule of the issue.
/// </summary>
/// <param name="name">The rule of the issue.</param>
/// <param name="uri">The URL containing information about the failing rule.</param>
/// <param name="name">The rule of the issue.
/// <c>null</c> or <see cref="string.Empty"/> if issue has no specific rule ID.</param>
/// <param name="uri">The URL containing information about the failing rule.
/// <c>null</c> if no URL is available.</param>
/// <returns>Issue Builder instance.</returns>
public IssueBuilder OfRule(string name, Uri uri)
{
Expand Down

0 comments on commit b0f4e1a

Please sign in to comment.