Skip to content
This repository was archived by the owner on Nov 10, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAttribute>()` | `.Has<TAttribute>()` | `.Have<TAttribute>()` |
| does not have attribute | `.Without<TAttribute>()` | `.DoesNotHave<TAttribute>()` | `.DoNotHave<TAttribute>()` |
| depends on assembly | `.WhichHaveADependencyOn("x")` | `.HasADependencyOn("x")` | `.HaveADependencyOn("x")` |
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Reflection;
using aweXpect.Reflection.Collections;
using aweXpect.Reflection.Helpers;

namespace aweXpect.Reflection;

public static partial class AssemblyFilters
{
/// <summary>
/// Filter for assemblies that are strong named.
/// </summary>
public static Filtered.Assemblies WhichAreStrongNamed(this Filtered.Assemblies @this)
=> @this.Which(Filter.Suffix<Assembly>(
assembly => assembly.IsStrongNamed(),
" which are strong named"));

/// <summary>
/// Filter for assemblies that are not strong named.
/// </summary>
public static Filtered.Assemblies WhichAreNotStrongNamed(this Filtered.Assemblies @this)
=> @this.Which(Filter.Suffix<Assembly>(
assembly => !assembly.IsStrongNamed(),
" which are not strong named"));
}
11 changes: 10 additions & 1 deletion Source/aweXpect.Reflection/Helpers/AssemblyHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
Expand Down Expand Up @@ -84,6 +84,15 @@ public static bool HasAttribute(
public static string? GetTargetFramework(this Assembly? assembly)
=> MapFrameworkName(assembly?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName);

/// <summary>
/// Checks if the <paramref name="assembly" /> is strong named.
/// </summary>
/// <remarks>
/// An assembly is considered strong named if its name carries a non-empty public key token.
/// </remarks>
public static bool IsStrongNamed(this Assembly? assembly)
=> assembly?.GetName().GetPublicKeyToken() is { Length: > 0, };

/// <summary>
/// Maps a <see cref="TargetFrameworkAttribute.FrameworkName" /> (e.g. <c>.NETCoreApp,Version=v8.0</c>)
/// to its short target framework moniker (e.g. <c>net8.0</c>).
Expand Down
128 changes: 128 additions & 0 deletions Source/aweXpect.Reflection/ThatAssemblies.AreStrongNamed.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Verifies that all items in the filtered collection of <see cref="Assembly" /> are strong named.
/// </summary>
public static AndOrResult<IEnumerable<Assembly?>, IThat<IEnumerable<Assembly?>>> AreStrongNamed(
this IThat<IEnumerable<Assembly?>> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint<IEnumerable<Assembly?>>((it, grammars)
=> new AreStrongNamedConstraint(it, grammars)),
subject);

#if NET8_0_OR_GREATER
/// <summary>
/// Verifies that all items in the filtered collection of <see cref="Assembly" /> are strong named.
/// </summary>
public static AndOrResult<IAsyncEnumerable<Assembly?>, IThat<IAsyncEnumerable<Assembly?>>> AreStrongNamed(
this IThat<IAsyncEnumerable<Assembly?>> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint<IAsyncEnumerable<Assembly?>>((it, grammars)
=> new AreStrongNamedConstraint(it, grammars)),
subject);
#endif

/// <summary>
/// Verifies that all items in the filtered collection of <see cref="Assembly" /> are not strong named.
/// </summary>
public static AndOrResult<IEnumerable<Assembly?>, IThat<IEnumerable<Assembly?>>> AreNotStrongNamed(
this IThat<IEnumerable<Assembly?>> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint<IEnumerable<Assembly?>>((it, grammars)
=> new AreNotStrongNamedConstraint(it, grammars)),
subject);

#if NET8_0_OR_GREATER
/// <summary>
/// Verifies that all items in the filtered collection of <see cref="Assembly" /> are not strong named.
/// </summary>
public static AndOrResult<IAsyncEnumerable<Assembly?>, IThat<IAsyncEnumerable<Assembly?>>> AreNotStrongNamed(
this IThat<IAsyncEnumerable<Assembly?>> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint<IAsyncEnumerable<Assembly?>>((it, grammars)
=> new AreNotStrongNamedConstraint(it, grammars)),
subject);
#endif

private sealed class AreStrongNamedConstraint(string it, ExpectationGrammars grammars)
: CollectionConstraintResult<Assembly?>(grammars),
IValueConstraint<IEnumerable<Assembly?>>
#if NET8_0_OR_GREATER
, IAsyncConstraint<IAsyncEnumerable<Assembly?>>
#endif
{
#if NET8_0_OR_GREATER
public async Task<ConstraintResult> IsMetBy(IAsyncEnumerable<Assembly?> actual,
CancellationToken cancellationToken)
=> await SetAsyncValue(actual, assembly => assembly.IsStrongNamed());
#endif

public ConstraintResult IsMetBy(IEnumerable<Assembly?> 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<Assembly?>(grammars),
IValueConstraint<IEnumerable<Assembly?>>
#if NET8_0_OR_GREATER
, IAsyncConstraint<IAsyncEnumerable<Assembly?>>
#endif
{
#if NET8_0_OR_GREATER
public async Task<ConstraintResult> IsMetBy(IAsyncEnumerable<Assembly?> actual,
CancellationToken cancellationToken)
=> await SetAsyncValue(actual, assembly => !assembly.IsStrongNamed());
#endif

public ConstraintResult IsMetBy(IEnumerable<Assembly?> 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));
}
}
}
59 changes: 59 additions & 0 deletions Source/aweXpect.Reflection/ThatAssembly.IsStrongNamed.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Verifies that the <see cref="Assembly" /> is strong named.
/// </summary>
public static AndOrResult<Assembly?, IThat<Assembly?>> IsStrongNamed(
this IThat<Assembly?> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint((it, grammars)
=> new IsStrongNamedConstraint(it, grammars)),
subject);

/// <summary>
/// Verifies that the <see cref="Assembly" /> is not strong named.
/// </summary>
public static AndOrResult<Assembly?, IThat<Assembly?>> IsNotStrongNamed(
this IThat<Assembly?> subject)
=> new(subject.Get().ExpectationBuilder.AddConstraint((it, grammars)
=> new IsStrongNamedConstraint(it, grammars).Invert()),
subject);

private sealed class IsStrongNamedConstraint(string it, ExpectationGrammars grammars)
: ConstraintResult.WithNotNullValue<Assembly?>(it, grammars),
IValueConstraint<Assembly?>
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Reflection.Assembly, bool> 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) { }
Expand Down Expand Up @@ -552,6 +554,10 @@ namespace aweXpect.Reflection
}
public static class ThatAssemblies
{
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>>> AreNotStrongNamed(this aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>> subject) { }
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>>> AreNotStrongNamed(this aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>> subject) { }
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>>> AreStrongNamed(this aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>> subject) { }
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>>> AreStrongNamed(this aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>> subject) { }
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>>> DoNotHave<TAttribute>(this aweXpect.Core.IThat<System.Collections.Generic.IAsyncEnumerable<System.Reflection.Assembly?>> subject, bool inherit = true)
where TAttribute : System.Attribute { }
public static aweXpect.Results.AndOrResult<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>, aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>>> DoNotHave<TAttribute>(this aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Reflection.Assembly?>> subject, bool inherit = true)
Expand Down Expand Up @@ -615,6 +621,8 @@ namespace aweXpect.Reflection
public static aweXpect.Results.StringEqualityTypeResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>> HasNoDependencyOn(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject, string unexpected) { }
public static aweXpect.Reflection.ThatAssembly.HasVersionResult HasVersion(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject) { }
public static aweXpect.Results.AndOrResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>> HasVersion(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject, System.Func<System.Version, bool> predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "") { }
public static aweXpect.Results.AndOrResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>> IsNotStrongNamed(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject) { }
public static aweXpect.Results.AndOrResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>> IsStrongNamed(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject) { }
public static aweXpect.Results.StringEqualityTypeResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>> Targets(this aweXpect.Core.IThat<System.Reflection.Assembly?> subject, string expected) { }
public class HasVersionResult : aweXpect.Results.AndOrResult<System.Reflection.Assembly?, aweXpect.Core.IThat<System.Reflection.Assembly?>>
{
Expand Down
Loading
Loading