-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
PartBuilderInheritanceTests.cs
96 lines (75 loc) · 3.64 KB
/
PartBuilderInheritanceTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Reflection;
using Xunit;
namespace System.Composition.Convention.Tests
{
public class PartBuilderInheritanceTests
{
private abstract class BaseClass
{
public string P1 { get; set; }
public string P2 { get; set; }
public IEnumerable<int> P3 { get; set; }
}
private class DerClass : BaseClass
{
public string P4 { get; set; }
public string P5 { get; set; }
}
[Fact]
public void ImportPropertyTargetingBaseClass_ShouldGenerateImportAttributeForP1Selected()
{
var builder = new ConventionBuilder();
builder.ForType<DerClass>().ImportProperties((p) => p.Name == "P1"); // P1 is string
var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P1") as ImportAttribute;
Assert.NotNull(pAttr); // Ensure P1 has ImportAttribute (default configured)
Assert.Null(pAttr.ContractName);
}
[Fact]
public void ImportPropertyTargetingBaseClass_ShouldGenerateImportManyForP3Selected()
{
var builder = new ConventionBuilder();
builder.ForType<DerClass>().ImportProperties((p) => p.Name == "P3"); // P2 is Enumerable
var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P3") as ImportManyAttribute;
Assert.NotNull(pAttr); // Ensure P3 has ImportManyAttribute (default configured)
Assert.Null(pAttr.ContractName);
}
[Fact]
public void ImportPropertyTargetingDerivedClass_ShouldGenerateImportAttributeForP4Selected()
{
var builder = new ConventionBuilder();
builder.ForType<DerClass>().ImportProperties((p) => p.Name == "P4"); // P1 is string
var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P4") as ImportAttribute;
Assert.NotNull(pAttr); // Ensure P1 has ImportAttribute (default configured)
Assert.Null(pAttr.ContractName);
}
[Fact]
public void ExportPropertyTargetingDerivedClass_ShouldGenerateExportForPropertySelected()
{
var builder = new ConventionBuilder();
builder.ForType<DerClass>().ExportProperties((p) => p.Name == "P4"); // P1 is string
var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P4") as ExportAttribute;
Assert.NotNull(pAttr);
Assert.Null(pAttr.ContractName);
Assert.Null(pAttr.ContractType);
}
[Fact]
public void ExportPropertyTargetingBaseClass_ShouldGenerateExportForPropertySelected()
{
var builder = new ConventionBuilder();
builder.ForType<DerClass>().ExportProperties((p) => p.Name == "P2"); // P2 is string
var exportAttribute = GetAttributeFromMember(builder, typeof(DerClass), "P2") as ExportAttribute;
Assert.NotNull(exportAttribute);
Assert.Null(exportAttribute.ContractName);
Assert.Null(exportAttribute.ContractType);
}
private static Attribute GetAttributeFromMember(ConventionBuilder builder, Type type, string member)
{
PropertyInfo pi = type.GetRuntimeProperty(member);
Attribute[] list = builder.GetDeclaredAttributes(type, pi);
return list[0] as Attribute;
}
}
}