Skip to content

Commit 7aba4c7

Browse files
egilactions-user
andcommitted
Improvements to parameter passing in c# based tests (#203)
* componentparametercollection * Support for passing multiple template and render fragments * Automated dotnet-format update * cascading values * Finished component parameter collection, closed #142, updated tests to component parameter factory * Fixed null warnings * Automated dotnet-format update * Fixes for CodeQL warnings * Added ChildContent and RenderFragment tests * Added support for passing template fragments * Removed ComponentParameterBuilder, added support for unmatched and cascading values * Switched to C# 9 compile * Automated dotnet-format update * unnamed cascading value with add * Removed .net move hack from workflows * Fix for null errors * Automated dotnet-format update * Added extra factory method and moved ComponentParameter out into Bunit namespace * Updated docs with new parameter passing logic * Automated dotnet-format update * Updated changelog Co-authored-by: Github Actions <actions@github.com>
1 parent 192ce6f commit 7aba4c7

57 files changed

Lines changed: 2099 additions & 1173 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/deploy-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232

3333
- uses: actions/setup-dotnet@v1
3434
with:
35-
dotnet-version: '3.1.x'
35+
dotnet-version: '3.1.x'
3636
- uses: actions/setup-dotnet@v1
3737
with:
3838
dotnet-version: '5.0.100-rc.1.20452.10'

.github/workflows/main.yml

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ jobs:
4040
- uses: actions/setup-dotnet@v1
4141
with:
4242
dotnet-version: '5.0.100-rc.1.20452.10'
43-
- name: Move .net SDK's to shared folder (hack)
44-
shell: pwsh
45-
run: |
46-
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
47-
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
48-
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
49-
foreach ($dir in $directories) {
50-
$from = $dir.FullName;
51-
$to = "$root/$version";
52-
Write-Host Copying from $from to $to;
53-
Copy-Item "$from\*" $to -Recurse -Force;
54-
}
5543

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

8763
- name: Running unit tests
8864
run: |
@@ -124,18 +100,6 @@ jobs:
124100
- uses: actions/setup-dotnet@v1
125101
with:
126102
dotnet-version: '5.0.100-rc.1.20452.10'
127-
- name: Move .net SDK's to shared folder (hack)
128-
shell: pwsh
129-
run: |
130-
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
131-
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
132-
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
133-
foreach ($dir in $directories) {
134-
$from = $dir.FullName;
135-
$to = "$root/$version";
136-
Write-Host Copying from $from to $to;
137-
Copy-Item "$from\*" $to -Recurse -Force;
138-
}
139103

140104
- name: Creating library package
141105
run: |
@@ -169,18 +133,6 @@ jobs:
169133
- uses: actions/setup-dotnet@v1
170134
with:
171135
dotnet-version: '5.0.100-rc.1.20452.10'
172-
- name: Move .net SDK's to shared folder (hack)
173-
shell: pwsh
174-
run: |
175-
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
176-
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
177-
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
178-
foreach ($dir in $directories) {
179-
$from = $dir.FullName;
180-
$to = "$root/$version";
181-
Write-Host Copying from $from to $to;
182-
Copy-Item "$from\*" $to -Recurse -Force;
183-
}
184136

185137
- name: Install dotnet-format
186138
run: dotnet tool install -g dotnet-format
@@ -238,18 +190,6 @@ jobs:
238190
- uses: actions/setup-dotnet@v1
239191
with:
240192
dotnet-version: '5.0.100-rc.1.20452.10'
241-
- name: Move .net SDK's to shared folder (hack)
242-
shell: pwsh
243-
run: |
244-
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
245-
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
246-
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
247-
foreach ($dir in $directories) {
248-
$from = $dir.FullName;
249-
$to = "$root/$version";
250-
Write-Host Copying from $from to $to;
251-
Copy-Item "$from\*" $to -Recurse -Force;
252-
}
253193

254194
- name: Initialize CodeQL
255195
uses: github/codeql-action/init@v1
@@ -296,18 +236,6 @@ jobs:
296236
- uses: actions/setup-dotnet@v1
297237
with:
298238
dotnet-version: '5.0.100-rc.1.20452.10'
299-
- name: DOTNET HACK
300-
shell: pwsh
301-
run: |
302-
$version = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Leaf;
303-
$root = Split-Path (Split-Path $ENV:DOTNET_ROOT -Parent) -Parent;
304-
$directories = Get-ChildItem $root | Where-Object { $_.Name -ne $version };
305-
foreach ($dir in $directories) {
306-
$from = $dir.FullName;
307-
$to = "$root/$version";
308-
Write-Host Copying from $from to $to;
309-
Copy-Item "$from\*" $to -Recurse -Force;
310-
}
311239

312240
- name: Creating library package for pre-release
313241
if: github.event_name != 'release'

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,31 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99
### Added
1010
List of new features.
1111

12+
- 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).
13+
- 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).
14+
1215
### Changed
1316
List of changes in existing functionality.
1417

18+
- 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).
19+
- `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).
20+
- 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).
21+
- 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>`.
22+
23+
For example, this makes it easier to pass e.g. both a markup string and a component to a `ChildContent` parameter:
24+
25+
```csharp
26+
var cut = ctx.RenderComponent<Component>(parameters => parameters
27+
.AddChildContent("<h1>Below you will find a most interesting alert!</h1>")
28+
.AddChildContent<Alert>(childParams => childParams
29+
.Add(p => p.Heading, "Alert heading")
30+
.Add(p => p.Type, AlertType.Warning)
31+
.AddChildContent("<p>Hello World</p>")
32+
)
33+
);
34+
```
35+
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
36+
1537
### Deprecated
1638
List of soon-to-be removed features.
1739

@@ -21,6 +43,8 @@ List of now removed features.
2143
### Fixed
2244
List of any bug fixes.
2345

46+
- 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.
47+
2448
### Security
2549
List of fixed security vulnerabilities.
2650

docs/samples/tests/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
44
<IsPackable>false</IsPackable>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>9.0</LangVersion>
66
<RazorLangVersion>3.0</RazorLangVersion>
77
</PropertyGroup>
88
</Project>

docs/samples/tests/mstest/BunitTestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Dispose()
2929
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
3030
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
3131

32-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
32+
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
3333
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("MSTest has not started executing tests yet");
3434
}
3535
}

docs/samples/tests/mstest/bunit.docs.mstest.samples.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference Include="../../../../src/bunit.core/bunit.core.csproj" />
1615
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
1716
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
1817
</ItemGroup>

docs/samples/tests/nunit/BunitTestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Dispose()
2929
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent
3030
=> _context?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
3131

32-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
32+
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) where TComponent : IComponent
3333
=> _context?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("NUnit has not started executing tests yet");
3434
}
3535
}

docs/samples/tests/nunit/bunit.docs.nunit.samples.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<ProjectReference Include="../../../../src/bunit.core/bunit.core.csproj" />
1514
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
1615
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
1716
</ItemGroup>

docs/samples/tests/razor/AllKindsOfParamsTest.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@
109109
<ComponentUnderTest>
110110
<TemplateParams Items=@(new string[]{ "Foo", "Bar", "Baz" }) TItem="string">
111111
<Template>
112-
<div class="item">
113112
<Item Value=@context></Item>
114-
</div>
115113
</Template>
116114
</TemplateParams>
117115
</ComponentUnderTest>
118116
</Fixture>
119117

118+
119+
120120
<Fixture Test="f => {}">
121121
<ComponentUnderTest>
122122
<UnmatchedParams some-unknown-param="a value" />

docs/samples/tests/razor/bunit.docs.razor.samples.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="bunit.core" Version="1.0.0-beta-10" />
10-
<PackageReference Include="bunit.web" Version="1.0.0-beta-10" />
11-
<PackageReference Include="bunit.xunit" Version="1.0.0-beta-10" />
129
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
1310
<PackageReference Include="xunit" Version="2.4.1" />
1411
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
@@ -18,6 +15,8 @@
1815
</ItemGroup>
1916

2017
<ItemGroup>
18+
<ProjectReference Include="../../../../src/bunit.web/bunit.web.csproj" />
19+
<ProjectReference Include="../../../../src/bunit.xunit/bunit.xunit.csproj" />
2120
<ProjectReference Include="../../components/bunit.docs.samples.csproj" />
2221
</ItemGroup>
2322

0 commit comments

Comments
 (0)