1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using Microsoft . AspNetCore . Razor . Language ;
5
+ using Microsoft . AspNetCore . Razor . Language . Intermediate ;
6
+ using Xunit ;
7
+
8
+ namespace Microsoft . AspNetCore . Mvc . Razor . Extensions
9
+ {
10
+ public class ConsolidatedMvcViewDocumentClassifierPassTest : RazorProjectEngineTestBase
11
+ {
12
+ protected override RazorLanguageVersion Version => RazorLanguageVersion . Latest ;
13
+
14
+ [ Fact ]
15
+ public void ConsolidatedMvcViewDocumentClassifierPass_SetsDifferentNamespace ( )
16
+ {
17
+ // Arrange
18
+ var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( "some-content" , "Test.cshtml" ) ) ;
19
+
20
+ var projectEngine = CreateProjectEngine ( ) ;
21
+ var irDocument = CreateIRDocument ( projectEngine , codeDocument ) ;
22
+ var pass = new ConsolidatedMvcViewDocumentClassifierPass
23
+ {
24
+ Engine = projectEngine . Engine
25
+ } ;
26
+
27
+ // Act
28
+ pass . Execute ( codeDocument , irDocument ) ;
29
+ var visitor = new Visitor ( ) ;
30
+ visitor . Visit ( irDocument ) ;
31
+
32
+ // Assert
33
+ Assert . Equal ( "AspNetCoreGeneratedDocument" , visitor . Namespace . Content ) ;
34
+ }
35
+
36
+ [ Fact ]
37
+ public void ConsolidatedMvcViewDocumentClassifierPass_SetsClass ( )
38
+ {
39
+ // Arrange
40
+ var properties = new RazorSourceDocumentProperties ( filePath : "ignored" , relativePath : "Test.cshtml" ) ;
41
+ var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( "some-content" , properties ) ) ;
42
+
43
+ var projectEngine = CreateProjectEngine ( ) ;
44
+ var irDocument = CreateIRDocument ( projectEngine , codeDocument ) ;
45
+ var pass = new ConsolidatedMvcViewDocumentClassifierPass
46
+ {
47
+ Engine = projectEngine . Engine
48
+ } ;
49
+
50
+ // Act
51
+ pass . Execute ( codeDocument , irDocument ) ;
52
+ var visitor = new Visitor ( ) ;
53
+ visitor . Visit ( irDocument ) ;
54
+
55
+ // Assert
56
+ Assert . Equal ( "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>" , visitor . Class . BaseType ) ;
57
+ Assert . Equal ( new [ ] { "internal" , "sealed" } , visitor . Class . Modifiers ) ;
58
+ Assert . Equal ( "Test" , visitor . Class . ClassName ) ;
59
+ }
60
+
61
+ [ Fact ]
62
+ public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass ( )
63
+ {
64
+ // Arrange
65
+ var properties = new RazorSourceDocumentProperties ( filePath : null , relativePath : null ) ;
66
+ var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( "some-content" , properties ) ) ;
67
+
68
+ var projectEngine = CreateProjectEngine ( ) ;
69
+ var irDocument = CreateIRDocument ( projectEngine , codeDocument ) ;
70
+ var pass = new ConsolidatedMvcViewDocumentClassifierPass
71
+ {
72
+ Engine = projectEngine . Engine
73
+ } ;
74
+
75
+ // Act
76
+ pass . Execute ( codeDocument , irDocument ) ;
77
+ var visitor = new Visitor ( ) ;
78
+ visitor . Visit ( irDocument ) ;
79
+
80
+ // Assert
81
+ Assert . Equal ( "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>" , visitor . Class . BaseType ) ;
82
+ Assert . Equal ( new [ ] { "internal" , "sealed" } , visitor . Class . Modifiers ) ;
83
+ Assert . Equal ( "AspNetCore_d9f877a857a7e9928eac04d09a59f25967624155" , visitor . Class . ClassName ) ;
84
+ }
85
+
86
+ [ Theory ]
87
+ [ InlineData ( "/Views/Home/Index.cshtml" , "_Views_Home_Index" ) ]
88
+ [ InlineData ( "/Areas/MyArea/Views/Home/About.cshtml" , "_Areas_MyArea_Views_Home_About" ) ]
89
+ public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName ( string relativePath , string expected )
90
+ {
91
+ // Arrange
92
+ var properties = new RazorSourceDocumentProperties ( filePath : "ignored" , relativePath : relativePath ) ;
93
+ var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( "some-content" , properties ) ) ;
94
+
95
+ var projectEngine = CreateProjectEngine ( ) ;
96
+ var irDocument = CreateIRDocument ( projectEngine , codeDocument ) ;
97
+ var pass = new ConsolidatedMvcViewDocumentClassifierPass
98
+ {
99
+ Engine = projectEngine . Engine
100
+ } ;
101
+
102
+ // Act
103
+ pass . Execute ( codeDocument , irDocument ) ;
104
+ var visitor = new Visitor ( ) ;
105
+ visitor . Visit ( irDocument ) ;
106
+
107
+ // Assert
108
+ Assert . Equal ( expected , visitor . Class . ClassName ) ;
109
+ Assert . Equal ( new [ ] { "internal" , "sealed" } , visitor . Class . Modifiers ) ;
110
+ }
111
+
112
+ [ Fact ]
113
+ public void ConsolidatedMvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod ( )
114
+ {
115
+ // Arrange
116
+ var codeDocument = RazorCodeDocument . Create ( RazorSourceDocument . Create ( "some-content" , "Test.cshtml" ) ) ;
117
+
118
+ var projectEngine = CreateProjectEngine ( ) ;
119
+ var irDocument = CreateIRDocument ( projectEngine , codeDocument ) ;
120
+ var pass = new ConsolidatedMvcViewDocumentClassifierPass
121
+ {
122
+ Engine = projectEngine . Engine
123
+ } ;
124
+
125
+ // Act
126
+ pass . Execute ( codeDocument , irDocument ) ;
127
+ var visitor = new Visitor ( ) ;
128
+ visitor . Visit ( irDocument ) ;
129
+
130
+ // Assert
131
+ Assert . Equal ( "ExecuteAsync" , visitor . Method . MethodName ) ;
132
+ Assert . Equal ( "global::System.Threading.Tasks.Task" , visitor . Method . ReturnType ) ;
133
+ Assert . Equal ( new [ ] { "public" , "async" , "override" } , visitor . Method . Modifiers ) ;
134
+ }
135
+
136
+ private static DocumentIntermediateNode CreateIRDocument ( RazorProjectEngine projectEngine , RazorCodeDocument codeDocument )
137
+ {
138
+ for ( var i = 0 ; i < projectEngine . Phases . Count ; i ++ )
139
+ {
140
+ var phase = projectEngine . Phases [ i ] ;
141
+ phase . Execute ( codeDocument ) ;
142
+
143
+ if ( phase is IRazorIntermediateNodeLoweringPhase )
144
+ {
145
+ break ;
146
+ }
147
+ }
148
+
149
+ return codeDocument . GetDocumentIntermediateNode ( ) ;
150
+ }
151
+
152
+ private class Visitor : IntermediateNodeWalker
153
+ {
154
+ public NamespaceDeclarationIntermediateNode Namespace { get ; private set ; }
155
+
156
+ public ClassDeclarationIntermediateNode Class { get ; private set ; }
157
+
158
+ public MethodDeclarationIntermediateNode Method { get ; private set ; }
159
+
160
+ public override void VisitMethodDeclaration ( MethodDeclarationIntermediateNode node )
161
+ {
162
+ Method = node ;
163
+ }
164
+
165
+ public override void VisitNamespaceDeclaration ( NamespaceDeclarationIntermediateNode node )
166
+ {
167
+ Namespace = node ;
168
+ base . VisitNamespaceDeclaration ( node ) ;
169
+ }
170
+
171
+ public override void VisitClassDeclaration ( ClassDeclarationIntermediateNode node )
172
+ {
173
+ Class = node ;
174
+ base . VisitClassDeclaration ( node ) ;
175
+ }
176
+ }
177
+ }
178
+ }
0 commit comments