Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ internal static void EnsureInitialized()
_ = State.Value;
}

Comment thread
vbreuss marked this conversation as resolved.
internal static ITestFrameworkAdapter DetectFramework(IEnumerable<Type> types)
/// <summary>
/// Detects a test framework adapter from the provided types.
/// </summary>
/// <returns>
/// An instance of <see cref="ITestFrameworkAdapter" /> if a matching framework is found;
/// otherwise <see langword="null" />.
/// </returns>
internal static ITestFrameworkAdapter? DetectFramework(IEnumerable<Type> types)
{
Type frameworkInterface = typeof(ITestFrameworkAdapter);
foreach (Type frameworkType in types
Expand All @@ -48,18 +55,35 @@ internal static ITestFrameworkAdapter DetectFramework(IEnumerable<Type> types)
}
}

return new FallbackTestFramework();
return null;
}

private static InitializationState Initialize()
{
ExecuteCustomInitializers();

ITestFrameworkAdapter testFramework = DetectFramework(AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => Customize.aweXpect.Reflection().ExcludedAssemblyPrefixes.Get()
.All(excludedAssemblyPrefix => !assembly.FullName!.StartsWith(excludedAssemblyPrefix)))
.SelectMany(assembly => assembly.GetTypes().Where(x => !x.IsNestedPrivate)));
return new InitializationState(testFramework);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => Customize.aweXpect.Reflection().ExcludedAssemblyPrefixes.Get()
.All(excludedAssemblyPrefix => !assembly.FullName!.StartsWith(excludedAssemblyPrefix))))
{
try
{
ITestFrameworkAdapter? testFrameworkAdapter = DetectFramework(
assembly.GetTypes().Where(x => !x.IsNestedPrivate));
if (testFrameworkAdapter is not null)
{
return new InitializationState(testFrameworkAdapter);
}
}
catch (ReflectionTypeLoadException ex)
{
// Ignore any ReflectionTypeLoadException and continue with the next assembly.
Debug.WriteLine($"ReflectionTypeLoadException caught: {ex.Message}");
Debug.WriteLine(ex.StackTrace);
}
}

return new InitializationState(new FallbackTestFramework());
}

private static void ExecuteCustomInitializers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ namespace aweXpect.Core.Tests.Core.Initialization;
public sealed class AweXpectInitializationTests
{
[Fact]
public async Task DetectFramework_WhenAllFrameworksAreNotAvailable_ShouldUseFallbackAdapter()
public async Task DetectFramework_WhenAllFrameworksAreNotAvailable_ShouldReturnNull()
{
ITestFrameworkAdapter result = AweXpectInitialization.DetectFramework([typeof(UnavailableFrameworkAdapter),]);
ITestFrameworkAdapter? result = AweXpectInitialization.DetectFramework([typeof(UnavailableFrameworkAdapter),]);

await That(result.IsAvailable).IsFalse();
await That(result.GetType().Name).IsEqualTo("FallbackTestFramework");
await That(result).IsNull();
}

[Fact]
Expand Down
7 changes: 3 additions & 4 deletions Tests/aweXpect.Core.Tests/TraceWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ public async Task ForSuccessfulDelegateWithReturnValue_ShouldTraceSuccessfulVeri
await That(callback).ExecutesWithin(500.Milliseconds());
}

await That(traceWriter.Messages).IsEqualTo([
"Checking expectation for callback delegate returning int 4 in 0:00",
" Successfully verified that callback executes within 0:00.500",
]);
await That(traceWriter.Messages).HasCount(2);
await That(traceWriter.Messages[0]).IsEqualTo("Checking expectation for callback delegate returning int 4 in 0:00").AsPrefix();
await That(traceWriter.Messages[1]).IsEqualTo(" Successfully verified that callback executes within 0:00.500");
}

[Fact]
Expand Down