Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions PowerKit.Tests/CellTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using FluentAssertions;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class CellTests
{
[Fact]
public void TryOpen_ValueNotSet_Test()
{
// Arrange
var cell = new Cell<int?>();

// Act
var result = cell.TryOpen(out var value);

// Assert
result.Should().BeFalse();
value.Should().BeNull();
}

[Fact]
public void Store_TryOpen_Test()
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
// Arrange
var cell = new Cell<int?>();

// Act
cell.Store(42);
var result = cell.TryOpen(out var value);

// Assert
result.Should().BeTrue();
value.Should().Be(42);
}

[Fact]
public void Store_Null_TryOpen_Test()
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
// Arrange
var cell = new Cell<int?>();

// Act
cell.Store(null);
var result = cell.TryOpen(out var value);

// Assert
result.Should().BeTrue();
value.Should().BeNull();
}
}
36 changes: 36 additions & 0 deletions PowerKit/Cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace PowerKit;

/// <summary>
/// Container for a value that may or may not be set.
/// Essentially <see cref="System.Nullable{T}" />, but for cases where null is also a valid value.
/// </summary>
internal class Cell<T>
{
private bool _isValueSet;
private T _value = default!;

/// <summary>
/// Stores the specified value in the cell.
/// </summary>
public void Store(T value)
{
_value = value;
_isValueSet = true;
}

/// <summary>
/// Tries to retrieve the value stored in the cell.
/// Returns <see langword="true" /> if a value has been stored, <see langword="false" /> otherwise.
/// </summary>
public bool TryOpen(out T value)
Comment thread
Tyrrrz marked this conversation as resolved.
{
if (_isValueSet)
{
value = _value;
return true;
}

value = default!;
return false;
}
}