forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVBBlockTest.cs
More file actions
367 lines (345 loc) · 17.7 KB
/
Copy pathVBBlockTest.cs
File metadata and controls
367 lines (345 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Resources;
using System.Web.Razor.Test.Framework;
using System.Web.Razor.Text;
using Microsoft.TestCommon;
namespace System.Web.Razor.Test.Parser.VB
{
public class VBBlockTest : VBHtmlCodeParserTestBase
{
[Fact]
public void ParseBlockMethodThrowsArgNullExceptionOnNullContext()
{
// Arrange
VBCodeParser parser = new VBCodeParser();
// Act and Assert
Assert.Throws<InvalidOperationException>(() => parser.ParseBlock(), RazorResources.Parser_Context_Not_Set);
}
[Fact]
public void ParseBlockAcceptsImplicitExpression()
{
ParseBlockTest("If True Then" + Environment.NewLine
+ " @foo" + Environment.NewLine
+ "End If",
new StatementBlock(
Factory.Code("If True Then\r\n ").AsStatement(),
new ExpressionBlock(
Factory.CodeTransition(),
Factory.Code("foo")
.AsImplicitExpression(VBCodeParser.DefaultKeywords, acceptTrailingDot: true)
.Accepts(AcceptedCharacters.NonWhiteSpace)),
Factory.Code("\r\nEnd If")
.AsStatement()
.Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockAcceptsIfStatementWithinCodeBlockIfInDesignTimeMode()
{
ParseBlockTest("If True Then" + Environment.NewLine
+ " @If True Then" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End If",
new StatementBlock(
Factory.Code("If True Then\r\n ").AsStatement(),
new StatementBlock(
Factory.CodeTransition(),
Factory.Code("If True Then\r\n End If\r\n")
.AsStatement()
.Accepts(AcceptedCharacters.None)),
Factory.Code(@"End If")
.AsStatement()
.Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockSupportsSpacesInStrings()
{
ParseBlockTest("for each p in db.Query(\"SELECT * FROM PRODUCTS\")" + Environment.NewLine
+ " @<p>@p.Name</p>" + Environment.NewLine
+ "next",
new StatementBlock(
Factory.Code("for each p in db.Query(\"SELECT * FROM PRODUCTS\")\r\n")
.AsStatement(),
new MarkupBlock(
Factory.Markup(" "),
Factory.MarkupTransition(),
Factory.Markup("<p>"),
new ExpressionBlock(
Factory.CodeTransition(),
Factory.Code("p.Name")
.AsImplicitExpression(VBCodeParser.DefaultKeywords)
.Accepts(AcceptedCharacters.NonWhiteSpace)),
Factory.Markup("</p>\r\n").Accepts(AcceptedCharacters.None)),
Factory.Code("next")
.AsStatement()
.Accepts(AcceptedCharacters.WhiteSpace | AcceptedCharacters.NonWhiteSpace)));
}
[Fact]
public void ParseBlockSupportsSimpleCodeBlock()
{
ParseBlockTest("Code" + Environment.NewLine
+ " If foo IsNot Nothing" + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End Code",
new StatementBlock(
Factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n If foo IsNot Nothing\r\n Bar(foo)\r\n End If\r\n")
.AsStatement(),
Factory.MetaCode("End Code").Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockRejectsNewlineBetweenEndAndCodeIfNotPrefixedWithUnderscore()
{
ParseBlockTest("Code" + Environment.NewLine
+ " If foo IsNot Nothing" + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End" + Environment.NewLine
+ "Code",
new StatementBlock(
Factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n If foo IsNot Nothing\r\n Bar(foo)\r\n End If\r\nEnd\r\nCode")
.AsStatement()),
new RazorError(
String.Format(RazorResources.ParseError_BlockNotTerminated, "Code", "End Code"),
SourceLocation.Zero));
}
[Fact]
public void ParseBlockAcceptsNewlineBetweenEndAndCodeIfPrefixedWithUnderscore()
{
ParseBlockTest("Code" + Environment.NewLine
+ " If foo IsNot Nothing" + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End _" + Environment.NewLine
+ "_" + Environment.NewLine
+ " _" + Environment.NewLine
+ "Code",
new StatementBlock(
Factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n If foo IsNot Nothing\r\n Bar(foo)\r\n End If\r\n")
.AsStatement(),
Factory.MetaCode("End _\r\n_\r\n _\r\nCode").Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockSupportsSimpleFunctionsBlock()
{
ParseBlockTest("Functions" + Environment.NewLine
+ " Public Sub Foo()" + Environment.NewLine
+ " Bar()" + Environment.NewLine
+ " End Sub" + Environment.NewLine
+ Environment.NewLine
+ " Private Function Bar() As Object" + Environment.NewLine
+ " Return Nothing" + Environment.NewLine
+ " End Function" + Environment.NewLine
+ "End Functions",
new FunctionsBlock(
Factory.MetaCode("Functions").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n Public Sub Foo()\r\n Bar()\r\n End Sub\r\n\r\n Private Function Bar() As Object\r\n Return Nothing\r\n End Function\r\n")
.AsFunctionsBody(),
Factory.MetaCode("End Functions").Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockRejectsNewlineBetweenEndAndFunctionsIfNotPrefixedWithUnderscore()
{
ParseBlockTest("Functions" + Environment.NewLine
+ " If foo IsNot Nothing" + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End" + Environment.NewLine
+ "Functions",
new FunctionsBlock(
Factory.MetaCode("Functions").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n If foo IsNot Nothing\r\n Bar(foo)\r\n End If\r\nEnd\r\nFunctions")
.AsFunctionsBody()),
new RazorError(
String.Format(RazorResources.ParseError_BlockNotTerminated, "Functions", "End Functions"),
SourceLocation.Zero));
}
[Fact]
public void ParseBlockAcceptsNewlineBetweenEndAndFunctionsIfPrefixedWithUnderscore()
{
ParseBlockTest("Functions" + Environment.NewLine
+ " If foo IsNot Nothing" + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " End If" + Environment.NewLine
+ "End _" + Environment.NewLine
+ "_" + Environment.NewLine
+ " _" + Environment.NewLine
+ "Functions",
new FunctionsBlock(
Factory.MetaCode("Functions").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n If foo IsNot Nothing\r\n Bar(foo)\r\n End If\r\n")
.AsFunctionsBody(),
Factory.MetaCode("End _\r\n_\r\n _\r\nFunctions").Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockCorrectlyHandlesExtraEndsInEndCode()
{
ParseBlockTest("Code" + Environment.NewLine
+ " Bar End" + Environment.NewLine
+ "End Code",
new StatementBlock(
Factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n Bar End\r\n").AsStatement(),
Factory.MetaCode("End Code").Accepts(AcceptedCharacters.None)));
}
[Fact]
public void ParseBlockCorrectlyHandlesExtraEndsInEndFunctions()
{
ParseBlockTest("Functions" + Environment.NewLine
+ " Bar End" + Environment.NewLine
+ "End Functions",
new FunctionsBlock(
Factory.MetaCode("Functions").Accepts(AcceptedCharacters.None),
Factory.Code("\r\n Bar End\r\n").AsFunctionsBody().AutoCompleteWith(null, atEndOfSpan: false),
Factory.MetaCode("End Functions").Accepts(AcceptedCharacters.None)));
}
[Theory]
[InlineData("If", "End", "If")]
[InlineData("Try", "End", "Try")]
[InlineData("While", "End", "While")]
[InlineData("Using", "End", "Using")]
[InlineData("With", "End", "With")]
public void KeywordAllowsNewlinesIfPrefixedByUnderscore(string startKeyword, string endKeyword1, string endKeyword2)
{
string code = startKeyword + Environment.NewLine
+ " ' In the block" + Environment.NewLine
+ endKeyword1 + " _" + Environment.NewLine
+ "_" + Environment.NewLine
+ "_" + Environment.NewLine
+ "_" + Environment.NewLine
+ "_" + Environment.NewLine
+ "_" + Environment.NewLine
+ " " + endKeyword2 + Environment.NewLine;
ParseBlockTest(code + "foo bar baz",
new StatementBlock(
Factory.Code(code)
.AsStatement()
.Accepts(AcceptedCharacters.None)));
}
[Theory]
[InlineData("While", "EndWhile", "End While")]
[InlineData("If", "EndIf", "End If")]
[InlineData("Select", "EndSelect", "End Select")]
[InlineData("Try", "EndTry", "End Try")]
[InlineData("With", "EndWith", "End With")]
[InlineData("Using", "EndUsing", "End Using")]
public void EndTerminatedKeywordRequiresSpaceBetweenEndAndKeyword(string startKeyword, string wrongEndKeyword, string endKeyword)
{
string code = startKeyword + Environment.NewLine
+ " ' This should not end the code" + Environment.NewLine
+ " " + wrongEndKeyword + Environment.NewLine
+ " ' But this should" + Environment.NewLine
+ endKeyword;
ParseBlockTest(code,
new StatementBlock(
Factory.Code(code)
.AsStatement()
.Accepts(AcceptedCharacters.None)));
}
[Theory]
[InlineData("While", "End While", false)]
[InlineData("Do", "Loop", true)]
[InlineData("If", "End If", false)]
[InlineData("Select", "End Select", false)]
[InlineData("For", "Next", true)]
[InlineData("Try", "End Try", false)]
[InlineData("With", "End With", false)]
[InlineData("Using", "End Using", false)]
public void EndSequenceInString(string keyword, string endSequence, bool acceptToEndOfLine)
{
string code = keyword + Environment.NewLine
+ " \"" + endSequence + "\"" + Environment.NewLine
+ endSequence + (acceptToEndOfLine ? " foo bar baz" : "") + Environment.NewLine;
ParseBlockTest(code + "biz boz",
new StatementBlock(
Factory.Code(code).AsStatement().Accepts(GetAcceptedCharacters(acceptToEndOfLine))));
}
[Theory]
[InlineData("While", "End While", false)]
[InlineData("Do", "Loop", true)]
[InlineData("If", "End If", false)]
[InlineData("Select", "End Select", false)]
[InlineData("For", "Next", true)]
[InlineData("Try", "End Try", false)]
[InlineData("With", "End With", false)]
[InlineData("Using", "End Using", false)]
private void CommentedEndSequence(string keyword, string endSequence, bool acceptToEndOfLine)
{
string code = keyword + Environment.NewLine
+ " '" + endSequence + Environment.NewLine
+ endSequence + (acceptToEndOfLine ? @" foo bar baz" : "") + Environment.NewLine;
ParseBlockTest(code + "biz boz",
new StatementBlock(
Factory.Code(code).AsStatement().Accepts(GetAcceptedCharacters(acceptToEndOfLine))));
}
[Theory]
[InlineData("While", "End While", false)]
[InlineData("Do", "Loop", true)]
[InlineData("If", "End If", false)]
[InlineData("Select", "End Select", false)]
[InlineData("For", "Next", true)]
[InlineData("Try", "End Try", false)]
[InlineData("With", "End With", false)]
[InlineData("SyncLock", "End SyncLock", false)]
[InlineData("Using", "End Using", false)]
private void NestedKeywordBlock(string keyword, string endSequence, bool acceptToEndOfLine)
{
string code = keyword + Environment.NewLine
+ " " + keyword + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ " " + endSequence + Environment.NewLine
+ endSequence + (acceptToEndOfLine ? " foo bar baz" : "") + Environment.NewLine;
ParseBlockTest(code + "biz boz",
new StatementBlock(
Factory.Code(code).AsStatement().Accepts(GetAcceptedCharacters(acceptToEndOfLine))));
}
[Theory]
[InlineData("While True", "End While", false)]
[InlineData("Do", "Loop", true)]
[InlineData("If foo IsNot Nothing", "End If", false)]
[InlineData("Select Case foo", "End Select", false)]
[InlineData("For Each p in Products", "Next", true)]
[InlineData("Try", "End Try", false)]
[InlineData("With", "End With", false)]
[InlineData("SyncLock", "End SyncLock", false)]
[InlineData("Using", "End Using", false)]
private void SimpleKeywordBlock(string keyword, string endSequence, bool acceptToEndOfLine)
{
string code = keyword + Environment.NewLine
+ " Bar(foo)" + Environment.NewLine
+ endSequence + (acceptToEndOfLine ? " foo bar baz" : "") + Environment.NewLine;
ParseBlockTest(code + "biz boz",
new StatementBlock(
Factory.Code(code).AsStatement().Accepts(GetAcceptedCharacters(acceptToEndOfLine))));
}
[Theory]
[InlineData("While True", "Exit While", "End While", false)]
[InlineData("Do", "Exit Do", "Loop", true)]
[InlineData("For Each p in Products", "Exit For", "Next", true)]
[InlineData("While True", "Continue While", "End While", false)]
[InlineData("Do", "Continue Do", "Loop", true)]
[InlineData("For Each p in Products", "Continue For", "Next", true)]
private void KeywordWithExitOrContinue(string startKeyword, string exitKeyword, string endKeyword, bool acceptToEndOfLine)
{
string code = startKeyword + Environment.NewLine
+ " ' This is before the exit" + Environment.NewLine
+ " " + exitKeyword + Environment.NewLine
+ " ' This is after the exit" + Environment.NewLine
+ endKeyword + Environment.NewLine;
ParseBlockTest(code + "foo bar baz",
new StatementBlock(
Factory.Code(code).AsStatement().Accepts(GetAcceptedCharacters(acceptToEndOfLine))));
}
private AcceptedCharacters GetAcceptedCharacters(bool acceptToEndOfLine)
{
return acceptToEndOfLine ?
AcceptedCharacters.WhiteSpace | AcceptedCharacters.NonWhiteSpace :
AcceptedCharacters.None;
}
}
}