Skip to content

Commit 71ff116

Browse files
committed
When an Add call to the component parameter collection builder was used to select a parameter that was inherited from a base component, the builder incorrectly reported the selected property/parameter as missing on the type
1 parent d551d48 commit 71ff116

4 files changed

Lines changed: 48 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ All notable changes to **bUnit** will be documented in this file. The project ad
44

55
<!-- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) -->
66

7-
## UNRELEASED [1.0.0-preview 1]
7+
## UNRELEASED [1.0.0 preview 01]
88

9-
The following section list all changes in 1.0.0 preview 1.
9+
The following section list all changes in 1.0.0 preview 01.
1010

1111
### Added
1212
List of new features.
@@ -22,6 +22,8 @@ List of now removed features.
2222
### Fixed
2323
List of any bug fixes.
2424

25+
- When an `Add` call to the component parameter collection builder was used to select a parameter that was inherited from a base component, the builder incorrectly reported the selected property/parameter as missing on the type. Reported by [@nickmuller](https://github.com/nickmuller) in [#250](https://github.com/egil/bUnit/issues/250).
26+
2527
## [1.0.0-beta 10] - 2020-09-15
2628

2729
The following section list all changes in beta-10.

src/bunit.core/ComponentParameterCollectionBuilder.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,17 @@ private static (string paramName, string? cascadingValueName, bool isCascading)
345345
{
346346
if (parameterSelector is null) throw new ArgumentNullException(nameof(parameterSelector));
347347

348-
if (!(parameterSelector.Body is MemberExpression memberExpression) || !(memberExpression.Member is PropertyInfo propertyInfo))
348+
if (!(parameterSelector.Body is MemberExpression memberExpression) || !(memberExpression.Member is PropertyInfo propInfoCandidate))
349349
throw new ArgumentException($"The parameter selector '{parameterSelector}' does not resolve to a public property on the component '{typeof(TComponent)}'.");
350350

351-
var paramAttr = propertyInfo.GetCustomAttribute<ParameterAttribute>(inherit: false);
352-
var cascadingParamAttr = propertyInfo.GetCustomAttribute<CascadingParameterAttribute>(inherit: false);
351+
var propertyInfo = propInfoCandidate.DeclaringType != TComponentType
352+
? TComponentType.GetProperty(propInfoCandidate.Name, propInfoCandidate.PropertyType)
353+
: propInfoCandidate;
354+
355+
var paramAttr = propertyInfo?.GetCustomAttribute<ParameterAttribute>(inherit: true);
356+
var cascadingParamAttr = propertyInfo?.GetCustomAttribute<CascadingParameterAttribute>(inherit: true);
353357

354-
if (paramAttr is null && cascadingParamAttr is null)
358+
if (propertyInfo is null || paramAttr is null && cascadingParamAttr is null)
355359
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.");
356360

357361
return (propertyInfo.Name, cascadingParamAttr?.Name, cascadingParamAttr is not null);

tests/bunit.core.tests/ComponentParameterCollectionBuilderTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,30 @@ public void Test203()
544544
.ShouldBeParameter(name + "NAME", input, isCascadingValue: true);
545545
}
546546

547+
[Fact(DisplayName = "Add of inherited overriden parameter works")]
548+
public void Test300()
549+
{
550+
var sut = new ComponentParameterCollectionBuilder<InheritedParamsWithOverride>();
551+
552+
sut.Add(x => x.Value, true);
553+
554+
sut.Build()
555+
.ShouldHaveSingleItem()
556+
.ShouldBeParameter("Value", true, isCascadingValue: false);
557+
}
558+
559+
[Fact(DisplayName = "Add of inherited parameter works")]
560+
public void Test301()
561+
{
562+
var sut = new ComponentParameterCollectionBuilder<InheritedParamsWithoutOverride>();
563+
564+
sut.Add(x => x.Value, true);
565+
566+
sut.Build()
567+
.ShouldHaveSingleItem()
568+
.ShouldBeParameter("Value", true, isCascadingValue: false);
569+
}
570+
547571
private class Params : ComponentBase
548572
{
549573
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? Attributes { get; set; }
@@ -565,13 +589,22 @@ private class Params : ComponentBase
565589
[CascadingParameter] public RenderFragment? RFCC { get; set; }
566590
public int _nonParam = -1;
567591
public object? NonParamProp { get; set; }
568-
569-
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
570592
public void SomeMethod() { }
571593
}
572594

573595
private class NoParams : ComponentBase { }
574596
private class NonChildContentParameter : ComponentBase { public RenderFragment? ChildContent { get; set; } }
575597
private class InhertedParams : Params { }
598+
private abstract class ParamsBase<T> : ComponentBase
599+
{
600+
public abstract T Value { get; set; }
601+
}
602+
private class InheritedParamsWithOverride : ParamsBase<bool?>
603+
{
604+
[Parameter] public override bool? Value { get; set; }
605+
}
606+
private class InheritedParamsWithoutOverride : InheritedParamsWithOverride
607+
{
608+
}
576609
}
577610
}

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.0.0-beta-12",
3+
"version": "1.0.0-preview-01",
44
"publicReleaseRefSpec": [
55
"^refs/heads/main$",
66
"^refs/heads/v\\d+(?:\\.\\d+)?$"

0 commit comments

Comments
 (0)