-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
CircularityTests.cs
206 lines (178 loc) · 6.36 KB
/
CircularityTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Composition.Hosting;
using System.Composition.Hosting.Core;
using System.Composition.UnitTests.Util;
using System.Composition.Runtime;
using Xunit;
namespace System.Composition.UnitTests
{
public class CircularityTests : ContainerTests
{
public interface IA { }
[Export, Shared]
public class BLazy
{
public Lazy<IA> A;
[ImportingConstructor]
public BLazy(Lazy<IA> ia)
{
A = ia;
}
}
[Export(typeof(IA))]
public class ACircular : IA
{
public BLazy B;
[ImportingConstructor]
public ACircular(BLazy b)
{
B = b;
}
}
[Export, Shared]
public class PropertyPropertyA
{
[Import]
public PropertyPropertyB B { get; set; }
}
[Export]
public class PropertyPropertyB
{
[Import]
public PropertyPropertyA A { get; set; }
}
[Export]
public class ConstructorPropertyA
{
[Import]
public ConstructorPropertyB B { get; set; }
}
[Export, Shared]
public class ConstructorPropertyB
{
[ImportingConstructor]
public ConstructorPropertyB(ConstructorPropertyA a)
{
A = a;
}
public ConstructorPropertyA A { get; private set; }
}
public class CircularM
{
public string Name { get; set; }
}
[Export, ExportMetadata("Name", "A")]
public class MetadataCircularityA
{
[Import]
public Lazy<MetadataCircularityB, CircularM> B { get; set; }
}
[Export, ExportMetadata("Name", "B"), Shared]
public class MetadataCircularityB
{
[Import]
public Lazy<MetadataCircularityA, CircularM> A { get; set; }
}
[Export, Shared]
public class NonPrereqSelfDependency
{
[Import]
public NonPrereqSelfDependency Self { get; set; }
}
[Export]
public class PrDepA
{
[ImportingConstructor]
public PrDepA(PrDepB b) { }
}
[Export]
public class PrDepB
{
[ImportingConstructor]
public PrDepB(PrDepA a) { }
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void CanHandleDefinitionCircularity()
{
var cc = CreateContainer(typeof(ACircular), typeof(BLazy));
var x = cc.GetExport<BLazy>();
Assert.IsAssignableFrom<ACircular>(x.A.Value);
Assert.IsAssignableFrom<BLazy>(((ACircular)x.A.Value).B);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void CanHandleDefinitionCircularity2()
{
var cc = CreateContainer(typeof(ACircular), typeof(BLazy));
var x = cc.GetExport<IA>();
Assert.IsAssignableFrom<BLazy>(((ACircular)((ACircular)x).B.A.Value).B);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void HandlesPropertyPropertyCircularity()
{
var cc = CreateContainer(typeof(PropertyPropertyA), typeof(PropertyPropertyB));
var a = cc.GetExport<PropertyPropertyA>();
Assert.Same(a.B.A, a);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void HandlesPropertyPropertyCircularityReversed()
{
var cc = CreateContainer(typeof(PropertyPropertyA), typeof(PropertyPropertyB));
var b = cc.GetExport<PropertyPropertyB>();
Assert.Same(b.A.B, b.A.B.A.B);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void HandlesConstructorPropertyCircularity()
{
var cc = CreateContainer(typeof(ConstructorPropertyA), typeof(ConstructorPropertyB));
var a = cc.GetExport<ConstructorPropertyA>();
Assert.Same(a.B.A.B.A, a.B.A);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void HandlesConstructorPropertyCircularityReversed()
{
var cc = CreateContainer(typeof(ConstructorPropertyA), typeof(ConstructorPropertyB));
var b = cc.GetExport<ConstructorPropertyB>();
Assert.Same(b, b.A.B);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void HandlesMetadataCircularity()
{
var cc = CreateContainer(typeof(MetadataCircularityA), typeof(MetadataCircularityB));
var a = cc.GetExport<MetadataCircularityA>();
Assert.Equal("B", a.B.Metadata.Name);
Assert.Equal("A", a.B.Value.A.Metadata.Name);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void SharedPartCanHaveNonPrereqDependencyOnSelf()
{
var cc = CreateContainer(typeof(NonPrereqSelfDependency));
var npsd = cc.GetExport<NonPrereqSelfDependency>();
Assert.Same(npsd, npsd.Self);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/24903", TargetFrameworkMonikers.NetFramework)]
public void PrerequisiteCircularitiesAreDetected()
{
var cc = CreateContainer(typeof(PrDepA), typeof(PrDepB));
var x = Assert.Throws<CompositionFailedException>(() =>
{
cc.GetExport<PrDepA>();
});
}
}
}