Skip to content

Commit eba7b64

Browse files
committed
turn on nullable for projects that include part root element tests
1 parent 81ac063 commit eba7b64

18 files changed

+86
-62
lines changed

src/DocumentFormat.OpenXml.Framework/Packaging/PartExtensionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public PartExtensionProvider()
2828
/// <param name="contentType">The content type</param>
2929
/// <param name="extension">Part Extension (".xml") to be used for the part with the specified content type.</param>
3030
/// <exception cref="ArgumentNullException">Thrown when either parameter is null.</exception>
31-
public void Register(string contentType, string extension)
31+
public void Register(string? contentType, string? extension)
3232
{
3333
if (contentType is null)
3434
{

src/DocumentFormat.OpenXml/Packaging/OpenXmlSupportedRelationshipExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace DocumentFormat.OpenXml.Packaging;
1111
/// </summary>
1212
public static class OpenXmlSupportedRelationshipExtensions
1313
{
14-
private static TChild InitPart<TChild>(this OpenXmlPartContainer parent, TChild childPart, PartTypeInfo partType, string? relId = null)
14+
private static TChild InitPart<TChild>(this OpenXmlPartContainer? parent, TChild childPart, PartTypeInfo partType, string? relId = null)
1515
where TChild : OpenXmlPart
1616
{
1717
if (parent == null)
@@ -54,7 +54,7 @@ private static T DoInitPart<T>(OpenXmlPartContainer parent, T childPart, string
5454
/// <param name="id">The relationship id. Optional, default to null.</param>
5555
/// <return>The newly added part</return>
5656
public static AlternativeFormatImportPart AddAlternativeFormatImportPart<T>(this T parent, PartTypeInfo partType, string? id = null)
57-
where T : OpenXmlPartContainer, ISupportedRelationship<AlternativeFormatImportPart>
57+
where T : OpenXmlPartContainer?, ISupportedRelationship<AlternativeFormatImportPart>?
5858
=> InitPart(parent, new AlternativeFormatImportPart(), partType, id);
5959

6060
/// <summary>

test/DocumentFormat.OpenXml.Framework.Tests/Builder/OpenXmlPackageBuilderTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using DocumentFormat.OpenXml.Builder;
5-
using DocumentFormat.OpenXml.Features;
6-
using DocumentFormat.OpenXml.Framework.Tests.Builder;
75
using DocumentFormat.OpenXml.Packaging;
86
using System.Collections.Generic;
9-
using System.IO;
107
using System.Linq;
118
using Xunit;
129

@@ -258,7 +255,7 @@ private static void Add(OpenXmlPackage package, int value)
258255

259256
private sealed class Builder : OpenXmlPackageBuilder<TestPackage>
260257
{
261-
public Builder(Builder other = null)
258+
public Builder(Builder? other = null)
262259
: base(other)
263260
{
264261
}

test/DocumentFormat.OpenXml.Framework.Tests/DocumentFormat.OpenXml.Framework.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<IsTestProject>true</IsTestProject>
66
<IsPackable>false</IsPackable>
77
<OutputType>Exe</OutputType>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910

1011
<ItemGroup>

test/DocumentFormat.OpenXml.Framework.Tests/ElementLookupTests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public void VerifyTypedRootsCanBeCreatedWord()
5858
// Act/Assert
5959
foreach (var rootType in allTypedParts)
6060
{
61+
#nullable disable
6162
var root = ((OpenXmlElement)Activator.CreateInstance(rootType))!;
63+
#nullable enable
6264
Assert.True(feature.TryCreate(root.QName, out var created));
6365
Assert.IsType(rootType, created);
6466
}
@@ -102,7 +104,7 @@ public CompositeRootElementFeature(params OpenXmlPackage[] package)
102104
_features = package.Select(p => p.Features.GetRequired<IRootElementFeature>()).ToArray();
103105
}
104106

105-
public bool TryCreate(in OpenXmlQualifiedName qname, [NotNullWhen(true)] out OpenXmlElement element)
107+
public bool TryCreate(in OpenXmlQualifiedName qname, [NotNullWhen(true)] out OpenXmlElement? element)
106108
{
107109
foreach (var feature in _features)
108110
{
@@ -131,8 +133,10 @@ ElementFactoryCollection GetLookup()
131133
{
132134
if (type.GetConstructor(Cached.Array<Type>()) is not null)
133135
{
136+
#nullable disable
134137
var instance = (OpenXmlElement)Activator.CreateInstance(type);
135-
return instance.Metadata.Children;
138+
#nullable enable
139+
return instance!.Metadata.Children;
136140
}
137141
else
138142
{
@@ -143,15 +147,15 @@ ElementFactoryCollection GetLookup()
143147
Children = GetLookup().Elements.Select(t => new OpenXmlTypeProxy(t.Type));
144148
}
145149

146-
public string Element { get; set; }
150+
public string? Element { get; set; }
147151

148-
public IEnumerable<OpenXmlTypeProxy> Children { get; set; }
152+
public IEnumerable<OpenXmlTypeProxy>? Children { get; set; }
149153

150-
public override bool Equals(object obj) => Equals(obj as LookupData);
154+
public override bool Equals(object? obj) => Equals(obj as LookupData);
151155

152-
public bool Equals(LookupData other)
156+
public bool Equals(LookupData? other)
153157
{
154-
if (other is null)
158+
if ((other is null) || (other.Children is null) || (Children is null))
155159
{
156160
return false;
157161
}
@@ -186,9 +190,9 @@ public OpenXmlTypeProxy(OpenXmlSchemaType type)
186190

187191
public OpenXmlQualifiedName Type { get; set; }
188192

189-
public bool Equals(OpenXmlTypeProxy other) => other is not null && Name.Equals(other.Name) && Type.Equals(other.Type);
193+
public bool Equals(OpenXmlTypeProxy? other) => other is not null && Name.Equals(other.Name) && Type.Equals(other.Type);
190194

191-
public override bool Equals(object obj) => obj is OpenXmlTypeProxy other && Equals(other);
195+
public override bool Equals(object? obj) => obj is OpenXmlTypeProxy other && Equals(other);
192196

193197
public override int GetHashCode() => Name.GetHashCode() ^ Type.GetHashCode();
194198

test/DocumentFormat.OpenXml.Framework.Tests/Features/PackageEventsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void PackageEventCalledOnClose()
1919
using (var package = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
2020
{
2121
package.AddPackageEventsFeature();
22-
package.Features.Get<IPackageEventsFeature>().Change += a => events.Add(a.Type);
22+
package.Features.Get<IPackageEventsFeature>()!.Change += a => events.Add(a.Type);
2323
}
2424

2525
Assert.Collection(
@@ -45,7 +45,7 @@ public void PartIdentifiedOpening()
4545
var deleting = default(OpenXmlPart);
4646
var deleted = default(OpenXmlPart);
4747

48-
package.Features.Get<IPartEventsFeature>().Change += a =>
48+
package.Features.Get<IPartEventsFeature>()!.Change += a =>
4949
{
5050
events.Add(a.Type);
5151

test/DocumentFormat.OpenXml.Framework.Tests/Features/PackageUriHandlingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private static string GetRelationshipContents(RelationshipInfo relationship)
244244

245245
private sealed class DisposableFeature : IDisposableFeature, IDisposable
246246
{
247-
private Action _dispose;
247+
private Action? _dispose;
248248

249249
void IDisposable.Dispose() => _dispose?.Invoke();
250250

test/DocumentFormat.OpenXml.Framework.Tests/ObjectSizeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void VerifySize(Type type, int size, int fullSize, int padding, int field
5050

5151
private static string GetTypeName(Type type)
5252
{
53-
return type.FullName.Substring(type.FullName.IndexOf('+') + 1).Replace('+', '.');
53+
return type.FullName!.Substring(type.FullName.IndexOf('+') + 1).Replace('+', '.');
5454
}
5555

5656
private sealed class TestOpenXmlElement : OpenXmlElement

test/DocumentFormat.OpenXml.Framework.Tests/OpenXmlPartTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void ExtensionTestKnownContentType()
7171
var contentTypeFeature = Substitute.For<IContentTypeFeature>();
7272
var expectedUri = new Uri(FilePath, UriKind.Relative);
7373

74-
partExtensionFeature.TryGetExtension(ContentType, out string expectedExt).Returns(callInfo =>
74+
partExtensionFeature.TryGetExtension(ContentType, out string? expectedExt).Returns(callInfo =>
7575
{
7676
callInfo[1] = ExpectedExt;
7777
return true;

test/DocumentFormat.OpenXml.Framework.Tests/PropertyBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void IsRequired()
6161

6262
private class SomeElement : OpenXmlElement
6363
{
64-
public StringValue Str { get; set; }
64+
public StringValue? Str { get; set; }
6565

6666
public override bool HasChildren => throw new NotImplementedException();
6767

0 commit comments

Comments
 (0)