mirror of
https://github.com/VSadov/Satori.git
synced 2025-06-10 18:11:04 +09:00
Override TextWriter.Write{Line}{Async} on StringWriter (dotnet/corefx#30667)
* add overloads
* fix overloads
* add tests
* move test to StringWriterTests.netcoreapp.cs
* revert StringWriterTests.cs updates
* address PR feedback
* fix test
* amend comments
Commit migrated from 57bb0fc7d6
This commit is contained in:
parent
862e09c609
commit
7955437fe6
2 changed files with 144 additions and 3 deletions
|
@ -2,6 +2,7 @@
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
@ -36,5 +37,61 @@ namespace System.IO.Tests
|
|||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteAsync(Memory<char>.Empty, new CancellationToken(true)));
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteLineAsync(Memory<char>.Empty, new CancellationToken(true)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestWriteStringBuilder()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
sw.Write(sb);
|
||||
Assert.Equal(sb.ToString(), sw.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TestWriteAsyncStringBuilder()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
await sw.WriteAsync(sb);
|
||||
Assert.Equal(sb.ToString(), sw.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestWriteAsyncStringBuilderCancelled()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
cts.Cancel();
|
||||
Assert.Equal(TaskStatus.Canceled, sw.WriteAsync(sb, cts.Token).Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestWriteLineStringBuilder()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
sw.WriteLine(sb);
|
||||
Assert.Equal(sb.ToString() + Environment.NewLine, sw.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TestWriteLineAsyncStringBuilder()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
await sw.WriteLineAsync(sb);
|
||||
Assert.Equal(sb.ToString() + Environment.NewLine, sw.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestWriteLineAsyncStringBuilderCancelled()
|
||||
{
|
||||
StringBuilder sb = getSb();
|
||||
StringWriter sw = new StringWriter();
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
cts.Cancel();
|
||||
Assert.Equal(TaskStatus.Canceled, sw.WriteLineAsync(sb, cts.Token).Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -129,7 +129,7 @@ namespace System.IO
|
|||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added affter the Write(char[], ...) overload, and so in case
|
||||
// This overload was added after the Write(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
base.Write(buffer);
|
||||
return;
|
||||
|
@ -159,11 +159,29 @@ namespace System.IO
|
|||
}
|
||||
}
|
||||
|
||||
public override void Write(StringBuilder value)
|
||||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added after the Write(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
base.Write(value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isOpen)
|
||||
{
|
||||
throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
|
||||
}
|
||||
|
||||
_sb.Append(value);
|
||||
}
|
||||
|
||||
public override void WriteLine(ReadOnlySpan<char> buffer)
|
||||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added affter the WriteLine(char[], ...) overload, and so in case
|
||||
// This overload was added after the WriteLine(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
base.WriteLine(buffer);
|
||||
return;
|
||||
|
@ -178,8 +196,27 @@ namespace System.IO
|
|||
WriteLine();
|
||||
}
|
||||
|
||||
#region Task based Async APIs
|
||||
public override void WriteLine(StringBuilder value)
|
||||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added after the WriteLine(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
base.WriteLine(value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isOpen)
|
||||
{
|
||||
throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
|
||||
}
|
||||
|
||||
_sb.Append(value);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
#region Task based Async APIs
|
||||
|
||||
public override Task WriteAsync(char value)
|
||||
{
|
||||
Write(value);
|
||||
|
@ -209,6 +246,29 @@ namespace System.IO
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteAsync(StringBuilder value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added after the WriteAsync(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
return base.WriteAsync(value, cancellationToken);
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
if (!_isOpen)
|
||||
{
|
||||
throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
|
||||
}
|
||||
|
||||
_sb.Append(value);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(char value)
|
||||
{
|
||||
WriteLine(value);
|
||||
|
@ -221,6 +281,30 @@ namespace System.IO
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(StringBuilder value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (GetType() != typeof(StringWriter))
|
||||
{
|
||||
// This overload was added after the WriteLineAsync(char[], ...) overload, and so in case
|
||||
// a derived type may have overridden it, we need to delegate to it, which the base does.
|
||||
return base.WriteLineAsync(value, cancellationToken);
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
if (!_isOpen)
|
||||
{
|
||||
throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
|
||||
}
|
||||
|
||||
_sb.Append(value);
|
||||
WriteLine();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(char[] buffer, int index, int count)
|
||||
{
|
||||
WriteLine(buffer, index, count);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue