From dc996d19482e4fc592a758078d6c0cb0a4ad7c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Thu, 4 Jun 2026 09:04:01 +0200 Subject: [PATCH] feat: add strong named assembly filter and assertions (#226) Add filtering and assertion methods for strong named assemblies, mirroring the existing assembly API: - Filter: WhichAreStrongNamed / WhichAreNotStrongNamed - Single assertion: IsStrongNamed / IsNotStrongNamed - Many assertion: AreStrongNamed / AreNotStrongNamed An assembly is considered strong named when its name carries a non-empty public key token. --- README.md | 4 + .../AssemblyFilters.WhichAreStrongNamed.cs | 24 +++ .../Helpers/AssemblyHelpers.cs | 11 +- .../ThatAssemblies.AreStrongNamed.cs | 128 ++++++++++++++++ .../ThatAssembly.IsStrongNamed.cs | 59 ++++++++ .../Expected/aweXpect.Reflection_net10.0.txt | 8 + .../Expected/aweXpect.Reflection_net8.0.txt | 8 + .../aweXpect.Reflection_netstandard2.0.txt | 6 + ...blyFilters.WhichAreNotStrongNamed.Tests.cs | 42 ++++++ ...semblyFilters.WhichAreStrongNamed.Tests.cs | 42 ++++++ .../TestHelpers/AssemblyExtensions.cs | 13 ++ .../ThatAssemblies.AreNotStrongNamed.Tests.cs | 142 ++++++++++++++++++ .../ThatAssemblies.AreStrongNamed.Tests.cs | 142 ++++++++++++++++++ .../ThatAssembly.IsNotStrongNamed.Tests.cs | 97 ++++++++++++ .../ThatAssembly.IsStrongNamed.Tests.cs | 97 ++++++++++++ 15 files changed, 822 insertions(+), 1 deletion(-) create mode 100644 Source/aweXpect.Reflection/Filters/AssemblyFilters.WhichAreStrongNamed.cs create mode 100644 Source/aweXpect.Reflection/ThatAssemblies.AreStrongNamed.cs create mode 100644 Source/aweXpect.Reflection/ThatAssembly.IsStrongNamed.cs create mode 100644 Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreNotStrongNamed.Tests.cs create mode 100644 Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreStrongNamed.Tests.cs create mode 100644 Tests/aweXpect.Reflection.Tests/TestHelpers/AssemblyExtensions.cs create mode 100644 Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreNotStrongNamed.Tests.cs create mode 100644 Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreStrongNamed.Tests.cs create mode 100644 Tests/aweXpect.Reflection.Tests/ThatAssembly.IsNotStrongNamed.Tests.cs create mode 100644 Tests/aweXpect.Reflection.Tests/ThatAssembly.IsStrongNamed.Tests.cs diff --git a/README.md b/README.md index a4548a31..e0509a48 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,8 @@ on them directly: | by name | `.WithName("x")` | `.HasName("x")` | `.HaveName("x")` | | not by name | `.WithoutName("x")` | `.DoesNotHaveName("x")` | `.DoNotHaveName("x")` | | by target framework | `.WhichTarget("net8.0")` | `.Targets("net8.0")` | `.Target("net8.0")` | +| strong named | `.WhichAreStrongNamed()` | `.IsStrongNamed()` | `.AreStrongNamed()` | +| not strong named | `.WhichAreNotStrongNamed()` | `.IsNotStrongNamed()` | `.AreNotStrongNamed()` | | has attribute | `.With()` | `.Has()` | `.Have()` | | does not have attribute | `.Without()` | `.DoesNotHave()` | `.DoNotHave()` | | depends on assembly | `.WhichHaveADependencyOn("x")` | `.HasADependencyOn("x")` | `.HaveADependencyOn("x")` | @@ -483,6 +485,8 @@ The target framework is matched against the short moniker form (e.g. `net8.0`, ` derived from the assembly's `[TargetFramework]` attribute. Assemblies without that attribute are treated as having no target framework and never match. +An assembly is considered strong named when its name carries a non-empty public key token. + ## Combining filters Filters chain naturally (each narrows the previous result). Several filters offer an `Or…` companion to diff --git a/Source/aweXpect.Reflection/Filters/AssemblyFilters.WhichAreStrongNamed.cs b/Source/aweXpect.Reflection/Filters/AssemblyFilters.WhichAreStrongNamed.cs new file mode 100644 index 00000000..88342b9e --- /dev/null +++ b/Source/aweXpect.Reflection/Filters/AssemblyFilters.WhichAreStrongNamed.cs @@ -0,0 +1,24 @@ +using System.Reflection; +using aweXpect.Reflection.Collections; +using aweXpect.Reflection.Helpers; + +namespace aweXpect.Reflection; + +public static partial class AssemblyFilters +{ + /// + /// Filter for assemblies that are strong named. + /// + public static Filtered.Assemblies WhichAreStrongNamed(this Filtered.Assemblies @this) + => @this.Which(Filter.Suffix( + assembly => assembly.IsStrongNamed(), + " which are strong named")); + + /// + /// Filter for assemblies that are not strong named. + /// + public static Filtered.Assemblies WhichAreNotStrongNamed(this Filtered.Assemblies @this) + => @this.Which(Filter.Suffix( + assembly => !assembly.IsStrongNamed(), + " which are not strong named")); +} diff --git a/Source/aweXpect.Reflection/Helpers/AssemblyHelpers.cs b/Source/aweXpect.Reflection/Helpers/AssemblyHelpers.cs index d384966f..990d6854 100644 --- a/Source/aweXpect.Reflection/Helpers/AssemblyHelpers.cs +++ b/Source/aweXpect.Reflection/Helpers/AssemblyHelpers.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using System.Runtime.Versioning; @@ -84,6 +84,15 @@ public static bool HasAttribute( public static string? GetTargetFramework(this Assembly? assembly) => MapFrameworkName(assembly?.GetCustomAttribute()?.FrameworkName); + /// + /// Checks if the is strong named. + /// + /// + /// An assembly is considered strong named if its name carries a non-empty public key token. + /// + public static bool IsStrongNamed(this Assembly? assembly) + => assembly?.GetName().GetPublicKeyToken() is { Length: > 0, }; + /// /// Maps a (e.g. .NETCoreApp,Version=v8.0) /// to its short target framework moniker (e.g. net8.0). diff --git a/Source/aweXpect.Reflection/ThatAssemblies.AreStrongNamed.cs b/Source/aweXpect.Reflection/ThatAssemblies.AreStrongNamed.cs new file mode 100644 index 00000000..1e76d9eb --- /dev/null +++ b/Source/aweXpect.Reflection/ThatAssemblies.AreStrongNamed.cs @@ -0,0 +1,128 @@ +using System.Collections.Generic; +using System.Reflection; +using System.Text; +#if NET8_0_OR_GREATER +using System.Threading; +using System.Threading.Tasks; +#endif +using aweXpect.Core; +using aweXpect.Core.Constraints; +using aweXpect.Reflection.Helpers; +using aweXpect.Results; + +// ReSharper disable PossibleMultipleEnumeration + +namespace aweXpect.Reflection; + +public static partial class ThatAssemblies +{ + /// + /// Verifies that all items in the filtered collection of are strong named. + /// + public static AndOrResult, IThat>> AreStrongNamed( + this IThat> subject) + => new(subject.Get().ExpectationBuilder.AddConstraint>((it, grammars) + => new AreStrongNamedConstraint(it, grammars)), + subject); + +#if NET8_0_OR_GREATER + /// + /// Verifies that all items in the filtered collection of are strong named. + /// + public static AndOrResult, IThat>> AreStrongNamed( + this IThat> subject) + => new(subject.Get().ExpectationBuilder.AddConstraint>((it, grammars) + => new AreStrongNamedConstraint(it, grammars)), + subject); +#endif + + /// + /// Verifies that all items in the filtered collection of are not strong named. + /// + public static AndOrResult, IThat>> AreNotStrongNamed( + this IThat> subject) + => new(subject.Get().ExpectationBuilder.AddConstraint>((it, grammars) + => new AreNotStrongNamedConstraint(it, grammars)), + subject); + +#if NET8_0_OR_GREATER + /// + /// Verifies that all items in the filtered collection of are not strong named. + /// + public static AndOrResult, IThat>> AreNotStrongNamed( + this IThat> subject) + => new(subject.Get().ExpectationBuilder.AddConstraint>((it, grammars) + => new AreNotStrongNamedConstraint(it, grammars)), + subject); +#endif + + private sealed class AreStrongNamedConstraint(string it, ExpectationGrammars grammars) + : CollectionConstraintResult(grammars), + IValueConstraint> +#if NET8_0_OR_GREATER + , IAsyncConstraint> +#endif + { +#if NET8_0_OR_GREATER + public async Task IsMetBy(IAsyncEnumerable actual, + CancellationToken cancellationToken) + => await SetAsyncValue(actual, assembly => assembly.IsStrongNamed()); +#endif + + public ConstraintResult IsMetBy(IEnumerable actual) + => SetValue(actual, assembly => assembly.IsStrongNamed()); + + protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("are all strong named"); + + protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(it).Append(" contained not strong named assemblies "); + Formatter.Format(stringBuilder, NotMatching, FormattingOptions.Indented(indentation)); + } + + protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("are not all strong named"); + + protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(it).Append(" only contained strong named assemblies "); + Formatter.Format(stringBuilder, Matching, FormattingOptions.Indented(indentation)); + } + } + + private sealed class AreNotStrongNamedConstraint(string it, ExpectationGrammars grammars) + : CollectionConstraintResult(grammars), + IValueConstraint> +#if NET8_0_OR_GREATER + , IAsyncConstraint> +#endif + { +#if NET8_0_OR_GREATER + public async Task IsMetBy(IAsyncEnumerable actual, + CancellationToken cancellationToken) + => await SetAsyncValue(actual, assembly => !assembly.IsStrongNamed()); +#endif + + public ConstraintResult IsMetBy(IEnumerable actual) + => SetValue(actual, assembly => !assembly.IsStrongNamed()); + + protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("are all not strong named"); + + protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(it).Append(" contained strong named assemblies "); + Formatter.Format(stringBuilder, NotMatching, FormattingOptions.Indented(indentation)); + } + + protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("also contain a strong named assembly"); + + protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(it).Append(" only contained not strong named assemblies "); + Formatter.Format(stringBuilder, Matching, FormattingOptions.Indented(indentation)); + } + } +} diff --git a/Source/aweXpect.Reflection/ThatAssembly.IsStrongNamed.cs b/Source/aweXpect.Reflection/ThatAssembly.IsStrongNamed.cs new file mode 100644 index 00000000..a4ec989d --- /dev/null +++ b/Source/aweXpect.Reflection/ThatAssembly.IsStrongNamed.cs @@ -0,0 +1,59 @@ +using System.Reflection; +using System.Text; +using aweXpect.Core; +using aweXpect.Core.Constraints; +using aweXpect.Reflection.Helpers; +using aweXpect.Results; + +namespace aweXpect.Reflection; + +public static partial class ThatAssembly +{ + /// + /// Verifies that the is strong named. + /// + public static AndOrResult> IsStrongNamed( + this IThat subject) + => new(subject.Get().ExpectationBuilder.AddConstraint((it, grammars) + => new IsStrongNamedConstraint(it, grammars)), + subject); + + /// + /// Verifies that the is not strong named. + /// + public static AndOrResult> IsNotStrongNamed( + this IThat subject) + => new(subject.Get().ExpectationBuilder.AddConstraint((it, grammars) + => new IsStrongNamedConstraint(it, grammars).Invert()), + subject); + + private sealed class IsStrongNamedConstraint(string it, ExpectationGrammars grammars) + : ConstraintResult.WithNotNullValue(it, grammars), + IValueConstraint + { + public ConstraintResult IsMetBy(Assembly? actual) + { + Actual = actual; + Outcome = actual.IsStrongNamed() ? Outcome.Success : Outcome.Failure; + return this; + } + + protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("is strong named"); + + protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(It).Append(" was not strong named "); + Formatter.Format(stringBuilder, Actual); + } + + protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) + => stringBuilder.Append("is not strong named"); + + protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) + { + stringBuilder.Append(It).Append(" was strong named "); + Formatter.Format(stringBuilder, Actual); + } + } +} diff --git a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net10.0.txt b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net10.0.txt index 959ad354..8efae4d9 100644 --- a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net10.0.txt +++ b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net10.0.txt @@ -34,6 +34,8 @@ namespace aweXpect.Reflection public static class AssemblyFilters { public static aweXpect.Reflection.Collections.Filtered.Assemblies Which(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreNotStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveADependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string expected) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveDependenciesOnlyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, params string[] allowed) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveNoDependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string unexpected) { } @@ -552,6 +554,10 @@ namespace aweXpect.Reflection } public static class ThatAssemblies { + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreNotStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreNotStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreStrongNamed(this aweXpect.Core.IThat> subject) { } public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> DoNotHave(this aweXpect.Core.IThat> subject, bool inherit = true) where TAttribute : System.Attribute { } public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> DoNotHave(this aweXpect.Core.IThat> subject, bool inherit = true) @@ -615,6 +621,8 @@ namespace aweXpect.Reflection public static aweXpect.Results.StringEqualityTypeResult> HasNoDependencyOn(this aweXpect.Core.IThat subject, string unexpected) { } public static aweXpect.Reflection.ThatAssembly.HasVersionResult HasVersion(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.AndOrResult> HasVersion(this aweXpect.Core.IThat subject, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Results.AndOrResult> IsNotStrongNamed(this aweXpect.Core.IThat subject) { } + public static aweXpect.Results.AndOrResult> IsStrongNamed(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.StringEqualityTypeResult> Targets(this aweXpect.Core.IThat subject, string expected) { } public class HasVersionResult : aweXpect.Results.AndOrResult> { diff --git a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net8.0.txt b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net8.0.txt index 2455dee8..fa43d3b7 100644 --- a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net8.0.txt +++ b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net8.0.txt @@ -34,6 +34,8 @@ namespace aweXpect.Reflection public static class AssemblyFilters { public static aweXpect.Reflection.Collections.Filtered.Assemblies Which(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreNotStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveADependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string expected) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveDependenciesOnlyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, params string[] allowed) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveNoDependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string unexpected) { } @@ -552,6 +554,10 @@ namespace aweXpect.Reflection } public static class ThatAssemblies { + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreNotStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreNotStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreStrongNamed(this aweXpect.Core.IThat> subject) { } public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> DoNotHave(this aweXpect.Core.IThat> subject, bool inherit = true) where TAttribute : System.Attribute { } public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> DoNotHave(this aweXpect.Core.IThat> subject, bool inherit = true) @@ -615,6 +621,8 @@ namespace aweXpect.Reflection public static aweXpect.Results.StringEqualityTypeResult> HasNoDependencyOn(this aweXpect.Core.IThat subject, string unexpected) { } public static aweXpect.Reflection.ThatAssembly.HasVersionResult HasVersion(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.AndOrResult> HasVersion(this aweXpect.Core.IThat subject, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Results.AndOrResult> IsNotStrongNamed(this aweXpect.Core.IThat subject) { } + public static aweXpect.Results.AndOrResult> IsStrongNamed(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.StringEqualityTypeResult> Targets(this aweXpect.Core.IThat subject, string expected) { } public class HasVersionResult : aweXpect.Results.AndOrResult> { diff --git a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_netstandard2.0.txt b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_netstandard2.0.txt index d9e6db67..32596294 100644 --- a/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_netstandard2.0.txt +++ b/Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_netstandard2.0.txt @@ -34,6 +34,8 @@ namespace aweXpect.Reflection public static class AssemblyFilters { public static aweXpect.Reflection.Collections.Filtered.Assemblies Which(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreNotStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } + public static aweXpect.Reflection.Collections.Filtered.Assemblies WhichAreStrongNamed(this aweXpect.Reflection.Collections.Filtered.Assemblies @this) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveADependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string expected) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveDependenciesOnlyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, params string[] allowed) { } public static aweXpect.Reflection.Collections.Filtered.Assemblies.StringEqualityResultType WhichHaveNoDependencyOn(this aweXpect.Reflection.Collections.Filtered.Assemblies @this, string unexpected) { } @@ -552,6 +554,8 @@ namespace aweXpect.Reflection } public static class ThatAssemblies { + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreNotStrongNamed(this aweXpect.Core.IThat> subject) { } + public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> AreStrongNamed(this aweXpect.Core.IThat> subject) { } public static aweXpect.Results.AndOrResult, aweXpect.Core.IThat>> DoNotHave(this aweXpect.Core.IThat> subject, bool inherit = true) where TAttribute : System.Attribute { } public static aweXpect.Results.StringEqualityTypeResult, aweXpect.Core.IThat>> DoNotHaveName(this aweXpect.Core.IThat> subject, string unexpected) { } @@ -600,6 +604,8 @@ namespace aweXpect.Reflection public static aweXpect.Results.StringEqualityTypeResult> HasNoDependencyOn(this aweXpect.Core.IThat subject, string unexpected) { } public static aweXpect.Reflection.ThatAssembly.HasVersionResult HasVersion(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.AndOrResult> HasVersion(this aweXpect.Core.IThat subject, System.Func predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { } + public static aweXpect.Results.AndOrResult> IsNotStrongNamed(this aweXpect.Core.IThat subject) { } + public static aweXpect.Results.AndOrResult> IsStrongNamed(this aweXpect.Core.IThat subject) { } public static aweXpect.Results.StringEqualityTypeResult> Targets(this aweXpect.Core.IThat subject, string expected) { } public class HasVersionResult : aweXpect.Results.AndOrResult> { diff --git a/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreNotStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreNotStrongNamed.Tests.cs new file mode 100644 index 00000000..3e733c1c --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreNotStrongNamed.Tests.cs @@ -0,0 +1,42 @@ +using aweXpect.Reflection.Collections; +using aweXpect.Reflection.Tests.TestHelpers; +using aweXpect.Reflection.Tests.TestHelpers.Types; + +namespace aweXpect.Reflection.Tests.Filters; + +public sealed partial class AssemblyFilters +{ + public sealed class WhichAreNotStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task ShouldDescribeTheFilter() + { + Filtered.Assemblies assemblies = In.AllLoadedAssemblies() + .WhichAreNotStrongNamed(); + + await That(assemblies.GetDescription()) + .IsEqualTo("in all loaded assemblies which are not strong named"); + } + + [Fact] + public async Task ShouldFilterForNotStrongNamedAssemblies() + { + Filtered.Assemblies assemblies = In.AssemblyContaining() + .WhichAreNotStrongNamed(); + + await That(assemblies).All().Satisfy(assembly => !assembly.IsStrongNamed()).And.IsNotEmpty(); + } + + [Fact] + public async Task ShouldNotMatchStrongNamedAssemblies() + { + Filtered.Assemblies assemblies = In.AssemblyContaining(typeof(In)) + .WhichAreNotStrongNamed(); + + await That(assemblies).IsEmpty(); + } + } + } +} diff --git a/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreStrongNamed.Tests.cs new file mode 100644 index 00000000..72b1ec6f --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/Filters/AssemblyFilters.WhichAreStrongNamed.Tests.cs @@ -0,0 +1,42 @@ +using aweXpect.Reflection.Collections; +using aweXpect.Reflection.Tests.TestHelpers; +using aweXpect.Reflection.Tests.TestHelpers.Types; + +namespace aweXpect.Reflection.Tests.Filters; + +public sealed partial class AssemblyFilters +{ + public sealed class WhichAreStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task ShouldDescribeTheFilter() + { + Filtered.Assemblies assemblies = In.AllLoadedAssemblies() + .WhichAreStrongNamed(); + + await That(assemblies.GetDescription()) + .IsEqualTo("in all loaded assemblies which are strong named"); + } + + [Fact] + public async Task ShouldFilterForStrongNamedAssemblies() + { + Filtered.Assemblies assemblies = In.AllLoadedAssemblies() + .WhichAreStrongNamed(); + + await That(assemblies).All().Satisfy(assembly => assembly.IsStrongNamed()).And.IsNotEmpty(); + } + + [Fact] + public async Task ShouldNotMatchAssembliesWithoutStrongName() + { + Filtered.Assemblies assemblies = In.AssemblyContaining() + .WhichAreStrongNamed(); + + await That(assemblies).IsEmpty(); + } + } + } +} diff --git a/Tests/aweXpect.Reflection.Tests/TestHelpers/AssemblyExtensions.cs b/Tests/aweXpect.Reflection.Tests/TestHelpers/AssemblyExtensions.cs new file mode 100644 index 00000000..ceece76d --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/TestHelpers/AssemblyExtensions.cs @@ -0,0 +1,13 @@ +using System.Reflection; + +namespace aweXpect.Reflection.Tests.TestHelpers; + +public static class AssemblyExtensions +{ + /// + /// Checks if the is strong named. + /// + /// The to check. + public static bool IsStrongNamed(this Assembly? assembly) + => assembly?.GetName().GetPublicKeyToken() is { Length: > 0, }; +} diff --git a/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreNotStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreNotStrongNamed.Tests.cs new file mode 100644 index 00000000..63dcfddb --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreNotStrongNamed.Tests.cs @@ -0,0 +1,142 @@ +using System.Collections.Generic; +using System.Reflection; +using aweXpect.Reflection.Tests.TestHelpers.Types; +#if NET8_0_OR_GREATER +using aweXpect.Reflection.Tests.TestHelpers; +#endif + +namespace aweXpect.Reflection.Tests; + +public sealed partial class ThatAssemblies +{ + public sealed class AreNotStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task WhenAllAssembliesAreNotStrongNamed_ShouldSucceed() + { + IEnumerable subject = new[] + { + typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).AreNotStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssembliesContainStrongNamedAssemblies_ShouldFail() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).AreNotStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + are all not strong named, + but it contained strong named assemblies [ + * + ] + """).AsWildcard(); + } + } + + public sealed class NegatedTests + { + [Fact] + public async Task WhenAllAssembliesAreNotStrongNamed_ShouldFail() + { + IEnumerable subject = new[] + { + typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).DoesNotComplyWith(they => they.AreNotStrongNamed()); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + also contain a strong named assembly, + but it only contained not strong named assemblies [ + * + ] + """).AsWildcard(); + } + + [Fact] + public async Task WhenAssembliesContainStrongNamedAssemblies_ShouldSucceed() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).DoesNotComplyWith(they => they.AreNotStrongNamed()); + } + + await That(Act).DoesNotThrow(); + } + } + +#if NET8_0_OR_GREATER + public sealed class AsyncEnumerableTests + { + [Fact] + public async Task WhenAllAssembliesAreNotStrongNamed_ShouldSucceed() + { + IAsyncEnumerable subject = new[] + { + typeof(PublicAbstractClass).Assembly, + }.ToTestAsyncEnumerable(); + + async Task Act() + { + await That(subject).AreNotStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssembliesContainStrongNamedAssemblies_ShouldFail() + { + IAsyncEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }.ToTestAsyncEnumerable(); + + async Task Act() + { + await That(subject).AreNotStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + are all not strong named, + but it contained strong named assemblies [ + * + ] + """).AsWildcard(); + } + } +#endif + } +} diff --git a/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreStrongNamed.Tests.cs new file mode 100644 index 00000000..f9a9d92c --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/ThatAssemblies.AreStrongNamed.Tests.cs @@ -0,0 +1,142 @@ +using System.Collections.Generic; +using System.Reflection; +using aweXpect.Reflection.Tests.TestHelpers.Types; +#if NET8_0_OR_GREATER +using aweXpect.Reflection.Tests.TestHelpers; +#endif + +namespace aweXpect.Reflection.Tests; + +public sealed partial class ThatAssemblies +{ + public sealed class AreStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task WhenAllAssembliesAreStrongNamed_ShouldSucceed() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(object).Assembly, + }; + + async Task Act() + { + await That(subject).AreStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssembliesContainNotStrongNamedAssemblies_ShouldFail() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).AreStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + are all strong named, + but it contained not strong named assemblies [ + * + ] + """).AsWildcard(); + } + } + + public sealed class NegatedTests + { + [Fact] + public async Task WhenAllAssembliesAreStrongNamed_ShouldFail() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(object).Assembly, + }; + + async Task Act() + { + await That(subject).DoesNotComplyWith(they => they.AreStrongNamed()); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + are not all strong named, + but it only contained strong named assemblies [ + * + ] + """).AsWildcard(); + } + + [Fact] + public async Task WhenAssembliesContainNotStrongNamedAssemblies_ShouldSucceed() + { + IEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }; + + async Task Act() + { + await That(subject).DoesNotComplyWith(they => they.AreStrongNamed()); + } + + await That(Act).DoesNotThrow(); + } + } + +#if NET8_0_OR_GREATER + public sealed class AsyncEnumerableTests + { + [Fact] + public async Task WhenAllAssembliesAreStrongNamed_ShouldSucceed() + { + IAsyncEnumerable subject = new[] + { + typeof(In).Assembly, typeof(object).Assembly, + }.ToTestAsyncEnumerable(); + + async Task Act() + { + await That(subject).AreStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssembliesContainNotStrongNamedAssemblies_ShouldFail() + { + IAsyncEnumerable subject = new[] + { + typeof(In).Assembly, typeof(PublicAbstractClass).Assembly, + }.ToTestAsyncEnumerable(); + + async Task Act() + { + await That(subject).AreStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + are all strong named, + but it contained not strong named assemblies [ + * + ] + """).AsWildcard(); + } + } +#endif + } +} diff --git a/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsNotStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsNotStrongNamed.Tests.cs new file mode 100644 index 00000000..5b2b48ef --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsNotStrongNamed.Tests.cs @@ -0,0 +1,97 @@ +using System.Reflection; +using aweXpect.Reflection.Tests.TestHelpers.Types; +using Xunit.Sdk; + +namespace aweXpect.Reflection.Tests; + +public sealed partial class ThatAssembly +{ + public sealed class IsNotStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task WhenAssemblyIsNotStrongNamed_ShouldSucceed() + { + Assembly subject = typeof(PublicAbstractClass).Assembly; + + async Task Act() + { + await That(subject).IsNotStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssemblyIsNull_ShouldFail() + { + Assembly? subject = null; + + async Task Act() + { + await That(subject).IsNotStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + is not strong named, + but it was + """); + } + + [Fact] + public async Task WhenAssemblyIsStrongNamed_ShouldFail() + { + Assembly subject = typeof(In).Assembly; + + async Task Act() + { + await That(subject).IsNotStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that subject + is not strong named, + but it was strong named {Formatter.Format(subject)} + """); + } + } + + public sealed class NegatedTests + { + [Fact] + public async Task WhenAssemblyIsNotStrongNamed_ShouldFail() + { + Assembly subject = typeof(PublicAbstractClass).Assembly; + + async Task Act() + { + await That(subject).DoesNotComplyWith(it => it.IsNotStrongNamed()); + } + + await That(Act).Throws() + .WithMessage($""" + Expected that subject + is strong named, + but it was not strong named {Formatter.Format(subject)} + """); + } + + [Fact] + public async Task WhenAssemblyIsStrongNamed_ShouldSucceed() + { + Assembly subject = typeof(In).Assembly; + + async Task Act() + { + await That(subject).DoesNotComplyWith(it => it.IsNotStrongNamed()); + } + + await That(Act).DoesNotThrow(); + } + } + } +} diff --git a/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsStrongNamed.Tests.cs b/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsStrongNamed.Tests.cs new file mode 100644 index 00000000..7d8f9369 --- /dev/null +++ b/Tests/aweXpect.Reflection.Tests/ThatAssembly.IsStrongNamed.Tests.cs @@ -0,0 +1,97 @@ +using System.Reflection; +using aweXpect.Reflection.Tests.TestHelpers.Types; +using Xunit.Sdk; + +namespace aweXpect.Reflection.Tests; + +public sealed partial class ThatAssembly +{ + public sealed class IsStrongNamed + { + public sealed class Tests + { + [Fact] + public async Task WhenAssemblyIsNotStrongNamed_ShouldFail() + { + Assembly subject = typeof(PublicAbstractClass).Assembly; + + async Task Act() + { + await That(subject).IsStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that subject + is strong named, + but it was not strong named {Formatter.Format(subject)} + """); + } + + [Fact] + public async Task WhenAssemblyIsNull_ShouldFail() + { + Assembly? subject = null; + + async Task Act() + { + await That(subject).IsStrongNamed(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that subject + is strong named, + but it was + """); + } + + [Fact] + public async Task WhenAssemblyIsStrongNamed_ShouldSucceed() + { + Assembly subject = typeof(In).Assembly; + + async Task Act() + { + await That(subject).IsStrongNamed(); + } + + await That(Act).DoesNotThrow(); + } + } + + public sealed class NegatedTests + { + [Fact] + public async Task WhenAssemblyIsNotStrongNamed_ShouldSucceed() + { + Assembly subject = typeof(PublicAbstractClass).Assembly; + + async Task Act() + { + await That(subject).DoesNotComplyWith(it => it.IsStrongNamed()); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenAssemblyIsStrongNamed_ShouldFail() + { + Assembly subject = typeof(In).Assembly; + + async Task Act() + { + await That(subject).DoesNotComplyWith(it => it.IsStrongNamed()); + } + + await That(Act).Throws() + .WithMessage($""" + Expected that subject + is not strong named, + but it was strong named {Formatter.Format(subject)} + """); + } + } + } +}