Files
dotnet-runtime/src/libraries/System.IO.FileSystem/tests/FSAssert.cs
T
Stephen Toub 60d1224ddd Re-enable StyleCop warnings SA1028 and SA1518 (trailing whitespace and blank lines) (dotnet/corefx#40113)
Mechanical/automated change.  The only manual changes were a) removing the two rules from the CodeAnalysis.ruleset file, b) removing spaces from one line of a .targets file that is used to generate a .cs file, and c) reverting the changes in a few test files where the naive replacement happened in the middle of a verbatim string where the whitespace actually mattered.

Commit migrated from https://github.com/dotnet/corefx/commit/09a6e66b0c2b31bd0f6e7e3170537d14027aeed0
2019-08-07 21:07:58 -04:00

74 lines
2.7 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// 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.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public static class FSAssert
{
public static void ThrowsSharingViolation(Action testCode)
{
Exception e = Assert.ThrowsAny<Exception>(testCode);
Assert.True(e is UnauthorizedAccessException || e is IOException, "Exception should be either UnauthorizedAccessException or IOException: " + e.ToString());
}
public static void CompletesSynchronously(Task task)
{
// The purpose of this method is to try and ensure that a task does a particular
// piece of work (like return a result or throw) synchronously.
// We cannot use Task's IAsyncResult implementation since it doesn't set
// CompletedSynchronously to anything but false.
TaskStatus status = task.Status;
Assert.NotEqual(TaskStatus.Faulted, status);
if (status == TaskStatus.Canceled)
{
// Could have happened synchronously. Don't assert but trigger the exception
task.GetAwaiter().GetResult();
}
else
{
// first wait for the task to complete without throwing
((IAsyncResult)task).AsyncWaitHandle.WaitOne();
// now assert, we ignore the result of the task intentionally,
// As it previously did not complete synchronously.
Assert.Equal(TaskStatus.RanToCompletion, status);
}
return;
}
public static T CompletesSynchronously<T>(Task<T> task)
{
CompletesSynchronously((Task)task);
// this should not throw or wait since we waited above and threw
// when it didn't complete synchronously.
return task.Result;
}
public static void IsCancelled(Task task, CancellationToken ct)
{
Assert.True(task.IsCanceled);
OperationCanceledException tce = Assert.ThrowsAny<OperationCanceledException>(() => task.GetAwaiter().GetResult());
Assert.NotNull(tce);
Assert.Equal(ct, tce.CancellationToken);
}
public static void EqualWhenOrdered<T>(IEnumerable<T> expected, IEnumerable<T> actual)
{
Assert.Equal(expected.OrderBy(e => e).Select(o => o), actual.OrderBy(a => a).Select(o => o));
}
}
}