Skip to content

feature/type-conversions #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions OnixLabs.Core.UnitTests.Data/Disposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace OnixLabs.Core.UnitTests.Data;

public sealed class Disposable : IDisposable, IAsyncDisposable
{
public bool IsDisposed { get; private set; }

public void Dispose()
{
IsDisposed = true;
}

public async ValueTask DisposeAsync()
{
IsDisposed = true;
await ValueTask.CompletedTask;
}
}
26 changes: 26 additions & 0 deletions OnixLabs.Core.UnitTests.Data/InvalidFormatProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace OnixLabs.Core.UnitTests.Data;

public sealed class InvalidFormatProvider : IFormatProvider
{
public static readonly InvalidFormatProvider Instance = new();

private InvalidFormatProvider()
{
}

public object? GetFormat(Type? formatType) => null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<IsTestProject>false</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.6.4"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
18 changes: 18 additions & 0 deletions OnixLabs.Core.UnitTests/EnumerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void EnumerationsShouldBeEqual()

// Then
Assert.Equal(a, b);
Assert.True(a == b);
Assert.False(a != b);
}

[Fact(DisplayName = "Enumerations should not be equal")]
Expand All @@ -40,6 +42,8 @@ public void EnumerationsShouldNotBeEqual()

// Then
Assert.NotEqual(a, b);
Assert.True(a != b);
Assert.False(a == b);
}

[Fact(DisplayName = "Enumeration should return all enumeration instances")]
Expand Down Expand Up @@ -149,4 +153,18 @@ public void EnumerationCompareToAsObjectShouldReturnTheCorrectValue()
// Then
Assert.Equal(-1, actual);
}

[Fact(DisplayName = "Enumeration.ToString should produce the expected result")]
public void EnumerationToStringShouldProduceExpectedResult()
{
// Given
const string expected = "Red";
Color red = Color.Red;

// When
string actual = red.ToString();

// Then
Assert.Equal(expected, actual);
}
}
70 changes: 70 additions & 0 deletions OnixLabs.Core.UnitTests/Linq/IEnumerableExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,76 @@ public void NoneShouldProduceExpectedResultFalseAll()
Assert.False(result);
}

[Fact(DisplayName = "IEnumerable.SequenceEqualOrNull should return true when the current and other enumerables are null.")]
public void SequenceEqualOrNullShouldReturnTrueWhenCurrentAndOtherEnumerablesAreNull()
{
// Given
IEnumerable<int>? enumerable = null;
IEnumerable<int>? other = null;

// When
bool result = enumerable.SequenceEqualOrNull(other);

// Then
Assert.True(result);
}

[Fact(DisplayName = "IEnumerable.SequenceEqualOrNull should return false when the current enumerable is not null and other enumerable is null.")]
public void SequenceEqualOrNullShouldReturnTrueWhenCurrentEnumerableIsNotNullAndOtherEnumerableIsNull()
{
// Given
IEnumerable<int> enumerable = [1, 2, 3];
IEnumerable<int>? other = null;

// When
bool result = enumerable.SequenceEqualOrNull(other);

// Then
Assert.False(result);
}

[Fact(DisplayName = "IEnumerable.SequenceEqualOrNull should return false when the current enumerable is null and other enumerable is not null.")]
public void SequenceEqualOrNullShouldReturnTrueWhenCurrentEnumerableIsNullAndOtherEnumerableIsNotNull()
{
// Given
IEnumerable<int>? enumerable = null;
IEnumerable<int> other = [1, 2, 3];

// When
bool result = enumerable.SequenceEqualOrNull(other);

// Then
Assert.False(result);
}

[Fact(DisplayName = "IEnumerable.SequenceEqualOrNull should return true when the current enumerable is equal to the other enumerable.")]
public void SequenceEqualOrNullShouldReturnTrueWhenCurrentEnumerableIsEqualToTheOtherEnumerable()
{
// Given
IEnumerable<int>? enumerable = [1, 2, 3];
IEnumerable<int> other = [1, 2, 3];

// When
bool result = enumerable.SequenceEqualOrNull(other);

// Then
Assert.True(result);
}

[Fact(DisplayName = "IEnumerable.SequenceEqualOrNull should return false when the current enumerable is not equal to the other enumerable.")]
public void SequenceEqualOrNullShouldReturnTrueWhenCurrentEnumerableIsNotEqualToTheOtherEnumerable()
{
// Given
IEnumerable<int>? enumerable = [1, 2, 3];
IEnumerable<int> other = [3, 2, 1];

// When
bool result = enumerable.SequenceEqualOrNull(other);

// Then
Assert.False(result);
}

[Fact(DisplayName = "IEnumerable.SingleOrNone should return success none when the enumerable contains no elements.")]
public void SingleOrNoneShouldReturnSuccessNoneWhenEnumerableContainsNoElements()
{
Expand Down
62 changes: 62 additions & 0 deletions OnixLabs.Core.UnitTests/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,68 @@ public void IsWithinRangeExclusiveShouldProduceExpectedResult(int value, int min
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "CompareToObject should produce zero if the current IComparable<T> is equal to the specified object.")]
public void CompareToObjectShouldProduceZeroIfTheCurrentIComparableIsEqualToTheSpecifiedObject()
{
// Given
const int expected = 0;

// When
int actual = 123.CompareToObject(123);

// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "CompareToObject should produce positive one if the specified object is null.")]
public void CompareToObjectShouldProducePositiveOneIfTheSpecifiedObjectIsNull()
{
// Given
const int expected = 1;

// When
int actual = 124.CompareToObject(null);

// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "CompareToObject should produce positive one if the current IComparable<T> greater than the specified object.")]
public void CompareToObjectShouldProducePositiveOneIfTheCurrentIComparableIsGreaterThanTheSpecifiedObject()
{
// Given
const int expected = 1;

// When
int actual = 124.CompareToObject(123);

// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "CompareToObject should produce negative one if the current IComparable<T> greater than the specified object.")]
public void CompareToObjectShouldProduceNegativeOneIfTheCurrentIComparableIsGreaterThanTheSpecifiedObject()
{
// Given
const int expected = -1;

// When
int actual = 122.CompareToObject(123);

// Then
Assert.Equal(expected, actual);
}

[Fact(DisplayName = "CompareToObject should throw ArgumentException if the specified object is of the incorrect type.")]
public void CompareToObjectShouldThrowArgumentExceptionIfSpecifiedObjectIsOfIncorrectType()
{
// When
Exception exception = Assert.Throws<ArgumentException>(() => 122.CompareToObject(123.456));

// Then
Assert.Equal("Object must be of type System.Int32 (Parameter 'obj')", exception.Message);
}

[Fact(DisplayName = "ToRecordString should produce a record formatted string")]
public void ToRecordStringShouldProduceExpectedResult()
{
Expand Down
8 changes: 4 additions & 4 deletions OnixLabs.Core.UnitTests/OnixLabs.Core.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.6.4"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
39 changes: 28 additions & 11 deletions OnixLabs.Core.UnitTests/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,39 +201,51 @@ public void OptionalNoneExplicitOperatorShouldProduceExpectedResult()
public void OptionalSomeValuesShouldBeConsideredEqual()
{
// Given
Optional<int> a = Optional<int>.Some(123);
Optional<int> b = Optional<int>.Some(123);
Some<int> a = Optional<int>.Some(123);
Some<int> b = Optional<int>.Some(123);
object bObject = b;

// When / Then
Assert.Equal(a, b);
Assert.True(a == b);
Assert.Equal(a, bObject);
Assert.True(a.Equals(b));
Assert.True(a.Equals(bObject));
Assert.True(a == b);
Assert.False(a != b);
}

[Fact(DisplayName = "Optional Some values should not be considered equal.")]
public void OptionalSomeValuesShouldNotBeConsideredEqual()
{
// Given
Optional<int> a = Optional<int>.Some(123);
Optional<int> b = Optional<int>.Some(456);
Some<int> a = Optional<int>.Some(123);
Some<int> b = Optional<int>.Some(456);
object bObject = b;

// When / Then
Assert.NotEqual(a, b);
Assert.True(a != b);
Assert.NotEqual(a, bObject);
Assert.False(a.Equals(b));
Assert.False(a.Equals(bObject));
Assert.False(a == b);
Assert.True(a != b);
}

[Fact(DisplayName = "Optional None values should be considered equal.")]
public void OptionalNoneValuesShouldBeConsideredEqual()
{
// Given
Optional<int> a = Optional<int>.None;
Optional<int> b = Optional<int>.None;
None<int> a = Optional<int>.None;
None<int> b = Optional<int>.None;
object bObject = b;

// When / Then
Assert.Equal(a, b);
Assert.True(a == b);
Assert.Equal(a, bObject);
Assert.True(a.Equals(b));
Assert.True(a.Equals(bObject));
Assert.True(a == b);
Assert.False(a != b);
}

[Fact(DisplayName = "Optional Some and None values should not be considered equal.")]
Expand All @@ -242,11 +254,15 @@ public void OptionalSomeAndNoneValuesShouldNotBeConsideredEqual()
// Given
Optional<int> a = Optional<int>.Some(123);
Optional<int> b = Optional<int>.None;
object bObject = b;

// When / Then
Assert.NotEqual(a, b);
Assert.True(a != b);
Assert.NotEqual(a, bObject);
Assert.False(a.Equals(b));
Assert.False(a.Equals(bObject));
Assert.False(a == b);
Assert.True(a != b);
}

[Fact(DisplayName = "Optional Some.GetHashCode should produce the expected result.")]
Expand Down Expand Up @@ -317,7 +333,8 @@ public void OptionalSomeGetValueOrDefaultWithDefaultValueShouldProduceExpectedRe
Optional<string> text = "abc";

// When
int actualNumber = number.GetValueOrDefault(456);
int? actualNumber = number.GetValueOrDefault(456);
// ReSharper disable once VariableCanBeNotNullable
string? actualText = text.GetValueOrDefault("xyz");

// Then
Expand Down
Loading
Loading