Skip to content

Commit

Permalink
add simple setup factory
Browse files Browse the repository at this point in the history
  • Loading branch information
fakefeik committed Apr 17, 2024
1 parent f7df978 commit 2d3c2df
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override void Configure(ISetupBuilder fixture, ISetupBuilder test)
[TestCase(10)]
public void Test(int n)
{
var item3 = SimpleTestContext.Current.Get("item3");
var item3 = SimpleTestContext.Current.TryGet("item3");
item3.Should().NotBeNull();

((Counter)item3!).InvocationsCount.Should().Be(0);
Expand Down
9 changes: 9 additions & 0 deletions NUnit.Middlewares/ISetupFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SkbKontur.NUnit.Middlewares
{
public interface ISetupFactory
{
ISetup Create(Type setupType, object[] args);
}
}
12 changes: 6 additions & 6 deletions NUnit.Middlewares/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public static ISetupBuilder Use(this ISetupBuilder builder, SetUpAsync<TearDown>
});
}

public static ISetupBuilder UseSetup(this ISetupBuilder builder, ISetup setup)
public static ISetupBuilder UseSetup<TSetup>(this ISetupBuilder builder, params object[] args)
where TSetup : ISetup
{
return builder.Use(async test =>
{
var factory = test.TryGetFromThisOrParentContext<ISetupFactory>() ?? setupFactory;
var setup = factory.Create(typeof(TSetup), args);

await setup.SetUpAsync(test).ConfigureAwait(false);

return () => setup.TearDownAsync(test);
});
}

public static ISetupBuilder UseSetup<TSetup>(this ISetupBuilder builder)
where TSetup : ISetup, new()
{
return builder.UseSetup(new TSetup());
}
private static readonly ISetupFactory setupFactory = new SetupFactory();
}
}
12 changes: 12 additions & 0 deletions NUnit.Middlewares/SetupFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace SkbKontur.NUnit.Middlewares
{
public class SetupFactory : ISetupFactory
{
public ISetup Create(Type setupType, object[] args)
{
return (ISetup)Activator.CreateInstance(setupType, args);
}
}
}
2 changes: 1 addition & 1 deletion NUnit.Middlewares/SimpleTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private SimpleTestContext(ITest test)

public static SimpleTestContext Current => new(TestExecutionContext.CurrentContext.CurrentTest);

public object? Get(string key) => test.GetRecursive(key);
public object? TryGet(string key) => test.TryGetRecursive(key);
public bool ContainsKey(string key) => test.ContainsKeyRecursive(key);
public IReadOnlyList<object>? this[string key] => (IReadOnlyList<object>?)test.ListRecursive(key);

Expand Down
63 changes: 49 additions & 14 deletions NUnit.Middlewares/SimpleTestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,91 @@ public static class SimpleTestContextExtensions
{
public static T Get<T>(this IPropertyBag properties)
{
return properties.Get<T>(typeof(T).Name);
return properties.Get<T>(TypeKey<T>());
}

public static T? TryGet<T>(this IPropertyBag properties)
{
return properties.TryGet<T>(TypeKey<T>());
}

public static T Get<T>(this IPropertyBag properties, string key)
{
return (T?)properties.Get(key)
return properties.TryGet<T>(key)
?? throw new KeyNotFoundException($"Cannot find item by key {key} in test properties");
}

public static T? TryGet<T>(this IPropertyBag properties, string key)
{
return (T?)properties.Get(key);
}

public static T GetFromThisOrParentContext<T>(this ITest test)
{
return test.GetFromThisOrParentContext<T>(typeof(T).Name);
return test.GetFromThisOrParentContext<T>(TypeKey<T>());
}

public static T? TryGetFromThisOrParentContext<T>(this ITest test)
{
return test.TryGetFromThisOrParentContext<T>(TypeKey<T>());
}

public static T GetFromThisOrParentContext<T>(this ITest test, string key)
{
return (T)test.GetRecursiveOrThrow(key);
return (T)test.GetRecursive(key);
}

public static T? TryGetFromThisOrParentContext<T>(this ITest test, string key)
{
return (T?)test.TryGetRecursive(key);
}

public static T Get<T>(this SimpleTestContext context)
{
return context.Get<T>(typeof(T).Name);
return context.Get<T>(TypeKey<T>());
}

public static T? TryGet<T>(this SimpleTestContext context)
{
return context.TryGet<T>(TypeKey<T>());
}

public static T Get<T>(this SimpleTestContext context, string key)
{
return (T?)context.Get(key)
return context.TryGet<T>(key)
?? throw new KeyNotFoundException($"Cannot find item by key {key} in test context");
}

public static T? TryGet<T>(this SimpleTestContext context, string key)
{
return (T?)context.TryGet(key);
}

public static void Set<T>(this IPropertyBag properties, T value)
where T : notnull
{
properties.Set(typeof(T).Name, value);
properties.Set(TypeKey<T>(), value);
}

public static object GetRecursiveOrThrow(this ITest test, string key)
public static object GetRecursive(this ITest test, string key)
{
return test.GetRecursive(key)
?? throw new KeyNotFoundException($"Cannot find item by key {key} in test {test.Name} or its parents");
return test.TryGetRecursive(key)
?? new KeyNotFoundException($"Cannot find item by key {key} in test {test.Name} or its parents");
}

public static object? GetRecursive(this ITest test, string key) =>
GetRecursive(test, key, (p, k) => p.Get(k));
public static object? TryGetRecursive(this ITest test, string key) =>
GetRecursive(test, key, static (p, k) => p.Get(k));

public static bool ContainsKeyRecursive(this ITest test, string key) =>
GetRecursive(test, key, (p, k) => p.ContainsKey(k) ? true : (bool?)null) ?? false;
GetRecursive(test, key, static (p, k) => p.ContainsKey(k) ? true : (bool?)null) ?? false;

public static IList? ListRecursive(this ITest test, string key) =>
GetRecursive(test, key, (p, k) => p[k]);
GetRecursive(test, key, static (p, k) => p[k]);

private static string TypeKey<T>()
{
return $"nunit-middlewares.{typeof(T).Name}";
}

private static T? GetRecursive<T>(ITest leaf, string key, Func<IPropertyBag, string, T?> getValue)
{
Expand Down

0 comments on commit 2d3c2df

Please sign in to comment.