-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* act の戻り値が `Task` や `IEnumerable<T>` の場合、`Act()` 内で即時実行して結果を確定するよう更新 (#1) * `ActAsync()` を削除 * `Assert()` の exception パラメーターを省略可能化。省略時に `TestActual` に例外が含まれていれば再スロー * テストを修正
- Loading branch information
Showing
10 changed files
with
331 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Inasync { | ||
|
||
public static partial class TestAA { | ||
|
||
/// <summary> | ||
/// テスト対象の非同期デリゲートを同期的に実行します。 | ||
/// </summary> | ||
/// <param name="act">テスト対象の非同期デリゲート。</param> | ||
/// <returns>非同期デリゲートの実行結果。</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception> | ||
public static TestActual Act(Func<Task> act) { | ||
if (act == null) { throw new ArgumentNullException(nameof(act)); } | ||
|
||
return Act(() => act().GetAwaiter().GetResult()); | ||
} | ||
|
||
/// <summary> | ||
/// テスト対象の非同期デリゲートを同期的に実行します。 | ||
/// </summary> | ||
/// <typeparam name="TReturn">テスト対象の非同期デリゲートの戻り値の型。</typeparam> | ||
/// <param name="act">テスト対象の非同期デリゲート。</param> | ||
/// <returns>非同期デリゲートの実行結果。</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception> | ||
public static TestActual<TReturn> Act<TReturn>(Func<Task<TReturn>> act) { | ||
if (act == null) { throw new ArgumentNullException(nameof(act)); } | ||
|
||
return Act(() => act().GetAwaiter().GetResult()); | ||
} | ||
|
||
/// <summary> | ||
/// テスト対象のデリゲートを実行し、戻り値のシーケンスを配列化します。 | ||
/// </summary> | ||
/// <typeparam name="TItem">テスト対象のデリゲートが戻り値とするシーケンスの要素の型。</typeparam> | ||
/// <param name="act">テスト対象のデリゲート。</param> | ||
/// <returns>デリゲートの実行結果。</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception> | ||
public static TestActual<TItem[]> Act<TItem>(Func<IEnumerable<TItem>> act) { | ||
if (act == null) { throw new ArgumentNullException(nameof(act)); } | ||
|
||
return Act(() => act().ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// テスト対象の非同期デリゲートを同期的に実行し、戻り値のシーケンスを配列化します。 | ||
/// </summary> | ||
/// <typeparam name="TItem">テスト対象の非同期デリゲートが戻り値とするシーケンスの要素の型。</typeparam> | ||
/// <param name="act">テスト対象の非同期デリゲート。</param> | ||
/// <returns>非同期デリゲートの実行結果。</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception> | ||
public static TestActual<TItem[]> Act<TItem>(Func<Task<IEnumerable<TItem>>> act) { | ||
if (act == null) { throw new ArgumentNullException(nameof(act)); } | ||
|
||
return Act(() => act().GetAwaiter().GetResult().ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Inasync.Tests { | ||
|
||
[TestClass] | ||
public class TestAA_Act_Tests { | ||
|
||
[TestMethod] | ||
public void Act() { | ||
Action TestCase(int testNumber, Action act, Type exceptionType, Type expectedExceptionType = null) => () => { | ||
var msg = "No." + testNumber; | ||
|
||
// Act | ||
TestActual testActual = default; | ||
Exception exception = null; | ||
try { | ||
testActual = TestAA.Act(act); | ||
} | ||
catch (Exception ex) { | ||
exception = ex; | ||
} | ||
|
||
// Assert | ||
if (exception == null) { | ||
Assert.AreEqual(typeof(TestActual), testActual.GetType(), msg); | ||
Assert.AreEqual(exceptionType, testActual.Exception?.GetType(), msg); | ||
} | ||
Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg); | ||
}; | ||
|
||
foreach (var action in new[]{ | ||
TestCase( 0, act: null , exceptionType: null , expectedExceptionType: typeof(ArgumentNullException)), | ||
TestCase( 1, act: () => { } , exceptionType: null ), | ||
TestCase( 2, act: () => throw new DummyException(), exceptionType: typeof(DummyException)), | ||
}) { action(); } | ||
} | ||
|
||
[TestMethod] | ||
public void Act_TReturn() { | ||
Action TestCase(int testNumber, Func<DummyObject> act, (DummyObject @return, Type exceptionType) expected, Type expectedExceptionType = null) => () => { | ||
var msg = "No." + testNumber; | ||
|
||
// Act | ||
TestActual<DummyObject> testActual = default; | ||
Exception exception = null; | ||
try { | ||
testActual = TestAA.Act(act); | ||
} | ||
catch (Exception ex) { | ||
exception = ex; | ||
} | ||
|
||
// Assert | ||
if (exception == null) { | ||
Assert.AreEqual(expected.exceptionType, testActual.Exception?.GetType(), msg); | ||
Assert.AreEqual(expected.@return, testActual.Return, msg); | ||
} | ||
Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg); | ||
}; | ||
|
||
var obj = new DummyObject(); | ||
foreach (var action in new[]{ | ||
TestCase( 0, act: null , expected: default , expectedExceptionType: typeof(ArgumentNullException)), | ||
TestCase( 1, act: () => obj , expected: (obj , null) ), | ||
TestCase( 2, act: () => throw new DummyException(), expected: (null, typeof(DummyException))), | ||
}) { action(); } | ||
} | ||
|
||
#region Helpers | ||
|
||
private sealed class DummyException : Exception { } | ||
|
||
private sealed class DummyObject { } | ||
|
||
#endregion Helpers | ||
} | ||
} |
Oops, something went wrong.