Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,157 @@ public void CommentOnSameLineAsHtml()
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
0 => "value",
_ => "no value"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithLessThan()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
< 9 => "less than 10"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithGreaterThan()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
> 10 => "greater than 10"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithMultipleComparisons()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
< 9 => "less than 10",
10 => "equal to 10",
> 10 => "greater than 10"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_Incomplete()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
0 => "value"

var val2 = "value2";
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithLessThan_Incomplete()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
< 9 => "less than 10"

var val2 = "value2";
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithWrongKeyword()
{
ParseDocumentTest("""
@{
var val = 0 using
{
0 => "value"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithWrongKeyword_AndLessThan()
{
ParseDocumentTest("""
@{
var val = 0 using
{
< 9 => "less than 10"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithMarkupInside()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
0 => <span>some <i>html</i></span>,
_ => "value"
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithMarkupInside_ViaAtSymbol()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
0 => @<span>zero</span>,
_ => @<span>one</span>
};
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithMarkupInside_WithLessThan()
{
ParseDocumentTest("""
@{
var val = 0 switch
{
< 10 => @<span>less than 10</span>,
_ => @<span>other</span>
};
}
""");
}

private void RunRazorCommentBetweenClausesTest(string preComment, string postComment, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.Any)
{
ParseDocumentTest(preComment + "@* Foo *@ @* Bar *@" + postComment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Language.Legacy;
Expand Down Expand Up @@ -76,4 +77,50 @@ public void ShouldAcceptConsecutiveEscapedQuotesInVerbatimStrings()
{
ParseDocumentTest("@(@\"\"\"\"\"\")");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression()
{
ParseDocumentTest("""
<span>@(value switch{
10 => "ten",
_ => "other"
})</span>

@code{
public int value = 10;
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithLessThan()
{
ParseDocumentTest("""
<span>@(value switch{
< 10 => "less than",
10 => "ten",
_ => "other"
})</span>

@code{
public int value = 10;
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")]
public void SwitchExpression_WithHtml()
{
ParseDocumentTest("""
<span>@(value switch{
10 => <span>ten</span>,
_ => "other"
})</span>

@code{
public int value = 10;
}
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [92] )
Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [92] )
MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [92] )
Code span at (2:0,2 [89] ) - Parent: Statement block at (0:0,0 [92] )
MetaCode span at (91:6,0 [1] ) - Parent: Statement block at (0:0,0 [92] )
Markup span at (92:6,1 [0] ) - Parent: Markup block at (0:0,0 [92] )
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
RazorDocument - [0..92)::92 - [@{LF var val = 0 switchLF {LF 0 => "value",LF _ => "no value"LF };LF}]
MarkupBlock - [0..92)::92
MarkupTextLiteral - [0..0)::0 - [] - Gen<Markup>
Marker;[];
CSharpCodeBlock - [0..92)::92
CSharpStatement - [0..92)::92
CSharpTransition - [0..1)::1 - Gen<None>
Transition;[@];
CSharpStatementBody - [1..92)::91
RazorMetaCode - [1..2)::1 - Gen<None>
LeftBrace;[{];
CSharpCodeBlock - [2..91)::89
CSharpStatementLiteral - [2..91)::89 - [LF var val = 0 switchLF {LF 0 => "value",LF _ => "no value"LF };LF] - Gen<Stmt>
NewLine;[LF];
Whitespace;[ ];
Keyword;[var];
Whitespace;[ ];
Identifier;[val];
Whitespace;[ ];
Assign;[=];
Whitespace;[ ];
NumericLiteral;[0];
Whitespace;[ ];
Keyword;[switch];
NewLine;[LF];
Whitespace;[ ];
LeftBrace;[{];
NewLine;[LF];
Whitespace;[ ];
NumericLiteral;[0];
Whitespace;[ ];
CSharpOperator;[=>];
Whitespace;[ ];
StringLiteral;["value"];
Comma;[,];
NewLine;[LF];
Whitespace;[ ];
Keyword;[_];
Whitespace;[ ];
CSharpOperator;[=>];
Whitespace;[ ];
StringLiteral;["no value"];
NewLine;[LF];
Whitespace;[ ];
RightBrace;[}];
Semicolon;[;];
NewLine;[LF];
RazorMetaCode - [91..92)::1 - Gen<None>
RightBrace;[}];
MarkupTextLiteral - [92..92)::0 - [] - Gen<Markup>
Marker;[];
EndOfFile;[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [86] )
Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [86] )
MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [86] )
Code span at (2:0,2 [84] ) - Parent: Statement block at (0:0,0 [86] )
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
RazorDocument - [0..86)::86 - [@{LF var val = 0 switchLF {LF 0 => "value"LFLF var val2 = "value2";LF}]
MarkupBlock - [0..86)::86
MarkupTextLiteral - [0..0)::0 - [] - Gen<Markup>
Marker;[];
CSharpCodeBlock - [0..86)::86
CSharpStatement - [0..86)::86
CSharpTransition - [0..1)::1 - Gen<None>
Transition;[@];
CSharpStatementBody - [1..86)::85
RazorMetaCode - [1..2)::1 - Gen<None>
LeftBrace;[{];
CSharpCodeBlock - [2..86)::84
CSharpStatementLiteral - [2..86)::84 - [LF var val = 0 switchLF {LF 0 => "value"LFLF var val2 = "value2";LF}] - Gen<Stmt>
NewLine;[LF];
Whitespace;[ ];
Keyword;[var];
Whitespace;[ ];
Identifier;[val];
Whitespace;[ ];
Assign;[=];
Whitespace;[ ];
NumericLiteral;[0];
Whitespace;[ ];
Keyword;[switch];
NewLine;[LF];
Whitespace;[ ];
LeftBrace;[{];
NewLine;[LF];
Whitespace;[ ];
NumericLiteral;[0];
Whitespace;[ ];
CSharpOperator;[=>];
Whitespace;[ ];
StringLiteral;["value"];
NewLine;[LF];
NewLine;[LF];
Whitespace;[ ];
Keyword;[var];
Whitespace;[ ];
Identifier;[val2];
Whitespace;[ ];
Assign;[=];
Whitespace;[ ];
StringLiteral;["value2"];
Semicolon;[;];
NewLine;[LF];
RightBrace;[}];
RazorMetaCode - [86..86)::0 - Gen<None>
RightBrace;[<Missing>];
EndOfFile;[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [79] )
Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [79] )
MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [79] )
Code span at (2:0,2 [76] ) - Parent: Statement block at (0:0,0 [79] )
MetaCode span at (78:5,0 [1] ) - Parent: Statement block at (0:0,0 [79] )
Markup span at (79:5,1 [0] ) - Parent: Markup block at (0:0,0 [79] )
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
RazorDocument - [0..79)::79 - [@{LF var val = 0 switchLF {LF > 10 => "greater than 10"LF };LF}]
MarkupBlock - [0..79)::79
MarkupTextLiteral - [0..0)::0 - [] - Gen<Markup>
Marker;[];
CSharpCodeBlock - [0..79)::79
CSharpStatement - [0..79)::79
CSharpTransition - [0..1)::1 - Gen<None>
Transition;[@];
CSharpStatementBody - [1..79)::78
RazorMetaCode - [1..2)::1 - Gen<None>
LeftBrace;[{];
CSharpCodeBlock - [2..78)::76
CSharpStatementLiteral - [2..78)::76 - [LF var val = 0 switchLF {LF > 10 => "greater than 10"LF };LF] - Gen<Stmt>
NewLine;[LF];
Whitespace;[ ];
Keyword;[var];
Whitespace;[ ];
Identifier;[val];
Whitespace;[ ];
Assign;[=];
Whitespace;[ ];
NumericLiteral;[0];
Whitespace;[ ];
Keyword;[switch];
NewLine;[LF];
Whitespace;[ ];
LeftBrace;[{];
NewLine;[LF];
Whitespace;[ ];
GreaterThan;[>];
Whitespace;[ ];
NumericLiteral;[10];
Whitespace;[ ];
CSharpOperator;[=>];
Whitespace;[ ];
StringLiteral;["greater than 10"];
NewLine;[LF];
Whitespace;[ ];
RightBrace;[}];
Semicolon;[;];
NewLine;[LF];
RazorMetaCode - [78..79)::1 - Gen<None>
RightBrace;[}];
MarkupTextLiteral - [79..79)::0 - [] - Gen<Markup>
Marker;[];
EndOfFile;[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [75] )
Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [75] )
MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [75] )
Code span at (2:0,2 [72] ) - Parent: Statement block at (0:0,0 [75] )
MetaCode span at (74:5,0 [1] ) - Parent: Statement block at (0:0,0 [75] )
Markup span at (75:5,1 [0] ) - Parent: Markup block at (0:0,0 [75] )
Loading