Skip to content
Closed
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
38 changes: 20 additions & 18 deletions OnixLabs.Core.UnitTests/EnumerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,30 @@ public void EnumerationsShouldNotBeEqual()
Assert.False(a == b);
}

[Fact(DisplayName = "Enumeration should return all enumeration instances")]
public void EnumerationsShouldReturnAllEnumerationInstances()
[Fact(DisplayName = "Enumeration.Deconstruct should return a value/name tuple")]
public void EnumerationDeconstructShouldReturnValueNameTuple()
{
// Given
IEnumerable<Color> colors = Color.GetAll();
Color color = Color.Red;

// When
(int value, string name) = color;

// Then
Assert.Contains(colors, item => item == Color.Red);
Assert.Contains(colors, item => item == Color.Green);
Assert.Contains(colors, item => item == Color.Blue);
Assert.Equal(1, value);
Assert.Equal("Red", name);
}

[Fact(DisplayName = "Enumeration.GetAll should return all enumeration entries")]
public void EnumerationGetAllShouldReturnAllEnumerationEntries()
{
// Given
IEnumerable<Color> entries = Color.GetAll();

// Then
Assert.Contains(Color.Blue, entries);
Assert.Contains(Color.Green, entries);
Assert.Contains(Color.Red, entries);
}

[Fact(DisplayName = "Enumeration.FromName should return the expected enumeration entry")]
Expand All @@ -77,18 +91,6 @@ public void EnumerationFromValueShouldReturnTheExpectedEnumerationEntry()
Assert.Equal(Color.Green, color);
}

[Fact(DisplayName = "Enumeration.GetAll should return all enumeration entries")]
public void EnumerationGetAllShouldReturnAllEnumerationEntries()
{
// Given
IEnumerable<Color> entries = Color.GetAll();

// Then
Assert.Contains(Color.Blue, entries);
Assert.Contains(Color.Green, entries);
Assert.Contains(Color.Red, entries);
}

[Fact(DisplayName = "Enumeration.GetEntries should return all enumeration entries")]
public void EnumerationGetEntriesShouldReturnAllEnumerationEntries()
{
Expand Down
16 changes: 2 additions & 14 deletions OnixLabs.Core/Enumeration.Comparable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@ namespace OnixLabs.Core;

public abstract partial class Enumeration<T>
{
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
/// other object.
/// </summary>
/// <param name="other">An object to compare with the current instance.</param>
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
/// <inheritdoc/>
public int CompareTo(T? other) => Value.CompareToNullable(other?.Value);

/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
/// other object.
/// </summary>
/// <param name="obj">An object to compare with the current instance.</param>
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
/// <inheritdoc/>
public int CompareTo(object? obj) => this.CompareToObject(obj);
}
25 changes: 25 additions & 0 deletions OnixLabs.Core/Enumeration.Deconstruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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;

public abstract partial class Enumeration<T>
{
/// <summary>
/// Deconstructs the current <see cref="Enumeration{T}"/> into a value and name tuple.
/// </summary>
/// <param name="value">The value of the current <see cref="Enumeration{T}"/> instance.</param>
/// <param name="name">The name of the current <see cref="Enumeration{T}"/> instance.</param>
public void Deconstruct(out int value, out string name) => (value, name) = (Value, Name);
}
41 changes: 19 additions & 22 deletions OnixLabs.Core/Enumeration.Equatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,41 @@ namespace OnixLabs.Core;

public abstract partial class Enumeration<T>
{
/// <summary>
/// Checks whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with the current object.</param>
/// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public bool Equals(T? other) =>
ReferenceEquals(this, other)
|| other is not null
&& other.GetType() == GetType()
&& other.Value == Value
&& other.Name == Name;

/// <summary>
/// Checks for equality between the current instance and another object.
/// </summary>
/// <param name="obj">The object to check for equality.</param>
/// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as T);

/// <summary>
/// Serves as a hash code function for the current instance.
/// </summary>
/// <returns>Returns a hash code for the current instance.</returns>
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(GetType(), Name, Value);

/// <summary>
/// Performs an equality comparison between two object instances.
/// Determines whether the specified <paramref name="left"/> <see cref="Enumeration{T}"/>
/// value is equal to the specified <paramref name="right"/> <see cref="Enumeration{T}"/> value.
/// </summary>
/// <param name="left">The left-hand instance to compare.</param>
/// <param name="right">The right-hand instance to compare.</param>
/// <returns>Returns <see langword="true"/> if the left-hand instance is equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
/// <param name="left">The left-hand value to compare.</param>
/// <param name="right">The right-hand value to compare.</param>
/// <returns>
/// Returns <see langword="true"/> if the specified <paramref name="left"/> <see cref="Enumeration{T}"/> value is equal to
/// the specified <paramref name="right"/> <see cref="Enumeration{T}"/> value; otherwise, <see langword="false"/>.
/// </returns>
public static bool operator ==(Enumeration<T> left, Enumeration<T> right) => Equals(left, right);

/// <summary>
/// Performs an inequality comparison between two object instances.
/// Determines whether the specified <paramref name="left"/> <see cref="Enumeration{T}"/>
/// value is not equal to the specified <paramref name="right"/> <see cref="Enumeration{T}"/> value.
/// </summary>
/// <param name="left">The left-hand instance to compare.</param>
/// <param name="right">The right-hand instance to compare.</param>
/// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
/// <param name="left">The left-hand value to compare.</param>
/// <param name="right">The right-hand value to compare.</param>
/// <returns>
/// Returns <see langword="true"/> if the specified <paramref name="left"/> <see cref="Enumeration{T}"/> value is not equal to
/// the specified <paramref name="right"/> <see cref="Enumeration{T}"/> value; otherwise, <see langword="false"/>.
/// </returns>
public static bool operator !=(Enumeration<T> left, Enumeration<T> right) => !Equals(left, right);
}
22 changes: 4 additions & 18 deletions OnixLabs.Core/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,13 @@ public static Optional<TStruct> Of<TStruct>(TStruct? value) where TStruct : stru
/// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(Optional<T>? left, Optional<T>? right) => !Equals(left, right);

/// <summary>
/// Checks whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with the current object.</param>
/// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public bool Equals(Optional<T>? other) => OptionalEqualityComparer<T>.Default.Equals(this, other);

/// <summary>
/// Checks for equality between the current instance and another object.
/// </summary>
/// <param name="obj">The object to check for equality.</param>
/// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public sealed override bool Equals(object? obj) => Equals(obj as Optional<T>);

/// <summary>
/// Serves as a hash code function for the current instance.
/// </summary>
/// <returns>Returns a hash code for the current instance.</returns>
/// <inheritdoc/>
// ReSharper disable once HeapView.PossibleBoxingAllocation
public sealed override int GetHashCode() => this is Some<T> some ? some.Value.GetHashCode() : 0;

Expand Down Expand Up @@ -258,10 +247,7 @@ public void Match(Action<T>? some = null, Action? none = null)
/// <returns>Returns a new, successful <see cref="Result{T}"/> instance containing the current <see cref="Optional{T}"/> instance.</returns>
public Result<Optional<T>> ToResult() => Result<Optional<T>>.Success(this);

/// <summary>
/// Returns a <see cref="String"/> that represents the current object.
/// </summary>
/// <returns>Returns a <see cref="String"/> that represents the current object.</returns>
/// <inheritdoc/>
// ReSharper disable once HeapView.PossibleBoxingAllocation
public sealed override string ToString() => this is Some<T> some ? some.Value.ToString() ?? string.Empty : nameof(None);
}
Expand Down
33 changes: 6 additions & 27 deletions OnixLabs.Core/Result.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ public static async Task<Result<T>> OfAsync(Func<CancellationToken, Task<T>> fun
/// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(Result<T>? left, Result<T>? right) => !Equals(left, right);

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <inheritdoc/>
public void Dispose()
{
// ReSharper disable once HeapView.PossibleBoxingAllocation
Expand All @@ -186,12 +184,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
/// </summary>
/// <returns>
/// Returns a task that represents the asynchronous dispose operation.
/// </returns>
/// <inheritdoc/>
public async ValueTask DisposeAsync()
{
// ReSharper disable once HeapView.PossibleBoxingAllocation
Expand All @@ -201,24 +194,13 @@ public async ValueTask DisposeAsync()
GC.SuppressFinalize(this);
}

/// <summary>
/// Checks whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with the current object.</param>
/// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public bool Equals(Result<T>? other) => ResultEqualityComparer<T>.Default.Equals(this, other);

/// <summary>
/// Checks for equality between the current instance and another object.
/// </summary>
/// <param name="obj">The object to check for equality.</param>
/// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public sealed override bool Equals(object? obj) => Equals(obj as Result<T>);

/// <summary>
/// Serves as a hash code function for the current instance.
/// </summary>
/// <returns>Returns a hash code for the current instance.</returns>
/// <inheritdoc/>
public sealed override int GetHashCode() => this switch
{
// ReSharper disable once HeapView.PossibleBoxingAllocation
Expand Down Expand Up @@ -855,10 +837,7 @@ public void Throw()
throw failure.Exception;
}

/// <summary>
/// Returns a <see cref="String"/> that represents the current object.
/// </summary>
/// <returns>Returns a <see cref="String"/> that represents the current object.</returns>
/// <inheritdoc/>
public sealed override string ToString() => this switch
{
// ReSharper disable once HeapView.PossibleBoxingAllocation
Expand Down
22 changes: 4 additions & 18 deletions OnixLabs.Core/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ public static async Task<Result> OfAsync(Func<CancellationToken, Task> func, Can
/// <returns>Returns <see langword="true"/> if the left-hand instance is not equal to the right-hand instance; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(Result? left, Result? right) => !Equals(left, right);

/// <summary>
/// Checks whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with the current object.</param>
/// <returns>Returns <see langword="true"/> if the current object is equal to the other parameter; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public bool Equals(Result? other)
{
if (ReferenceEquals(this, other)) return true;
Expand All @@ -167,17 +163,10 @@ public bool Equals(Result? other)
return this is Success && other is Success;
}

/// <summary>
/// Checks for equality between the current instance and another object.
/// </summary>
/// <param name="obj">The object to check for equality.</param>
/// <returns>Returns <see langword="true"/> if the object is equal to the current instance; otherwise, <see langword="false"/>.</returns>
/// <inheritdoc/>
public sealed override bool Equals(object? obj) => Equals(obj as Result);

/// <summary>
/// Serves as a hash code function for the current instance.
/// </summary>
/// <returns>Returns a hash code for the current instance.</returns>
/// <inheritdoc/>
public sealed override int GetHashCode() => this is Failure failure ? failure.Exception.GetHashCode() : 0;

/// <summary>
Expand Down Expand Up @@ -738,10 +727,7 @@ public void Throw()
throw failure.Exception;
}

/// <summary>
/// Returns a <see cref="String"/> that represents the current object.
/// </summary>
/// <returns>Returns a <see cref="String"/> that represents the current object.</returns>
/// <inheritdoc/>
public sealed override string ToString() => this is Failure failure ? failure.Exception.Message : string.Empty;
}

Expand Down
34 changes: 34 additions & 0 deletions OnixLabs.Core/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@
/// <param name="subject">The subject to evaluate.</param>
/// <returns>Returns <see langword="true"/> if the subject satisfies the specification; otherwise, <see langword="false"/>.</returns>
public bool IsSatisfiedBy(T subject) => Criteria.Compile().Invoke(subject);

/// <summary>
/// Combines the specified <paramref name="left"/> specification and the
/// specified <see cref="right"/> specification using a logical AND operation.

Check warning on line 102 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved

Check warning on line 102 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved

Check warning on line 102 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved
/// </summary>
/// <param name="left">The left-hand specification to combine.</param>
/// <param name="right">The right-hand specification to combine.</param>
/// <returns>
/// Returns a combined specification that evaluates to <see langword="true"/> if both specifications are satisfied;
/// otherwise, the specification evaluates to <see langword="false"/>.
/// </returns>
public static Specification<T> operator &(Specification<T> left, Specification<T> right) => left.And(right);

/// <summary>
/// Combines the specified <paramref name="left"/> specification and the
/// specified <see cref="right"/> specification using a logical OR operation.

Check warning on line 114 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved

Check warning on line 114 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved

Check warning on line 114 in OnixLabs.Core/Specification.cs

View workflow job for this annotation

GitHub Actions / build

XML comment has cref attribute 'right' that could not be resolved
/// </summary>
/// <param name="left">The left-hand specification to combine.</param>
/// <param name="right">The right-hand specification to combine.</param>
/// <returns>
/// Returns a combined specification that evaluates to <see langword="true"/> if both specifications are satisfied;
/// otherwise, the specification evaluates to <see langword="false"/>.
/// </returns>
public static Specification<T> operator |(Specification<T> left, Specification<T> right) => left.Or(right);

/// <summary>
/// Negates the specified <paramref name="specification"/> specification.
/// </summary>
/// <param name="specification">The specification to negate.</param>
/// <returns>
/// Returns a specification that evaluates to <see langword="true"/> if the current specification is not satisfied;
/// otherwise, the specification evaluates to <see langword="false"/>.
/// </returns>
public static Specification<T> operator !(Specification<T> specification) => specification.Not();
}

/// <summary>
Expand Down
Loading
Loading