-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathPartConstraintRuleTests.cs
More file actions
238 lines (192 loc) · 8.89 KB
/
PartConstraintRuleTests.cs
File metadata and controls
238 lines (192 loc) · 8.89 KB
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) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Features;
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Xunit;
namespace DocumentFormat.OpenXml.Tests
{
public class PartConstraintRuleTests
{
private readonly ITestOutputHelper _output;
private static readonly HashSet<Type> _abstractOpenXmlParts = new HashSet<Type>
{
typeof(OpenXmlPart),
typeof(ExtendedPart),
typeof(StylesPart),
typeof(CustomUIPart),
};
public PartConstraintRuleTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void AllExpectedParts()
{
var fromFramework = new[] { typeof(ExtendedPart), typeof(OpenXmlPart) };
var actual = typeof(SpreadsheetDocument)
.GetTypeInfo()
.Assembly
.DefinedTypes
.Where(t => t != typeof(OpenXmlPart))
.Concat(fromFramework)
.Distinct()
.Where(t => typeof(OpenXmlPart).GetTypeInfo().IsAssignableFrom(t))
.Select(t => t.FullName)
.OrderBy(v => v, StringComparer.Ordinal)
.ToList();
var expected = _cachedConstraintData.Value
.Select(v => v.Key)
.Concat(_abstractOpenXmlParts.Select(t => t.FullName))
.OrderBy(v => v, StringComparer.Ordinal)
.ToList();
var missing = expected.Except(actual);
var added = actual.Except(expected);
Assert.Empty(missing);
Assert.Empty(added);
}
[MemberData(nameof(GetOpenXmlParts))]
[Theory]
public void ValidatePart(Type partType)
{
var part = InitializePart(partType);
// If there are added parts, PartConstraintData.json needs to be updated with the new part's data and relationship.
var expectedConstraints = GetConstraintData(part);
var constraints = part.Features.GetRequired<IPartConstraintFeature>();
var targets = part.Features.GetRequired<ITargetFeature>();
Assert.Equal(expectedConstraints.IsContentTypeFixed, part.IsContentTypeFixed);
Assert.Equal(expectedConstraints.RelationshipType, part.RelationshipType);
Assert.Equal(expectedConstraints.TargetFileExtension, targets.Extension);
Assert.Equal(expectedConstraints.TargetName, targets.Name);
Assert.Equal(expectedConstraints.TargetPath, targets.Path);
if (part.IsContentTypeFixed)
{
Assert.Equal(expectedConstraints.ContentType, part.ContentType);
}
#if DEBUG
_output.WriteObjectToTempFile("expected constraints", expectedConstraints.Parts.OrderBy(p => p.RelationshipType));
_output.WriteObjectToTempFile("actual constraints", constraints.Rules.OrderBy(p => p.RelationshipType).Select(p => new PartConstraintRule2(p)));
#endif
Assert.Equal(
expectedConstraints.Parts.OrderBy(p => p.RelationshipType),
constraints.Rules.OrderBy(p => p.RelationshipType).Select(p => new PartConstraintRule2(p)));
}
[Fact]
public void ExportData()
{
var result = GetParts()
.Select(InitializePart)
.Select(t =>
{
var targets = t.Features.GetRequired<ITargetFeature>();
return new
{
Name = t.GetType().FullName,
ContentType = t.IsContentTypeFixed ? t.ContentType : null,
IsContentTypeFixed = t.IsContentTypeFixed,
RelationshipType = t.RelationshipType,
TargetFileExtension = targets.Extension,
TargetName = targets.Name,
TargetPath = targets.Path,
Parts = t.Features.GetRequired<IPartConstraintFeature>().Rules,
};
})
.OrderBy(d => d.Name, StringComparer.Ordinal);
_output.WriteObjectToTempFile("typed parts", result);
}
public static IEnumerable<object[]> GetOpenXmlParts() => GetParts().Select(p => new[] { p });
private static IEnumerable<Type> GetParts() => typeof(SpreadsheetDocument)
.GetTypeInfo()
.Assembly
.GetTypes()
.Where(t => !t.IsAbstract)
.Where(typeof(OpenXmlPart).IsAssignableFrom)
.Where(a => !_abstractOpenXmlParts.Contains(a));
private static OpenXmlPart InitializePart(Type type)
{
var part = (OpenXmlPart)Activator.CreateInstance(type, true);
var appType = Substitute.For<IApplicationTypeFeature>();
appType.Type.Returns(ApplicationType.None);
part.Features.Set(appType);
return part;
}
private static ConstraintData GetConstraintData(OpenXmlPart part) => _cachedConstraintData.Value[part.GetType().FullName];
private static Lazy<Dictionary<string, ConstraintData>> _cachedConstraintData = new Lazy<Dictionary<string, ConstraintData>>(() =>
{
var names = typeof(PartConstraintRuleTests).GetTypeInfo().Assembly.GetManifestResourceNames();
// If there are added parts, PartConstraintData.json needs to be updated with the new part's data and relationship.
using (var stream = typeof(PartConstraintRuleTests).GetTypeInfo().Assembly.GetManifestResourceStream("DocumentFormat.OpenXml.Packaging.Tests.data.PartConstraintData.json"))
using (var reader = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<ConstraintData[]>(reader.ReadToEnd(), new StringEnumConverter())
.ToDictionary(t => t.Name, StringComparer.Ordinal);
}
});
#pragma warning disable CA1812
private class ConstraintData
{
public string Name { get; set; }
public string ContentType { get; set; }
public bool IsContentTypeFixed { get; set; }
public string RelationshipType { get; set; }
public string TargetFileExtension { get; set; }
public string TargetName { get; set; }
public string TargetPath { get; set; }
public PartConstraintRule2[] Parts { get; set; }
}
#pragma warning restore CA1712
private class PartConstraintRule2
{
public string RelationshipType { get; set; }
public string ContentType { get; set; }
public bool MinOccursIsNonZero { get; set; }
public bool MaxOccursGreatThanOne { get; set; }
public FileFormatVersions FileFormat { get; set; }
public PartConstraintRule2()
{
}
public PartConstraintRule2(PartConstraintRule rule)
{
FileFormat = rule.FileFormat;
MaxOccursGreatThanOne = rule.MaxOccursGreatThanOne;
MinOccursIsNonZero = rule.MinOccursIsNonZero;
RelationshipType = rule.RelationshipType;
ContentType = rule.ContentType;
}
public override bool Equals(object obj)
{
if (obj is PartConstraintRule2 other)
{
return string.Equals(RelationshipType, other.RelationshipType, StringComparison.Ordinal)
&& string.Equals(ContentType, other.ContentType, StringComparison.Ordinal)
&& MinOccursIsNonZero == other.MinOccursIsNonZero
&& MaxOccursGreatThanOne == other.MaxOccursGreatThanOne
&& FileFormat == other.FileFormat;
}
return false;
}
public override int GetHashCode()
{
const int HashMultiplier = 31;
unchecked
{
int hash = 17;
hash = (hash * HashMultiplier) + StringComparer.Ordinal.GetHashCode(RelationshipType);
hash = (hash * HashMultiplier) + StringComparer.Ordinal.GetHashCode(ContentType);
hash = (hash * HashMultiplier) + MinOccursIsNonZero.GetHashCode();
hash = (hash * HashMultiplier) + MaxOccursGreatThanOne.GetHashCode();
hash = (hash * HashMultiplier) + FileFormat.GetHashCode();
return hash;
}
}
}
}
}