Skip to content
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: 2 additions & 2 deletions Docs/pages/08-analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ The source generator warns with `aweXpect2001` when a type named in

## Expectation generator

The source generator in `aweXpect.Core` reports an error for a method marked with `[GenerateExpectation]` that it
cannot turn into an expectation, see [generated expectations](./11-extending/01-generated-expectations.md):
The source generator in `aweXpect.Core` reports an error for a method marked with `[GenerateExpectation]` or
`[GenerateCollectionExpectation]` that it cannot turn into an expectation, see [generated expectations](./11-extending/01-generated-expectations.md):

| Rule | Reported when |
|----------------|-----------------------------------------------------------------------------------------------------------|
Expand Down
41 changes: 41 additions & 0 deletions Docs/pages/11-extending/01-generated-expectations.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,47 @@ private static bool IsUntitled([NotNullWhen(false)] Track? track)

Here `IsNotUntitled()` fails for a `null` subject, so the subject cannot be `null` after it.

## Collections

`[GenerateCollectionExpectation]` generates an expectation on the items of a collection from the same kind of method,
e.g. `AreLongerThan` for `That(tracks).All().AreLongerThan(…)`. It takes the name, the expectation text on a single
item and its plural:

```csharp no-compile
[GenerateCollectionExpectation("AreLongerThan", "is longer than {minimum}", "are longer than {minimum}")]
private static bool IsLongerThan(Track track, TimeSpan minimum)
=> track.Duration > minimum;
```

Together with `[GenerateExpectation]`, the text on a single item can be left out, as it is the expectation text of
`[GenerateExpectation]` without `{not}`:

```csharp no-compile
[GenerateExpectation("Is{Not}LongerThan", "is {not} longer than {minimum}")]
[GenerateCollectionExpectation("AreLongerThan", "are longer than {minimum}")]
private static bool IsLongerThan(Track track, TimeSpan minimum)
=> track.Duration > minimum;
```

```csharp no-compile
await Expect.That(tracks).All().AreLongerThan(TimeSpan.FromMinutes(2));
await Expect.That(tracks).AtLeast(2).AreLongerThan(TimeSpan.FromMinutes(2));
await Expect.That(tracks).None().AreLongerThan(TimeSpan.FromMinutes(5));
```

- The method follows the rules above, and its first parameter is the item.
- The expectation text describes a single item, e.g. "is longer than … for all items". The plural text is used
where the quantifier is the subject, e.g. "has values of which at least 2 are longer than …". The quantifier
carries the negation, so neither text contains `{not}`.
- The result counts the items, e.g. "but only 1 of 3 were", with "did" instead of "were" when the text does not start
with a form of "be". The failure message lists the matching or not matching items and the collection.
- The expectation extends the elements of every kind of collection that can hold the item: an `IEnumerable<T>`, an
`ImmutableArray<T>` and, on .NET 8 or later, an `IAsyncEnumerable<T>`. A `string` item extends the elements of
the collections of strings.
- A `null` item does not match, unless the item parameter is nullable: then the method decides. `[DisallowNull]` and
`NullableOverload` work like for a subject.
- `Summary` and `Remarks` replace or extend the XML documentation, which is derived from the plural text.

## Diagnostics

A method that the generator cannot turn into an expectation is reported as an error, see the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ if (expectedValue is IOptionsProvider<ExpectationBuilder> { Options: Equivalency

An expectation on the items of a collection, i.e. an extension method on `IEnumerableElements<TItem>`, which
`All()`, `AtLeast(2)` and the other quantifiers return, derives from `QuantifiedCollectionConstraint<TValue, TItem>`.
`IEnumerableElements<TItem>` gives access to the quantifier and the subject:
`IEnumerableElements<TItem>` gives access to the quantifier and the subject. For a condition on a single item,
[`[GenerateCollectionExpectation]`](./01-generated-expectations.md#collections) generates such an expectation.

```csharp
public static AndOrResult<IEnumerable<Track>, IThat<IEnumerable<Track>?>> AreRadioFriendly(
Expand Down
3 changes: 2 additions & 1 deletion Docs/pages/11-extending/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static partial class ThatTrack
```

The source generator in `aweXpect.Core` writes `IsRadioFriendly()` and `IsNotRadioFriendly()` with their failure
messages, see [generated expectations](./01-generated-expectations.md).
messages, see [generated expectations](./01-generated-expectations.md). With `[GenerateCollectionExpectation]`, the
same method also verifies the items of a collection, e.g. `That(tracks).All().AreRadioFriendly()`.

When an expectation needs more than a condition, e.g. options, a different result or a nested expectation, write it
yourself as described in the rest of this section.
Expand Down
1 change: 1 addition & 0 deletions Pipeline/Build.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ partial class Build
if (BuildScope == BuildScope.CoreOnly)
{
projects.Add(Solution.Tests.aweXpect_Core_Tests, Configuration.Debug);
projects.Add(Solution.Tests.aweXpect_Generators_Tests, Configuration.Debug);
}

foreach (var (project, configuration) in projects)
Expand Down
10 changes: 6 additions & 4 deletions Pipeline/Build.UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ partial class Build
Project[] UnitTestProjects(BuildScope buildScope)
=> buildScope switch
{
BuildScope.CoreOnly => [Solution.Tests.aweXpect_Core_Tests,],
// The generator of aweXpect.Core is tested by compiling its output against the Core project, which only the
// Debug configuration references, so its coverage counts for a Core change as well.
BuildScope.CoreOnly => [Solution.Tests.aweXpect_Core_Tests, Solution.Tests.aweXpect_Generators_Tests,],
BuildScope.MainOnly =>
[
Solution.Tests.aweXpect_Analyzers_Tests,
Expand Down Expand Up @@ -94,9 +96,9 @@ Project[] UnitTestProjects(BuildScope buildScope)
.EnableNoLogo());

// The projects that `UnitTests` did not run in Debug: under `CoreOnly` it runs its own projects in Debug.
Project[] projects = UnitTestProjects(BuildScope == BuildScope.CoreOnly
? BuildScope.MainOnly
: BuildScope.Default);
Project[] projects = BuildScope == BuildScope.CoreOnly
? UnitTestProjects(BuildScope.MainOnly).Except(UnitTestProjects(BuildScope.CoreOnly)).ToArray()
: UnitTestProjects(BuildScope.Default);

AbsolutePath resultsDirectory = TestResultsDirectory / Configuration.Debug;
RunUnitTests(projects, framework => framework != NetFramework,
Expand Down
2 changes: 1 addition & 1 deletion Pipeline/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ partial class Build : FalloutBuild
/// <para />
/// Afterward, you can update the package reference in `Directory.Packages.props` and reset this flag.
/// </summary>
readonly BuildScope BuildScope = BuildScope.Default;
readonly BuildScope BuildScope = BuildScope.CoreOnly;

[Parameter("Github Token")] readonly string GithubToken;
[GitRepository] readonly GitRepository Repository;
Expand Down
4 changes: 2 additions & 2 deletions Source/aweXpect.Core.Generators/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Rule ID | Category | Severity | Notes
--------------|--------------------------|----------|---------------------------------------------------------------------
aweXpect4001 | aweXpect.Core.Generators | Error | The class of a `GenerateExpectation` method cannot hold extensions.
aweXpect4002 | aweXpect.Core.Generators | Error | A `GenerateExpectation` method or its texts are not supported.
aweXpect4001 | aweXpect.Core.Generators | Error | The class of a marked method cannot hold extensions.
aweXpect4002 | aweXpect.Core.Generators | Error | A marked method or its texts are not supported.
aweXpect4003 | aweXpect.Core.Generators | Error | The `Result` of a `GenerateExpectation` names no suitable method.
Loading
Loading