PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mdavid/aspnetwebstack
C# | 345 lines | 330 code | 15 blank | 0 comment | 0 complexity | b5f74a45d43f0657475bcaf18b0589a0 MD5 | raw file
  1. using System.Web.Razor.Generator;
  2. using System.Web.Razor.Parser;
  3. using System.Web.Razor.Parser.SyntaxTree;
  4. using System.Web.Razor.Resources;
  5. using System.Web.Razor.Test.Framework;
  6. using System.Web.Razor.Text;
  7. using Xunit;
  8. namespace System.Web.Razor.Test.Parser.CSharp
  9. {
  10. public class CSharpHelperTest : CsHtmlMarkupParserTestBase
  11. {
  12. [Fact]
  13. public void ParseHelperCorrectlyParsesHelperWithNoSpaceInBody()
  14. {
  15. ParseDocumentTest("@helper Foo(){@Bar()}",
  16. new MarkupBlock(
  17. Factory.EmptyHtml(),
  18. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(){", 8, 0, 8), headerComplete: true),
  19. Factory.CodeTransition(),
  20. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  21. Factory.Code("Foo(){").Hidden().Accepts(AcceptedCharacters.None),
  22. new StatementBlock(
  23. Factory.EmptyCSharp().AsStatement(),
  24. new ExpressionBlock(
  25. Factory.CodeTransition(),
  26. Factory.Code("Bar()")
  27. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: true)
  28. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  29. Factory.EmptyCSharp().AsStatement()),
  30. Factory.Code("}").Hidden().Accepts(AcceptedCharacters.None)),
  31. Factory.EmptyHtml()));
  32. }
  33. [Fact]
  34. public void ParseHelperCorrectlyParsesIncompleteHelperPreceedingCodeBlock()
  35. {
  36. ParseDocumentTest(@"@helper
  37. @{}",
  38. new MarkupBlock(
  39. Factory.EmptyHtml(),
  40. new HelperBlock(
  41. Factory.CodeTransition(),
  42. Factory.MetaCode("helper")),
  43. Factory.Markup("\r\n"),
  44. new StatementBlock(
  45. Factory.CodeTransition(),
  46. Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
  47. Factory.EmptyCSharp().AsStatement(),
  48. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
  49. Factory.EmptyHtml()),
  50. new RazorError(
  51. String.Format(
  52. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  53. RazorResources.ErrorComponent_Newline),
  54. 7, 0, 7));
  55. }
  56. [Fact]
  57. public void ParseHelperRequiresSpaceBeforeSignature()
  58. {
  59. ParseDocumentTest("@helper{",
  60. new MarkupBlock(
  61. Factory.EmptyHtml(),
  62. new HelperBlock(
  63. Factory.CodeTransition(),
  64. Factory.MetaCode("helper")),
  65. Factory.Markup("{")),
  66. new RazorError(
  67. String.Format(
  68. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  69. String.Format(RazorResources.ErrorComponent_Character, "{")),
  70. 7, 0, 7));
  71. }
  72. [Fact]
  73. public void ParseHelperOutputsErrorButContinuesIfLParenFoundAfterHelperKeyword()
  74. {
  75. ParseDocumentTest("@helper () {",
  76. new MarkupBlock(
  77. Factory.EmptyHtml(),
  78. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("() {", 8, 0, 8), headerComplete: true),
  79. Factory.CodeTransition(),
  80. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  81. Factory.Code("() {").Hidden().Accepts(AcceptedCharacters.None),
  82. new StatementBlock(
  83. Factory.EmptyCSharp()
  84. .AsStatement()
  85. .AutoCompleteWith("}")))),
  86. new RazorError(
  87. String.Format(
  88. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  89. String.Format(RazorResources.ErrorComponent_Character, "(")),
  90. 8, 0, 8),
  91. new RazorError(
  92. String.Format(
  93. RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  94. "helper", "}", "{"),
  95. 1, 0, 1));
  96. }
  97. [Fact]
  98. public void ParseHelperStatementOutputsMarkerHelperHeaderSpanOnceKeywordComplete()
  99. {
  100. ParseDocumentTest("@helper ",
  101. new MarkupBlock(
  102. Factory.EmptyHtml(),
  103. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>(String.Empty, 8, 0, 8), headerComplete: false),
  104. Factory.CodeTransition(),
  105. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  106. Factory.EmptyCSharp().Hidden())),
  107. new RazorError(
  108. String.Format(
  109. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  110. RazorResources.ErrorComponent_EndOfFile),
  111. 8, 0, 8));
  112. }
  113. [Fact]
  114. public void ParseHelperStatementMarksHelperSpanAsCanGrowIfMissingTrailingSpace()
  115. {
  116. ParseDocumentTest("@helper",
  117. new MarkupBlock(
  118. Factory.EmptyHtml(),
  119. new HelperBlock(
  120. Factory.CodeTransition(),
  121. Factory.MetaCode("helper").Accepts(AcceptedCharacters.Any))),
  122. new RazorError(
  123. String.Format(
  124. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  125. RazorResources.ErrorComponent_EndOfFile),
  126. 7, 0, 7));
  127. }
  128. [Fact]
  129. public void ParseHelperStatementCapturesWhitespaceToEndOfLineIfHelperStatementMissingName()
  130. {
  131. ParseDocumentTest(@"@helper
  132. ",
  133. new MarkupBlock(
  134. Factory.EmptyHtml(),
  135. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>(" ", 8, 0, 8), headerComplete: false),
  136. Factory.CodeTransition(),
  137. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  138. Factory.Code(" \r\n").Hidden()),
  139. Factory.Markup(@" ")),
  140. new RazorError(
  141. String.Format(
  142. RazorResources.ParseError_Unexpected_Character_At_Helper_Name_Start,
  143. RazorResources.ErrorComponent_Newline),
  144. 30, 0, 30));
  145. }
  146. [Fact]
  147. public void ParseHelperStatementCapturesWhitespaceToEndOfLineIfHelperStatementMissingOpenParen()
  148. {
  149. ParseDocumentTest(@"@helper Foo
  150. ",
  151. new MarkupBlock(
  152. Factory.EmptyHtml(),
  153. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo ", 8, 0, 8), headerComplete: false),
  154. Factory.CodeTransition(),
  155. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  156. Factory.Code("Foo \r\n").Hidden()),
  157. Factory.Markup(" ")),
  158. new RazorError(
  159. String.Format(RazorResources.ParseError_MissingCharAfterHelperName, "("),
  160. 15, 0, 15));
  161. }
  162. [Fact]
  163. public void ParseHelperStatementCapturesAllContentToEndOfFileIfHelperStatementMissingCloseParenInParameterList()
  164. {
  165. ParseDocumentTest(@"@helper Foo(Foo Bar
  166. Biz
  167. Boz",
  168. new MarkupBlock(
  169. Factory.EmptyHtml(),
  170. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(Foo Bar\r\nBiz\r\nBoz", 8, 0, 8), headerComplete: false),
  171. Factory.CodeTransition(),
  172. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  173. Factory.Code("Foo(Foo Bar\r\nBiz\r\nBoz").Hidden())),
  174. new RazorError(
  175. RazorResources.ParseError_UnterminatedHelperParameterList,
  176. 11, 0, 11));
  177. }
  178. [Fact]
  179. public void ParseHelperStatementCapturesWhitespaceToEndOfLineIfHelperStatementMissingOpenBraceAfterParameterList()
  180. {
  181. ParseDocumentTest(@"@helper Foo(string foo)
  182. ",
  183. new MarkupBlock(
  184. Factory.EmptyHtml(),
  185. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo) ", 8, 0, 8), headerComplete: false),
  186. Factory.CodeTransition(),
  187. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  188. Factory.Code("Foo(string foo) \r\n").Hidden())),
  189. new RazorError(
  190. String.Format(RazorResources.ParseError_MissingCharAfterHelperParameters, "{"),
  191. 29, 1, 0));
  192. }
  193. [Fact]
  194. public void ParseHelperStatementContinuesParsingHelperUntilEOF()
  195. {
  196. ParseDocumentTest(@"@helper Foo(string foo) {
  197. <p>Foo</p>",
  198. new MarkupBlock(
  199. Factory.EmptyHtml(),
  200. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo) {", 8, 0, 8), headerComplete: true),
  201. Factory.CodeTransition(),
  202. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  203. Factory.Code(@"Foo(string foo) {").Hidden().Accepts(AcceptedCharacters.None),
  204. new StatementBlock(
  205. Factory.Code(" \r\n")
  206. .AsStatement()
  207. .AutoCompleteWith("}"),
  208. new MarkupBlock(
  209. Factory.Markup(" <p>Foo</p>").Accepts(AcceptedCharacters.None)),
  210. Factory.EmptyCSharp().AsStatement()))),
  211. new RazorError(
  212. String.Format(
  213. RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  214. "helper", "}", "{"),
  215. 1, 0, 1));
  216. }
  217. [Fact]
  218. public void ParseHelperStatementCorrectlyParsesHelperWithEmbeddedCode()
  219. {
  220. ParseDocumentTest(@"@helper Foo(string foo) {
  221. <p>@foo</p>
  222. }",
  223. new MarkupBlock(
  224. Factory.EmptyHtml(),
  225. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo) {", 8, 0, 8), headerComplete: true),
  226. Factory.CodeTransition(),
  227. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  228. Factory.Code(@"Foo(string foo) {").Hidden().Accepts(AcceptedCharacters.None),
  229. new StatementBlock(
  230. Factory.Code(" \r\n").AsStatement(),
  231. new MarkupBlock(
  232. Factory.Markup(" <p>"),
  233. new ExpressionBlock(
  234. Factory.CodeTransition(),
  235. Factory.Code("foo")
  236. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  237. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  238. Factory.Markup("</p>\r\n").Accepts(AcceptedCharacters.None)),
  239. Factory.EmptyCSharp().AsStatement()),
  240. Factory.Code("}").Hidden().Accepts(AcceptedCharacters.None)),
  241. Factory.EmptyHtml()));
  242. }
  243. [Fact]
  244. public void ParseHelperStatementCorrectlyParsesHelperWithNewlinesBetweenCloseParenAndOpenBrace()
  245. {
  246. ParseDocumentTest(@"@helper Foo(string foo)
  247. {
  248. <p>@foo</p>
  249. }",
  250. new MarkupBlock(
  251. Factory.EmptyHtml(),
  252. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo)\r\n\r\n\r\n\r\n{", 8, 0, 8), headerComplete: true),
  253. Factory.CodeTransition(),
  254. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  255. Factory.Code("Foo(string foo)\r\n\r\n\r\n\r\n{").Hidden().Accepts(AcceptedCharacters.None),
  256. new StatementBlock(
  257. Factory.Code(" \r\n").AsStatement(),
  258. new MarkupBlock(
  259. Factory.Markup(@" <p>"),
  260. new ExpressionBlock(
  261. Factory.CodeTransition(),
  262. Factory.Code("foo")
  263. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  264. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  265. Factory.Markup("</p>\r\n").Accepts(AcceptedCharacters.None)),
  266. Factory.EmptyCSharp().AsStatement()),
  267. Factory.Code("}").Hidden().Accepts(AcceptedCharacters.None)),
  268. Factory.EmptyHtml()));
  269. }
  270. [Fact]
  271. public void ParseHelperStatementGivesWhitespaceAfterOpenBraceToMarkupInDesignMode()
  272. {
  273. ParseDocumentTest(@"@helper Foo(string foo) {
  274. ",
  275. new MarkupBlock(
  276. Factory.EmptyHtml(),
  277. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo) {", 8, 0, 8), headerComplete: true),
  278. Factory.CodeTransition(),
  279. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  280. Factory.Code(@"Foo(string foo) {").Hidden().Accepts(AcceptedCharacters.None),
  281. new StatementBlock(
  282. Factory.Code(" \r\n ")
  283. .AsStatement()
  284. .AutoCompleteWith("}")))),
  285. designTimeParser: true,
  286. expectedErrors: new[]
  287. {
  288. new RazorError(
  289. String.Format(
  290. RazorResources.ParseError_Expected_EndOfBlock_Before_EOF,
  291. "helper", "}", "{"),
  292. new SourceLocation(1, 0, 1))
  293. });
  294. }
  295. [Fact]
  296. public void ParseHelperAcceptsNestedHelpersButOutputsError()
  297. {
  298. ParseDocumentTest(@"@helper Foo(string foo) {
  299. @helper Bar(string baz) {
  300. }
  301. }",
  302. new MarkupBlock(
  303. Factory.EmptyHtml(),
  304. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Foo(string foo) {", 8, 0, 8), headerComplete: true),
  305. Factory.CodeTransition(),
  306. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  307. Factory.Code(@"Foo(string foo) {").Hidden().Accepts(AcceptedCharacters.None),
  308. new StatementBlock(
  309. Factory.Code("\r\n ").AsStatement(),
  310. new HelperBlock(new HelperCodeGenerator(new LocationTagged<string>("Bar(string baz) {", 39, 1, 12), headerComplete: true),
  311. Factory.CodeTransition(),
  312. Factory.MetaCode("helper ").Accepts(AcceptedCharacters.None),
  313. Factory.Code(@"Bar(string baz) {").Hidden().Accepts(AcceptedCharacters.None),
  314. new StatementBlock(
  315. Factory.Code("\r\n ").AsStatement()),
  316. Factory.Code("}").Hidden().Accepts(AcceptedCharacters.None)),
  317. Factory.Code("\r\n").AsStatement()),
  318. Factory.Code("}").Hidden().Accepts(AcceptedCharacters.None)),
  319. Factory.EmptyHtml()),
  320. designTimeParser: true,
  321. expectedErrors: new[]
  322. {
  323. new RazorError(RazorResources.ParseError_Helpers_Cannot_Be_Nested, 38, 1, 11)
  324. });
  325. }
  326. }
  327. }