Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to parameter passing #203

Merged
merged 21 commits into from
Sep 27, 2020
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
14 changes: 1 addition & 13 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,10 @@ jobs:

- uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.x'
dotnet-version: '3.1.x'
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: DOTNET HACK
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Building library
run: dotnet build /p:PublicRelease=true
Expand Down
72 changes: 0 additions & 72 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: Move .net SDK's to shared folder (hack)
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Building library in release mode
run: dotnet build -c Release -p:ContinuousIntegrationBuild=true
Expand All @@ -71,18 +59,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: Move .net SDK's to shared folder (hack)
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Running unit tests
run: |
Expand Down Expand Up @@ -124,18 +100,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: Move .net SDK's to shared folder (hack)
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Creating library package
run: |
Expand Down Expand Up @@ -169,18 +133,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: Move .net SDK's to shared folder (hack)
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Install dotnet-format
run: dotnet tool install -g dotnet-format
Expand Down Expand Up @@ -238,18 +190,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: Move .net SDK's to shared folder (hack)
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Initialize CodeQL
uses: github/codeql-action/init@v1
Expand Down Expand Up @@ -296,18 +236,6 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.100-rc.1.20452.10'
- name: DOTNET HACK
shell: pwsh
run: |
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
foreach ($dir in $directories) {
$from = $dir.FullName;
$to = "$root/$version";
Write-Host Copying from $from to $to;
Copy-Item "$from\*" $to -Recurse -Force;
}

- name: Creating library package for pre-release
if: github.event_name != 'release'
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,31 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
List of new features.

- Two new overloads to the `RenderFragment()` and `ChildContent()` component parameter factory methods have been added that takes a `RenderFragment` as input. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
- Added a `ComponentParameterCollection` type. The `ComponentParameterCollection` is a collection of component parameters, that knows how to turn those components parameters into a `RenderFragment`, which will render a component and pass any parameters inside the collection to that component. That logic was spread out over multiple places in bUnit, and is now owned by the `ComponentParameterCollection` type. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).

### Changed
List of changes in existing functionality.

- The `ComponentParameterBuilder` has been renamed to `ComponentParameterCollectionBuilder`, since it now builds the `ComponentParameterCollection` type, introduced in this release of bUnit. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
- `ComponentParameterCollectionBuilder` now allows adding cascading values that is not directly used by the component type it targets. This makes it possible to add cascading values to children of the target component. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
- The `Add(object)` has been replaced by `AddCascadingValue(object)` in `ComponentParameterCollectionBuilder`, to make it more clear that an unnnamed cascading value is being passed to the target component or one of its child components. It it is also possible to pass unnamed cascading values using the `Add(parameterSelector, value)` method, which now correctly detect if the selected cascading value parameter is named or unnamed. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
- It is now possible to call the `Add()`, `AddChildContent()` methods on `ComponentParameterCollectionBuilder`, and the factory methods `RenderFragment()`, `ChildContent()`, and `Template()`, _**multiple times**_ for the same parameter, if it is of type `RenderFragment` or `RenderFragment<TValue>`. Doing so previously would either result in an exception or just the last passed `RenderFragment` to be used. Now all the provided `RenderFragment` or `RenderFragment<TValue>` will be combined at runtime into a single `RenderFragment` or `RenderFragment<TValue>`.

For example, this makes it easier to pass e.g. both a markup string and a component to a `ChildContent` parameter:

```csharp
var cut = ctx.RenderComponent<Component>(parameters => parameters
.AddChildContent("<h1>Below you will find a most interesting alert!</h1>")
.AddChildContent<Alert>(childParams => childParams
.Add(p => p.Heading, "Alert heading")
.Add(p => p.Type, AlertType.Warning)
.AddChildContent("<p>Hello World</p>")
)
);
```
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).

### Deprecated
List of soon-to-be removed features.

Expand All @@ -21,6 +43,8 @@ List of now removed features.
### Fixed
List of any bug fixes.

- Using the ComponentParameterCollectionBuilder's `Add(p => p.Param, value)` method to add a unnamed cascading value didn't create an unnnamed cascading value parameter. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203). Credits to [Ben Sampica (@benjaminsampica)](https://github.com/benjaminsampica) for reporting and helping investigate this issue.

### Security
List of fixed security vulnerabilities.

Expand Down
2 changes: 1 addition & 1 deletion docs/samples/tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion docs/samples/tests/mstest/BunitTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Dispose()
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");

public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
}
}
1 change: 0 additions & 1 deletion docs/samples/tests/mstest/bunit.docs.mstest.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../../src/bunit.core/bunit.core.csproj" />
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion docs/samples/tests/nunit/BunitTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Dispose()
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");

public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
}
}
1 change: 0 additions & 1 deletion docs/samples/tests/nunit/bunit.docs.nunit.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../../src/bunit.core/bunit.core.csproj" />
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions docs/samples/tests/razor/AllKindsOfParamsTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@
<ComponentUnderTest>
<TemplateParams Items=@(new string[]{ "Foo", "Bar", "Baz" }) TItem="string">
<Template>
<div class="item">
<Item Value=@context></Item>
</div>
</Template>
</TemplateParams>
</ComponentUnderTest>
</Fixture>



<Fixture Test="f => {}">
<ComponentUnderTest>
<UnmatchedParams some-unknown-param="a value" />
Expand Down
5 changes: 2 additions & 3 deletions docs/samples/tests/razor/bunit.docs.razor.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit.core" Version="1.0.0-beta-10" />
<PackageReference Include="bunit.web" Version="1.0.0-beta-10" />
<PackageReference Include="bunit.xunit" Version="1.0.0-beta-10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
Expand All @@ -18,6 +15,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
<ProjectReference Include="../../../../src/bunit.xunit/bunit.xunit.csproj" />
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
</ItemGroup>

Expand Down
81 changes: 42 additions & 39 deletions docs/samples/tests/xunit/AllKindsOfParamsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Bunit.Rendering;

using static Bunit.ComponentParameterFactory;

Expand Down Expand Up @@ -125,17 +126,24 @@ public void ComponentAndMarkupAsChildContent()
{
using var ctx = new TestContext();

// Using factory method
var cut1 = ctx.RenderComponent<ChildContentParams>(
ChildContent("<h1>Below you will find a most interesting alert!</h1>"),
ChildContent<Alert>(
("Heading", "Alert heading"),
("Type", AlertType.Warning),
ChildContent("<p>Hello World</p>")
)
);

// Using parameter builder
var cut = ctx.RenderComponent<ChildContentParams>(parameters => parameters
.Add(p => p.ChildContent, (RenderFragment)(builder =>
{
builder.AddMarkupContent(1, "<h1>Below you will find a most interesting alert!</h1>");
builder.OpenComponent<Alert>(2);
builder.AddAttribute(3, "Heading", "Alert heading");
builder.AddAttribute(4, "Type", AlertType.Warning);
builder.AddAttribute(5, "ChildContent", (RenderFragment)(alertBuilder => alertBuilder.AddMarkupContent(1, "<p>Hello World</p>")));
builder.CloseComponent();
}))
var cut2 = ctx.RenderComponent<ChildContentParams>(parameters => parameters
.AddChildContent("<h1>Below you will find a most interesting alert!</h1>")
.AddChildContent<Alert>(childParams => childParams
.Add(p => p.Heading, "Alert heading")
.Add(p => p.Type, AlertType.Warning)
.AddChildContent("<p>Hello World</p>")
)
);
}

Expand Down Expand Up @@ -200,17 +208,24 @@ public void ComponentAndMarkupAsRenderFragment()
{
using var ctx = new TestContext();

// Using factory method
var cut1 = ctx.RenderComponent<RenderFragmentParams>(
RenderFragment("Content", "<h1>Below you will find a most interesting alert!</h1>"),
RenderFragment<Alert>("Content",
("Heading", "Alert heading"),
("Type", AlertType.Warning),
ChildContent("<p>Hello World</p>")
)
);

// Using parameter builder
var cut = ctx.RenderComponent<RenderFragmentParams>(parameters => parameters
.Add(p => p.Content, (RenderFragment)(builder =>
{
builder.AddMarkupContent(1, "<h1>Below you will find a most interesting alert!</h1>");
builder.OpenComponent<Alert>(2);
builder.AddAttribute(3, "Heading", "Alert heading");
builder.AddAttribute(4, "Type", AlertType.Warning);
builder.AddAttribute(5, "ChildContent", (RenderFragment)(alertBuilder => alertBuilder.AddMarkupContent(1, "<p>Hello World</p>")));
builder.CloseComponent();
}))
var cut2 = ctx.RenderComponent<RenderFragmentParams>(parameters => parameters
.Add(p => p.Content, "<h1>Below you will find a most interesting alert!</h1>")
.Add<Alert>(p => p.Content, childParams => childParams
.Add(p => p.Heading, "Alert heading")
.Add(p => p.Type, AlertType.Warning)
.AddChildContent("<p>Hello World</p>")
)
);
}

Expand Down Expand Up @@ -240,29 +255,17 @@ public void HtmlAndComponentTemplateParams()
// Using factory method
var cut1 = ctx.RenderComponent<TemplateParams<string>>(
("Items", new string[] { "Foo", "Bar", "Baz" }),
Template<string>("Template", item => builder =>
{
builder.OpenElement(1, "div");
builder.AddAttribute(2, "class", "item");
builder.OpenComponent<Item>(3);
builder.AddAttribute(4, "Value", item);
builder.CloseComponent();
builder.CloseElement();
Template<Item, string>("Template", value => new ComponentParameter[] {
("Value", value)
})
);

// Using parameter builder
var cut2 = ctx.RenderComponent<TemplateParams<string>>(parameters => parameters
.Add(p => p.Items, new[] { "Foo", "Bar", "Baz" })
.Add(p => p.Template, item => builder =>
{
builder.OpenElement(1, "div");
builder.AddAttribute(2, "class", "item");
builder.OpenComponent<Item>(3);
builder.AddAttribute(4, "Value", item);
builder.CloseComponent();
builder.CloseElement();
})
.Add<Item, string>(p => p.Template, value => itemParams => itemParams
.Add(p => p.Value, value)
)
);
}

Expand Down Expand Up @@ -295,7 +298,7 @@ public void UnnamedCascadingParamsTest()

// Using parameter builder
var cut2 = ctx.RenderComponent<CascadingParams>(parameters => parameters
.Add(isDarkTheme)
.AddCascadingValue(isDarkTheme)
);

// Using parameter builder and selecting unnamed cascading parameter
Expand Down Expand Up @@ -335,7 +338,7 @@ public void UnnamedAndNamedCascadingParamsTest()

// Using parameter builder
var cut2 = ctx.RenderComponent<CascadingParams>(parameters => parameters
.Add(isDarkTheme)
.AddCascadingValue(isDarkTheme)
.Add(p => p.UserName, "Egil Hansen")
.Add(p => p.Email, "[email protected]")
);
Expand Down
1 change: 0 additions & 1 deletion docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../../src/bunit.core/bunit.core.csproj" />
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
<ProjectReference Include="../../../../src/bunit.xunit/bunit.xunit.csproj" />
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
Expand Down
Loading