Skip to content

Commit

Permalink
Enable nullable in tests (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym committed Nov 19, 2023
1 parent 020be7d commit 621a544
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
37 changes: 37 additions & 0 deletions src/Jab.FunctionalTests.Common/CompilerFeatureRequiredAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that compiler support for a particular feature is required for the location where this attribute is applied.
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
public sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
FeatureName = featureName;
}

/// <summary>
/// The name of the compiler feature.
/// </summary>
public string FeatureName { get; }

/// <summary>
/// If true, the compiler can choose to allow access to the location where this attribute is applied if it does not understand <see cref="FeatureName"/>.
/// </summary>
public bool IsOptional { get; init; }

/// <summary>
/// The <see cref="FeatureName"/> used for the ref structs C# feature.
/// </summary>
public const string RefStructs = nameof(RefStructs);

/// <summary>
/// The <see cref="FeatureName"/> used for the required members C# feature.
/// </summary>
public const string RequiredMembers = nameof(RequiredMembers);
}
}
36 changes: 22 additions & 14 deletions src/Jab.FunctionalTests.Common/ContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -63,8 +64,10 @@ internal partial class CanCreateSingletonContainer { }
[Fact]
public void CanUseSingletonInstance()
{
CanUseSingletonInstanceContainer c = new();
c.MyIServiceInstance = new AnotherServiceImplementation();
CanUseSingletonInstanceContainer c = new()
{
MyIServiceInstance = new AnotherServiceImplementation()
};
var implementationWithParameter = Assert.IsType<ServiceImplementationWithParameter>(c.GetService<IService>());
var anotherImplementation = c.GetService<IAnotherService>();

Expand All @@ -78,9 +81,9 @@ public void CanUseSingletonInstance()
[Singleton(typeof(IAnotherService), Instance = "MyIServiceInstance")]
internal partial class CanUseSingletonInstanceContainer
{
public IAnotherService MyIServiceInstance { get; set; }
public required IAnotherService MyIServiceInstance { get; set; }
}

[Fact]
public void CanUseStaticSingletonInstance()
{
Expand Down Expand Up @@ -218,12 +221,14 @@ public IService<T> CreateMyIServiceInstance<T>()
[Fact]
public void CanUseFuncFactory()
{
CanUseFuncFactoryContainer c = new();
int invocationCount = 0;
c.Factory = (IAnotherService s) =>
CanUseFuncFactoryContainer c = new()
{
invocationCount++;
return new ServiceImplementation<IAnotherService>(s);
Factory = (IAnotherService s) =>
{
invocationCount++;
return new ServiceImplementation<IAnotherService>(s);
}
};
var service = c.GetService<IService<IAnotherService>>();
Assert.NotNull(service.InnerService);
Expand All @@ -236,7 +241,7 @@ public void CanUseFuncFactory()
internal partial class CanUseFuncFactoryContainer
{
public delegate IService<IAnotherService> FactoryDelegate(IAnotherService s);
public FactoryDelegate Factory;
public required FactoryDelegate Factory;
}

[Fact]
Expand Down Expand Up @@ -343,8 +348,10 @@ internal partial class CanResolveOpenGenericServiceContainer { }
[Fact]
public void CanResolveEnumerableOfMixedOpenGenericService()
{
CanResolveEnumerableOfMixedOpenGenericServiceContainer c = new();
c.Instance = new ServiceImplementation<IAnotherService>(new AnotherServiceImplementation());
CanResolveEnumerableOfMixedOpenGenericServiceContainer c = new()
{
Instance = new ServiceImplementation<IAnotherService>(new AnotherServiceImplementation())
};

var services = c.GetService<IEnumerable<IService<IAnotherService>>>();
var array = Assert.IsType<IService<IAnotherService>[]>(services);
Expand All @@ -358,7 +365,7 @@ public void CanResolveEnumerableOfMixedOpenGenericService()
[Transient(typeof(IAnotherService), typeof(AnotherServiceImplementation))]
internal partial class CanResolveEnumerableOfMixedOpenGenericServiceContainer
{
public IService<IAnotherService> Instance { get; set; }
public required IService<IAnotherService> Instance { get; set; }
}

[Fact]
Expand Down Expand Up @@ -1175,13 +1182,14 @@ public void CanUseModuleWithStaticFactory()
CanUseModuleWithStaticFactoryContainer c = new();

var service = c.GetService<IService<IService>>();
var provider = c.GetService<IService<IServiceProvider>>();
Assert.IsType<ServiceImplementation>(service.InnerService);
}

[ServiceProvider]
[Import(typeof(IModuleWithStaticFactory))]
internal partial class CanUseModuleWithStaticFactoryContainer { }

[ServiceProviderModule]
[Singleton(typeof(IService<>), Factory = nameof(Factory))]
[Singleton(typeof(IService), Instance = nameof(Instance))]
Expand Down
16 changes: 16 additions & 0 deletions src/Jab.FunctionalTests.Common/RequiredMemberAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
/// <summary>Specifies that a type has required members or that a member is required.</summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
[EditorBrowsable(EditorBrowsableState.Never)]
#if SYSTEM_PRIVATE_CORELIB
public
#else
internal
#endif
sealed class RequiredMemberAttribute : Attribute
{ }
}

0 comments on commit 621a544

Please sign in to comment.