PageRenderTime 138ms CodeModel.GetById 21ms RepoModel.GetById 9ms app.codeStats 0ms

/test/System.Web.Razor.Test/Parser/CSharp/CSharpErrorTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 604 lines | 599 code | 5 blank | 0 comment | 43 complexity | da72cd519643f0703101c43cfff1f900 MD5 | raw file
  1. using System.Web.Razor.Parser;
  2. using System.Web.Razor.Parser.SyntaxTree;
  3. using System.Web.Razor.Resources;
  4. using System.Web.Razor.Test.Framework;
  5. using System.Web.Razor.Text;
  6. using Xunit;
  7. namespace System.Web.Razor.Test.Parser.CSharp
  8. {
  9. public class CSharpErrorTest : CsHtmlCodeParserTestBase
  10. {
  11. [Fact]
  12. public void ParseBlockHandlesQuotesAfterTransition()
  13. {
  14. ParseBlockTest("@\"",
  15. new ExpressionBlock(
  16. Factory.CodeTransition(),
  17. Factory.EmptyCSharp()
  18. .AsImplicitExpression(KeywordSet)
  19. .Accepts(AcceptedCharacters.NonWhiteSpace)
  20. ),
  21. new RazorError(
  22. String.Format(RazorResources.ParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS, '"'),
  23. 1, 0, 1));
  24. }
  25. [Fact]
  26. public void ParseBlockCapturesWhitespaceToEndOfLineInInvalidUsingStatementAndTreatsAsFileCode()
  27. {
  28. ParseBlockTest(@"using
  29. ",
  30. new StatementBlock(
  31. Factory.Code("using \r\n").AsStatement()
  32. ));
  33. }
  34. [Fact]
  35. public void ParseBlockMethodOutputsOpenCurlyAsCodeSpanIfEofFoundAfterOpenCurlyBrace()
  36. {
  37. ParseBlockTest("{",
  38. new StatementBlock(
  39. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  40. Factory.EmptyCSharp()
  41. .AsStatement()
  42. .With(new AutoCompleteEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString) { AutoCompleteString = "}" })
  43. ),
  44. new RazorError(
  45. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  46. RazorResources.BlockName_Code,
  47. "}", "{"),
  48. SourceLocation.Zero));
  49. }
  50. [Fact]
  51. public void ParseBlockMethodOutputsZeroLengthCodeSpanIfStatementBlockEmpty()
  52. {
  53. ParseBlockTest("{}",
  54. new StatementBlock(
  55. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  56. Factory.EmptyCSharp().AsStatement(),
  57. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)
  58. ));
  59. }
  60. [Fact]
  61. public void ParseBlockMethodProducesErrorIfNewlineFollowsTransition()
  62. {
  63. ParseBlockTest(@"@
  64. ",
  65. new ExpressionBlock(
  66. Factory.CodeTransition(),
  67. Factory.EmptyCSharp()
  68. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  69. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  70. new RazorError(RazorResources.ParseError_Unexpected_WhiteSpace_At_Start_Of_CodeBlock_CS, new SourceLocation(1, 0, 1)));
  71. }
  72. [Fact]
  73. public void ParseBlockMethodProducesErrorIfWhitespaceBetweenTransitionAndBlockStartInEmbeddedExpression()
  74. {
  75. ParseBlockTest(@"{
  76. @ {}
  77. }",
  78. new StatementBlock(
  79. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  80. Factory.Code("\r\n ").AsStatement(),
  81. new ExpressionBlock(
  82. Factory.CodeTransition(),
  83. Factory.EmptyCSharp()
  84. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: true)
  85. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  86. Factory.Code(" {}\r\n").AsStatement(),
  87. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)
  88. ),
  89. new RazorError(RazorResources.ParseError_Unexpected_WhiteSpace_At_Start_Of_CodeBlock_CS, 8, 1, 5));
  90. }
  91. [Fact]
  92. public void ParseBlockMethodProducesErrorIfEOFAfterTransitionInEmbeddedExpression()
  93. {
  94. ParseBlockTest(@"{
  95. @",
  96. new StatementBlock(
  97. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  98. Factory.Code("\r\n ").AsStatement(),
  99. new ExpressionBlock(
  100. Factory.CodeTransition(),
  101. Factory.EmptyCSharp()
  102. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: true)
  103. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  104. Factory.EmptyCSharp().AsStatement()
  105. ),
  106. new RazorError(RazorResources.ParseError_Unexpected_EndOfFile_At_Start_Of_CodeBlock, 8, 1, 5),
  107. new RazorError(
  108. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF, RazorResources.BlockName_Code, "}", "{"),
  109. SourceLocation.Zero));
  110. }
  111. [Fact]
  112. public void ParseBlockMethodParsesNothingIfFirstCharacterIsNotIdentifierStartOrParenOrBrace()
  113. {
  114. ParseBlockTest("@!!!",
  115. new ExpressionBlock(
  116. Factory.CodeTransition(),
  117. Factory.EmptyCSharp()
  118. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  119. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  120. new RazorError(
  121. String.Format(RazorResources.ParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS, "!"),
  122. 1, 0, 1));
  123. }
  124. [Fact]
  125. public void ParseBlockShouldReportErrorAndTerminateAtEOFIfIfParenInExplicitExpressionUnclosed()
  126. {
  127. ParseBlockTest(@"(foo bar
  128. baz",
  129. new ExpressionBlock(
  130. Factory.MetaCode("(").Accepts(AcceptedCharacters.None),
  131. Factory.Code("foo bar\r\nbaz").AsExpression()
  132. ),
  133. new RazorError(
  134. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  135. RazorResources.BlockName_ExplicitExpression, ')', '('),
  136. new SourceLocation(0, 0, 0)));
  137. }
  138. [Fact]
  139. public void ParseBlockShouldReportErrorAndTerminateAtMarkupIfIfParenInExplicitExpressionUnclosed()
  140. {
  141. ParseBlockTest(@"(foo bar
  142. <html>
  143. baz
  144. </html",
  145. new ExpressionBlock(
  146. Factory.MetaCode("(").Accepts(AcceptedCharacters.None),
  147. Factory.Code("foo bar\r\n").AsExpression()
  148. ),
  149. new RazorError(
  150. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  151. RazorResources.BlockName_ExplicitExpression, ')', '('),
  152. new SourceLocation(0, 0, 0)));
  153. }
  154. [Fact]
  155. public void ParseBlockCorrectlyHandlesInCorrectTransitionsIfImplicitExpressionParensUnclosed()
  156. {
  157. ParseBlockTest(@"Href(
  158. <h1>@Html.Foo(Bar);</h1>
  159. ",
  160. new ExpressionBlock(
  161. Factory.Code("Href(\r\n")
  162. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  163. ),
  164. new RazorError(
  165. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  166. "(", ")"),
  167. new SourceLocation(4, 0, 4)));
  168. }
  169. [Fact]
  170. // Test for fix to Dev10 884975 - Incorrect Error Messaging
  171. public void ParseBlockShouldReportErrorAndTerminateAtEOFIfParenInImplicitExpressionUnclosed()
  172. {
  173. ParseBlockTest(@"Foo(Bar(Baz)
  174. Biz
  175. Boz",
  176. new ExpressionBlock(
  177. Factory.Code("Foo(Bar(Baz)\r\nBiz\r\nBoz")
  178. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  179. ),
  180. new RazorError(String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  181. "(", ")"),
  182. new SourceLocation(3, 0, 3)));
  183. }
  184. [Fact]
  185. // Test for fix to Dev10 884975 - Incorrect Error Messaging
  186. public void ParseBlockShouldReportErrorAndTerminateAtMarkupIfParenInImplicitExpressionUnclosed()
  187. {
  188. ParseBlockTest(@"Foo(Bar(Baz)
  189. Biz
  190. <html>
  191. Boz
  192. </html>",
  193. new ExpressionBlock(
  194. Factory.Code("Foo(Bar(Baz)\r\nBiz\r\n")
  195. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  196. ),
  197. new RazorError(String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  198. "(", ")"),
  199. new SourceLocation(3, 0, 3)));
  200. }
  201. [Fact]
  202. // Test for fix to Dev10 884975 - Incorrect Error Messaging
  203. public void ParseBlockShouldReportErrorAndTerminateAtEOFIfBracketInImplicitExpressionUnclosed()
  204. {
  205. ParseBlockTest(@"Foo[Bar[Baz]
  206. Biz
  207. Boz",
  208. new ExpressionBlock(
  209. Factory.Code("Foo[Bar[Baz]\r\nBiz\r\nBoz")
  210. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  211. ),
  212. new RazorError(
  213. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  214. "[", "]"),
  215. new SourceLocation(3, 0, 3)));
  216. }
  217. [Fact]
  218. // Test for fix to Dev10 884975 - Incorrect Error Messaging
  219. public void ParseBlockShouldReportErrorAndTerminateAtMarkupIfBracketInImplicitExpressionUnclosed()
  220. {
  221. ParseBlockTest(@"Foo[Bar[Baz]
  222. Biz
  223. <b>
  224. Boz
  225. </b>",
  226. new ExpressionBlock(
  227. Factory.Code("Foo[Bar[Baz]\r\nBiz\r\n")
  228. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  229. ),
  230. new RazorError(
  231. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  232. "[", "]"),
  233. new SourceLocation(3, 0, 3)));
  234. }
  235. // Simple EOF handling errors:
  236. [Fact]
  237. public void ParseBlockReportsErrorIfExplicitCodeBlockUnterminatedAtEOF()
  238. {
  239. ParseBlockTest("{ var foo = bar; if(foo != null) { bar(); } ",
  240. new StatementBlock(
  241. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  242. Factory.Code(" var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  243. ),
  244. new RazorError(
  245. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  246. RazorResources.BlockName_Code, '}', '{'),
  247. SourceLocation.Zero));
  248. }
  249. [Fact]
  250. public void ParseBlockReportsErrorIfClassBlockUnterminatedAtEOF()
  251. {
  252. ParseBlockTest("functions { var foo = bar; if(foo != null) { bar(); } ",
  253. new FunctionsBlock(
  254. Factory.MetaCode("functions {").Accepts(AcceptedCharacters.None),
  255. Factory.Code(" var foo = bar; if(foo != null) { bar(); } ").AsFunctionsBody()
  256. ),
  257. new RazorError(
  258. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  259. "functions", '}', '{'),
  260. SourceLocation.Zero));
  261. }
  262. [Fact]
  263. public void ParseBlockReportsErrorIfIfBlockUnterminatedAtEOF()
  264. {
  265. RunUnterminatedSimpleKeywordBlock("if");
  266. }
  267. [Fact]
  268. public void ParseBlockReportsErrorIfElseBlockUnterminatedAtEOF()
  269. {
  270. ParseBlockTest("if(foo) { baz(); } else { var foo = bar; if(foo != null) { bar(); } ",
  271. new StatementBlock(
  272. Factory.Code("if(foo) { baz(); } else { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  273. ),
  274. new RazorError(
  275. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  276. "else", '}', '{'),
  277. new SourceLocation(19, 0, 19)));
  278. }
  279. [Fact]
  280. public void ParseBlockReportsErrorIfElseIfBlockUnterminatedAtEOF()
  281. {
  282. ParseBlockTest("if(foo) { baz(); } else if { var foo = bar; if(foo != null) { bar(); } ",
  283. new StatementBlock(
  284. Factory.Code("if(foo) { baz(); } else if { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  285. ),
  286. new RazorError(
  287. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  288. "else if", '}', '{'),
  289. new SourceLocation(19, 0, 19)));
  290. }
  291. [Fact]
  292. public void ParseBlockReportsErrorIfDoBlockUnterminatedAtEOF()
  293. {
  294. ParseBlockTest("do { var foo = bar; if(foo != null) { bar(); } ",
  295. new StatementBlock(
  296. Factory.Code("do { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  297. ),
  298. new RazorError(
  299. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  300. "do", '}', '{'),
  301. SourceLocation.Zero));
  302. }
  303. [Fact]
  304. public void ParseBlockReportsErrorIfTryBlockUnterminatedAtEOF()
  305. {
  306. ParseBlockTest("try { var foo = bar; if(foo != null) { bar(); } ",
  307. new StatementBlock(
  308. Factory.Code("try { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  309. ),
  310. new RazorError(
  311. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  312. "try", '}', '{'),
  313. SourceLocation.Zero));
  314. }
  315. [Fact]
  316. public void ParseBlockReportsErrorIfCatchBlockUnterminatedAtEOF()
  317. {
  318. ParseBlockTest("try { baz(); } catch(Foo) { var foo = bar; if(foo != null) { bar(); } ",
  319. new StatementBlock(
  320. Factory.Code("try { baz(); } catch(Foo) { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  321. ),
  322. new RazorError(
  323. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  324. "catch", '}', '{'),
  325. new SourceLocation(15, 0, 15)));
  326. }
  327. [Fact]
  328. public void ParseBlockReportsErrorIfFinallyBlockUnterminatedAtEOF()
  329. {
  330. ParseBlockTest("try { baz(); } finally { var foo = bar; if(foo != null) { bar(); } ",
  331. new StatementBlock(
  332. Factory.Code("try { baz(); } finally { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
  333. ),
  334. new RazorError(
  335. String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  336. "finally", '}', '{'),
  337. new SourceLocation(15, 0, 15)));
  338. }
  339. [Fact]
  340. public void ParseBlockReportsErrorIfForBlockUnterminatedAtEOF()
  341. {
  342. RunUnterminatedSimpleKeywordBlock("for");
  343. }
  344. [Fact]
  345. public void ParseBlockReportsErrorIfForeachBlockUnterminatedAtEOF()
  346. {
  347. RunUnterminatedSimpleKeywordBlock("foreach");
  348. }
  349. [Fact]
  350. public void ParseBlockReportsErrorIfWhileBlockUnterminatedAtEOF()
  351. {
  352. RunUnterminatedSimpleKeywordBlock("while");
  353. }
  354. [Fact]
  355. public void ParseBlockReportsErrorIfSwitchBlockUnterminatedAtEOF()
  356. {
  357. RunUnterminatedSimpleKeywordBlock("switch");
  358. }
  359. [Fact]
  360. public void ParseBlockReportsErrorIfLockBlockUnterminatedAtEOF()
  361. {
  362. RunUnterminatedSimpleKeywordBlock("lock");
  363. }
  364. [Fact]
  365. public void ParseBlockReportsErrorIfUsingBlockUnterminatedAtEOF()
  366. {
  367. RunUnterminatedSimpleKeywordBlock("using");
  368. }
  369. [Fact]
  370. public void ParseBlockRequiresControlFlowStatementsToHaveBraces()
  371. {
  372. string expectedMessage = String.Format(RazorResources.ParseError_SingleLine_ControlFlowStatements_Not_Allowed, "{", "<");
  373. ParseBlockTest("if(foo) <p>Bar</p> else if(bar) <p>Baz</p> else <p>Boz</p>",
  374. new StatementBlock(
  375. Factory.Code("if(foo) ").AsStatement(),
  376. new MarkupBlock(
  377. Factory.Markup("<p>Bar</p> ").Accepts(AcceptedCharacters.None)
  378. ),
  379. Factory.Code("else if(bar) ").AsStatement(),
  380. new MarkupBlock(
  381. Factory.Markup("<p>Baz</p> ").Accepts(AcceptedCharacters.None)
  382. ),
  383. Factory.Code("else ").AsStatement(),
  384. new MarkupBlock(
  385. Factory.Markup("<p>Boz</p>").Accepts(AcceptedCharacters.None)
  386. ),
  387. Factory.EmptyCSharp().AsStatement()
  388. ),
  389. new RazorError(expectedMessage, 8, 0, 8),
  390. new RazorError(expectedMessage, 32, 0, 32),
  391. new RazorError(expectedMessage, 48, 0, 48));
  392. }
  393. [Fact]
  394. public void ParseBlockIncludesUnexpectedCharacterInSingleStatementControlFlowStatementError()
  395. {
  396. ParseBlockTest("if(foo)) { var bar = foo; }",
  397. new StatementBlock(
  398. Factory.Code("if(foo)) { var bar = foo; }").AsStatement()
  399. ),
  400. new RazorError(
  401. String.Format(RazorResources.ParseError_SingleLine_ControlFlowStatements_Not_Allowed,
  402. "{", ")"),
  403. new SourceLocation(7, 0, 7)));
  404. }
  405. [Fact]
  406. public void ParseBlockOutputsErrorIfAtSignFollowedByLessThanSignAtStatementStart()
  407. {
  408. ParseBlockTest("if(foo) { @<p>Bar</p> }",
  409. new StatementBlock(
  410. Factory.Code("if(foo) {").AsStatement(),
  411. new MarkupBlock(
  412. Factory.Markup(" "),
  413. Factory.MarkupTransition(),
  414. Factory.Markup("<p>Bar</p> ").Accepts(AcceptedCharacters.None)
  415. ),
  416. Factory.Code("}").AsStatement()
  417. ),
  418. new RazorError(
  419. RazorResources.ParseError_AtInCode_Must_Be_Followed_By_Colon_Paren_Or_Identifier_Start,
  420. 10, 0, 10));
  421. }
  422. [Fact]
  423. public void ParseBlockTerminatesIfBlockAtEOLWhenRecoveringFromMissingCloseParen()
  424. {
  425. ParseBlockTest(@"if(foo bar
  426. baz",
  427. new StatementBlock(
  428. Factory.Code("if(foo bar\r\n").AsStatement()
  429. ),
  430. new RazorError(
  431. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  432. "(", ")"),
  433. new SourceLocation(2, 0, 2)));
  434. }
  435. [Fact]
  436. public void ParseBlockTerminatesForeachBlockAtEOLWhenRecoveringFromMissingCloseParen()
  437. {
  438. ParseBlockTest(@"foreach(foo bar
  439. baz",
  440. new StatementBlock(
  441. Factory.Code("foreach(foo bar\r\n").AsStatement()
  442. ),
  443. new RazorError(
  444. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  445. "(", ")"),
  446. new SourceLocation(7, 0, 7)));
  447. }
  448. [Fact]
  449. public void ParseBlockTerminatesWhileClauseInDoStatementAtEOLWhenRecoveringFromMissingCloseParen()
  450. {
  451. ParseBlockTest(@"do { } while(foo bar
  452. baz",
  453. new StatementBlock(
  454. Factory.Code("do { } while(foo bar\r\n").AsStatement()
  455. ),
  456. new RazorError(
  457. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  458. "(", ")"),
  459. new SourceLocation(12, 0, 12)));
  460. }
  461. [Fact]
  462. public void ParseBlockTerminatesUsingBlockAtEOLWhenRecoveringFromMissingCloseParen()
  463. {
  464. ParseBlockTest(@"using(foo bar
  465. baz",
  466. new StatementBlock(
  467. Factory.Code("using(foo bar\r\n").AsStatement()
  468. ),
  469. new RazorError(
  470. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  471. "(", ")"),
  472. new SourceLocation(5, 0, 5)));
  473. }
  474. [Fact]
  475. public void ParseBlockResumesIfStatementAfterOpenParen()
  476. {
  477. ParseBlockTest(@"if(
  478. else { <p>Foo</p> }",
  479. new StatementBlock(
  480. Factory.Code("if(\r\nelse {").AsStatement(),
  481. new MarkupBlock(
  482. Factory.Markup(" <p>Foo</p> ").Accepts(AcceptedCharacters.None)
  483. ),
  484. Factory.Code("}").AsStatement().Accepts(AcceptedCharacters.None)
  485. ),
  486. new RazorError(
  487. String.Format(RazorResources.ParseError_Expected_CloseBracket_Before_EOF,
  488. "(", ")"),
  489. new SourceLocation(2, 0, 2)));
  490. }
  491. [Fact]
  492. public void ParseBlockTerminatesNormalCSharpStringsAtEOLIfEndQuoteMissing()
  493. {
  494. SingleSpanBlockTest(@"if(foo) {
  495. var p = ""foo bar baz
  496. ;
  497. }",
  498. BlockType.Statement, SpanKind.Code,
  499. new RazorError(RazorResources.ParseError_Unterminated_String_Literal, 23, 1, 12));
  500. }
  501. [Fact]
  502. public void ParseBlockTerminatesNormalStringAtEndOfFile()
  503. {
  504. SingleSpanBlockTest("if(foo) { var foo = \"blah blah blah blah blah", BlockType.Statement, SpanKind.Code,
  505. new RazorError(RazorResources.ParseError_Unterminated_String_Literal, 20, 0, 20),
  506. new RazorError(String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF, "if", '}', '{'), SourceLocation.Zero));
  507. }
  508. [Fact]
  509. public void ParseBlockTerminatesVerbatimStringAtEndOfFile()
  510. {
  511. SingleSpanBlockTest(@"if(foo) { var foo = @""blah
  512. blah;
  513. <p>Foo</p>
  514. blah
  515. blah",
  516. BlockType.Statement, SpanKind.Code,
  517. new RazorError(RazorResources.ParseError_Unterminated_String_Literal, 20, 0, 20),
  518. new RazorError(String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF, "if", '}', '{'), SourceLocation.Zero));
  519. }
  520. [Fact]
  521. public void ParseBlockCorrectlyParsesMarkupIncorrectyAssumedToBeWithinAStatement()
  522. {
  523. ParseBlockTest(@"if(foo) {
  524. var foo = ""foo bar baz
  525. <p>Foo is @foo</p>
  526. }",
  527. new StatementBlock(
  528. Factory.Code("if(foo) {\r\n var foo = \"foo bar baz\r\n ").AsStatement(),
  529. new MarkupBlock(
  530. Factory.Markup("<p>Foo is "),
  531. new ExpressionBlock(
  532. Factory.CodeTransition(),
  533. Factory.Code("foo")
  534. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  535. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  536. Factory.Markup("</p>\r\n").Accepts(AcceptedCharacters.None)),
  537. Factory.Code("}").AsStatement()
  538. ),
  539. new RazorError(
  540. RazorResources.ParseError_Unterminated_String_Literal,
  541. 25, 1, 14));
  542. }
  543. [Fact]
  544. public void ParseBlockCorrectlyParsesAtSignInDelimitedBlock()
  545. {
  546. ParseBlockTest("(Request[\"description\"] ?? @photo.Description)",
  547. new ExpressionBlock(
  548. Factory.MetaCode("(").Accepts(AcceptedCharacters.None),
  549. Factory.Code("Request[\"description\"] ?? @photo.Description").AsExpression(),
  550. Factory.MetaCode(")").Accepts(AcceptedCharacters.None)
  551. ));
  552. }
  553. private void RunUnterminatedSimpleKeywordBlock(string keyword)
  554. {
  555. SingleSpanBlockTest(keyword + " (foo) { var foo = bar; if(foo != null) { bar(); } ", BlockType.Statement, SpanKind.Code,
  556. new RazorError(String.Format(RazorResources.ParseError_Expected_EndOfBlock_Before_EOF, keyword, '}', '{'), SourceLocation.Zero));
  557. }
  558. }
  559. }