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

Ensure model directives are mapped at runtime #11007

Merged
merged 15 commits into from
Oct 17, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ @model Type1

// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("Type1", baseType.ModelType.Content);
Assert.NotNull(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -102,8 +107,13 @@ @model Type2

// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("Type1", baseType.ModelType.Content);
Assert.NotNull(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -129,7 +139,13 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

// ISSUE: https://github.com/dotnet/razor/issues/10987 we don't issue a warning or emit anything for the unused model
Assert.Null(baseType.ModelType);
}

[Fact]
Expand All @@ -154,7 +170,13 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("dynamic", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -179,7 +201,13 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("dynamic", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -209,7 +237,13 @@ @model SomeType
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<SomeType>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("SomeType", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -97,7 +98,9 @@ public void MvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
var baseNode = Assert.IsType<BaseTypeWithModel>(visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage", baseNode.BaseType.Content);
Assert.Equal("TModel", baseNode.ModelType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -122,7 +125,9 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
var baseNode = Assert.IsType<BaseTypeWithModel>(visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage", baseNode.BaseType.Content);
Assert.Equal("TModel", baseNode.ModelType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ namespace AspNetCore
#line default
#line hidden
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"Sha256", @"319d5fa6f848e64d19bf7eab2f5e3339cdfc75b02a9bc6f2773eed1a40f5e9d0", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml")]
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<MyModel>
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithModel : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<
#line (1,8)-(1,15) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml"
MyModel

#line default
#line hidden
>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
Source Location: (54:2,8 [9] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
Source Location: (7:0,7 [7] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
|MyModel|
Generated Location: (1359:19,0 [7] )
|MyModel|

Source Location: (54:2,8 [9] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
|MyService|
Generated Location: (1651:27,0 [9] )
Generated Location: (1792:33,0 [9] )
|MyService|

Source Location: (72:2,26 [4] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
|Html|
Generated Location: (1816:33,0 [4] )
Generated Location: (1957:39,0 [4] )
|Html|

Source Location: (24:1,8 [5] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
|MyApp|
Generated Location: (2084:41,0 [5] )
Generated Location: (2225:47,0 [5] )
|MyApp|

Source Location: (30:1,14 [14] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithModel.cshtml)
|MyPropertyName|
Generated Location: (2236:47,0 [14] )
Generated Location: (2377:53,0 [14] )
|MyPropertyName|

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ namespace AspNetCore
#line default
#line hidden
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"Sha256", @"771acb56660727ab6e4ca50e95bde0cf2a72af8de3e9ec1cd4b72969645cb9af", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml")]
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<MyModel>
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_InjectWithSemicolon : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<
#line (1,8)-(1,15) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml"
MyModel

#line default
#line hidden
>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
Source Location: (129:4,8 [9] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
Source Location: (7:0,7 [7] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyModel|
Generated Location: (1383:19,0 [7] )
|MyModel|

Source Location: (129:4,8 [9] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyService|
Generated Location: (1675:27,0 [9] )
Generated Location: (1820:33,0 [9] )
|MyService|

Source Location: (147:4,26 [5] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|Html2|
Generated Location: (1844:33,0 [5] )
Generated Location: (1989:39,0 [5] )
|Html2|

Source Location: (93:3,8 [5] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyApp|
Generated Location: (2117:41,0 [5] )
Generated Location: (2262:47,0 [5] )
|MyApp|

Source Location: (99:3,14 [15] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyPropertyName2|
Generated Location: (2273:47,0 [15] )
Generated Location: (2418:53,0 [15] )
|MyPropertyName2|

Source Location: (58:2,8 [9] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyService|
Generated Location: (2556:55,0 [9] )
Generated Location: (2701:61,0 [9] )
|MyService|

Source Location: (76:2,26 [4] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|Html|
Generated Location: (2725:61,0 [4] )
Generated Location: (2870:67,0 [4] )
|Html|

Source Location: (24:1,8 [5] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyApp|
Generated Location: (2997:69,0 [5] )
Generated Location: (3142:75,0 [5] )
|MyApp|

Source Location: (30:1,14 [14] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InjectWithSemicolon.cshtml)
|MyPropertyName|
Generated Location: (3153:75,0 [14] )
Generated Location: (3298:81,0 [14] )
|MyPropertyName|

Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ @model Type1

// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("Type1", baseType.ModelType.Content);
Assert.NotNull(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -102,8 +107,13 @@ @model Type2

// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<Type1>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("Type1", baseType.ModelType.Content);
Assert.NotNull(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -129,7 +139,13 @@ @model Type1
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

// ISSUE: https://github.com/dotnet/razor/issues/10987 we don't issue a warning or emit anything for the unused model
Assert.Null(baseType.ModelType);
}

[Fact]
Expand All @@ -154,7 +170,13 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("dynamic", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);
}

[Fact]
Expand All @@ -179,7 +201,13 @@ @inherits BaseType<TModel>
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<dynamic>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("dynamic", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -209,7 +237,13 @@ @model SomeType
// Assert
var @class = FindClassNode(irDocument);
Assert.NotNull(@class);
Assert.Equal("BaseType<SomeType>", @class.BaseType.Content);
var baseType = @class.BaseType;

Assert.Equal("BaseType", baseType.BaseType.Content);
Assert.NotNull(baseType.BaseType.Source);

Assert.Equal("SomeType", baseType.ModelType.Content);
Assert.Null(baseType.ModelType.Source);

var @namespace = FindNamespaceNode(irDocument);
var usingNode = Assert.IsType<UsingDirectiveIntermediateNode>(@namespace.Children[0]);
Expand Down Expand Up @@ -238,7 +272,7 @@ private NamespaceDeclarationIntermediateNode FindNamespaceNode(IntermediateNode

private RazorEngine CreateRuntimeEngine()
{
return CreateEngineCore();
return CreateEngineCore(designTime: false);
}

private RazorEngine CreateDesignTimeEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -97,7 +98,9 @@ public void MvcViewDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
var baseNode = Assert.IsType<BaseTypeWithModel>(visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage", baseNode.BaseType.Content);
Assert.Equal("TModel", baseNode.ModelType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -122,7 +125,9 @@ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType.Content);
var baseNode = Assert.IsType<BaseTypeWithModel>(visitor.Class.BaseType);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage", baseNode.BaseType.Content);
Assert.Equal("TModel", baseNode.ModelType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_ec563e63d931b806184cb02f79875e4f3b21d1ca043ad06699424459128b58c0", visitor.Class.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void RazorPageDocumentClassifierPass_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
Assert.Equal("Test", visitor.Class.ClassName);
}
Expand All @@ -249,7 +249,7 @@ public void RazorPageDocumentClassifierPass_NullFilePath_SetsClass()
visitor.Visit(irDocument);

// Assert
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.Content);
Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType.BaseType.Content);
Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
AssertEx.Equal("AspNetCore_c3b458108610c1a2aa6eede0a5685ede853e036732db515609b2a23ca15359e1", visitor.Class.ClassName);
}
Expand Down
Loading
Loading