Skip to content

Commit

Permalink
Merge pull request #1405 from bUnit-dev/release/v1.27
Browse files Browse the repository at this point in the history
Release of new minor version v1.27
  • Loading branch information
egil authored Mar 2, 2024
2 parents da14d46 + 289bd9d commit a3d2520
Show file tree
Hide file tree
Showing 60 changed files with 706 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"docfx": {
"version": "2.74.1",
"version": "2.75.3",
"commands": [
"docfx"
]
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ jobs:
6.0.x
7.0.x
8.0.x
9.0.x
- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand Down Expand Up @@ -109,7 +110,7 @@ jobs:
- name: Validate package
shell: pwsh
run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NUGET_DIRECTORY }}/*.nupkg") --excluded-rules IconMustBeSet

run-test:
strategy:
fail-fast: false
Expand All @@ -132,6 +133,7 @@ jobs:
6.0.x
7.0.x
8.0.x
9.0.x
- name: 🧪 Run unit tests
run: dotnet test -c release --blame --blame-crash --blame-hang
Expand Down Expand Up @@ -163,7 +165,7 @@ jobs:
path: ${{ env.NUGET_DIRECTORY }}

- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand Down Expand Up @@ -209,9 +211,10 @@ jobs:
6.0.x
7.0.x
8.0.x
9.0.x
- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand Down Expand Up @@ -245,7 +248,7 @@ jobs:
- name: 'Checkout Repository'
uses: actions/checkout@v4
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v4

release-preview:
if: github.event_name == 'workflow_dispatch' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/v2')
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/docs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
git config --global commit.gpgsign true
- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand All @@ -77,6 +77,7 @@ jobs:
6.0.x
7.0.x
8.0.x
9.0.x
- name: 🎨 Setup color
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prepare-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
git config --global commit.gpgsign true
- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
git config --global commit.gpgsign true
- name: ⚙️ Setup GIT versioning
uses: dotnet/[email protected].1
uses: dotnet/[email protected].2
with:
setAllVars: true

Expand All @@ -62,9 +62,10 @@ jobs:
6.0.x
7.0.x
8.0.x
9.0.x
- name: 🛠️ Update changelog
uses: thomaseizinger/keep-a-changelog-new-release@1.3.0
uses: thomaseizinger/keep-a-changelog-new-release@2.0.0
with:
version: ${{ env.NBGV_SemVer2 }}

Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Added
- Support for `IKeyedServiceProvider` in net8.0. Reported by [@ViRuSTriNiTy](https://github.com/ViRuSTriNiTy). By [@linkdotnet](https://github.com/linkdotnet).
- Support for `net9.0`. NOTE, there is no commitment as of now to support net9.0 in bUnit v1. However. Support for net9.0 may move to a future v2 release of bUnit and be deprecated in v1. However, allowing bUnit to build and work with net9.0 previews allows our users to keep testing!

### Fixed
- Support for `SupplyFromQueryParameter` in net8.0. Reported by [@aayjaychan](https://github.com/aayjaychan). Fixed by [@egil](https://github.com/egil) and [@linkdotnet](https://github.com/linkdotnet).

## [1.26.64] - 2023-12-20

### Changed

- Upgraded AngleSharp to 1.0.7.
- Upgraded AngleSharp to 1.0.7. Info: The usage of `AngleSharpWrappers` is not needed anymore. Any usage of `Unwrap` should not be needed anymore.

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<DotNet6Version>6.0.*</DotNet6Version>
<DotNet7Version>7.0.*</DotNet7Version>
<DotNet8Version>8.0.*</DotNet8Version>
<DotNet9Version>9.0.0-*</DotNet9Version>
</PropertyGroup>

<!-- Solution wide properties -->
Expand Down Expand Up @@ -51,7 +52,7 @@
<!-- Shared code analyzers used for all projects in the solution -->
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.20.0.85982" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Label="Implicit usings"
Expand Down
4 changes: 2 additions & 2 deletions benchmark/bunit.benchmarks/bunit.benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,4 +17,4 @@
<ProjectReference Include="..\bunit.benchmarks.assets\bunit.benchmarks.assets.csproj" />
</ItemGroup>

</Project>
</Project>
4 changes: 2 additions & 2 deletions bunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.testassets", "tests\b
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".workflows", ".workflows", "{3B2F3419-5336-4147-A212-E19091195203}"
ProjectSection(SolutionItems) = preProject
.github\workflows\ci.yml = .github\workflows\ci.yml
.github\workflows\docs-deploy.yml = .github\workflows\docs-deploy.yml
.github\workflows\prepare-release.yml = .github\workflows\prepare-release.yml
.github\workflows\release-preview.yml = .github\workflows\release-preview.yml
.github\workflows\release.yml = .github\workflows\release.yml
.github\workflows\verification.yml = .github\workflows\verification.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{F6084D31-2A92-4794-A47E-A8F2254E6970}"
Expand All @@ -72,7 +72,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.generators.internal",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.generators.tests", "tests\bunit.generators.tests\bunit.generators.tests.csproj", "{09046981-D9EC-4295-8502-721AC54E1F12}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bunit.generators", "src\bunit.generators\bunit.generators.csproj", "{A7C6A2AA-FF8F-4ED1-8590-5324FC566059}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.generators", "src\bunit.generators\bunit.generators.csproj", "{A7C6A2AA-FF8F-4ED1-8590-5324FC566059}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
8 changes: 8 additions & 0 deletions docs/site/docs/extensions/bunit-generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ internal partial class ThirdPartyStub { }
```

Current limitations of this approach is that he stubbed type is not allowed to be nested inside the test class.

## Reported Diagnostics
The generators will report a range of diagnostics to help understanding what issue is present. The following table shows the diagnostics that are reported.

| Diagnostic ID | Error | Severity | Description |
| ------------- | ----------------------------------------------- | -------- | ---------------------------------------------------------------------------- |
| BUNIT0001 | Stubbing nested classes ({0}) is not supported. | Warning | Types annotated with `ComponentStub` can not be nested inside another class. |
| BUNIT0002 | Class ({0}) is not partial. | Warning | Types annotated with `ComponentStub` must be partial. |
34 changes: 34 additions & 0 deletions docs/site/docs/providing-input/passing-parameters-to-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,40 @@ When rendering a `RenderFragment` using the <xref:Bunit.TestContext.Render(Micro
}
```

## Passing query parameters (`SupplyParameterFromQuery`) to a component
In .NET 6 and later, components can receive parameters from a query string if the parameter is annotated with the `[SupplyParameterFromQuery]` attribute in addition to the `[Parameter]` attribute.

In .NET 8 however, the `[Parameter]` attribute is no longer required, which means a value cannot be passed to the component during testing using the normal methods, e.g. the <xref:Bunit.ComponentParameterCollectionBuilder`1>'s `Add` method, if a component parameter is only annotated with the `[SupplyParameterFromQuery]` attribute. Instead, pass a query string parameters by setting it using the <xref:Bunit.TestDoubles.FakeNavigationManager>.

For example:

```razor
@code {
[SupplyParameterFromQuery]
public string Name { get; set; }
}
```

A simple example of how to test a component that receives parameters from the query string:

```razor
@inherits TestContext
@code {
[Fact]
public void Component_receives_parameters_from_query_string()
{
var navigationManager = Services.GetRequiredService<NavigationManager>();
var uri = navigationManager.GetUriWithQueryParameter("Name", "bUnit");
navigationManager.NavigateTo(uri);
var cut = RenderComponent<SupplyFromQueryParameterComponent>();
cut.Instance.Name.ShouldBe("bUnit");
}
}
```

## Further Reading

- <xref:inject-services>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"rollForward": "latestMajor",
"allowPrerelease": false
"allowPrerelease": true
}
}
54 changes: 52 additions & 2 deletions src/bunit.core/ComponentParameterCollectionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Bunit.Extensions;
using Bunit.Rendering;

namespace Bunit;

Expand Down Expand Up @@ -48,7 +50,11 @@ public ComponentParameterCollectionBuilder(Action<ComponentParameterCollectionBu
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, TValue>> parameterSelector, [AllowNull] TValue value)
{
#if !NET8_0_OR_GREATER
var (name, cascadingValueName, isCascading) = GetParameterInfo(parameterSelector);
#else
var (name, cascadingValueName, isCascading) = GetParameterInfo(parameterSelector, value);
#endif
return isCascading
? AddCascadingValueParameter(cascadingValueName, value)
: AddParameter<TValue>(name, value);
Expand Down Expand Up @@ -338,7 +344,11 @@ public ComponentParameterCollectionBuilder<TComponent> Bind<TValue>(
Action<TValue> changedAction,
Expression<Func<TValue>>? valueExpression = null)
{
#if !NET8_0_OR_GREATER
var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector);
#else
var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector, initialValue);
#endif

if (isCascading)
throw new ArgumentException("Using Bind with a cascading parameter is not allowed.", parameterName);
Expand Down Expand Up @@ -393,7 +403,7 @@ static string TrimEnd(string source, string value)
/// or a <see cref="CascadingParameterAttribute"/>.
/// </summary>
/// <remarks>
/// This is an untyped version of the <see cref="Add{TValue}(Expression{Func{TComponent, TValue}}, TValue)"/> method. Always
/// This is an untyped version of this method named <see cref="AddUnmatched"/>. Always
/// prefer the strongly typed <c>Add</c> methods whenever possible.
/// </remarks>
/// <typeparam name="TValue">Value type.</typeparam>
Expand Down Expand Up @@ -426,7 +436,12 @@ public bool TryAdd<TValue>(string name, [AllowNull] TValue value)
/// <returns>The created <see cref="ComponentParameterCollection"/>.</returns>
public ComponentParameterCollection Build() => parameters;

private static (string Name, string? CascadingValueName, bool IsCascading) GetParameterInfo<TValue>(Expression<Func<TComponent, TValue>> parameterSelector)
private static (string Name, string? CascadingValueName, bool IsCascading) GetParameterInfo<TValue>(
Expression<Func<TComponent, TValue>> parameterSelector
#if NET8_0_OR_GREATER
, object? value
#endif
)
{
if (parameterSelector is null)
throw new ArgumentNullException(nameof(parameterSelector));
Expand All @@ -439,12 +454,47 @@ private static (string Name, string? CascadingValueName, bool IsCascading) GetPa
: propInfoCandidate;

var paramAttr = propertyInfo?.GetCustomAttribute<ParameterAttribute>(inherit: true);
#if !NET8_0_OR_GREATER
var cascadingParamAttr = propertyInfo?.GetCustomAttribute<CascadingParameterAttribute>(inherit: true);

if (propertyInfo is null || (paramAttr is null && cascadingParamAttr is null))
throw new ArgumentException($"The parameter selector '{parameterSelector}' does not resolve to a public property on the component '{typeof(TComponent)}' with a [Parameter] or [CascadingParameter] attribute.", nameof(parameterSelector));

return (propertyInfo.Name, CascadingValueName: cascadingParamAttr?.Name, IsCascading: cascadingParamAttr is not null);
#else
var cascadingParamAttrBase = propertyInfo?.GetCustomAttribute<CascadingParameterAttributeBase>(inherit: true);

if (propertyInfo is null || (paramAttr is null && cascadingParamAttrBase is null))
throw new ArgumentException($"The parameter selector '{parameterSelector}' does not resolve to a public property on the component '{typeof(TComponent)}' with a [Parameter] or [CascadingParameter]attribute.", nameof(parameterSelector));

if (cascadingParamAttrBase is null)
return (propertyInfo.Name, CascadingValueName: null, IsCascading: false);

var name = cascadingParamAttrBase switch
{
CascadingParameterAttribute cpa => cpa.Name,
SupplyParameterFromQueryAttribute s => throw CreateErrorMessageForSupplyFromQuery(value, propertyInfo, s.Name),
_ => throw new NotSupportedException($"The type '{cascadingParamAttrBase.GetType()}' is not supported"),
};

return (propertyInfo.Name, CascadingValueName: name, IsCascading: true);

static ArgumentException CreateErrorMessageForSupplyFromQuery(
object? value,
MemberInfo propertyInfo,
string? name)
{
var cascadingParameterName = name ?? propertyInfo.Name;

return new ArgumentException($"""
To pass a value to a SupplyParameterFromQuery parameter, use the NavigationManager and navigate to the URI.
For example:
var uri = NavigationManager.GetUriWithQueryParameter("{cascadingParameterName}", "{value}");
NavigationManager.NavigateTo(uri);
""");
}
#endif
}

private static bool HasChildContentParameter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void SetParametersAndRender<TComponent>(this IRenderedComponentBas
if (renderedComponent is null)
throw new ArgumentNullException(nameof(renderedComponent));

var renderer = renderedComponent.Services.GetRequiredService<TestRenderer>();
var renderer = (TestRenderer)renderedComponent.Services.GetRequiredService<TestContextBase>().Renderer;

try
{
Expand Down
Loading

0 comments on commit a3d2520

Please sign in to comment.