-
-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathRegistrationOnlyIfTests.cs
169 lines (147 loc) · 6.49 KB
/
RegistrationOnlyIfTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Test.Scenarios.Adapters;
using Autofac.Test.Scenarios.ScannedAssembly;
using Xunit;
namespace Autofac.Test
{
public class RegistrationOnlyIfTests
{
public delegate object SimpleFactory();
public interface IService
{
}
public interface IService<T>
{
}
[Fact]
public void IfNotRegistered_CanFilterEnumerableServices()
{
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>().As<IService>();
builder.RegisterType<ServiceB>().As<IService>().IfNotRegistered(typeof(IService));
builder.RegisterType<ServiceC>().As<IService>();
var container = builder.Build();
var result = container.Resolve<IEnumerable<IService>>().ToArray();
Assert.Equal(2, result.Length);
Assert.Contains(result, r => r.GetType() == typeof(ServiceA));
Assert.Contains(result, r => r.GetType() == typeof(ServiceC));
}
[Fact]
public void IfNotRegistered_CanFilterSingleServices()
{
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>().As<IService>();
builder.RegisterType<ServiceB>().As<IService>().IfNotRegistered(typeof(IService));
var container = builder.Build();
Assert.IsType<ServiceA>(container.Resolve<IService>());
}
[Fact]
public void OnlyIf_CanFilterEnumerableServices()
{
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>().As<IService>();
builder.RegisterType<ServiceB>().As<IService>().OnlyIf(reg => !reg.IsRegistered(new TypedService(typeof(IService))));
builder.RegisterType<ServiceC>().As<IService>();
var container = builder.Build();
var result = container.Resolve<IEnumerable<IService>>().ToArray();
Assert.Equal(2, result.Length);
Assert.Contains(result, r => r.GetType() == typeof(ServiceA));
Assert.Contains(result, r => r.GetType() == typeof(ServiceC));
}
[Fact]
public void OnlyIf_CanFilterSingleServices()
{
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>().As<IService>();
builder.RegisterType<ServiceB>().As<IService>().OnlyIf(reg => !reg.IsRegistered(new TypedService(typeof(IService))));
var container = builder.Build();
Assert.IsType<ServiceA>(container.Resolve<IService>());
}
[Fact]
public void OnlyIf_EnabledByStandardRegistrations()
{
// This shouldn't throw during any of the calls. If it does
// it means the registration extension hasn't been updated
// to store the callback container with the registration builder.
var builder = new ContainerBuilder();
builder.Register((ctx, p) => new object()).OnlyIf(r => true);
builder.Register(ctx => new object()).OnlyIf(r => true);
builder.RegisterAdapter<Command, ToolbarButton>((ctx, cmd) => new ToolbarButton(cmd)).As<IToolbarButton>().OnlyIf(r => true);
builder.RegisterAdapter<Command, ToolbarButton>((ctx, p, cmd) => new ToolbarButton(cmd)).As<IToolbarButton>().OnlyIf(r => true);
builder.RegisterAdapter<Command, ToolbarButton>(cmd => new ToolbarButton(cmd)).As<IToolbarButton>().OnlyIf(r => true);
builder.RegisterAssemblyTypes(typeof(AComponent).GetTypeInfo().Assembly).OnlyIf(r => true);
builder.RegisterDecorator<IService>((ctx, p, s) => new Decorator(s), "from").OnlyIf(r => true);
builder.RegisterDecorator<IService>((ctx, s) => new Decorator(s), "from").OnlyIf(r => true);
builder.RegisterDecorator<IService>(s => new Decorator(s), "from").OnlyIf(r => true);
builder.RegisterGeneratedFactory(typeof(SimpleFactory)).OnlyIf(r => true);
builder.RegisterGeneratedFactory(typeof(SimpleFactory), new TypedService(typeof(object))).OnlyIf(r => true);
builder.RegisterGeneratedFactory<SimpleFactory>().OnlyIf(r => true);
builder.RegisterGeneratedFactory<SimpleFactory>(new TypedService(typeof(object))).OnlyIf(r => true);
builder.RegisterGeneric(typeof(SimpleGeneric<>)).OnlyIf(r => true);
builder.RegisterGenericDecorator(typeof(Decorator<>), typeof(IService<>), fromKey: "b").OnlyIf(r => true);
builder.RegisterInstance(new object()).OnlyIf(r => true);
builder.RegisterType(typeof(object)).OnlyIf(r => true);
builder.RegisterType<object>().OnlyIf(r => true);
builder.RegisterTypes(typeof(object)).OnlyIf(r => true);
}
[Fact]
public void OnlyIf_NullPredicate()
{
var builder = new ContainerBuilder();
var rb = builder.RegisterType<object>();
Assert.Throws<ArgumentNullException>(() => rb.OnlyIf(null));
}
[Fact]
public void OnlyIf_NullRegistration()
{
Assert.Throws<ArgumentNullException>(() => RegistrationExtensions.OnlyIf<object, ConcreteReflectionActivatorData, SingleRegistrationStyle>(null, reg => true));
}
[Fact]
public void OnlyIf_StandaloneBuilder()
{
var rb = RegistrationBuilder.ForType(typeof(object));
Assert.Throws<NotSupportedException>(() => rb.OnlyIf(reg => true));
}
public class Decorator<T> : IService<T>
{
private readonly IService<T> _decorated;
public Decorator(IService<T> decorated)
{
this._decorated = decorated;
}
public IService<T> Decorated
{
get { return this._decorated; }
}
}
public class Decorator : IService
{
private readonly IService _decorated;
public Decorator(IService decorated)
{
this._decorated = decorated;
}
public IService Decorated
{
get { return this._decorated; }
}
}
public class ServiceA : IService
{
}
public class ServiceB : IService
{
}
public class ServiceC : IService
{
}
public class SimpleGeneric<T>
{
}
}
}