Skip to content

Commit

Permalink
Optional: Improve ToString tests / test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Sep 6, 2021
1 parent 10c70fe commit 8189704
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Absent_Constructor_ExpectAbsentIsTrue()
{
var actual = new Optional<RefType>();
Assert.True(actual.IsAbsent);
}

[Test]
public void Absent_Constructor_ExpectPresentIsFalse()
{
var actual = new Optional<RefType>();
Assert.False(actual.IsPresent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Absent_Default_ExpectAbsentIsTrue()
{
var actual = default(Optional<RefType>);
Assert.True(actual.IsAbsent);
}

[Test]
public void Absent_Default_ExpectPresentIsFalse()
{
var actual = default(Optional<RefType>);
Assert.False(actual.IsPresent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Absent_Value_ExpectAbsentIsTrue()
{
var actual = Optional<StructType>.Absent;
Assert.True(actual.IsAbsent);
}

[Test]
public void Absent_Value_ExpectPresentIsFalse()
{
var actual = Optional<StructType>.Absent;
Assert.False(actual.IsPresent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Present_Constructor_SourceIsNull_ExpectPresentIsTrue()
{
var actual = new Optional<StructType?>(null);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Constructor_SourceIsNull_ExpectAbsentIsFalse()
{
var actual = new Optional<StructType?>(null);
Assert.False(actual.IsAbsent);
}

[Test]
public void Present_Constructor_SourceIsNotNull_ExpectPresentIsTrue()
{
var actual = new Optional<RefType>(PlusFifteenIdRefType);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Constructor_SourceIsNotNull_ExpectAbsentIsFalse()
{
var actual = new Optional<RefType>(MinusFifteenIdRefType);
Assert.False(actual.IsAbsent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Present_Explicit_SourceIsNull_ExpectPresentIsTrue()
{
var actual = Optional<StructType?>.Present(null);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Explicit_SourceIsNull_ExpectAbsentIsFalse()
{
var actual = Optional<StructType?>.Present(null);
Assert.False(actual.IsAbsent);
}

[Test]
public void Present_Explicit_SourceIsNotNull_ExpectPresentIsTrue()
{
var actual = Optional<RefType>.Present(PlusFifteenIdRefType);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Explicit_SourceIsNotNull_ExpectAbsentIsFalse()
{
var actual = Optional<RefType>.Present(MinusFifteenIdRefType);
Assert.False(actual.IsAbsent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable enable

using NUnit.Framework;
using PrimeFuncPack.UnitTest;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests
{
partial class OptionalTest
{
[Test]
public void Present_Implicit_SourceIsNull_ExpectPresentIsTrue()
{
var actual = Optional<StructType?>.Present(null);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Implicit_SourceIsNull_ExpectAbsentIsFalse()
{
var actual = Optional<StructType?>.Present(null);
Assert.False(actual.IsAbsent);
}

[Test]
public void Present_Implicit_SourceIsNotNull_ExpectPresentIsTrue()
{
var actual = Optional<RefType>.Present(PlusFifteenIdRefType);
Assert.True(actual.IsPresent);
}

[Test]
public void Present_Implicit_SourceIsNotNull_ExpectAbsentIsFalse()
{
var actual = Optional<RefType>.Present(MinusFifteenIdRefType);
Assert.False(actual.IsAbsent);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public void ToString_SourceIsPresentAndValueToStringIsNull_ExpectPresentEmptyStr
[Test]
[TestCase(EmptyString)]
[TestCase(TabString)]
[TestCase(TwoTabsString)]
[TestCase(WhiteSpaceString)]
[TestCase(TwoWhiteSpacesString)]
[TestCase(ThreeWhiteSpacesString)]
[TestCase(MixedWhiteSpacesString)]
[TestCase(SomeString)]
public void ToString_SourceIsPresentAndValueToStringIsNotNull_ExpectSourceValueToStringResult(
string sourceValueToStringResult)
Expand All @@ -77,5 +81,35 @@ public void ToString_SourceIsPresentAndValueToStringIsNotNull_ExpectSourceValueT

Assert.AreEqual(expected, actual);
}

// TODO: Add test case source including decimal with point
[Test]
[TestCase(null)]
[TestCase(EmptyString)]
[TestCase(TabString)]
[TestCase(TwoTabsString)]
[TestCase(WhiteSpaceString)]
[TestCase(TwoWhiteSpacesString)]
[TestCase(ThreeWhiteSpacesString)]
[TestCase(MixedWhiteSpacesString)]
[TestCase(SomeString)]
[TestCase(-1)]
[TestCase(0)]
[TestCase(1)]
public void ToString_Common(
object? sourceValue)
{
var source = Optional<object?>.Present(sourceValue);

var actual = source.ToString();

var expected = string.Format(
CultureInfo.InvariantCulture,
"A present value of type {0}: {1}",
typeof(object),
sourceValue);

Assert.AreEqual(expected, actual);
}
}
}

0 comments on commit 8189704

Please sign in to comment.