-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
LightContainerTests.cs
134 lines (117 loc) · 3.72 KB
/
LightContainerTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Composition;
using System.Composition.Hosting;
using System.Composition.Convention;
using System.Reflection;
using TestLibrary;
using Xunit;
namespace System.Composition.UnitTests
{
public class LightContainerTests : ContainerTests
{
public interface IA { }
[Export(typeof(IA))]
public class A : IA, IDisposable
{
public bool IsDisposed;
public void Dispose()
{
IsDisposed = true;
}
}
[Export(typeof(IA))]
public class A2 : IA { }
[Export]
public class B
{
public IA A;
[ImportingConstructor]
public B(IA ia)
{
A = ia;
}
}
public class BarePart { }
public class HasPropertyA
{
public IA A { get; set; }
}
[Fact]
public void CreatesInstanceWithNoDependencies()
{
var cc = CreateContainer(typeof(A));
var x = cc.GetExport<IA>();
Assert.IsAssignableFrom<A>(x);
}
[Fact]
public void DefaultLifetimeIsNonShared()
{
var cc = CreateContainer(typeof(A));
var x = cc.GetExport<IA>();
var y = cc.GetExport<IA>();
Assert.NotSame(x, y);
}
[Fact]
public void Composes()
{
var cc = CreateContainer(typeof(A), typeof(B));
var x = cc.GetExport<B>();
Assert.IsAssignableFrom<A>(x.A);
}
[Fact]
public void CanSpecifyExportsWithConventionBuilder()
{
var rb = new ConventionBuilder();
rb.ForType<BarePart>().Export();
var cc = CreateContainer(rb, typeof(BarePart));
var x = cc.GetExport<BarePart>();
Assert.NotNull(x);
}
[Fact]
public void CanSpecifyLifetimeWithConventionBuilder()
{
var rb = new ConventionBuilder();
rb.ForType<BarePart>().Export().Shared();
var cc = CreateContainer(rb, typeof(BarePart));
var x = cc.GetExport<BarePart>();
var y = cc.GetExport<BarePart>();
Assert.Same(x, y);
}
[Fact]
public void InjectsPropertyImports()
{
var rb = new ConventionBuilder();
rb.ForType<HasPropertyA>().ImportProperty(a => a.A).Export();
var cc = CreateContainer(rb, typeof(HasPropertyA), typeof(A));
var x = cc.GetExport<HasPropertyA>();
Assert.IsAssignableFrom<A>(x.A);
}
[Fact]
public void VerifyAssemblyNameCanBeUsedWithContainer()
{
var test = new ContainerConfiguration()
.WithAssembly(typeof(ClassWithDependency).GetTypeInfo().Assembly)
.CreateContainer();
var b = test.GetExport<ClassWithDependency>();
Assert.NotNull(b);
Assert.NotNull(b._dep);
}
[Fact]
public void VerifyAssemblyWithTwoBaseTypeWithOnlyOneExportedWorks()
{
var test = new ContainerConfiguration()
.WithAssembly(typeof(ClassWithDependency).GetTypeInfo().Assembly)
.CreateContainer();
var b = test.GetExport<ClassWithDependencyAndSameBaseType>();
Assert.NotNull(b);
Assert.NotNull(b._dep);
Assert.Equal(typeof(TestDependency), b._dep.GetType());
}
}
}