Skip to content

Commit d83aa2f

Browse files
authored
Release/v0.2.0 (#3)
* act の戻り値が `Task` や `IEnumerable<T>` の場合、`Act()` 内で即時実行して結果を確定するよう更新 (#1) * `ActAsync()` を削除 * `Assert()` の exception パラメーターを省略可能化。省略時に `TestActual` に例外が含まれていれば再スロー * テストを修正
1 parent a46ce12 commit d83aa2f

10 files changed

+331
-184
lines changed

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ deploy:
1616
appveyor_repo_tag: true
1717
- provider: NuGet
1818
api_key:
19-
secure: zUlOhbjj+3Jsywco3QlyLXz4zSXS9fqQdEWTOCpmwzEl5cBLHFSrYnOR8xnNaSaB
19+
secure: vQBIGqLxb5sEvl0To9hWNmzmzaXw4INcIDqrPjj5pjQoLuYQzDj9mrqEdJI6NZJK
2020
on:
2121
appveyor_repo_tag: true

src/Inasync.TestAA/Inasync.TestAA.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageProjectUrl>https://github.com/in-async/TestAA</PackageProjectUrl>
1010
<PackageLicenseUrl>https://github.com/in-async/TestAA/blob/master/LICENSE</PackageLicenseUrl>
1111
<PackageTags>library test unittest aaa</PackageTags>
12-
<Version>0.1.0</Version>
12+
<Version>0.2.0</Version>
1313
</PropertyGroup>
1414

1515
</Project>

src/Inasync.TestAA/TestAA.Act.cs

-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
32

43
namespace Inasync {
54

@@ -44,50 +43,5 @@ public static TestActual<TReturn> Act<TReturn>(Func<TReturn> act) {
4443
return new TestActual<TReturn>(default, exception: ex);
4544
}
4645
}
47-
48-
/// <summary>
49-
/// テスト対象の非同期デリゲートを実行します。
50-
/// </summary>
51-
/// <param name="act">テスト対象の非同期デリゲート。</param>
52-
/// <returns>非同期デリゲートの実行結果。</returns>
53-
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
54-
public static Task<TestActual> ActAsync(Func<Task> act) {
55-
if (act == null) { throw new ArgumentNullException(nameof(act)); }
56-
57-
return Internal();
58-
59-
async Task<TestActual> Internal() {
60-
try {
61-
await act().ConfigureAwait(false);
62-
return new TestActual(exception: null);
63-
}
64-
catch (Exception ex) {
65-
return new TestActual(exception: ex);
66-
}
67-
}
68-
}
69-
70-
/// <summary>
71-
/// テスト対象の非同期デリゲートを実行します。
72-
/// </summary>
73-
/// <typeparam name="TReturn">テスト対象の非同期デリゲートの戻り値の型。</typeparam>
74-
/// <param name="act">テスト対象の非同期デリゲート。</param>
75-
/// <returns>非同期デリゲートの実行結果。</returns>
76-
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
77-
public static Task<TestActual<TReturn>> ActAsync<TReturn>(Func<Task<TReturn>> act) {
78-
if (act == null) { throw new ArgumentNullException(nameof(act)); }
79-
80-
return Internal();
81-
82-
async Task<TestActual<TReturn>> Internal() {
83-
try {
84-
var @return = await act().ConfigureAwait(false);
85-
return new TestActual<TReturn>(@return, exception: null);
86-
}
87-
catch (Exception ex) {
88-
return new TestActual<TReturn>(default, exception: ex);
89-
}
90-
}
91-
}
9246
}
9347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Inasync {
7+
8+
public static partial class TestAA {
9+
10+
/// <summary>
11+
/// テスト対象の非同期デリゲートを同期的に実行します。
12+
/// </summary>
13+
/// <param name="act">テスト対象の非同期デリゲート。</param>
14+
/// <returns>非同期デリゲートの実行結果。</returns>
15+
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
16+
public static TestActual Act(Func<Task> act) {
17+
if (act == null) { throw new ArgumentNullException(nameof(act)); }
18+
19+
return Act(() => act().GetAwaiter().GetResult());
20+
}
21+
22+
/// <summary>
23+
/// テスト対象の非同期デリゲートを同期的に実行します。
24+
/// </summary>
25+
/// <typeparam name="TReturn">テスト対象の非同期デリゲートの戻り値の型。</typeparam>
26+
/// <param name="act">テスト対象の非同期デリゲート。</param>
27+
/// <returns>非同期デリゲートの実行結果。</returns>
28+
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
29+
public static TestActual<TReturn> Act<TReturn>(Func<Task<TReturn>> act) {
30+
if (act == null) { throw new ArgumentNullException(nameof(act)); }
31+
32+
return Act(() => act().GetAwaiter().GetResult());
33+
}
34+
35+
/// <summary>
36+
/// テスト対象のデリゲートを実行し、戻り値のシーケンスを配列化します。
37+
/// </summary>
38+
/// <typeparam name="TItem">テスト対象のデリゲートが戻り値とするシーケンスの要素の型。</typeparam>
39+
/// <param name="act">テスト対象のデリゲート。</param>
40+
/// <returns>デリゲートの実行結果。</returns>
41+
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
42+
public static TestActual<TItem[]> Act<TItem>(Func<IEnumerable<TItem>> act) {
43+
if (act == null) { throw new ArgumentNullException(nameof(act)); }
44+
45+
return Act(() => act().ToArray());
46+
}
47+
48+
/// <summary>
49+
/// テスト対象の非同期デリゲートを同期的に実行し、戻り値のシーケンスを配列化します。
50+
/// </summary>
51+
/// <typeparam name="TItem">テスト対象の非同期デリゲートが戻り値とするシーケンスの要素の型。</typeparam>
52+
/// <param name="act">テスト対象の非同期デリゲート。</param>
53+
/// <returns>非同期デリゲートの実行結果。</returns>
54+
/// <exception cref="ArgumentNullException"><paramref name="act"/> is <c>null</c>.</exception>
55+
public static TestActual<TItem[]> Act<TItem>(Func<Task<IEnumerable<TItem>>> act) {
56+
if (act == null) { throw new ArgumentNullException(nameof(act)); }
57+
58+
return Act(() => act().GetAwaiter().GetResult().ToArray());
59+
}
60+
}
61+
}

src/Inasync.TestAA/TestAA.Assert.cs

+16-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ public static partial class TestAA {
88
/// Act の実行結果を検証します。
99
/// </summary>
1010
/// <param name="actual">Act の実行結果。</param>
11-
/// <param name="exception">Act で生じた例外を検証するデリゲート。</param>
12-
/// <param name="others">その他の Act の実行結果を検証するデリゲート。</param>
13-
/// <exception cref="ArgumentNullException"><paramref name="exception"/> is <c>null</c>.</exception>
14-
public static void Assert(this TestActual actual, Action<Exception> exception, Action others = null) {
15-
if (exception == null) { throw new ArgumentNullException(nameof(exception)); }
11+
/// <param name="exception">Act で生じた例外を検証するデリゲート。<c>null</c> の場合、<paramref name="actual"/> に例外が含まれていれば再スローされる。</param>
12+
/// <param name="others">その他の Act の実行結果を検証するデリゲート。追加の検証が必要なければ <c>null</c>。</param>
13+
public static void Assert(this TestActual actual, Action<Exception> exception = null, Action others = null) {
14+
if (exception != null) {
15+
exception(actual.Exception);
16+
}
17+
else if (actual.Exception != null) { throw actual.Exception; }
1618

17-
exception(actual.Exception);
1819
others?.Invoke();
1920
}
2021

@@ -24,19 +25,22 @@ public static void Assert(this TestActual actual, Action<Exception> exception, A
2425
/// <typeparam name="TReturn">Act の戻り値の型。</typeparam>
2526
/// <param name="actual">Act の実行結果。</param>
2627
/// <param name="return">Act の戻り値を検証するデリゲート。</param>
27-
/// <param name="exception">Act で生じた例外を検証するデリゲート。</param>
28-
/// <param name="others">その他の Act の実行結果を検証するデリゲート。</param>
29-
/// <exception cref="ArgumentNullException"><paramref name="return"/> or <paramref name="exception"/> is <c>null</c>.</exception>
30-
public static void Assert<TReturn>(this TestActual<TReturn> actual, Action<TReturn> @return, Action<Exception> exception, Action others = null) {
28+
/// <param name="exception">Act で生じた例外を検証するデリゲート。<c>null</c> の場合、<paramref name="actual"/> に例外が含まれていれば再スローされる。</param>
29+
/// <param name="others">その他の Act の実行結果を検証するデリゲート。追加の検証が必要なければ <c>null</c>。</param>
30+
/// <exception cref="ArgumentNullException"><paramref name="return"/> is <c>null</c>.</exception>
31+
public static void Assert<TReturn>(this TestActual<TReturn> actual, Action<TReturn> @return, Action<Exception> exception = null, Action others = null) {
3132
if (@return == null) { throw new ArgumentNullException(nameof(@return)); }
32-
if (exception == null) { throw new ArgumentNullException(nameof(exception)); }
3333

3434
// 戻り値の検証は非例外時のみ (例外時には actual.Return は必ず default なので、改めて検証する必要が無い)。
3535
if (actual.Exception == null) {
3636
@return(actual.Return);
3737
}
3838

39-
exception(actual.Exception);
39+
if (exception != null) {
40+
exception(actual.Exception);
41+
}
42+
else if (actual.Exception != null) { throw actual.Exception; }
43+
4044
others?.Invoke();
4145
}
4246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace Inasync.Tests {
5+
6+
[TestClass]
7+
public class TestAA_Act_Tests {
8+
9+
[TestMethod]
10+
public void Act() {
11+
Action TestCase(int testNumber, Action act, Type exceptionType, Type expectedExceptionType = null) => () => {
12+
var msg = "No." + testNumber;
13+
14+
// Act
15+
TestActual testActual = default;
16+
Exception exception = null;
17+
try {
18+
testActual = TestAA.Act(act);
19+
}
20+
catch (Exception ex) {
21+
exception = ex;
22+
}
23+
24+
// Assert
25+
if (exception == null) {
26+
Assert.AreEqual(typeof(TestActual), testActual.GetType(), msg);
27+
Assert.AreEqual(exceptionType, testActual.Exception?.GetType(), msg);
28+
}
29+
Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg);
30+
};
31+
32+
foreach (var action in new[]{
33+
TestCase( 0, act: null , exceptionType: null , expectedExceptionType: typeof(ArgumentNullException)),
34+
TestCase( 1, act: () => { } , exceptionType: null ),
35+
TestCase( 2, act: () => throw new DummyException(), exceptionType: typeof(DummyException)),
36+
}) { action(); }
37+
}
38+
39+
[TestMethod]
40+
public void Act_TReturn() {
41+
Action TestCase(int testNumber, Func<DummyObject> act, (DummyObject @return, Type exceptionType) expected, Type expectedExceptionType = null) => () => {
42+
var msg = "No." + testNumber;
43+
44+
// Act
45+
TestActual<DummyObject> testActual = default;
46+
Exception exception = null;
47+
try {
48+
testActual = TestAA.Act(act);
49+
}
50+
catch (Exception ex) {
51+
exception = ex;
52+
}
53+
54+
// Assert
55+
if (exception == null) {
56+
Assert.AreEqual(expected.exceptionType, testActual.Exception?.GetType(), msg);
57+
Assert.AreEqual(expected.@return, testActual.Return, msg);
58+
}
59+
Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg);
60+
};
61+
62+
var obj = new DummyObject();
63+
foreach (var action in new[]{
64+
TestCase( 0, act: null , expected: default , expectedExceptionType: typeof(ArgumentNullException)),
65+
TestCase( 1, act: () => obj , expected: (obj , null) ),
66+
TestCase( 2, act: () => throw new DummyException(), expected: (null, typeof(DummyException))),
67+
}) { action(); }
68+
}
69+
70+
#region Helpers
71+
72+
private sealed class DummyException : Exception { }
73+
74+
private sealed class DummyObject { }
75+
76+
#endregion Helpers
77+
}
78+
}

0 commit comments

Comments
 (0)