Skip to content

Commit

Permalink
Merge pull request #1 from Kobus-Smit/ace
Browse files Browse the repository at this point in the history
Add nullable conversion extension methods for strings and objects
  • Loading branch information
dimitrietataru authored Feb 18, 2024
2 parents 7a93fb0 + 5e431d2 commit 98ba3da
Show file tree
Hide file tree
Showing 169 changed files with 4,253 additions and 1 deletion.
128 changes: 127 additions & 1 deletion README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ internal void GivenToBooleanOrDefaultInvariantWhenInputIsNotValidThenResultIsDef
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullInvariantWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = true;
bool expected = true;

// Act
bool? actual = @this.ToBooleanOrNullInvariant();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullInvariantWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
bool? actual = @this.ToBooleanOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToBooleanOrNullInvariantWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
bool? actual = @this.ToBooleanOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToBooleanInvariantWhenInputIsValidThenResultIsExpected()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ internal void GivenToBooleanOrDefaultLocalWhenInputIsNotValidThenResultIsDefault
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullLocalWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = true;
bool expected = true;

// Act
bool? actual = @this.ToBooleanOrNullLocal();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullLocalWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
bool? actual = @this.ToBooleanOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToBooleanOrNullLocalWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
bool? actual = @this.ToBooleanOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToBooleanLocalWhenInputIsValidThenResultIsExpected()
{
Expand Down
40 changes: 40 additions & 0 deletions src/Ace.CSharp.Extensions.Tests/System.Object/To.BooleanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ internal void GivenToBooleanOrDefaultWhenInputIsNotValidThenResultIsDefault()
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = true;
bool expected = true;

// Act
bool? actual = @this.ToBooleanOrNull(provider: default);

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToBooleanOrNullWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
bool? actual = @this.ToBooleanOrNull(provider: default);

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToBooleanOrNullWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
bool? actual = @this.ToBooleanOrNull(provider: default);

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToBooleanWhenInputIsValidThenResultIsExpected()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ internal void GivenToByteOrDefaultInvariantWhenInputIsNotValidThenResultIsDefaul
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullInvariantWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = byte.MaxValue;
byte expected = byte.MaxValue;

// Act
byte? actual = @this.ToByteOrNullInvariant();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullInvariantWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
byte? actual = @this.ToByteOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToByteOrNullInvariantWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
byte? actual = @this.ToByteOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToByteInvariantWhenInputIsValidThenResultIsExpected()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ internal void GivenToByteOrDefaultLocalWhenInputIsNotValidThenResultIsDefault()
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullLocalWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = byte.MaxValue;
byte expected = byte.MaxValue;

// Act
byte? actual = @this.ToByteOrNullLocal();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullLocalWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
byte? actual = @this.ToByteOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToByteOrNullLocalWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
byte? actual = @this.ToByteOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToByteLocalWhenInputIsValidThenResultIsExpected()
{
Expand Down
40 changes: 40 additions & 0 deletions src/Ace.CSharp.Extensions.Tests/System.Object/To.ByteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ internal void GivenToByteOrDefaultWhenInputIsNotValidThenResultIsDefault()
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = byte.MaxValue;
byte expected = byte.MaxValue;

// Act
byte? actual = @this.ToByteOrNull(provider: default);

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToByteOrNullWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
byte? actual = @this.ToByteOrNull(provider: default);

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToByteOrNullWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
byte? actual = @this.ToByteOrNull(provider: default);

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToByteWhenInputIsValidThenResultIsExpected()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ internal void GivenToCharOrDefaultInvariantWhenInputIsNotValidThenResultIsDefaul
actual.Should().Be(expected);
}

[Fact]
internal void GivenToCharOrNullInvariantWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = '*';
char expected = '*';

// Act
char? actual = @this.ToCharOrNullInvariant();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToCharOrNullInvariantWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
char? actual = @this.ToCharOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToCharOrNullInvariantWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
char? actual = @this.ToCharOrNullInvariant();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToCharInvariantWhenInputIsValidThenResultIsExpected()
{
Expand Down
40 changes: 40 additions & 0 deletions src/Ace.CSharp.Extensions.Tests/System.Object/To.CharLocalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ internal void GivenToCharOrDefaultLocalWhenInputIsNotValidThenResultIsDefault()
actual.Should().Be(expected);
}

[Fact]
internal void GivenToCharOrNullLocalWhenInputIsValidThenResultIsExpected()
{
// Arrange
object @this = '*';
char expected = '*';

// Act
char? actual = @this.ToCharOrNullLocal();

// Assert
actual.Should().Be(expected);
}

[Fact]
internal void GivenToCharOrNullLocalWhenInputIsNotValidThenResultIsNull()
{
// Arrange
object @this = "foo";

// Act
char? actual = @this.ToCharOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenToCharOrNullLocalWhenInputIsNullThenResultIsNull()
{
// Arrange
object? @this = null;

// Act
char? actual = @this.ToCharOrNullLocal();

// Assert
actual.Should().BeNull();
}

[Fact]
internal void GivenTryConvertToCharLocalWhenInputIsValidThenResultIsExpected()
{
Expand Down
Loading

0 comments on commit 98ba3da

Please sign in to comment.