Skip to content

Commit

Permalink
[BDB-14] Create rule StringMustNotContainAnyWhiteSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
phamhongphuc1403 committed Aug 18, 2024
1 parent cf9466a commit 9746054
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ namespace BuildingBlock.Core.Domain.Rules.Implementations;

public class StringMustContainOnlyLetters : IBusinessRule
{
private readonly string _name;
private readonly string _value;

public StringMustContainOnlyLetters(string value)
public StringMustContainOnlyLetters(string value, string name)
{
_value = value;
_name = name;
}

public bool IsBroken()
{
if (_value.All(char.IsLetter)) return false;

Message = "String must only contain only letters";
Message = $"{_name} must contain only letters";

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public bool IsBroken()
{
if (!string.IsNullOrEmpty(_value) && !string.IsNullOrWhiteSpace(_value)) return false;

Message = $"{_name} with value: {_value} can not be empty or contain only white spaces.";
Message = $"{_name} with value: '{_value}' can not be empty or contain only white spaces.";
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using BuildingBlock.Core.Domain.Rules.Abstractions;

namespace BuildingBlock.Core.Domain.Rules.Implementations;

public class StringMustNotContainAnyWhiteSpace : IBusinessRule
{
private readonly string _name;
private readonly string _value;

public StringMustNotContainAnyWhiteSpace(string value, string name)
{
_value = value;
_name = name;
}

public string? Message { get; private set; }

public bool IsBroken()
{
if (!_value.Contains(' ')) return false;

Message = $"{_name} with value: '{_value}' can not contain any white space.";

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;
using BuildingBlock.Core.Domain.ValueObjects.Abstractions;

namespace BuildingBlock.Core.Domain.ValueObjects;
namespace BuildingBlock.Core.Domain.Shared.Utils;

public static class ValueObjectUtility
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BuildingBlock.Core.Domain.Rules.Implementations;
using BuildingBlock.Core.Domain.Shared.Utils;

namespace BuildingBlock.Core.Domain.ValueObjects.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,43 @@ public class ShouldReturnTrue
public void WhenStringContainsWhiteSpace()
{
// Arrange
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("String with white space");
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("String with white space", "key");

// Act
var result = stringMustContainOnlyLetters.IsBroken();

// Assert
result.Should().BeTrue();
stringMustContainOnlyLetters.Message.Should().Be("String must only contain only letters");
stringMustContainOnlyLetters.Message.Should().Be("key must contain only letters");
}

[Fact]
public void WhenStringContainsNumber()
{
// Arrange
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("String with number 1");
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("String with number 1", "key");

// Act
var result = stringMustContainOnlyLetters.IsBroken();

// Assert
result.Should().BeTrue();
stringMustContainOnlyLetters.Message.Should().Be("String must only contain only letters");
stringMustContainOnlyLetters.Message.Should().Be("key must contain only letters");
}

[Fact]
public void WhenStringContainsSpecialCharacter()
{
// Arrange
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("String with special character !");
var stringMustContainOnlyLetters =
new StringMustContainOnlyLetters("String with special character !", "key");

// Act
var result = stringMustContainOnlyLetters.IsBroken();

// Assert
result.Should().BeTrue();
stringMustContainOnlyLetters.Message.Should().Be("String must only contain only letters");
stringMustContainOnlyLetters.Message.Should().Be("key must contain only letters");
}
}

Expand All @@ -56,7 +57,7 @@ public class ShouldReturnFalse
public void WhenStringContainsOnlyLetters()
{
// Arrange
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("StringWithOnlyLetters");
var stringMustContainOnlyLetters = new StringMustContainOnlyLetters("StringWithOnlyLetters", "key");

// Act
var result = stringMustContainOnlyLetters.IsBroken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,51 @@ namespace Tests.Core.Domain.Rules;

public class StringMustNotBeEmptyOrWhiteSpaceTest
{
private const string Name = "String";

public class ShouldReturnTrue
{
[Fact]
public void WhenStringIsEmpty()
{
// Arrange
var value = string.Empty;
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(value, Name);
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(string.Empty, "string");

// Act
var result = stringCanNotBeEmptyOrWhiteSpaceRule.IsBroken();

// Assert
result.Should().BeTrue();
stringCanNotBeEmptyOrWhiteSpaceRule.Message.Should()
.Be($"{Name} with value: {value} can not be empty or contain only white spaces.");
.Be("string with value: '' can not be empty or contain only white spaces.");
}

[Fact]
public void WhenStringIsNull()
{
// Arrange
string? value = null;
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(value, Name);
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(null, "string");

// Act
var result = stringCanNotBeEmptyOrWhiteSpaceRule.IsBroken();

// Assert
result.Should().BeTrue();
stringCanNotBeEmptyOrWhiteSpaceRule.Message.Should()
.Be($"{Name} with value: {value} can not be empty or contain only white spaces.");
.Be("string with value: '' can not be empty or contain only white spaces.");
}

[Fact]
public void WhenStringContainsOnlyWhiteSpaces()
{
// Arrange
var value = " ";
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(value, Name);
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(" ", "string");

// Act
var result = stringCanNotBeEmptyOrWhiteSpaceRule.IsBroken();

// Assert
result.Should().BeTrue();
stringCanNotBeEmptyOrWhiteSpaceRule.Message.Should()
.Be($"{Name} with value: {value} can not be empty or contain only white spaces.");
.Be("string with value: ' ' can not be empty or contain only white spaces.");
}
}

Expand All @@ -64,8 +59,7 @@ public class ShouldReturnFalse
public void WhenStringContainsCharacters()
{
// Arrange
var value = "test";
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace(value, Name);
var stringCanNotBeEmptyOrWhiteSpaceRule = new StringMustNotBeEmptyOrWhiteSpace("test", "string");

// Act
var result = stringCanNotBeEmptyOrWhiteSpaceRule.IsBroken();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using BuildingBlock.Core.Domain.Rules.Implementations;
using FluentAssertions;

namespace Tests.Core.Domain.Rules;

public class StringMustNotContainAnyWhiteSpaceTest
{
public class ShouldReturnTrue
{
[Fact]
public void WhenStringContainsWhiteSpace()
{
// Arrange
var stringMustNotContainAnyWhiteSpace =
new StringMustNotContainAnyWhiteSpace("String with white space", "string");

// Act
var result = stringMustNotContainAnyWhiteSpace.IsBroken();

// Assert
result.Should().BeTrue();
stringMustNotContainAnyWhiteSpace.Message.Should()
.Be("string with value: 'String with white space' can not contain any white space.");
}
}

public class ShouldReturnFalse
{
[Fact]
public void WhenStringDoesNotContainWhiteSpace()
{
// Arrange
var stringMustNotContainAnyWhiteSpace =
new StringMustNotContainAnyWhiteSpace("StringWithoutWhiteSpace", "string");

// Act
var result = stringMustNotContainAnyWhiteSpace.IsBroken();

// Assert
result.Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public void WhenRuleIsBroken()

// Assert
action.Should().Throw<ValidationException>();
rule.Message.Should().Be($"{name} with value: {value} can not be empty or contain only white spaces.");
rule.Message.Should()
.Be($"{name} with value: '{value}' can not be empty or contain only white spaces.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BuildingBlock.Core.Domain.ValueObjects;
using BuildingBlock.Core.Domain.ValueObjects.Abstractions;
using BuildingBlock.Core.Domain.Shared.Utils;
using BuildingBlock.Core.Domain.ValueObjects.Implementations;
using FluentAssertions;

namespace Tests.Core.Domain.ValueObjects;
Expand All @@ -8,34 +8,19 @@ public class ObjectValueUtilityTest
{
public class ToNormalizedName
{
public class TestValueObject : ValueObject
{
public string ActualData;

public TestValueObject()
{
ActualData = this.ToNormalizedName();
}

protected override IEnumerable<object?> GetValues()
{
yield return ActualData;
}
}

public class ShouldNormalizeClassName
{
[Fact]
public void Case1()
{
// Arrange
var mockValueObject = new TestValueObject();
var mockValueObject = new EmailAddress("test@123");

// Act
var normalizedName = mockValueObject.ToNormalizedName();

// Assert
normalizedName.Should().Be("test value object");
normalizedName.Should().Be("email address");
}
}
}
Expand Down

0 comments on commit 9746054

Please sign in to comment.