Skip to content

Commit 9a8f4d6

Browse files
authored
feat!: Remove EntryAssembly and ExecutingAssembly methods and refactor (#331)
* feat!: remove `EntryAssembly`/`ExecutingAssembly` source methods `Assembly.GetExecutingAssembly()` is resolved inside the library (stack crawl mark), so `In.ExecutingAssembly()` always returned the aweXpect.Reflection assembly itself - never the caller's assembly. `Assembly.GetEntryAssembly()` returns the test host (or `null`) under test runners. Both made namespace-clarified architecture assertions silently pass on empty collections in their primary use case. Removes `In.EntryAssembly()`, `In.ExecutingAssembly()`, `Types.InEntryAssembly()`, `Types.InExecutingAssembly()` and the matching `InNamespaceResult` clarifiers. Use `In.AssemblyContaining<T>()` / `Types.InAssemblyContaining<T>()` instead, which resolve the assembly reliably from the type argument. * refactor: reuse `In.Assemblies` in `InNamespaceResult.InAssemblies` The clarifier inlined a byte-for-byte copy of the `In.Assemblies` body (including the description string), the only one of the clarifiers not delegating to `In.*`. Delegate to keep the description and null handling from drifting between `Types.InAssemblies(...)` and `Types.InNamespace(...).InAssemblies(...)`. * refactor: collapse `InNamespaceResult` clarifiers into a shared helper Each clarifier repeated `<source>.Types().WithinNamespace(_namespace)`. The shared `From` helper makes the invariant structural: a future clarifier cannot compile without re-applying the namespace filter. * docs: align the `In` class summary with the `In`/`Types` split The summary still read `Static entry point for assemblies.` although `In` also selects types and members, and no longer covers selection by criteria since `In.Namespace` moved to `Types.InNamespace`. Mirror the README positioning: `In` starts from concrete reflection objects, `Types` selects by criteria. * style: add missing newline at end of `Types.cs` * docs: document the mirroring contract between `In` and `Types` Every criteria-based source exists in three places (`In.*`, `Types.In*` and the `InNamespaceResult` clarifiers) that are only kept in sync manually; note this on the `Types` class so a new source is not added to one surface only.
1 parent 9a8b7c1 commit 9a8f4d6

9 files changed

Lines changed: 17 additions & 130 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ evaluated collection that you can navigate and filter further.
9494
| `In.AllLoadedAssemblies()` | all currently loaded assemblies (system assemblies [excluded](#assembly-exclusions)) |
9595
| `In.Assemblies(a1, a2, …)` / `In.Assemblies(collection)` | the given assemblies |
9696
| `In.AssemblyContaining<T>()` / `In.AssemblyContaining(typeof(T))` | the assembly that declares `T` |
97-
| `In.EntryAssembly()` | the entry assembly |
98-
| `In.ExecutingAssembly()` | the executing assembly |
9997
| `In.Type<T>()` / `In.Type(typeof(T))` | a single type |
10098
| `In.Types<T1, T2>()` / `In.Types<T1, T2, T3>()` / `In.Types(t1, t2, …)` | the given types |
10199
| `In.Constructors(…)` / `In.Events(…)` / `In.Fields(…)` / `In.Methods(…)` / `In.Properties(…)` | the given members |
@@ -111,7 +109,6 @@ point for architecture rules:
111109
| `Types.InAllLoadedAssemblies()` | all types in all currently loaded assemblies |
112110
| `Types.InAssemblies(a1, a2, …)` | all types in the given assemblies |
113111
| `Types.InAssemblyContaining<T>()` / `…(typeof(T))` | all types in the assembly that declares `T` |
114-
| `Types.InEntryAssembly()` / `Types.InExecutingAssembly()` | all types in the entry / executing assembly |
115112

116113
`Types.InNamespace(…)` searches all loaded assemblies by default; chain one of the same `In*` methods directly
117114
after it to clarify the assembly source (it can only be specified once, before any further filters):

Source/aweXpect.Reflection/Collections/Filtered.Types.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -507,41 +507,30 @@ internal InNamespaceResult(string @namespace)
507507
/// current <see cref="System.AppDomain.CurrentDomain" /> (the default).
508508
/// </summary>
509509
public Types InAllLoadedAssemblies()
510-
=> In.AllLoadedAssemblies().Types().WithinNamespace(_namespace);
510+
=> From(In.AllLoadedAssemblies());
511511

512512
/// <summary>
513513
/// Clarifies that the types within the namespace are searched in the given <paramref name="assemblies" />.
514514
/// </summary>
515515
public Types InAssemblies(params IEnumerable<Assembly?> assemblies)
516-
=> new Assemblies(assemblies, $"in the assemblies {Formatter.Format(assemblies)}").Types()
517-
.WithinNamespace(_namespace);
516+
=> From(In.Assemblies(assemblies));
518517

519518
/// <summary>
520519
/// Clarifies that the types within the namespace are searched in the assembly that contains
521520
/// the <typeparamref name="TType" />.
522521
/// </summary>
523522
public Types InAssemblyContaining<TType>()
524-
=> In.AssemblyContaining<TType>().Types().WithinNamespace(_namespace);
523+
=> From(In.AssemblyContaining<TType>());
525524

526525
/// <summary>
527526
/// Clarifies that the types within the namespace are searched in the assembly that contains
528527
/// the <paramref name="type" />.
529528
/// </summary>
530529
public Types InAssemblyContaining(Type type)
531-
=> In.AssemblyContaining(type).Types().WithinNamespace(_namespace);
530+
=> From(In.AssemblyContaining(type));
532531

533-
/// <summary>
534-
/// Clarifies that the types within the namespace are searched in the <see cref="Assembly.GetEntryAssembly()" />.
535-
/// </summary>
536-
public Types InEntryAssembly()
537-
=> In.EntryAssembly().Types().WithinNamespace(_namespace);
538-
539-
/// <summary>
540-
/// Clarifies that the types within the namespace are searched in the
541-
/// <see cref="Assembly.GetExecutingAssembly()" />.
542-
/// </summary>
543-
public Types InExecutingAssembly()
544-
=> In.ExecutingAssembly().Types().WithinNamespace(_namespace);
532+
private Types From(Assemblies assemblies)
533+
=> assemblies.Types().WithinNamespace(_namespace);
545534
}
546535

547536
/// <summary>

Source/aweXpect.Reflection/In.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
namespace aweXpect.Reflection;
1212

1313
/// <summary>
14-
/// Static entry point for assemblies.
14+
/// Static entry point for selecting assemblies, types and members from concrete reflection objects.
1515
/// </summary>
16+
/// <remarks>
17+
/// To select types <em>by criteria</em> (e.g. by namespace), use <see cref="aweXpect.Reflection.Types" /> instead.
18+
/// </remarks>
1619
public static class In
1720
{
1821
/// <summary>
@@ -50,18 +53,6 @@ public static Filtered.Assemblies AssemblyContaining<TType>()
5053
public static Filtered.Assemblies AssemblyContaining(Type type)
5154
=> new(type.Assembly, $"in assembly containing type {Formatter.Format(type)}");
5255

53-
/// <summary>
54-
/// Defines expectations on the <see cref="Assembly.GetEntryAssembly()" />.
55-
/// </summary>
56-
public static Filtered.Assemblies EntryAssembly()
57-
=> new(Assembly.GetEntryAssembly(), "in entry assembly");
58-
59-
/// <summary>
60-
/// Defines expectations on the <see cref="Assembly.GetExecutingAssembly()" />.
61-
/// </summary>
62-
public static Filtered.Assemblies ExecutingAssembly()
63-
=> new(Assembly.GetExecutingAssembly(), "in executing assembly");
64-
6556
/// <summary>
6657
/// Defines expectations on the type <typeparamref name="TType" />.
6758
/// </summary>

Source/aweXpect.Reflection/Types.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ namespace aweXpect.Reflection;
88
/// <summary>
99
/// Static entry point for selecting types by criteria.
1010
/// </summary>
11+
/// <remarks>
12+
/// The <c>In*</c> methods mirror the assembly sources on <see cref="In" /> (each is the corresponding
13+
/// <c>In.*(…).Types()</c>) and are mirrored again as source clarifiers on
14+
/// <see cref="Filtered.Types.InNamespaceResult" />. When adding a new assembly source, keep all three
15+
/// surfaces in sync.
16+
/// </remarks>
1117
public static class Types
1218
{
1319
/// <summary>
@@ -40,18 +46,6 @@ public static Filtered.Types InAssemblyContaining<TType>()
4046
public static Filtered.Types InAssemblyContaining(Type type)
4147
=> In.AssemblyContaining(type).Types();
4248

43-
/// <summary>
44-
/// Defines expectations on all types in the <see cref="Assembly.GetEntryAssembly()" />.
45-
/// </summary>
46-
public static Filtered.Types InEntryAssembly()
47-
=> In.EntryAssembly().Types();
48-
49-
/// <summary>
50-
/// Defines expectations on all types in the <see cref="Assembly.GetExecutingAssembly()" />.
51-
/// </summary>
52-
public static Filtered.Types InExecutingAssembly()
53-
=> In.ExecutingAssembly().Types();
54-
5549
/// <summary>
5650
/// Defines expectations on all types within the <paramref name="namespace" /> (including sub-namespaces).
5751
/// </summary>
@@ -66,4 +60,4 @@ public static Filtered.Types InExecutingAssembly()
6660
/// </remarks>
6761
public static Filtered.Types.InNamespaceResult InNamespace(string @namespace)
6862
=> new(@namespace);
69-
}
63+
}

Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net10.0.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ namespace aweXpect.Reflection
324324
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining(System.Type type) { }
325325
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining<TType>() { }
326326
public static aweXpect.Reflection.Collections.Filtered.Constructors Constructors(System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> constructors) { }
327-
public static aweXpect.Reflection.Collections.Filtered.Assemblies EntryAssembly() { }
328327
public static aweXpect.Reflection.Collections.Filtered.Events Events(System.Collections.Generic.IEnumerable<System.Reflection.EventInfo> events) { }
329-
public static aweXpect.Reflection.Collections.Filtered.Assemblies ExecutingAssembly() { }
330328
public static aweXpect.Reflection.Collections.Filtered.Fields Fields(System.Collections.Generic.IEnumerable<System.Reflection.FieldInfo> fields) { }
331329
public static aweXpect.Reflection.Collections.Filtered.Methods Methods(System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo> methods) { }
332330
public static aweXpect.Reflection.Collections.Filtered.Properties Properties(System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> properties) { }
@@ -2036,8 +2034,6 @@ namespace aweXpect.Reflection
20362034
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
20372035
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
20382036
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
2039-
public static aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
2040-
public static aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
20412037
public static aweXpect.Reflection.Collections.Filtered.Types.InNamespaceResult InNamespace(string @namespace) { }
20422038
}
20432039
}
@@ -2213,8 +2209,6 @@ namespace aweXpect.Reflection.Collections
22132209
public aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
22142210
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
22152211
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
2216-
public aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
2217-
public aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
22182212
}
22192213
public sealed class NamespaceDependencyFilterResult : aweXpect.Reflection.Collections.Filtered.Types
22202214
{

Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_net8.0.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ namespace aweXpect.Reflection
324324
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining(System.Type type) { }
325325
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining<TType>() { }
326326
public static aweXpect.Reflection.Collections.Filtered.Constructors Constructors(System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> constructors) { }
327-
public static aweXpect.Reflection.Collections.Filtered.Assemblies EntryAssembly() { }
328327
public static aweXpect.Reflection.Collections.Filtered.Events Events(System.Collections.Generic.IEnumerable<System.Reflection.EventInfo> events) { }
329-
public static aweXpect.Reflection.Collections.Filtered.Assemblies ExecutingAssembly() { }
330328
public static aweXpect.Reflection.Collections.Filtered.Fields Fields(System.Collections.Generic.IEnumerable<System.Reflection.FieldInfo> fields) { }
331329
public static aweXpect.Reflection.Collections.Filtered.Methods Methods(System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo> methods) { }
332330
public static aweXpect.Reflection.Collections.Filtered.Properties Properties(System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> properties) { }
@@ -2036,8 +2034,6 @@ namespace aweXpect.Reflection
20362034
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
20372035
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
20382036
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
2039-
public static aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
2040-
public static aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
20412037
public static aweXpect.Reflection.Collections.Filtered.Types.InNamespaceResult InNamespace(string @namespace) { }
20422038
}
20432039
}
@@ -2213,8 +2209,6 @@ namespace aweXpect.Reflection.Collections
22132209
public aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
22142210
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
22152211
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
2216-
public aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
2217-
public aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
22182212
}
22192213
public sealed class NamespaceDependencyFilterResult : aweXpect.Reflection.Collections.Filtered.Types
22202214
{

Tests/aweXpect.Reflection.Api.Tests/Expected/aweXpect.Reflection_netstandard2.0.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ namespace aweXpect.Reflection
324324
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining(System.Type type) { }
325325
public static aweXpect.Reflection.Collections.Filtered.Assemblies AssemblyContaining<TType>() { }
326326
public static aweXpect.Reflection.Collections.Filtered.Constructors Constructors(System.Collections.Generic.IEnumerable<System.Reflection.ConstructorInfo> constructors) { }
327-
public static aweXpect.Reflection.Collections.Filtered.Assemblies EntryAssembly() { }
328327
public static aweXpect.Reflection.Collections.Filtered.Events Events(System.Collections.Generic.IEnumerable<System.Reflection.EventInfo> events) { }
329-
public static aweXpect.Reflection.Collections.Filtered.Assemblies ExecutingAssembly() { }
330328
public static aweXpect.Reflection.Collections.Filtered.Fields Fields(System.Collections.Generic.IEnumerable<System.Reflection.FieldInfo> fields) { }
331329
public static aweXpect.Reflection.Collections.Filtered.Methods Methods(System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo> methods) { }
332330
public static aweXpect.Reflection.Collections.Filtered.Properties Properties(System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> properties) { }
@@ -1672,8 +1670,6 @@ namespace aweXpect.Reflection
16721670
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
16731671
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
16741672
public static aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
1675-
public static aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
1676-
public static aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
16771673
public static aweXpect.Reflection.Collections.Filtered.Types.InNamespaceResult InNamespace(string @namespace) { }
16781674
}
16791675
}
@@ -1849,8 +1845,6 @@ namespace aweXpect.Reflection.Collections
18491845
public aweXpect.Reflection.Collections.Filtered.Types InAssemblies(System.Collections.Generic.IEnumerable<System.Reflection.Assembly?> assemblies) { }
18501846
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining(System.Type type) { }
18511847
public aweXpect.Reflection.Collections.Filtered.Types InAssemblyContaining<TType>() { }
1852-
public aweXpect.Reflection.Collections.Filtered.Types InEntryAssembly() { }
1853-
public aweXpect.Reflection.Collections.Filtered.Types InExecutingAssembly() { }
18541848
}
18551849
public sealed class NamespaceDependencyFilterResult : aweXpect.Reflection.Collections.Filtered.Types
18561850
{

Tests/aweXpect.Reflection.Tests/InTests.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,6 @@ public async Task Constructors_WithEnumerable_ShouldContainProvidedConstructors(
8484
await That(sut.GetDescription()).IsEqualTo("in the constructors ").AsPrefix();
8585
}
8686

87-
[Fact]
88-
public async Task EntryAssembly_ShouldContainExpectedAssembly()
89-
{
90-
Assembly? expectedAssembly = Assembly.GetEntryAssembly();
91-
92-
Filtered.Assemblies sut = In.EntryAssembly();
93-
94-
#if NET8_0_OR_GREATER
95-
await That(sut).HasSingle().Which
96-
.IsEqualTo(expectedAssembly);
97-
#else
98-
await That(sut).IsEmpty();
99-
#endif
100-
await That(sut.GetDescription()).IsEqualTo("in entry assembly");
101-
}
102-
10387
[Fact]
10488
public async Task Events_WithEnumerable_ShouldContainProvidedEvents()
10589
{
@@ -111,18 +95,6 @@ public async Task Events_WithEnumerable_ShouldContainProvidedEvents()
11195
await That(sut.GetDescription()).IsEqualTo("in the events ").AsPrefix();
11296
}
11397

114-
[Fact]
115-
public async Task ExecutingAssembly_ShouldContainExpectedAssembly()
116-
{
117-
Assembly expectedAssembly = typeof(In).Assembly;
118-
119-
Filtered.Assemblies sut = In.ExecutingAssembly();
120-
121-
await That(sut).HasSingle().Which
122-
.IsEqualTo(expectedAssembly);
123-
await That(sut.GetDescription()).IsEqualTo("in executing assembly");
124-
}
125-
12698
[Fact]
12799
public async Task Fields_ShouldExcludeCompilerGeneratedBackingFields()
128100
{

Tests/aweXpect.Reflection.Tests/TypesTests.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,6 @@ public async Task InAssemblyContaining_WithType_ShouldContainTypesFromAssemblyOf
6565
await That(sut.GetDescription()).IsEqualTo("types in assembly containing type In");
6666
}
6767

68-
[Fact]
69-
public async Task InEntryAssembly_ShouldUseEntryAssemblyAsSource()
70-
{
71-
Filtered.Types sut = Types.InEntryAssembly();
72-
73-
await That(sut).DoesNotContain(typeof(TypesTests));
74-
await That(sut.GetDescription()).IsEqualTo("types in entry assembly");
75-
}
76-
77-
[Fact]
78-
public async Task InExecutingAssembly_ShouldContainTypesFromExecutingAssembly()
79-
{
80-
Filtered.Types sut = Types.InExecutingAssembly();
81-
82-
await That(sut).Contains(typeof(In));
83-
await That(sut.GetDescription()).IsEqualTo("types in executing assembly");
84-
}
85-
8668
[Fact]
8769
public async Task InNamespace_ShouldContainTypesWithinNamespaceIncludingSubNamespaces()
8870
{
@@ -196,26 +178,6 @@ await That(sut.GetDescription())
196178
.IsEqualTo($"types within namespace \"{NamespaceScope}\" in assembly containing type In");
197179
}
198180

199-
[Fact]
200-
public async Task InNamespace_InEntryAssembly_ShouldUseEntryAssemblyAsSource()
201-
{
202-
Filtered.Types sut = Types.InNamespace(NamespaceScope).InEntryAssembly();
203-
204-
await That(sut).IsEmpty();
205-
await That(sut.GetDescription())
206-
.IsEqualTo($"types within namespace \"{NamespaceScope}\" in entry assembly");
207-
}
208-
209-
[Fact]
210-
public async Task InNamespace_InExecutingAssembly_ShouldUseExecutingAssemblyAsSource()
211-
{
212-
Filtered.Types sut = Types.InNamespace(NamespaceScope).InExecutingAssembly();
213-
214-
await That(sut).IsEmpty();
215-
await That(sut.GetDescription())
216-
.IsEqualTo($"types within namespace \"{NamespaceScope}\" in executing assembly");
217-
}
218-
219181
[Fact]
220182
public async Task InNamespace_AfterClarification_OriginalShouldStillUseAllLoadedAssemblies()
221183
{

0 commit comments

Comments
 (0)