Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

Commit 96e14b5

Browse files
committed
Add tests to cover generics and type inference
1 parent 3c8ce63 commit 96e14b5

File tree

49 files changed

+1960
-0
lines changed

Some content is hidden

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

49 files changed

+1960
-0
lines changed

src/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs

+292
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,156 @@ public class MyComponent : ComponentBase
18191819
CompileToAssembly(generated);
18201820
}
18211821

1822+
[Fact]
1823+
public void BindToGenericComponent_InferredType_WithGetSet_EventCallback()
1824+
{
1825+
// Arrange
1826+
AdditionalSyntaxTrees.Add(Parse(@"
1827+
using System;
1828+
using Microsoft.AspNetCore.Components;
1829+
1830+
namespace Test
1831+
{
1832+
public class MyComponent<TValue> : ComponentBase
1833+
{
1834+
[Parameter]
1835+
public TValue Value { get; set; }
1836+
1837+
[Parameter]
1838+
public EventCallback<TValue> ValueChanged { get; set; }
1839+
}
1840+
1841+
public class CustomValue
1842+
{
1843+
}
1844+
}"));
1845+
1846+
// Act
1847+
var generated = CompileToCSharp(@"
1848+
<MyComponent @bind-Value:get=""ParentValue"" @bind-Value:set=""UpdateValue"" />
1849+
@code {
1850+
public CustomValue ParentValue { get; set; } = new CustomValue();
1851+
1852+
public EventCallback<CustomValue> UpdateValue { get; set; }
1853+
}");
1854+
1855+
// Assert
1856+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1857+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1858+
CompileToAssembly(generated);
1859+
}
1860+
1861+
[Fact]
1862+
public void BindToGenericComponent_ExplicitType_WithGetSet_EventCallback()
1863+
{
1864+
// Arrange
1865+
AdditionalSyntaxTrees.Add(Parse(@"
1866+
using System;
1867+
using Microsoft.AspNetCore.Components;
1868+
1869+
namespace Test
1870+
{
1871+
public class MyComponent<TValue> : ComponentBase
1872+
{
1873+
[Parameter]
1874+
public TValue Value { get; set; }
1875+
1876+
[Parameter]
1877+
public EventCallback<TValue> ValueChanged { get; set; }
1878+
}
1879+
1880+
public class CustomValue
1881+
{
1882+
}
1883+
}"));
1884+
1885+
// Act
1886+
var generated = CompileToCSharp(@"
1887+
<MyComponent TValue=""CustomValue"" @bind-Value:get=""ParentValue"" @bind-Value:set=""UpdateValue"" />
1888+
@code {
1889+
public CustomValue ParentValue { get; set; } = new CustomValue();
1890+
1891+
public EventCallback<CustomValue> UpdateValue { get; set; }
1892+
}");
1893+
1894+
// Assert
1895+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1896+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1897+
CompileToAssembly(generated);
1898+
}
1899+
1900+
[Fact]
1901+
public void GenericComponentBindToGenericComponent_InferredType_WithGetSet_EventCallback()
1902+
{
1903+
// Arrange
1904+
AdditionalSyntaxTrees.Add(Parse(@"
1905+
using System;
1906+
using Microsoft.AspNetCore.Components;
1907+
1908+
namespace Test
1909+
{
1910+
public class MyComponent<TValue> : ComponentBase
1911+
{
1912+
[Parameter]
1913+
public TValue Value { get; set; }
1914+
1915+
[Parameter]
1916+
public EventCallback<TValue> ValueChanged { get; set; }
1917+
}
1918+
}"));
1919+
1920+
// Act
1921+
var generated = CompileToCSharp(@"
1922+
@typeparam TParam
1923+
<MyComponent @bind-Value:get=""ParentValue"" @bind-Value:set=""UpdateValue"" />
1924+
@code {
1925+
public TParam ParentValue { get; set; } = default;
1926+
1927+
public EventCallback<TParam> UpdateValue { get; set; }
1928+
}");
1929+
1930+
// Assert
1931+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1932+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1933+
CompileToAssembly(generated);
1934+
}
1935+
1936+
[Fact]
1937+
public void GenericBindToGenericComponent_ExplicitType_WithGetSet_EventCallback()
1938+
{
1939+
// Arrange
1940+
AdditionalSyntaxTrees.Add(Parse(@"
1941+
using System;
1942+
using Microsoft.AspNetCore.Components;
1943+
1944+
namespace Test
1945+
{
1946+
public class MyComponent<TValue> : ComponentBase
1947+
{
1948+
[Parameter]
1949+
public TValue Value { get; set; }
1950+
1951+
[Parameter]
1952+
public EventCallback<TValue> ValueChanged { get; set; }
1953+
}
1954+
}"));
1955+
1956+
// Act
1957+
var generated = CompileToCSharp(@"
1958+
@typeparam TParam
1959+
<MyComponent TValue=""TParam"" @bind-Value:get=""ParentValue"" @bind-Value:set=""UpdateValue"" />
1960+
@code {
1961+
public TParam ParentValue { get; set; } = default;
1962+
1963+
public EventCallback<TParam> UpdateValue { get; set; }
1964+
}");
1965+
1966+
// Assert
1967+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1968+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1969+
CompileToAssembly(generated);
1970+
}
1971+
18221972
[Fact]
18231973
public void BindToComponent_WithGetSet_EventCallback_ReceivesAction()
18241974
{
@@ -1992,6 +2142,148 @@ public void Update() { }
19922142
CompileToAssembly(generated);
19932143
}
19942144

2145+
[Fact]
2146+
public void BindToGenericComponent_InferredType_WithAfter_Action()
2147+
{
2148+
// Arrange
2149+
AdditionalSyntaxTrees.Add(Parse(@"
2150+
using System;
2151+
using Microsoft.AspNetCore.Components;
2152+
2153+
namespace Test
2154+
{
2155+
public class MyComponent<TValue> : ComponentBase
2156+
{
2157+
[Parameter]
2158+
public TValue Value { get; set; }
2159+
2160+
[Parameter]
2161+
public Action<TValue> ValueChanged { get; set; }
2162+
}
2163+
}"));
2164+
2165+
// Act
2166+
var generated = CompileToCSharp(@"
2167+
<MyComponent @bind-Value:get=""ParentValue"" @bind-Value:after=""Update"" />
2168+
@code {
2169+
public int ParentValue { get; set; } = 42;
2170+
2171+
public void Update() { }
2172+
}");
2173+
2174+
// Assert
2175+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
2176+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
2177+
CompileToAssembly(generated);
2178+
}
2179+
2180+
[Fact]
2181+
public void BindToGenericComponent_ExplicitType_WithAfter_Action()
2182+
{
2183+
// Arrange
2184+
AdditionalSyntaxTrees.Add(Parse(@"
2185+
using System;
2186+
using Microsoft.AspNetCore.Components;
2187+
2188+
namespace Test
2189+
{
2190+
public class MyComponent<TValue> : ComponentBase
2191+
{
2192+
[Parameter]
2193+
public TValue Value { get; set; }
2194+
2195+
[Parameter]
2196+
public Action<TValue> ValueChanged { get; set; }
2197+
}
2198+
}"));
2199+
2200+
// Act
2201+
var generated = CompileToCSharp(@"
2202+
<MyComponent TValue=""int"" @bind-Value:get=""ParentValue"" @bind-Value:after=""Update"" />
2203+
@code {
2204+
public int ParentValue { get; set; } = 42;
2205+
2206+
public void Update() { }
2207+
}");
2208+
2209+
// Assert
2210+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
2211+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
2212+
CompileToAssembly(generated);
2213+
}
2214+
2215+
[Fact]
2216+
public void GenericComponentBindToGenericComponent_InferredType_WithAfter_Action()
2217+
{
2218+
// Arrange
2219+
AdditionalSyntaxTrees.Add(Parse(@"
2220+
using System;
2221+
using Microsoft.AspNetCore.Components;
2222+
2223+
namespace Test
2224+
{
2225+
public class MyComponent<TValue> : ComponentBase
2226+
{
2227+
[Parameter]
2228+
public TValue Value { get; set; }
2229+
2230+
[Parameter]
2231+
public Action<TValue> ValueChanged { get; set; }
2232+
}
2233+
}"));
2234+
2235+
// Act
2236+
var generated = CompileToCSharp(@"
2237+
@typeparam TParam
2238+
<MyComponent @bind-Value:get=""ParentValue"" @bind-Value:after=""Update"" />
2239+
@code {
2240+
public TParam ParentValue { get; set; }
2241+
2242+
public void Update() { }
2243+
}");
2244+
2245+
// Assert
2246+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
2247+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
2248+
CompileToAssembly(generated);
2249+
}
2250+
2251+
[Fact]
2252+
public void GenericComponentBindToGenericComponent_ExplicitType_WithAfter_Action()
2253+
{
2254+
// Arrange
2255+
AdditionalSyntaxTrees.Add(Parse(@"
2256+
using System;
2257+
using Microsoft.AspNetCore.Components;
2258+
2259+
namespace Test
2260+
{
2261+
public class MyComponent<TValue> : ComponentBase
2262+
{
2263+
[Parameter]
2264+
public TValue Value { get; set; }
2265+
2266+
[Parameter]
2267+
public Action<TValue> ValueChanged { get; set; }
2268+
}
2269+
}"));
2270+
2271+
// Act
2272+
var generated = CompileToCSharp(@"
2273+
@typeparam TParam
2274+
<MyComponent TValue=""TParam"" @bind-Value:get=""ParentValue"" @bind-Value:after=""Update"" />
2275+
@code {
2276+
public TParam ParentValue { get; set; }
2277+
2278+
public void Update() { }
2279+
}");
2280+
2281+
// Assert
2282+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
2283+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
2284+
CompileToAssembly(generated);
2285+
}
2286+
19952287
[Fact]
19962288
public void BindToComponent_WithAfter_ActionLambda()
19972289
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line hidden
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Components;
11+
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
12+
{
13+
#pragma warning disable 219
14+
private void __RazorDirectiveTokenHelpers__() {
15+
}
16+
#pragma warning restore 219
17+
#pragma warning disable 0414
18+
private static System.Object __o = null;
19+
#pragma warning restore 0414
20+
#pragma warning disable 1998
21+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
22+
{
23+
__o = typeof(
24+
#nullable restore
25+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
26+
int
27+
28+
#line default
29+
#line hidden
30+
#nullable disable
31+
);
32+
__o = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<int>(
33+
#nullable restore
34+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
35+
ParentValue
36+
37+
#line default
38+
#line hidden
39+
#nullable disable
40+
);
41+
__o = new global::System.Action<int>(
42+
__value => { ParentValue = __value; global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.InvokeSynchronousDelegate(Update); });
43+
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
44+
}
45+
));
46+
#nullable restore
47+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
48+
__o = typeof(global::Test.MyComponent<>);
49+
50+
#line default
51+
#line hidden
52+
#nullable disable
53+
}
54+
#pragma warning restore 1998
55+
#nullable restore
56+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
57+
58+
public int ParentValue { get; set; } = 42;
59+
60+
public void Update() { }
61+
62+
#line default
63+
#line hidden
64+
#nullable disable
65+
}
66+
}
67+
#pragma warning restore 1591

0 commit comments

Comments
 (0)