Skip to content
Open
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
111 changes: 111 additions & 0 deletions csharp/Platform.IO.Tests/ConsoleCancellationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Platform.IO.Tests
{
public class ConsoleCancellationTests
{
[Fact]
public void Constructor_InitializesProperties()
{
using var cancellation = new ConsoleCancellation();

Assert.NotNull(cancellation.Source);
Assert.False(cancellation.IsRequested);
Assert.True(cancellation.NotRequested);
}

[Fact]
public void ForceCancellation_SetsIsRequestedToTrue()
{
using var cancellation = new ConsoleCancellation();

Assert.False(cancellation.IsRequested);
Assert.True(cancellation.NotRequested);

cancellation.ForceCancellation();

Assert.True(cancellation.IsRequested);
Assert.False(cancellation.NotRequested);
}

[Fact]
public async Task Wait_ReturnsWhenCancellationRequested()
{
using var cancellation = new ConsoleCancellation();

// Start wait in background task
var waitTask = Task.Run(() => cancellation.Wait());

// Give wait a moment to start
await Task.Delay(100);

// Verify task is still running
Assert.False(waitTask.IsCompleted);

// Force cancellation
cancellation.ForceCancellation();

// Wait should complete quickly now
await waitTask;

Assert.True(waitTask.IsCompleted);
}

[Fact]
public void Token_IsCancellationRequested_ReflectsSourceState()
{
using var cancellation = new ConsoleCancellation();

Assert.False(cancellation.Token.IsCancellationRequested);

cancellation.ForceCancellation();

Assert.True(cancellation.Token.IsCancellationRequested);
}

[Fact]
public void Dispose_DoesNotThrowException()
{
var cancellation = new ConsoleCancellation();

// Should dispose without throwing
cancellation.Dispose();

// The base class DisposableBase doesn't allow multiple dispose calls by design
// This is correct behavior, so we just test that single dispose works
}

[Fact]
public void ConsoleCancelKeyPress_TriggersCancellation()
{
using var cancellation = new ConsoleCancellation();

Assert.False(cancellation.IsRequested);

// Since ConsoleCancelEventArgs is internal and hard to construct,
// we'll test this by directly calling ForceCancellation which simulates the same behavior
cancellation.ForceCancellation();

Assert.True(cancellation.IsRequested);
}

[Fact]
public void ConsoleCancelKeyPress_DoesNotCancelIfAlreadyRequested()
{
using var cancellation = new ConsoleCancellation();

// First cancellation
cancellation.ForceCancellation();
Assert.True(cancellation.IsRequested);

// Calling ForceCancellation again should not throw
cancellation.ForceCancellation();

// Should still be cancelled, no exception should be thrown
Assert.True(cancellation.IsRequested);
}
}
}
85 changes: 85 additions & 0 deletions csharp/Platform.IO.Tests/ConsoleHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.IO;
using Xunit;

namespace Platform.IO.Tests
{
public class ConsoleHelpersTests
{
[Fact]
public void GetOrReadArgumentTest_WithValidIndex()
{
var args = new[] { "arg1", "arg2", "arg3" };
var result = ConsoleHelpers.GetOrReadArgument(1, args);
Assert.Equal("arg2", result);
}

[Fact]
public void GetOrReadArgumentTest_WithValidIndexAndMessage()
{
var args = new[] { "arg1", "arg2", "arg3" };
var result = ConsoleHelpers.GetOrReadArgument(2, "Custom message", args);
Assert.Equal("arg3", result);
}

[Fact]
public void GetOrReadArgumentTest_WithInvalidIndex()
{
var args = new[] { "arg1", "arg2" };

// Simulate user input by redirecting stdin
var input = "user_input\n";
var inputStream = new StringReader(input);
Console.SetIn(inputStream);

var result = ConsoleHelpers.GetOrReadArgument(5, args);
Assert.Equal("user_input", result);
}

[Fact]
public void GetOrReadArgumentTest_WithEmptyInput()
{
var args = new string[] { };

// Simulate empty user input
var input = "\n";
var inputStream = new StringReader(input);
Console.SetIn(inputStream);

var result = ConsoleHelpers.GetOrReadArgument(0, args);
Assert.Equal("", result);
}

[Fact]
public void GetOrReadArgumentTest_TrimsWhitespace()
{
var args = new[] { " trimmed " };
var result = ConsoleHelpers.GetOrReadArgument(0, args);
Assert.Equal("trimmed", result);
}

[Fact]
public void GetOrReadArgumentTest_TrimsQuotes()
{
var args = new[] { "\"quoted\"" };
var result = ConsoleHelpers.GetOrReadArgument(0, args);
Assert.Equal("quoted", result);
}

[Fact]
public void GetOrReadArgumentTest_TrimsQuotesAndWhitespace()
{
var args = new[] { " \"quoted and trimmed\" " };
var result = ConsoleHelpers.GetOrReadArgument(0, args);
Assert.Equal("quoted and trimmed", result);
}

[Fact]
public void DebugTest_CallableWithoutException()
{
// Debug method should not throw exceptions
ConsoleHelpers.Debug("Test debug message");
ConsoleHelpers.Debug("Formatted message: {0}", "value");
}
}
}
Loading
Loading