Skip to content

Commit ff81b1f

Browse files
committed
Tweaks to tests after reordering
1 parent c8d9b1b commit ff81b1f

22 files changed

Lines changed: 1054 additions & 687 deletions

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ dotnet_sort_system_directives_first = true
1313
[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}]
1414
indent_size = 2
1515

16+
[*.{htm,html,css,scss}]
17+
indent_size = 2
18+
1619
[*.json]
1720
indent_size = 2
1821

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using Microsoft.AspNetCore.Components;
44

@@ -9,8 +9,8 @@ namespace Bunit
99
/// </summary>
1010
public abstract class FragmentBase : IComponent
1111
{
12-
internal static void NoopTestMethod() { }
13-
internal static Task NoopTestMethodAsync() => Task.CompletedTask;
12+
protected static void NoopTestMethod() { }
13+
protected static Task NoopTestMethodAsync() => Task.CompletedTask;
1414

1515
/// <summary>
1616
/// Gets or sets the child content of the fragment.

src/bunit.core/RazorTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components;
7+
8+
namespace Bunit
9+
{
10+
/// <summary>
11+
/// Represents a component used to define tests in Razor files.
12+
/// </summary>
13+
public abstract class RazorTest : FragmentBase
14+
{
15+
/// <summary>
16+
/// A description or name for the test that will be displayed if the test fails.
17+
/// </summary>
18+
[Parameter] public string? Description { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets a reason for skipping the test. If not set (null), the test will not be skipped.
22+
/// </summary>
23+
[Parameter] public string? Skip { get; set; }
24+
25+
public abstract Task RunTest();
26+
27+
/// <inheritdoc/>
28+
public override string ToString() => $"{Description ?? "[no description]"}";
29+
}
30+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.ExceptionServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Components;
8+
using Microsoft.AspNetCore.Components.RenderTree;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Bunit
12+
{
13+
/// <summary>
14+
/// Represents a renderer specifically for rendering Razor-based test files (but not the actual tests inside).
15+
/// </summary>
16+
public class RazorTestRenderer : Renderer
17+
{
18+
private Exception? _unhandledException;
19+
20+
/// <inheritdoc/>
21+
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
22+
23+
/// <summary>
24+
/// Creates an instance of the <see cref="RazorTestRenderer"/>.
25+
/// </summary>
26+
public RazorTestRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory) : base(serviceProvider, loggerFactory) { }
27+
28+
/// <summary>
29+
/// Renders an instance of the specified Razor-based test.
30+
/// </summary>
31+
/// <param name="componentType">Razor-based test to render.</param>
32+
/// <returns>A list of <see cref="FragmentBase"/> test definitions found in the test file.</returns>
33+
public async Task<IReadOnlyList<RazorTest>> GetRazorTestsFromComponent(Type componentType)
34+
{
35+
var componentId = await Dispatcher.InvokeAsync(() => RenderComponent(componentType)).ConfigureAwait(false);
36+
AssertNoUnhandledExceptions();
37+
return GetRazorTests(componentId);
38+
}
39+
40+
private async Task<int> RenderComponent(Type componentType)
41+
{
42+
var component = InstantiateComponent(componentType);
43+
var componentId = AssignRootComponentId(component);
44+
await RenderRootComponentAsync(componentId).ConfigureAwait(false);
45+
return componentId;
46+
}
47+
48+
private IReadOnlyList<RazorTest> GetRazorTests(int fromComponentId)
49+
{
50+
var result = new List<RazorTest>();
51+
var ownFrames = GetCurrentRenderTreeFrames(fromComponentId);
52+
for (int i = 0; i < ownFrames.Count; i++)
53+
{
54+
ref var frame = ref ownFrames.Array[i];
55+
if (frame.FrameType == RenderTreeFrameType.Component)
56+
{
57+
if (frame.Component is RazorTest component)
58+
{
59+
result.Add(component);
60+
}
61+
}
62+
}
63+
return result;
64+
}
65+
66+
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) => Task.CompletedTask;
67+
68+
protected override void HandleException(Exception exception) => _unhandledException = exception;
69+
70+
private void AssertNoUnhandledExceptions()
71+
{
72+
if (_unhandledException is { } unhandled)
73+
{
74+
_unhandledException = null;
75+
ExceptionDispatchInfo.Capture(unhandled).Throw();
76+
}
77+
}
78+
}
79+
}

src/bunit.web.tests/Components/TestComponentBaseTest/BlazorElementReferencesIncludedInRenderedMarkup.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@inherits TestComponentBase<BlazorElementReferencesIncludedInRenderedMarkup>
1+
@inherits TestComponentBase
22

33
<Fixture Test="Test">
44
<Fragment>

src/bunit.web.tests/Components/TestComponentBaseTest/CorrectImplicitRazorTestContextAvailable.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@inherits TestComponentBase
1+
@inherits TestComponentBase
22

33
<Fixture Setup="Setup1" Test="Test1" Tests="new Action[] { Test1_1 }">
44
<ComponentUnderTest>
@@ -102,4 +102,4 @@
102102

103103
@code{
104104
class Dep3 : ITestDep { public string Name => nameof(Dep3); }
105-
}
105+
}
Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
@inherits TestComponentBase
1+
@inherits TestComponentBase
22

3-
<Fixture Setup="Setup"
4-
Test="Test1"
5-
TestAsync="TestAsync1"
6-
Tests=@(new Action[] { Test2, Test3 })
7-
TestsAsync=@(new Func<Task>[] { TestAsync2, TestAsync3 })>
8-
<ComponentUnderTest><div /></ComponentUnderTest>
3+
<Fixture Setup="Setup"
4+
Test="Test1"
5+
TestAsync="TestAsync1"
6+
Tests=@(new Action[] { Test2, Test3 })
7+
TestsAsync=@(new Func<Task>[] { TestAsync2, TestAsync3 })>
8+
<ComponentUnderTest><div /></ComponentUnderTest>
99
</Fixture>
1010
@code{
11-
List<string> callOrder = new List<string>();
12-
IRazorTestContext? seenContext;
11+
List<string> callOrder = new List<string>();
12+
IRazorTestContext? seenContext;
1313

14-
void Setup()
15-
{
16-
seenContext = this;
17-
callOrder.Add(nameof(Setup));
18-
callOrder.Count.ShouldBe(1);
19-
callOrder[0].ShouldBe(nameof(Setup));
20-
}
14+
void Setup()
15+
{
16+
seenContext = this;
17+
callOrder.Add(nameof(Setup));
18+
callOrder.Count.ShouldBe(1);
19+
callOrder[0].ShouldBe(nameof(Setup));
20+
}
2121

22-
void Test1()
23-
{
24-
callOrder.Add(nameof(Test1));
25-
callOrder.Count.ShouldBe(2);
26-
callOrder[0].ShouldBe(nameof(Setup));
27-
callOrder[1].ShouldBe(nameof(Test1));
28-
this.ShouldBe(seenContext);
29-
}
22+
void Test1()
23+
{
24+
callOrder.Add(nameof(Test1));
25+
callOrder.Count.ShouldBe(2);
26+
callOrder[0].ShouldBe(nameof(Setup));
27+
callOrder[1].ShouldBe(nameof(Test1));
28+
this.ShouldBe(seenContext);
29+
}
3030

31-
Task TestAsync1()
32-
{
33-
callOrder.Add(nameof(TestAsync1));
34-
callOrder.Count.ShouldBe(3);
35-
callOrder[0].ShouldBe(nameof(Setup));
36-
callOrder[1].ShouldBe(nameof(Test1));
37-
callOrder[2].ShouldBe(nameof(TestAsync1));
38-
this.ShouldBe(seenContext);
39-
return Task.CompletedTask;
40-
}
31+
Task TestAsync1()
32+
{
33+
callOrder.Add(nameof(TestAsync1));
34+
callOrder.Count.ShouldBe(3);
35+
callOrder[0].ShouldBe(nameof(Setup));
36+
callOrder[1].ShouldBe(nameof(Test1));
37+
callOrder[2].ShouldBe(nameof(TestAsync1));
38+
this.ShouldBe(seenContext);
39+
return Task.CompletedTask;
40+
}
4141

42-
void Test2()
43-
{
44-
callOrder.Add(nameof(Test2));
45-
callOrder.Count.ShouldBe(4);
46-
callOrder[0].ShouldBe(nameof(Setup));
47-
callOrder[1].ShouldBe(nameof(Test1));
48-
callOrder[2].ShouldBe(nameof(TestAsync1));
49-
callOrder[3].ShouldBe(nameof(Test2));
50-
this.ShouldBe(seenContext);
51-
}
42+
void Test2()
43+
{
44+
callOrder.Add(nameof(Test2));
45+
callOrder.Count.ShouldBe(4);
46+
callOrder[0].ShouldBe(nameof(Setup));
47+
callOrder[1].ShouldBe(nameof(Test1));
48+
callOrder[2].ShouldBe(nameof(TestAsync1));
49+
callOrder[3].ShouldBe(nameof(Test2));
50+
this.ShouldBe(seenContext);
51+
}
5252

53-
void Test3()
54-
{
55-
callOrder.Add(nameof(Test3));
56-
callOrder.Count.ShouldBe(5);
57-
callOrder[0].ShouldBe(nameof(Setup));
58-
callOrder[1].ShouldBe(nameof(Test1));
59-
callOrder[2].ShouldBe(nameof(TestAsync1));
60-
callOrder[3].ShouldBe(nameof(Test2));
61-
callOrder[4].ShouldBe(nameof(Test3));
53+
void Test3()
54+
{
55+
callOrder.Add(nameof(Test3));
56+
callOrder.Count.ShouldBe(5);
57+
callOrder[0].ShouldBe(nameof(Setup));
58+
callOrder[1].ShouldBe(nameof(Test1));
59+
callOrder[2].ShouldBe(nameof(TestAsync1));
60+
callOrder[3].ShouldBe(nameof(Test2));
61+
callOrder[4].ShouldBe(nameof(Test3));
6262

63-
this.ShouldBe(seenContext);
64-
}
63+
this.ShouldBe(seenContext);
64+
}
6565

66-
Task TestAsync2()
67-
{
68-
callOrder.Add(nameof(TestAsync2));
69-
callOrder.Count.ShouldBe(6);
70-
callOrder[0].ShouldBe(nameof(Setup));
71-
callOrder[1].ShouldBe(nameof(Test1));
72-
callOrder[2].ShouldBe(nameof(TestAsync1));
73-
callOrder[3].ShouldBe(nameof(Test2));
74-
callOrder[4].ShouldBe(nameof(Test3));
75-
callOrder[5].ShouldBe(nameof(TestAsync2));
76-
this.ShouldBe(seenContext);
77-
return Task.CompletedTask;
78-
}
66+
Task TestAsync2()
67+
{
68+
callOrder.Add(nameof(TestAsync2));
69+
callOrder.Count.ShouldBe(6);
70+
callOrder[0].ShouldBe(nameof(Setup));
71+
callOrder[1].ShouldBe(nameof(Test1));
72+
callOrder[2].ShouldBe(nameof(TestAsync1));
73+
callOrder[3].ShouldBe(nameof(Test2));
74+
callOrder[4].ShouldBe(nameof(Test3));
75+
callOrder[5].ShouldBe(nameof(TestAsync2));
76+
this.ShouldBe(seenContext);
77+
return Task.CompletedTask;
78+
}
7979

80-
Task TestAsync3()
81-
{
82-
callOrder.Add(nameof(TestAsync3));
83-
callOrder.Count.ShouldBe(7);
84-
callOrder[0].ShouldBe(nameof(Setup));
85-
callOrder[1].ShouldBe(nameof(Test1));
86-
callOrder[2].ShouldBe(nameof(TestAsync1));
87-
callOrder[3].ShouldBe(nameof(Test2));
88-
callOrder[4].ShouldBe(nameof(Test3));
89-
callOrder[5].ShouldBe(nameof(TestAsync2));
90-
callOrder[6].ShouldBe(nameof(TestAsync3));
91-
this.ShouldBe(seenContext);
92-
return Task.CompletedTask;
93-
}
94-
}
80+
Task TestAsync3()
81+
{
82+
callOrder.Add(nameof(TestAsync3));
83+
callOrder.Count.ShouldBe(7);
84+
callOrder[0].ShouldBe(nameof(Setup));
85+
callOrder[1].ShouldBe(nameof(Test1));
86+
callOrder[2].ShouldBe(nameof(TestAsync1));
87+
callOrder[3].ShouldBe(nameof(Test2));
88+
callOrder[4].ShouldBe(nameof(Test3));
89+
callOrder[5].ShouldBe(nameof(TestAsync2));
90+
callOrder[6].ShouldBe(nameof(TestAsync3));
91+
this.ShouldBe(seenContext);
92+
return Task.CompletedTask;
93+
}
94+
}

0 commit comments

Comments
 (0)