Skip to content

Commit 5950928

Browse files
author
N. Taylor Mullen
committed
Convert CSharpSpecialBlockTest to use baselines.
#2263
1 parent a7fd53b commit 5950928

11 files changed

+202
-197
lines changed

test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpSpecialBlockTest.cs

Lines changed: 16 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -2,245 +2,64 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.Razor.Language.Extensions;
65
using Xunit;
76

87
namespace Microsoft.AspNetCore.Razor.Language.Legacy
98
{
109
public class CSharpSpecialBlockTest : CsHtmlCodeParserTestBase
1110
{
12-
[Fact]
13-
public void ParseInheritsStatementMarksInheritsSpanAsCanGrowIfMissingTrailingSpace()
11+
public CSharpSpecialBlockTest()
1412
{
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;
7414
}
7515

7616
[Fact]
7717
public void NamespaceImportInsideCodeBlockCausesError()
7818
{
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; }");
8920
}
9021

9122
[Fact]
9223
public void TypeAliasInsideCodeBlockIsNotHandledSpecially()
9324
{
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; }");
11726
}
11827

11928
[Fact]
12029
public void NonKeywordStatementInCodeBlockIsHandledCorrectly()
12130
{
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+
}");
13235
}
13336

13437
[Fact]
13538
public void ParseBlockBalancesBracesOutsideStringsIfFirstCharacterIsBraceAndReturnsSpanOfTypeCode()
13639
{
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}\"); } }");
14941
}
15042

15143
[Fact]
15244
public void ParseBlockBalancesParensOutsideStringsIfFirstCharacterIsParenAndReturnsSpanOfTypeExpression()
15345
{
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}\"); } )");
20947
}
21048

21149
[Fact]
21250
public void ParseBlockIgnoresSingleSlashAtStart()
21351
{
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");
22353
}
22454

22555
[Fact]
22656
public void ParseBlockTerminatesSingleLineCommentAtEndOfLine()
22757
{
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+
"}");
24463
}
24564
}
24665
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(1,3): Error RZ2004: Namespace imports and type aliases cannot be placed within code blocks. They must immediately follow an "@" character in markup. It is recommended that you put them at the top of the page, as in the following example:
2+
3+
@using System.Drawing;
4+
@{
5+
// OK here to use types from System.Drawing in the page.
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Statement block - Gen<None> - 37 - (0:0,0)
2+
MetaCode span - Gen<None> - [{] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1
3+
CSharpSymbolType.LeftBrace;[{];
4+
Code span - Gen<Stmt> - [ using Foo.Bar.Baz; var foo = bar; ] - AutoCompleteEditHandler;Accepts:Any,AutoComplete:[<null>];AtEOL - (1:0,1) - Symbols:19
5+
CSharpSymbolType.WhiteSpace;[ ];
6+
CSharpSymbolType.Keyword;[using];
7+
CSharpSymbolType.WhiteSpace;[ ];
8+
CSharpSymbolType.Identifier;[Foo];
9+
CSharpSymbolType.Dot;[.];
10+
CSharpSymbolType.Identifier;[Bar];
11+
CSharpSymbolType.Dot;[.];
12+
CSharpSymbolType.Identifier;[Baz];
13+
CSharpSymbolType.Semicolon;[;];
14+
CSharpSymbolType.WhiteSpace;[ ];
15+
CSharpSymbolType.Identifier;[var];
16+
CSharpSymbolType.WhiteSpace;[ ];
17+
CSharpSymbolType.Identifier;[foo];
18+
CSharpSymbolType.WhiteSpace;[ ];
19+
CSharpSymbolType.Assign;[=];
20+
CSharpSymbolType.WhiteSpace;[ ];
21+
CSharpSymbolType.Identifier;[bar];
22+
CSharpSymbolType.Semicolon;[;];
23+
CSharpSymbolType.WhiteSpace;[ ];
24+
MetaCode span - Gen<None> - [}] - SpanEditHandler;Accepts:None - (36:0,36) - Symbols:1
25+
CSharpSymbolType.RightBrace;[}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Statement block - Gen<None> - 56 - (0:0,0)
2+
MetaCode span - Gen<None> - [{] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1
3+
CSharpSymbolType.LeftBrace;[{];
4+
Code span - Gen<Stmt> - [LF List<dynamic> photos = gallery.Photo.ToList();LF] - AutoCompleteEditHandler;Accepts:Any,AutoComplete:[<null>];AtEOL - (1:0,1) - Symbols:20
5+
CSharpSymbolType.NewLine;[LF];
6+
CSharpSymbolType.WhiteSpace;[ ];
7+
CSharpSymbolType.Identifier;[List];
8+
CSharpSymbolType.LessThan;[<];
9+
CSharpSymbolType.Identifier;[dynamic];
10+
CSharpSymbolType.GreaterThan;[>];
11+
CSharpSymbolType.WhiteSpace;[ ];
12+
CSharpSymbolType.Identifier;[photos];
13+
CSharpSymbolType.WhiteSpace;[ ];
14+
CSharpSymbolType.Assign;[=];
15+
CSharpSymbolType.WhiteSpace;[ ];
16+
CSharpSymbolType.Identifier;[gallery];
17+
CSharpSymbolType.Dot;[.];
18+
CSharpSymbolType.Identifier;[Photo];
19+
CSharpSymbolType.Dot;[.];
20+
CSharpSymbolType.Identifier;[ToList];
21+
CSharpSymbolType.LeftParenthesis;[(];
22+
CSharpSymbolType.RightParenthesis;[)];
23+
CSharpSymbolType.Semicolon;[;];
24+
CSharpSymbolType.NewLine;[LF];
25+
MetaCode span - Gen<None> - [}] - SpanEditHandler;Accepts:None - (55:2,0) - Symbols:1
26+
CSharpSymbolType.RightBrace;[}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Statement block - Gen<None> - 52 - (0:0,0)
2+
MetaCode span - Gen<None> - [{] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1
3+
CSharpSymbolType.LeftBrace;[{];
4+
Code span - Gen<Stmt> - [foo"b}ar" if(condition) { string.Format("{0}"); } ] - AutoCompleteEditHandler;Accepts:Any,AutoComplete:[<null>];AtEOL - (1:0,1) - Symbols:20
5+
CSharpSymbolType.Identifier;[foo];
6+
CSharpSymbolType.StringLiteral;["b}ar"];
7+
CSharpSymbolType.WhiteSpace;[ ];
8+
CSharpSymbolType.Keyword;[if];
9+
CSharpSymbolType.LeftParenthesis;[(];
10+
CSharpSymbolType.Identifier;[condition];
11+
CSharpSymbolType.RightParenthesis;[)];
12+
CSharpSymbolType.WhiteSpace;[ ];
13+
CSharpSymbolType.LeftBrace;[{];
14+
CSharpSymbolType.WhiteSpace;[ ];
15+
CSharpSymbolType.Keyword;[string];
16+
CSharpSymbolType.Dot;[.];
17+
CSharpSymbolType.Identifier;[Format];
18+
CSharpSymbolType.LeftParenthesis;[(];
19+
CSharpSymbolType.StringLiteral;["{0}"];
20+
CSharpSymbolType.RightParenthesis;[)];
21+
CSharpSymbolType.Semicolon;[;];
22+
CSharpSymbolType.WhiteSpace;[ ];
23+
CSharpSymbolType.RightBrace;[}];
24+
CSharpSymbolType.WhiteSpace;[ ];
25+
MetaCode span - Gen<None> - [}] - SpanEditHandler;Accepts:None - (51:0,51) - Symbols:1
26+
CSharpSymbolType.RightBrace;[}];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Expression block - Gen<Expr> - 52 - (0:0,0)
2+
MetaCode span - Gen<None> - [(] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1
3+
CSharpSymbolType.LeftParenthesis;[(];
4+
Code span - Gen<Expr> - [foo"b)ar" if(condition) { string.Format("{0}"); } ] - SpanEditHandler;Accepts:Any - (1:0,1) - Symbols:20
5+
CSharpSymbolType.Identifier;[foo];
6+
CSharpSymbolType.StringLiteral;["b)ar"];
7+
CSharpSymbolType.WhiteSpace;[ ];
8+
CSharpSymbolType.Keyword;[if];
9+
CSharpSymbolType.LeftParenthesis;[(];
10+
CSharpSymbolType.Identifier;[condition];
11+
CSharpSymbolType.RightParenthesis;[)];
12+
CSharpSymbolType.WhiteSpace;[ ];
13+
CSharpSymbolType.LeftBrace;[{];
14+
CSharpSymbolType.WhiteSpace;[ ];
15+
CSharpSymbolType.Keyword;[string];
16+
CSharpSymbolType.Dot;[.];
17+
CSharpSymbolType.Identifier;[Format];
18+
CSharpSymbolType.LeftParenthesis;[(];
19+
CSharpSymbolType.StringLiteral;["{0}"];
20+
CSharpSymbolType.RightParenthesis;[)];
21+
CSharpSymbolType.Semicolon;[;];
22+
CSharpSymbolType.WhiteSpace;[ ];
23+
CSharpSymbolType.RightBrace;[}];
24+
CSharpSymbolType.WhiteSpace;[ ];
25+
MetaCode span - Gen<None> - [)] - SpanEditHandler;Accepts:None - (51:0,51) - Symbols:1
26+
CSharpSymbolType.RightParenthesis;[)];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(1,2): Error RZ1005: "/" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Expression block - Gen<Expr> - 1 - (0:0,0)
2+
Transition span - Gen<None> - [@] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1
3+
CSharpSymbolType.Transition;[@];
4+
Code span - Gen<Expr> - [] - ImplicitExpressionEditHandler;Accepts:NonWhiteSpace;ImplicitExpression[RTD];K14 - (1:0,1) - Symbols:1
5+
CSharpSymbolType.Unknown;[];

0 commit comments

Comments
 (0)