-
-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathLightweightAdapterRegistrationExtensionsTests.cs
238 lines (197 loc) · 8.36 KB
/
LightweightAdapterRegistrationExtensionsTests.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using Autofac.Core;
using Autofac.Features.Indexed;
using Autofac.Features.Metadata;
using Autofac.Test.Scenarios.Adapters;
using Xunit;
namespace Autofac.Test.Features.LightweightAdapters
{
public class LightweightAdapterRegistrationExtensionsTests
{
public class AdaptingTypeToType
{
private readonly IEnumerable<Command> _commands = new[]
{
new Command(),
new Command(),
};
private readonly IEnumerable<IToolbarButton> _toolbarButtons;
public AdaptingTypeToType()
{
var builder = new ContainerBuilder();
foreach (var command in _commands)
{
builder.RegisterInstance(command);
}
builder.RegisterAdapter<Command, ToolbarButton>(cmd => new ToolbarButton(cmd))
.As<IToolbarButton>();
var container = builder.Build();
_toolbarButtons = container.Resolve<IEnumerable<IToolbarButton>>();
}
[Fact]
public void AdaptingTypeSeesKeysOfAdapteeType()
{
var builder = new ContainerBuilder();
builder.RegisterType<Command>().Keyed<Command>("Command");
builder.RegisterType<AnotherCommand>().Keyed<AnotherCommand>("AnotherCommand");
builder.RegisterAdapter<Command, AnotherCommand>(c => new AnotherCommand());
var container = builder.Build();
var command = container.Resolve<IIndex<string, AnotherCommand>>()["Command"];
Assert.NotNull(command);
Assert.IsType<AnotherCommand>(command);
var anotherCommand = container.Resolve<IIndex<string, AnotherCommand>>()["AnotherCommand"];
Assert.NotNull(anotherCommand);
Assert.IsType<AnotherCommand>(anotherCommand);
}
[Fact]
public void EachInstanceOfTheTargetTypeIsAdapted()
{
Assert.True(_commands.All(cmd => _toolbarButtons.Any(b => b.Command == cmd)));
}
}
public class OnTopOfAnotherAdapter
{
private readonly Command _from = new Command();
private const string NameKey = "Name";
private const string Name = "N";
private readonly ToolbarButton _to;
public OnTopOfAnotherAdapter()
{
var builder = new ContainerBuilder();
builder.RegisterInstance(_from).WithMetadata(NameKey, Name);
builder.RegisterAdapter<Meta<Command>, ToolbarButton>(
cmd => new ToolbarButton(cmd.Value, (string)cmd.Metadata[NameKey]));
var container = builder.Build();
_to = container.Resolve<ToolbarButton>();
}
[Fact]
public void AdaptedMetadataIsPassed()
{
Assert.Equal(Name, _to.Name);
}
}
public class DecoratingServiceThatHasDefaultImplementation
{
private readonly IContainer _container;
public DecoratingServiceThatHasDefaultImplementation()
{
const string from = "from";
var builder = new ContainerBuilder();
builder.RegisterType<Implementer1>().As<IService>().Named<IService>(from);
builder.RegisterDecorator<IService>(s => new Decorator(s), from);
_container = builder.Build();
}
[Fact(Skip = "Issue #529 => #880")]
public void InstanceWithDefaultImplementationIsDecorated()
{
var decorator = _container.Resolve<IService>();
Assert.IsType<Decorator>(decorator);
Assert.IsType<Implementer1>(((Decorator)decorator).Decorated);
}
}
public interface IService
{
}
public class Implementer1 : IService
{
}
public class Implementer2 : IService
{
}
public class Decorator : IService
{
public Decorator(IService decorated)
{
Decorated = decorated;
}
public IService Decorated { get; }
}
public class DecoratorParameterization
{
private readonly IContainer _container;
public DecoratorParameterization()
{
var builder = new ContainerBuilder();
builder.Register((ctx, p) => new ParameterizedImplementer(p)).Named<IParameterizedService>("from");
builder.RegisterDecorator<IParameterizedService>((ctx, p, s) => new ParameterizedDecorator1(s, p), "from", "to");
builder.RegisterDecorator<IParameterizedService>((ctx, p, s) => new ParameterizedDecorator2(s, p), "to");
_container = builder.Build();
}
[Fact]
public void ParametersGoToTheDecoratedInstance()
{
var resolved = _container.Resolve<IParameterizedService>(TypedParameter.From<IService>(new Implementer1()));
var dec2 = Assert.IsType<ParameterizedDecorator2>(resolved);
Assert.Empty(dec2.Parameters);
var dec1 = Assert.IsType<ParameterizedDecorator1>(dec2.Implementer);
Assert.Empty(dec1.Parameters);
var imp = Assert.IsType<ParameterizedImplementer>(dec1.Implementer);
Assert.Single(imp.Parameters);
}
public interface IParameterizedService
{
IEnumerable<Parameter> Parameters { get; }
}
public class ParameterizedImplementer : IParameterizedService
{
public ParameterizedImplementer(IEnumerable<Parameter> parameters)
{
Parameters = parameters;
}
public IEnumerable<Parameter> Parameters { get; }
}
public class ParameterizedDecorator1 : IParameterizedService
{
public ParameterizedDecorator1(IParameterizedService implementer, IEnumerable<Parameter> parameters)
{
Implementer = implementer;
Parameters = parameters;
}
public IParameterizedService Implementer { get; }
public IEnumerable<Parameter> Parameters { get; }
}
public class ParameterizedDecorator2 : IParameterizedService
{
public ParameterizedDecorator2(IParameterizedService implementer, IEnumerable<Parameter> parameters)
{
Implementer = implementer;
Parameters = parameters;
}
public IParameterizedService Implementer { get; }
public IEnumerable<Parameter> Parameters { get; }
}
}
public class DecoratingANamedService
{
private readonly IContainer _container;
public DecoratingANamedService()
{
const string from = "from";
var builder = new ContainerBuilder();
builder.RegisterType<Implementer1>().Named<IService>(from);
builder.RegisterType<Implementer2>().Named<IService>(from);
builder.RegisterDecorator<IService>(s => new Decorator(s), from);
_container = builder.Build();
}
[Fact]
public void TheDefaultNamedInstanceIsTheDefaultDecoratedInstance()
{
var d = _container.Resolve<IService>();
Assert.IsType<Implementer2>(((Decorator)d).Decorated);
}
[Fact]
public void AllInstancesAreDecorated()
{
var all = _container.Resolve<IEnumerable<IService>>()
.Cast<Decorator>()
.Select(d => d.Decorated);
Assert.Equal(2, all.Count());
Assert.Contains(all, i => i is Implementer1);
Assert.Contains(all, i => i is Implementer2);
}
}
}
}