-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
OptionalImportTests.cs
75 lines (64 loc) · 2.15 KB
/
OptionalImportTests.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
// 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.Composition.UnitTests
{
public class OptionalImportTests : ContainerTests
{
private class Missing { }
[Export]
private class Supplied { }
[Export]
private class HasOptionalConstructorParameter
{
public Missing Missing { get; set; }
public Supplied Supplied { get; set; }
[ImportingConstructor]
public HasOptionalConstructorParameter([Import(AllowDefault = true)] Missing missing, Supplied supplied)
{
Missing = missing;
Supplied = supplied;
}
}
[Export]
private class HasOptionalProperty
{
private Missing _missing;
public bool WasMissingSetterCalled { get; set; }
[Import(AllowDefault = true)]
public Missing Missing
{
get { return _missing; }
set
{
WasMissingSetterCalled = true;
_missing = value;
}
}
[Import]
public Supplied Supplied { get; set; }
}
[Fact]
public void MissingOptionalConstructorParametersAreSuppliedTheirDefaultValue()
{
var cc = CreateContainer(typeof(Supplied), typeof(HasOptionalConstructorParameter));
var ocp = cc.GetExport<HasOptionalConstructorParameter>();
Assert.Null(ocp.Missing);
Assert.NotNull(ocp.Supplied);
}
[Fact]
public void MissingOptionalPropertyImportsAreIgnored()
{
var cc = CreateContainer(typeof(Supplied), typeof(HasOptionalProperty));
var op = cc.GetExport<HasOptionalProperty>();
Assert.NotNull(op.Supplied);
Assert.Null(op.Missing);
Assert.False(op.WasMissingSetterCalled);
}
}
}