|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
5 |
| -using Microsoft.AspNetCore.Razor.Language.Extensions; |
6 | 5 | using Xunit;
|
7 | 6 |
|
8 | 7 | namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
9 | 8 | {
|
10 | 9 | public class CSharpSpecialBlockTest : CsHtmlCodeParserTestBase
|
11 | 10 | {
|
12 |
| - [Fact] |
13 |
| - public void ParseInheritsStatementMarksInheritsSpanAsCanGrowIfMissingTrailingSpace() |
| 11 | + public CSharpSpecialBlockTest() |
14 | 12 | {
|
15 |
| - // Arrange |
16 |
| - var chunkGenerator = new DirectiveChunkGenerator(InheritsDirective.Directive); |
17 |
| - chunkGenerator.Diagnostics.Add( |
18 |
| - RazorDiagnosticFactory.CreateParsing_UnexpectedEOFAfterDirective( |
19 |
| - new SourceSpan(new SourceLocation(9, 0, 9), 1), InheritsDirective.Directive.Directive, "type")); |
20 |
| - |
21 |
| - // Act & Assert |
22 |
| - ParseDocumentTest( |
23 |
| - "@inherits", |
24 |
| - new[] { InheritsDirective.Directive }, |
25 |
| - new MarkupBlock( |
26 |
| - Factory.EmptyHtml(), |
27 |
| - new DirectiveBlock(chunkGenerator, |
28 |
| - Factory.CodeTransition(), |
29 |
| - Factory.MetaCode("inherits").Accepts(AcceptedCharactersInternal.None), |
30 |
| - Factory.Span(SpanKindInternal.Code, string.Empty, CSharpSymbolType.Unknown) |
31 |
| - .AsDirectiveToken(DirectiveTokenDescriptor.CreateToken(DirectiveTokenKind.Type))), |
32 |
| - Factory.EmptyHtml())); |
33 |
| - } |
34 |
| - |
35 |
| - [Fact] |
36 |
| - public void InheritsBlockAcceptsMultipleGenericArguments() |
37 |
| - { |
38 |
| - ParseDocumentTest( |
39 |
| - "@inherits Foo.Bar<Biz<Qux>, string, int>.Baz", |
40 |
| - new[] { InheritsDirective.Directive }, |
41 |
| - new MarkupBlock( |
42 |
| - Factory.EmptyHtml(), |
43 |
| - new DirectiveBlock(new DirectiveChunkGenerator(InheritsDirective.Directive), |
44 |
| - Factory.CodeTransition(), |
45 |
| - Factory.MetaCode("inherits").Accepts(AcceptedCharactersInternal.None), |
46 |
| - Factory.Span(SpanKindInternal.Code, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.WhiteSpace), |
47 |
| - Factory.Span(SpanKindInternal.Code, "Foo.Bar<Biz<Qux>, string, int>.Baz", markup: false).AsDirectiveToken(InheritsDirective.Directive.Tokens[0])), |
48 |
| - Factory.EmptyHtml())); |
49 |
| - } |
50 |
| - |
51 |
| - [Fact] |
52 |
| - public void InheritsBlockOutputsErrorIfInheritsNotFollowedByTypeButAcceptsEntireLineAsCode() |
53 |
| - { |
54 |
| - // Arrange |
55 |
| - var chunkGenerator = new DirectiveChunkGenerator(InheritsDirective.Directive); |
56 |
| - chunkGenerator.Diagnostics.Add( |
57 |
| - RazorDiagnosticFactory.CreateParsing_DirectiveExpectsTypeName( |
58 |
| - new SourceSpan(new SourceLocation(25, 0, 25), Environment.NewLine.Length), |
59 |
| - InheritsDirective.Directive.Directive)); |
60 |
| - |
61 |
| - // Act & Assert |
62 |
| - ParseDocumentTest( |
63 |
| - "@inherits " + Environment.NewLine + "foo", |
64 |
| - new[] { InheritsDirective.Directive }, |
65 |
| - new MarkupBlock( |
66 |
| - Factory.EmptyHtml(), |
67 |
| - new DirectiveBlock(chunkGenerator, |
68 |
| - Factory.CodeTransition(), |
69 |
| - Factory.MetaCode("inherits").Accepts(AcceptedCharactersInternal.None), |
70 |
| - Factory.Span(SpanKindInternal.Code, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.WhiteSpace), |
71 |
| - Factory.Span(SpanKindInternal.Code, string.Empty, CSharpSymbolType.Unknown) |
72 |
| - .AsDirectiveToken(DirectiveTokenDescriptor.CreateToken(DirectiveTokenKind.Type))), |
73 |
| - Factory.Markup(Environment.NewLine + "foo"))); |
| 13 | + UseBaselineTests = true; |
74 | 14 | }
|
75 | 15 |
|
76 | 16 | [Fact]
|
77 | 17 | public void NamespaceImportInsideCodeBlockCausesError()
|
78 | 18 | {
|
79 |
| - ParseBlockTest("{ using Foo.Bar.Baz; var foo = bar; }", |
80 |
| - new StatementBlock( |
81 |
| - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), |
82 |
| - Factory.Code(" using Foo.Bar.Baz; var foo = bar; ") |
83 |
| - .AsStatement() |
84 |
| - .AutoCompleteWith(autoCompleteString: null), |
85 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) |
86 |
| - ), |
87 |
| - RazorDiagnosticFactory.CreateParsing_NamespaceImportAndTypeAliasCannotExistWithinCodeBlock( |
88 |
| - new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 5))); |
| 19 | + ParseBlockTest("{ using Foo.Bar.Baz; var foo = bar; }"); |
89 | 20 | }
|
90 | 21 |
|
91 | 22 | [Fact]
|
92 | 23 | public void TypeAliasInsideCodeBlockIsNotHandledSpecially()
|
93 | 24 | {
|
94 |
| - ParseBlockTest("{ using Foo = Bar.Baz; var foo = bar; }", |
95 |
| - new StatementBlock( |
96 |
| - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), |
97 |
| - Factory.Code(" using Foo = Bar.Baz; var foo = bar; ") |
98 |
| - .AsStatement() |
99 |
| - .AutoCompleteWith(autoCompleteString: null), |
100 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) |
101 |
| - ), |
102 |
| - RazorDiagnosticFactory.CreateParsing_NamespaceImportAndTypeAliasCannotExistWithinCodeBlock( |
103 |
| - new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 5))); |
104 |
| - } |
105 |
| - |
106 |
| - [Fact] |
107 |
| - public void Plan9FunctionsKeywordInsideCodeBlockIsNotHandledSpecially() |
108 |
| - { |
109 |
| - ParseBlockTest("{ functions Foo; }", |
110 |
| - new StatementBlock( |
111 |
| - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), |
112 |
| - Factory.Code(" functions Foo; ") |
113 |
| - .AsStatement() |
114 |
| - .AutoCompleteWith(autoCompleteString: null), |
115 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) |
116 |
| - )); |
| 25 | + ParseBlockTest("{ using Foo = Bar.Baz; var foo = bar; }"); |
117 | 26 | }
|
118 | 27 |
|
119 | 28 | [Fact]
|
120 | 29 | public void NonKeywordStatementInCodeBlockIsHandledCorrectly()
|
121 | 30 | {
|
122 |
| - ParseBlockTest("{" + Environment.NewLine |
123 |
| - + " List<dynamic> photos = gallery.Photo.ToList();" + Environment.NewLine |
124 |
| - + "}", |
125 |
| - new StatementBlock( |
126 |
| - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), |
127 |
| - Factory.Code($"{Environment.NewLine} List<dynamic> photos = gallery.Photo.ToList();{Environment.NewLine}") |
128 |
| - .AsStatement() |
129 |
| - .AutoCompleteWith(autoCompleteString: null), |
130 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) |
131 |
| - )); |
| 31 | + ParseBlockTest( |
| 32 | +@"{ |
| 33 | + List<dynamic> photos = gallery.Photo.ToList(); |
| 34 | +}"); |
132 | 35 | }
|
133 | 36 |
|
134 | 37 | [Fact]
|
135 | 38 | public void ParseBlockBalancesBracesOutsideStringsIfFirstCharacterIsBraceAndReturnsSpanOfTypeCode()
|
136 | 39 | {
|
137 |
| - // Arrange |
138 |
| - const string code = "foo\"b}ar\" if(condition) { string.Format(\"{0}\"); } "; |
139 |
| - |
140 |
| - // Act/Assert |
141 |
| - ParseBlockTest("{" + code + "}", |
142 |
| - new StatementBlock( |
143 |
| - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), |
144 |
| - Factory.Code(code) |
145 |
| - .AsStatement() |
146 |
| - .AutoCompleteWith(autoCompleteString: null), |
147 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) |
148 |
| - )); |
| 40 | + ParseBlockTest("{foo\"b}ar\" if(condition) { string.Format(\"{0}\"); } }"); |
149 | 41 | }
|
150 | 42 |
|
151 | 43 | [Fact]
|
152 | 44 | public void ParseBlockBalancesParensOutsideStringsIfFirstCharacterIsParenAndReturnsSpanOfTypeExpression()
|
153 | 45 | {
|
154 |
| - // Arrange |
155 |
| - const string code = "foo\"b)ar\" if(condition) { string.Format(\"{0}\"); } "; |
156 |
| - |
157 |
| - // Act/Assert |
158 |
| - ParseBlockTest("(" + code + ")", |
159 |
| - new ExpressionBlock( |
160 |
| - Factory.MetaCode("(").Accepts(AcceptedCharactersInternal.None), |
161 |
| - Factory.Code(code).AsExpression(), |
162 |
| - Factory.MetaCode(")").Accepts(AcceptedCharactersInternal.None) |
163 |
| - )); |
164 |
| - } |
165 |
| - |
166 |
| - [Fact] |
167 |
| - public void ParseBlockBalancesBracesAndOutputsContentAsClassLevelCodeSpanIfFirstIdentifierIsFunctionsKeyword() |
168 |
| - { |
169 |
| - const string code = " foo(); \"bar}baz\" "; |
170 |
| - ParseBlockTest( |
171 |
| - "functions {" + code + "} zoop", |
172 |
| - new[] { FunctionsDirective.Directive }, |
173 |
| - new DirectiveBlock(new DirectiveChunkGenerator(FunctionsDirective.Directive), |
174 |
| - Factory.MetaCode("functions").Accepts(AcceptedCharactersInternal.None), |
175 |
| - Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace), |
176 |
| - Factory.MetaCode("{").AutoCompleteWith(null, atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None), |
177 |
| - Factory.Code(code).AsCodeBlock(), |
178 |
| - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None))); |
179 |
| - } |
180 |
| - |
181 |
| - [Fact] |
182 |
| - public void ParseBlockDoesNoErrorRecoveryForFunctionsBlock() |
183 |
| - { |
184 |
| - // Arrange |
185 |
| - var chunkGenerator = new DirectiveChunkGenerator(FunctionsDirective.Directive); |
186 |
| - chunkGenerator.Diagnostics.Add( |
187 |
| - RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF( |
188 |
| - new SourceSpan(new SourceLocation(10, 0, 10), contentLength: 1), "functions", "}", "{")); |
189 |
| - |
190 |
| - // Act & Assert |
191 |
| - ParseBlockTest( |
192 |
| - "functions { { { { { } zoop", |
193 |
| - new[] { FunctionsDirective.Directive }, |
194 |
| - new DirectiveBlock(chunkGenerator, |
195 |
| - Factory.MetaCode("functions").Accepts(AcceptedCharactersInternal.None), |
196 |
| - Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace), |
197 |
| - Factory.MetaCode("{").AutoCompleteWith("}", atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None), |
198 |
| - Factory.Code(" { { { { } zoop").AsCodeBlock())); |
199 |
| - } |
200 |
| - |
201 |
| - [Fact] |
202 |
| - public void ParseBlockIgnoresFunctionsUnlessAllLowerCase() |
203 |
| - { |
204 |
| - ParseBlockTest("Functions { foo() }", |
205 |
| - new ExpressionBlock( |
206 |
| - Factory.Code("Functions") |
207 |
| - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) |
208 |
| - .Accepts(AcceptedCharactersInternal.NonWhiteSpace))); |
| 46 | + ParseBlockTest("(foo\"b)ar\" if(condition) { string.Format(\"{0}\"); } )"); |
209 | 47 | }
|
210 | 48 |
|
211 | 49 | [Fact]
|
212 | 50 | public void ParseBlockIgnoresSingleSlashAtStart()
|
213 | 51 | {
|
214 |
| - ParseBlockTest("@/ foo", |
215 |
| - new ExpressionBlock( |
216 |
| - Factory.CodeTransition(), |
217 |
| - Factory.EmptyCSharp() |
218 |
| - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) |
219 |
| - .Accepts(AcceptedCharactersInternal.NonWhiteSpace)), |
220 |
| - RazorDiagnosticFactory.CreateParsing_UnexpectedCharacterAtStartOfCodeBlock( |
221 |
| - new SourceSpan(new SourceLocation(1, 0, 1), contentLength: 1), |
222 |
| - "/")); |
| 52 | + ParseBlockTest("@/ foo"); |
223 | 53 | }
|
224 | 54 |
|
225 | 55 | [Fact]
|
226 | 56 | public void ParseBlockTerminatesSingleLineCommentAtEndOfLine()
|
227 | 57 | {
|
228 |
| - ParseBlockTest("if(!false) {" + Environment.NewLine |
229 |
| - + " // Foo" + Environment.NewLine |
230 |
| - + "\t<p>A real tag!</p>" + Environment.NewLine |
231 |
| - + "}", |
232 |
| - new StatementBlock( |
233 |
| - Factory.Code($"if(!false) {{{Environment.NewLine} // Foo{Environment.NewLine}").AsStatement(), |
234 |
| - new MarkupBlock( |
235 |
| - Factory.Markup("\t"), |
236 |
| - new MarkupTagBlock( |
237 |
| - Factory.Markup("<p>").Accepts(AcceptedCharactersInternal.None)), |
238 |
| - Factory.Markup("A real tag!"), |
239 |
| - new MarkupTagBlock( |
240 |
| - Factory.Markup("</p>").Accepts(AcceptedCharactersInternal.None)), |
241 |
| - Factory.Markup(Environment.NewLine).Accepts(AcceptedCharactersInternal.None)), |
242 |
| - Factory.Code("}").AsStatement() |
243 |
| - )); |
| 58 | + ParseBlockTest( |
| 59 | +"if(!false) {" + Environment.NewLine + |
| 60 | +" // Foo" + Environment.NewLine + |
| 61 | +"\t<p>A real tag!</p>" + Environment.NewLine + |
| 62 | +"}"); |
244 | 63 | }
|
245 | 64 | }
|
246 | 65 | }
|
0 commit comments