-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3f348b
commit 2df474a
Showing
17 changed files
with
352 additions
and
145 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
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
src/StaticSettingsTests/CombinationsTests.CallbackResults.verified.txt
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,11 @@ | ||
{ | ||
beforeKeys: [ | ||
10, | ||
value2 | ||
], | ||
afterKeys: [ | ||
10, | ||
value2 | ||
], | ||
afterResult: 10 value2 | ||
} |
6 changes: 6 additions & 0 deletions
6
src/StaticSettingsTests/CombinationsTests.CallbacksTest.verified.txt
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,6 @@ | ||
{ | ||
1, value1: 1 value1, | ||
1, value2: 1 value2, | ||
10, value1: 10 value1, | ||
10, value2: 10 value2 | ||
} |
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
src/StaticSettingsTests/CombinationsTests.ExceptionCallbackResults.verified.txt
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,15 @@ | ||
{ | ||
beforeKeys: [ | ||
10, | ||
value2 | ||
], | ||
exceptionKeys: [ | ||
10, | ||
value2 | ||
], | ||
exceptionResult: { | ||
$type: Exception, | ||
Type: Exception, | ||
Message: Message | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/StaticSettingsTests/CombinationsTests.ExceptionCallbacksTest.verified.txt
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,6 @@ | ||
{ | ||
1, value1: Exception: Message, | ||
1, value2: Exception: Message, | ||
10, value1: Exception: Message, | ||
10, value2: Exception: Message | ||
} |
File renamed without changes.
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,233 @@ | ||
public class CombinationsTests | ||
{ | ||
#region GlobalCaptureExceptions | ||
|
||
[ModuleInitializer] | ||
public static void Initialize() => | ||
CombinationSettings.CaptureExceptions(); | ||
|
||
#endregion | ||
|
||
[Fact] | ||
public Task Defaults() | ||
{ | ||
string[] list = ["A", "b", "C"]; | ||
return Combination().Verify( | ||
_ => _.ToLower(), | ||
list); | ||
} | ||
|
||
[Fact] | ||
public async Task CallbacksTest() | ||
{ | ||
var beforeCalled = false; | ||
IReadOnlyList<object?>? beforeKeys = null; | ||
object? afterResult = null; | ||
IReadOnlyList<object?>? afterKeys = null; | ||
IReadOnlyList<object?>? exceptionKeys = null; | ||
Exception? exceptionResult = null; | ||
var afterCalled = false; | ||
var exceptionCalled = false; | ||
int[] params1 = [1, 10]; | ||
string[] params2 = ["value1", "value2"]; | ||
CombinationSettings.UseCallbacks( | ||
keys => | ||
{ | ||
beforeKeys = keys; | ||
beforeCalled = true; | ||
return Task.CompletedTask; | ||
}, | ||
(keys, result) => | ||
{ | ||
afterResult = result; | ||
afterKeys = keys; | ||
afterCalled = true; | ||
return Task.CompletedTask; | ||
}, | ||
(keys, exception) => | ||
{ | ||
exceptionResult = exception; | ||
exceptionKeys = keys; | ||
exceptionCalled = true; | ||
return Task.CompletedTask; | ||
}); | ||
await Combination() | ||
.Verify( | ||
(param1, param2) => $"{param1} {param2}", | ||
params1, | ||
params2); | ||
Assert.True(beforeCalled); | ||
Assert.True(afterCalled); | ||
Assert.False(exceptionCalled); | ||
await Verify(new | ||
{ | ||
beforeKeys, | ||
afterKeys, | ||
afterResult, | ||
exceptionKeys, | ||
exceptionResult | ||
}) | ||
.UseMethodName("CallbackResults"); | ||
} | ||
|
||
[Fact] | ||
public async Task ExceptionCallbacksTest() | ||
{ | ||
var beforeCalled = false; | ||
IReadOnlyList<object?>? beforeKeys = null; | ||
object? afterResult = null; | ||
IReadOnlyList<object?>? afterKeys = null; | ||
IReadOnlyList<object?>? exceptionKeys = null; | ||
Exception? exceptionResult = null; | ||
var afterCalled = false; | ||
var exceptionCalled = false; | ||
int[] params1 = [1, 10]; | ||
string[] params2 = ["value1", "value2"]; | ||
CombinationSettings.UseCallbacks( | ||
keys => | ||
{ | ||
beforeKeys = keys; | ||
beforeCalled = true; | ||
return Task.CompletedTask; | ||
}, | ||
(keys, result) => | ||
{ | ||
afterResult = result; | ||
afterKeys = keys; | ||
afterCalled = true; | ||
return Task.CompletedTask; | ||
}, | ||
(keys, exception) => | ||
{ | ||
exceptionResult = exception; | ||
exceptionKeys = keys; | ||
exceptionCalled = true; | ||
return Task.CompletedTask; | ||
}); | ||
|
||
static string Method(int i, string s) => | ||
throw new("Message"); | ||
|
||
await Combination() | ||
.Verify( | ||
Method, | ||
params1, | ||
params2); | ||
Assert.True(beforeCalled); | ||
Assert.False(afterCalled); | ||
Assert.True(exceptionCalled); | ||
await Verify(new | ||
{ | ||
beforeKeys, | ||
afterKeys, | ||
afterResult, | ||
exceptionKeys, | ||
exceptionResult | ||
}) | ||
.UseMethodName("ExceptionCallbackResults") | ||
.IgnoreStackTrace(); | ||
} | ||
|
||
[Fact] | ||
public Task WithCaptureExceptions() | ||
{ | ||
string[] a = ["A", "b", "C"]; | ||
return Combination(captureExceptions: true) | ||
.Verify( | ||
a => | ||
{ | ||
if (a == "b") | ||
{ | ||
throw new ArgumentException("B is not allowed"); | ||
} | ||
return a.ToLower(); | ||
}, | ||
a); | ||
} | ||
|
||
[Fact] | ||
public Task WithNoCaptureExceptions() | ||
{ | ||
string[] a = ["A", "b", "C"]; | ||
return Assert.ThrowsAsync<ArgumentException>( | ||
() => Combination(captureExceptions: false) | ||
.Verify( | ||
a => | ||
{ | ||
if (a == "b") | ||
{ | ||
throw new ArgumentException("B is not allowed"); | ||
} | ||
return a.ToLower(); | ||
}, | ||
a)); | ||
} | ||
|
||
static string BuildAddress(int streetNumber, string street, string city) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(street); | ||
ArgumentException.ThrowIfNullOrWhiteSpace(city); | ||
ArgumentOutOfRangeException.ThrowIfLessThan(streetNumber, 1); | ||
|
||
return $"{streetNumber} {street}, {city}"; | ||
} | ||
|
||
#region CombinationSample_CaptureExceptionsFalse | ||
|
||
[Fact] | ||
public Task BuildAddressExceptionsDisabledTest() | ||
{ | ||
int[] streetNumbers = [1, 10]; | ||
string[] streets = ["Smith St", "Wallace St"]; | ||
string[] cities = ["Sydney", "Chicago"]; | ||
return Combination(captureExceptions: false) | ||
.Verify( | ||
BuildAddress, | ||
streetNumbers, | ||
streets, | ||
cities); | ||
} | ||
|
||
#endregion | ||
|
||
#region CombinationSample_CustomSerializationModuleInitializer | ||
|
||
static CustomCombinationConverter customConverter = new(); | ||
|
||
[ModuleInitializer] | ||
public static void Init() => | ||
VerifierSettings.AddExtraSettings(_ => _.Converters.Insert(0, customConverter)); | ||
|
||
#endregion | ||
|
||
#region CombinationSample_CustomSerialization | ||
|
||
[Fact] | ||
public Task Combination_CustomSerialization() | ||
{ | ||
int[] streetNumbers = [1, 10]; | ||
string[] streets = ["Smith St", "Wallace St"]; | ||
string[] cities = ["Sydney", "Chicago"]; | ||
return Combination() | ||
.Verify( | ||
BuildAddress, | ||
streetNumbers, | ||
streets, | ||
cities); | ||
} | ||
|
||
#endregion | ||
|
||
#region CombinationSample_CustomSerializationConverter | ||
|
||
class CustomCombinationConverter : | ||
CombinationResultsConverter | ||
{ | ||
protected override string BuildPropertyName(IReadOnlyList<CombinationKey> keys) => | ||
string.Join(", ", keys.Select(_ => _.Value)); | ||
} | ||
|
||
#endregion | ||
} |
Oops, something went wrong.