Skip to content

Commit a230bcf

Browse files
committed
[test] Add test against Family
1 parent c881af9 commit a230bcf

File tree

6 files changed

+106
-29
lines changed

6 files changed

+106
-29
lines changed

Depository.Abstraction/Interfaces/IDepositoryFamily.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface IDepositoryFamily
44
{
5-
public Task<List<object>> GetChildren(object father);
6-
public Task<List<object>> GetParents(object child);
5+
public Task<List<object>> GetChildrenAsync(object father);
6+
public Task<List<object>> GetParentsAsync(object child);
77
}

Depository.Core/Depository.Family.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class Depository
77

88
private readonly ConditionalWeakTable<object, List<WeakReference>> _fatherToChildRelation = new();
99
private readonly ConditionalWeakTable<object, List<WeakReference>> _childToFatherRelation = new();
10-
public Task<List<object>> GetChildren(object father)
10+
public Task<List<object>> GetChildrenAsync(object father)
1111
{
1212
if (_fatherToChildRelation.TryGetValue(father, out var refs))
1313
{
@@ -17,7 +17,7 @@ public Task<List<object>> GetChildren(object father)
1717
return Task.FromResult(new List<object>());
1818
}
1919

20-
public Task<List<object>> GetParents(object child)
20+
public Task<List<object>> GetParentsAsync(object child)
2121
{
2222
if (_childToFatherRelation.TryGetValue(child, out var refs))
2323
{
+38-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
11
using Depository.Abstraction.Interfaces;
2+
using Depository.Abstraction.Models.Options;
23

34
namespace Depository.Extensions;
45

56
public static class ResolveExtension
67
{
7-
public static async Task<T> ResolveAsync<T>(this IDepositoryResolve depository)
8+
public static async Task<T> ResolveAsync<T>(this IDepositoryResolve depository, DependencyResolveOption? option)
89
{
9-
return (T)await depository.ResolveDependencyAsync(typeof(T));
10+
return (T)await depository.ResolveDependencyAsync(typeof(T), option);
1011
}
1112

12-
public static async Task<T> ResolveAsync<T>(this IDepositoryResolve depository, string relationName)
13+
public static async Task<T> ResolveAsync<T>(this IDepositoryResolve depository, string? relationName = null, bool? includeDisabled = false, IDepositoryResolveScope? scope = null, Dictionary<Type,object>? fatherImplementations = null)
1314
{
15+
if (relationName != null || includeDisabled == true || scope != null || fatherImplementations != null)
16+
{
17+
var option = new DependencyResolveOption
18+
{
19+
Scope = scope,
20+
IncludeDisabled = includeDisabled is true,
21+
RelationName = relationName,
22+
FatherImplementations = fatherImplementations
23+
};
24+
return (T)await depository.ResolveDependencyAsync(typeof(T), option);
25+
}
1426
return (T)await depository.ResolveDependencyAsync(typeof(T));
27+
1528
}
1629

17-
public static async Task<List<T>> ResolveMultipleAsync<T>(this IDepositoryResolve depository)
30+
public static async Task<List<T>> ResolveMultipleAsync<T>(this IDepositoryResolve depository, DependencyResolveOption? option)
1831
{
1932
return (await depository.ResolveDependenciesAsync(typeof(T)))
2033
.Select(o => (T)o)
2134
.ToList();
2235
}
36+
37+
public static async Task<List<T>> ResolveMultipleAsync<T>(this IDepositoryResolve depository, string? relationName = null, bool? includeDisabled = false, IDepositoryResolveScope? scope = null, Dictionary<Type,object>? fatherImplementations = null)
38+
{
39+
if (relationName != null || includeDisabled == true || scope != null || fatherImplementations != null)
40+
{
41+
var option = new DependencyResolveOption
42+
{
43+
Scope = scope,
44+
IncludeDisabled = includeDisabled is true,
45+
RelationName = relationName,
46+
FatherImplementations = fatherImplementations
47+
};
48+
return (await depository.ResolveDependenciesAsync(typeof(T), option))
49+
.Select(o => (T)o)
50+
.ToList();
51+
}
52+
53+
return (await depository.ResolveDependenciesAsync(typeof(T)))
54+
.Select(o => (T)o)
55+
.ToList();
56+
}
2357
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Depository.Core;
2+
using Depository.Demo.Implements;
3+
using Depository.Demo.Interfaces;
4+
using Depository.Extensions;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace Depository.Demo;
9+
10+
public class DepositoryFamilyTests
11+
{
12+
[Fact]
13+
public async void InjectCustomFather()
14+
{
15+
// Arrange
16+
var depository = DepositoryFactory.CreateNew();
17+
await depository.AddSingletonAsync<IConstructorInjectService, CustomFatherService>();
18+
var guidGen = new EmptyGuidGenerator();
19+
20+
// Action
21+
var result = await depository.ResolveAsync<IConstructorInjectService>(fatherImplementations: new Dictionary<Type, object>
22+
{ { typeof(IGuidGenerator), guidGen } });
23+
var fathers = await depository.GetParentsAsync(result);
24+
var children = await depository.GetChildrenAsync(guidGen);
25+
26+
// Assert
27+
result.Should().NotBeNull();
28+
children.Should().OnlyContain(t => t.Equals(result));
29+
fathers.Should().OnlyContain(t => t.Equals(guidGen));
30+
31+
}
32+
}

Depository.Tests/DepositoryResolveTests.cs

+21-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DepositoryResolveTests
1818
[Fact]
1919
public async void ResolveSingleRegisteredService_ToSingleResolve_ShouldReturnEmptyGuidGenerator()
2020
{
21-
// Init
21+
// Arrange
2222
var depository = CreateNewDepository();
2323
var description = new DependencyDescription
2424
{
@@ -45,7 +45,7 @@ public async void ResolveSingleRegisteredService_ToSingleResolve_ShouldReturnEmp
4545
[Fact]
4646
public async void ResolveSingleRegisteredService_InScope_ToSingleResolve_ShouldReturnEmptyGuidGenerator()
4747
{
48-
// Init
48+
// Arrange
4949
var depository = CreateNewDepository();
5050
var description = new DependencyDescription
5151
{
@@ -96,7 +96,7 @@ public async void ResolveSingleRegisteredService_InScope_ToSingleResolve_ShouldR
9696
[Fact]
9797
public async void ResolveMultipleRegisteredService_ToSingleResolve_ShouldReturnRandomGuidGenerator()
9898
{
99-
// Init
99+
// Arrange
100100
var depository = CreateNewDepository();
101101
var description = new DependencyDescription
102102
{
@@ -132,7 +132,7 @@ public async void ResolveMultipleRegisteredService_ToSingleResolve_ShouldReturnR
132132
[Fact]
133133
public async void ResolveMultipleRegisteredService_ToMultipleResolves_ShouldReturnMultipleGuidGenerators()
134134
{
135-
// Init
135+
// Arrange
136136
var depository = CreateNewDepository();
137137
var description = new DependencyDescription
138138
{
@@ -168,7 +168,7 @@ public async void ResolveMultipleRegisteredService_ToMultipleResolves_ShouldRetu
168168
[Fact]
169169
public async void ResolveMultipleRegisteredService_ToIEnumerable_ShouldReturnMultipleGuidGenerators()
170170
{
171-
// Init
171+
// Arrange
172172
var depository = CreateNewDepository();
173173
var description = new DependencyDescription
174174
{
@@ -204,7 +204,7 @@ public async void ResolveMultipleRegisteredService_ToIEnumerable_ShouldReturnMul
204204
[Fact]
205205
public async void ResolveService_ToDefaultImplement_ShouldReturnDefaultImplement()
206206
{
207-
// Init
207+
// Arrange
208208
var depository = CreateNewDepository();
209209
var description = new DependencyDescription
210210
{
@@ -231,7 +231,7 @@ public async void ResolveService_ToDefaultImplement_ShouldReturnDefaultImplement
231231
[Fact]
232232
public async void ResolveMultipleService_ToDefaultImplements_UsingResolves_ShouldAllReturnDefaultImplement()
233233
{
234-
// Init
234+
// Arrange
235235
var depository = CreateNewDepository();
236236
var description = new DependencyDescription
237237
{
@@ -265,7 +265,7 @@ public async void ResolveMultipleService_ToDefaultImplements_UsingResolves_Shoul
265265
[Fact]
266266
public async void ResolveMultipleService_ToDefaultImplement_UsingIEnumerable_ShouldAllReturnDefaultImplement()
267267
{
268-
// Init
268+
// Arrange
269269
var depository = CreateNewDepository();
270270
var description = new DependencyDescription
271271
{
@@ -302,7 +302,7 @@ public async void ResolveMultipleService_ToDefaultImplement_UsingIEnumerable_Sho
302302
[Fact]
303303
public async void ResolveSingleRegisteredService_ToSingleResolve_UsingExtension_ShouldReturnEmptyGuidGenerator()
304304
{
305-
// Init
305+
// Arrange
306306
var depository = CreateNewDepository();
307307
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>();
308308

@@ -317,7 +317,7 @@ public async void ResolveSingleRegisteredService_ToSingleResolve_UsingExtension_
317317
[Fact]
318318
public async void ResolveMultipleRegisteredService_ToSingleResolve_UsingExtension_ShouldReturnRandomGuidGenerator()
319319
{
320-
// Init
320+
// Arrange
321321
var depository = CreateNewDepository();
322322
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>();
323323
await depository.AddSingletonAsync<IGuidGenerator, RandomGuidGenerator>();
@@ -335,7 +335,7 @@ public async void ResolveMultipleRegisteredService_ToSingleResolve_UsingExtensio
335335
public async void
336336
ResolveMultipleRegisteredService_ToMultipleResolves_UsingExtension_ShouldReturnMultipleGuidGenerators()
337337
{
338-
// Init
338+
// Arrange
339339
var depository = CreateNewDepository();
340340
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>();
341341
await depository.AddSingletonAsync<IGuidGenerator, RandomGuidGenerator>();
@@ -353,7 +353,7 @@ public async void
353353
[Fact]
354354
public async void ResolveMultipleRegisteredService_ToIEnumerable_UsingExtension_ShouldReturnMultipleGuidGenerators()
355355
{
356-
// Init
356+
// Arrange
357357
var depository = CreateNewDepository();
358358
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>();
359359
await depository.AddSingletonAsync<IGuidGenerator, RandomGuidGenerator>();
@@ -371,7 +371,7 @@ public async void ResolveMultipleRegisteredService_ToIEnumerable_UsingExtension_
371371
[Fact]
372372
public async void ResolveGeneric_ToNormalType_ShouldReturnNormalType()
373373
{
374-
// Init
374+
// Arrange
375375
var depository = CreateNewDepository();
376376
await depository.AddSingletonAsync<ITypeGeneric<string>, TypeGeneric<string>>();
377377

@@ -387,7 +387,7 @@ public async void ResolveGeneric_ToNormalType_ShouldReturnNormalType()
387387
[Fact]
388388
public async void ResolveGeneric_ToNormalType_ShouldReturnGenericType()
389389
{
390-
// Init
390+
// Arrange
391391
var depository = CreateNewDepository();
392392
await depository.AddSingletonAsync(typeof(ITypeGeneric<>), typeof(TypeGeneric<>));
393393

@@ -403,7 +403,7 @@ public async void ResolveGeneric_ToNormalType_ShouldReturnGenericType()
403403
[Fact]
404404
public async void ResolveConstructorInject_ShouldBeNormal()
405405
{
406-
// Init
406+
// Arrange
407407
var depository = CreateNewDepository();
408408
await depository.AddSingletonAsync(typeof(IConstructorInjectService), typeof(ConstructorInjectService));
409409
await depository.AddSingletonAsync(typeof(IGuidGenerator), typeof(RandomGuidGenerator));
@@ -418,7 +418,7 @@ public async void ResolveConstructorInject_ShouldBeNormal()
418418
[Fact]
419419
public async void ResolveIEnumerableConstructorInject_ShouldBeNormal()
420420
{
421-
// Init
421+
// Arrange
422422
var depository = CreateNewDepository();
423423
await depository.AddSingletonAsync(typeof(IConstructorInjectService),
424424
typeof(ConstructorIEnumerableInjectService));
@@ -435,7 +435,7 @@ await depository.AddSingletonAsync(typeof(IConstructorInjectService),
435435
[Fact]
436436
public async void ResolveConstructorNotification_ShouldBeNormal()
437437
{
438-
// Init
438+
// Arrange
439439
var depository = CreateNewDepository(option => option.AutoNotifyDependencyChange = true);
440440
await depository.AddSingletonAsync(typeof(IConstructorInjectService),
441441
typeof(ConstructorInjectNotifiableService));
@@ -456,7 +456,7 @@ await depository.AddSingletonAsync(typeof(INotifyDependencyChanged<IGuidGenerato
456456
[Fact]
457457
public async void ResolveIEnumerableConstructorNotification_ShouldBeNormal()
458458
{
459-
// Init
459+
// Arrange
460460
var depository = CreateNewDepository(option => option.AutoNotifyDependencyChange = true);
461461
await depository.AddSingletonAsync(typeof(IConstructorInjectService),
462462
typeof(ConstructorInjectNotifiableService));
@@ -478,7 +478,7 @@ await depository.AddSingletonAsync(typeof(INotifyDependencyChanged<IGuidGenerato
478478
[Fact]
479479
public async void ResolveService_ToDefaultImplement_UsingExtension_ShouldReturnDefaultImplement()
480480
{
481-
// Init
481+
// Arrange
482482
var depository = CreateNewDepository();
483483
var emptyGuidGenerator = new EmptyGuidGenerator();
484484
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>(emptyGuidGenerator);
@@ -493,7 +493,7 @@ public async void ResolveService_ToDefaultImplement_UsingExtension_ShouldReturnD
493493
[Fact]
494494
public async void ResolveMultipleService_ToDefaultImplement_UsingExtension_ShouldAllReturnDefaultImplement()
495495
{
496-
// Init
496+
// Arrange
497497
var depository = CreateNewDepository();
498498
var emptyGuidGenerator = new EmptyGuidGenerator();
499499
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>(emptyGuidGenerator);
@@ -509,7 +509,7 @@ public async void ResolveMultipleService_ToDefaultImplement_UsingExtension_Shoul
509509
[Fact]
510510
public async void ResolveMultipleService_ToDefaultImplement_UsingExtension_IEnumerable_ShouldAllReturnDefaultImplement()
511511
{
512-
// Init
512+
// Arrange
513513
var depository = CreateNewDepository();
514514
var emptyGuidGenerator = new EmptyGuidGenerator();
515515
await depository.AddSingletonAsync<IGuidGenerator, EmptyGuidGenerator>(emptyGuidGenerator);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Depository.Demo.Interfaces;
2+
3+
namespace Depository.Demo.Implements;
4+
5+
public class CustomFatherService : IConstructorInjectService
6+
{
7+
public CustomFatherService(IGuidGenerator generator)
8+
{
9+
10+
}
11+
}

0 commit comments

Comments
 (0)