-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from sveinungf/dev/uap
Remove default constructors on structs for .NET Standard 2.0
- Loading branch information
Showing
8 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
SpreadCheetah.Test/Helpers/NoDefaultConstructorForStructsCondition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using ArchUnitNET.Domain; | ||
using ArchUnitNET.Domain.Extensions; | ||
using ArchUnitNET.Fluent.Conditions; | ||
|
||
namespace SpreadCheetah.Test.Helpers; | ||
|
||
internal sealed class NoDefaultConstructorForStructsCondition : ICondition<IType> | ||
{ | ||
public string Description => "not have a default constructor"; | ||
|
||
public IEnumerable<ConditionResult> Check(IEnumerable<IType> objects, Architecture architecture) | ||
{ | ||
foreach (var obj in objects) | ||
{ | ||
yield return Check(obj); | ||
} | ||
} | ||
|
||
private static ConditionResult Check(IType type) | ||
{ | ||
var pass = true; | ||
|
||
if (type is Struct structType) | ||
{ | ||
foreach (var constructor in structType.GetConstructors()) | ||
{ | ||
if (constructor.IsStatic is true) | ||
continue; | ||
|
||
if (!constructor.Parameters.Any()) | ||
{ | ||
pass = false; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return new ConditionResult(type, pass: pass, failDescription: pass ? null : "has a default constructor"); | ||
} | ||
|
||
public bool CheckEmpty() => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if !NET5_0_OR_GREATER | ||
using ArchUnitNET.Domain; | ||
using ArchUnitNET.Loader; | ||
using ArchUnitNET.xUnit; | ||
using SpreadCheetah.Test.Helpers; | ||
using static ArchUnitNET.Fluent.ArchRuleDefinition; | ||
|
||
namespace SpreadCheetah.Test.Tests; | ||
|
||
public class ArchitectureTests | ||
{ | ||
private static readonly Architecture Architecture = new ArchLoader().LoadAssemblies(typeof(Spreadsheet).Assembly).Build(); | ||
|
||
/// <summary> | ||
/// Structs with a default constructor leads to a compilation error on UWP: https://github.com/sveinungf/spreadcheetah/issues/58 | ||
/// </summary> | ||
[Fact] | ||
public void Architecture_NoDefaultConstructorForStructs() | ||
{ | ||
// Arrange | ||
var rule = Types().Should().FollowCustomCondition(new NoDefaultConstructorForStructsCondition()); | ||
|
||
// Act & Assert | ||
rule.Check(Architecture); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters