PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Web.Razor.Test/Parser/PartialParsing/VBPartialParsingTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 372 lines | 327 code | 37 blank | 8 comment | 0 complexity | f38591dbd4e224a365d3a8e23e1342f9 MD5 | raw file
  1. using System.Web.Razor.Parser;
  2. using System.Web.Razor.Parser.SyntaxTree;
  3. using System.Web.Razor.Test.Framework;
  4. using System.Web.Razor.Text;
  5. using System.Web.WebPages.TestUtils;
  6. using Xunit;
  7. namespace System.Web.Razor.Test.Parser.PartialParsing
  8. {
  9. public class VBPartialParsingTest : PartialParsingTestBase<VBRazorCodeLanguage>
  10. {
  11. [Fact]
  12. public void ImplicitExpressionProvisionallyAcceptsDeleteOfIdentifierPartsIfDotRemains()
  13. {
  14. var factory = SpanFactory.CreateVbHtml();
  15. StringTextBuffer changed = new StringTextBuffer("foo @User. baz");
  16. StringTextBuffer old = new StringTextBuffer("foo @User.Name baz");
  17. RunPartialParseTest(new TextChange(10, 4, old, 0, changed),
  18. new MarkupBlock(
  19. factory.Markup("foo "),
  20. new ExpressionBlock(
  21. factory.CodeTransition(),
  22. factory.Code("User.")
  23. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  24. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  25. factory.Markup(" baz")),
  26. additionalFlags: PartialParseResult.Provisional);
  27. }
  28. [Fact]
  29. public void ImplicitExpressionAcceptsDeleteOfIdentifierPartsIfSomeOfIdentifierRemains()
  30. {
  31. var factory = SpanFactory.CreateVbHtml();
  32. StringTextBuffer changed = new StringTextBuffer("foo @Us baz");
  33. StringTextBuffer old = new StringTextBuffer("foo @User baz");
  34. RunPartialParseTest(new TextChange(7, 2, old, 0, changed),
  35. new MarkupBlock(
  36. factory.Markup("foo "),
  37. new ExpressionBlock(
  38. factory.CodeTransition(),
  39. factory.Code("Us")
  40. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  41. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  42. factory.Markup(" baz")));
  43. }
  44. [Fact]
  45. public void ImplicitExpressionProvisionallyAcceptsMultipleInsertionIfItCausesIdentifierExpansionAndTrailingDot()
  46. {
  47. var factory = SpanFactory.CreateVbHtml();
  48. StringTextBuffer changed = new StringTextBuffer("foo @User. baz");
  49. StringTextBuffer old = new StringTextBuffer("foo @U baz");
  50. RunPartialParseTest(new TextChange(6, 0, old, 4, changed),
  51. new MarkupBlock(
  52. factory.Markup("foo "),
  53. new ExpressionBlock(
  54. factory.CodeTransition(),
  55. factory.Code("User.")
  56. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  57. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  58. factory.Markup(" baz")),
  59. additionalFlags: PartialParseResult.Provisional);
  60. }
  61. [Fact]
  62. public void ImplicitExpressionAcceptsMultipleInsertionIfItOnlyCausesIdentifierExpansion()
  63. {
  64. var factory = SpanFactory.CreateVbHtml();
  65. StringTextBuffer changed = new StringTextBuffer("foo @barbiz baz");
  66. StringTextBuffer old = new StringTextBuffer("foo @bar baz");
  67. RunPartialParseTest(new TextChange(8, 0, old, 3, changed),
  68. new MarkupBlock(
  69. factory.Markup("foo "),
  70. new ExpressionBlock(
  71. factory.CodeTransition(),
  72. factory.Code("barbiz")
  73. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  74. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  75. factory.Markup(" baz")));
  76. }
  77. [Fact]
  78. public void ImplicitExpressionRejectsChangeWhichWouldHaveBeenAcceptedIfLastChangeWasProvisionallyAcceptedOnDifferentSpan()
  79. {
  80. var factory = SpanFactory.CreateVbHtml();
  81. // Arrange
  82. TextChange dotTyped = new TextChange(8, 0, new StringTextBuffer("foo @foo @bar"), 1, new StringTextBuffer("foo @foo. @bar"));
  83. TextChange charTyped = new TextChange(14, 0, new StringTextBuffer("foo @foo. @barb"), 1, new StringTextBuffer("foo @foo. @barb"));
  84. TestParserManager manager = CreateParserManager();
  85. manager.InitializeWithDocument(dotTyped.OldBuffer);
  86. // Apply the dot change
  87. Assert.Equal(PartialParseResult.Provisional | PartialParseResult.Accepted, manager.CheckForStructureChangesAndWait(dotTyped));
  88. // Act (apply the identifier start char change)
  89. PartialParseResult result = manager.CheckForStructureChangesAndWait(charTyped);
  90. // Assert
  91. Assert.Equal(PartialParseResult.Rejected, result);
  92. Assert.False(manager.Parser.LastResultProvisional, "LastResultProvisional flag should have been cleared but it was not");
  93. ParserTestBase.EvaluateParseTree(manager.Parser.CurrentParseTree,
  94. new MarkupBlock(
  95. factory.Markup("foo "),
  96. new ExpressionBlock(
  97. factory.CodeTransition(),
  98. factory.Code("foo")
  99. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  100. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  101. factory.Markup(". "),
  102. new ExpressionBlock(
  103. factory.CodeTransition(),
  104. factory.Code("barb")
  105. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  106. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  107. factory.EmptyHtml()));
  108. }
  109. [Fact]
  110. public void ImplicitExpressionAcceptsIdentifierTypedAfterDotIfLastChangeWasProvisionalAcceptanceOfDot()
  111. {
  112. var factory = SpanFactory.CreateVbHtml();
  113. // Arrange
  114. TextChange dotTyped = new TextChange(8, 0, new StringTextBuffer("foo @foo bar"), 1, new StringTextBuffer("foo @foo. bar"));
  115. TextChange charTyped = new TextChange(9, 0, new StringTextBuffer("foo @foo. bar"), 1, new StringTextBuffer("foo @foo.b bar"));
  116. TestParserManager manager = CreateParserManager();
  117. manager.InitializeWithDocument(dotTyped.OldBuffer);
  118. // Apply the dot change
  119. Assert.Equal(PartialParseResult.Provisional | PartialParseResult.Accepted, manager.CheckForStructureChangesAndWait(dotTyped));
  120. // Act (apply the identifier start char change)
  121. PartialParseResult result = manager.CheckForStructureChangesAndWait(charTyped);
  122. // Assert
  123. Assert.Equal(PartialParseResult.Accepted, result);
  124. Assert.False(manager.Parser.LastResultProvisional, "LastResultProvisional flag should have been cleared but it was not");
  125. ParserTestBase.EvaluateParseTree(manager.Parser.CurrentParseTree,
  126. new MarkupBlock(
  127. factory.Markup("foo "),
  128. new ExpressionBlock(
  129. factory.CodeTransition(),
  130. factory.Code("foo.b")
  131. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  132. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  133. factory.Markup(" bar")));
  134. }
  135. [Fact]
  136. public void ImplicitExpressionAcceptsIdentifierExpansionAtEndOfNonWhitespaceCharacters()
  137. {
  138. var factory = SpanFactory.CreateVbHtml();
  139. StringTextBuffer changed = new StringTextBuffer(@"@Code
  140. @food
  141. End Code");
  142. StringTextBuffer old = new StringTextBuffer(@"@Code
  143. @foo
  144. End Code");
  145. RunPartialParseTest(new TextChange(15, 0, old, 1, changed),
  146. new MarkupBlock(
  147. factory.EmptyHtml(),
  148. new StatementBlock(
  149. factory.CodeTransition(),
  150. factory.MetaCode("Code")
  151. .Accepts(AcceptedCharacters.None),
  152. factory.Code("\r\n ").AsStatement(),
  153. new ExpressionBlock(
  154. factory.CodeTransition(),
  155. factory.Code("food")
  156. .AsImplicitExpression(VBCodeParser.DefaultKeywords, acceptTrailingDot: true)
  157. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  158. factory.Code("\r\n").AsStatement(),
  159. factory.MetaCode("End Code").Accepts(AcceptedCharacters.None)),
  160. factory.EmptyHtml()));
  161. }
  162. [Fact]
  163. public void ImplicitExpressionProvisionallyAcceptsDotAfterIdentifierInMarkup()
  164. {
  165. var factory = SpanFactory.CreateVbHtml();
  166. StringTextBuffer changed = new StringTextBuffer("foo @foo. bar");
  167. StringTextBuffer old = new StringTextBuffer("foo @foo bar");
  168. RunPartialParseTest(new TextChange(8, 0, old, 1, changed),
  169. new MarkupBlock(
  170. factory.Markup("foo "),
  171. new ExpressionBlock(
  172. factory.CodeTransition(),
  173. factory.Code("foo.")
  174. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  175. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  176. factory.Markup(" bar")),
  177. additionalFlags: PartialParseResult.Provisional);
  178. }
  179. [Fact]
  180. public void ImplicitExpressionAcceptsAdditionalIdentifierCharactersIfEndOfSpanIsIdentifier()
  181. {
  182. var factory = SpanFactory.CreateVbHtml();
  183. StringTextBuffer changed = new StringTextBuffer("foo @foob baz");
  184. StringTextBuffer old = new StringTextBuffer("foo @foo bar");
  185. RunPartialParseTest(new TextChange(8, 0, old, 1, changed),
  186. new MarkupBlock(
  187. factory.Markup("foo "),
  188. new ExpressionBlock(
  189. factory.CodeTransition(),
  190. factory.Code("foob")
  191. .AsImplicitExpression(VBCodeParser.DefaultKeywords)
  192. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  193. factory.Markup(" bar")));
  194. }
  195. [Fact]
  196. public void ImplicitExpressionAcceptsAdditionalIdentifierStartCharactersIfEndOfSpanIsDot()
  197. {
  198. var factory = SpanFactory.CreateVbHtml();
  199. StringTextBuffer changed = new StringTextBuffer("@Code @foo.b End Code");
  200. StringTextBuffer old = new StringTextBuffer("@Code @foo. End Code");
  201. RunPartialParseTest(new TextChange(11, 0, old, 1, changed),
  202. new MarkupBlock(
  203. factory.EmptyHtml(),
  204. new StatementBlock(
  205. factory.CodeTransition(),
  206. factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
  207. factory.Code(" ").AsStatement(),
  208. new ExpressionBlock(
  209. factory.CodeTransition(),
  210. factory.Code("foo.b")
  211. .AsImplicitExpression(VBCodeParser.DefaultKeywords, acceptTrailingDot: true)
  212. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  213. factory.Code(" ").AsStatement(),
  214. factory.MetaCode("End Code").Accepts(AcceptedCharacters.None)),
  215. factory.EmptyHtml()));
  216. }
  217. [Fact]
  218. public void ImplicitExpressionAcceptsDotIfTrailingDotsAreAllowed()
  219. {
  220. var factory = SpanFactory.CreateVbHtml();
  221. StringTextBuffer changed = new StringTextBuffer("@Code @foo. End Code");
  222. StringTextBuffer old = new StringTextBuffer("@Code @foo End Code");
  223. RunPartialParseTest(new TextChange(10, 0, old, 1, changed),
  224. new MarkupBlock(
  225. factory.EmptyHtml(),
  226. new StatementBlock(
  227. factory.CodeTransition(),
  228. factory.MetaCode("Code").Accepts(AcceptedCharacters.None),
  229. factory.Code(" ").AsStatement(),
  230. new ExpressionBlock(
  231. factory.CodeTransition(),
  232. factory.Code("foo.")
  233. .AsImplicitExpression(VBCodeParser.DefaultKeywords, acceptTrailingDot: true)
  234. .Accepts(AcceptedCharacters.NonWhiteSpace)),
  235. factory.Code(" ").AsStatement(),
  236. factory.MetaCode("End Code").Accepts(AcceptedCharacters.None)),
  237. factory.EmptyHtml()));
  238. }
  239. [Fact]
  240. public void ImplicitExpressionCorrectlyTriggersReparseIfFunctionsKeywordTyped()
  241. {
  242. RunTypeKeywordTest("functions");
  243. }
  244. [Fact]
  245. public void ImplicitExpressionCorrectlyTriggersReparseIfCodeKeywordTyped()
  246. {
  247. RunTypeKeywordTest("code");
  248. }
  249. [Fact]
  250. public void ImplicitExpressionCorrectlyTriggersReparseIfSectionKeywordTyped()
  251. {
  252. RunTypeKeywordTest("section");
  253. }
  254. [Fact]
  255. public void ImplicitExpressionCorrectlyTriggersReparseIfDoKeywordTyped()
  256. {
  257. RunTypeKeywordTest("do");
  258. }
  259. [Fact]
  260. public void ImplicitExpressionCorrectlyTriggersReparseIfWhileKeywordTyped()
  261. {
  262. RunTypeKeywordTest("while");
  263. }
  264. [Fact]
  265. public void ImplicitExpressionCorrectlyTriggersReparseIfIfKeywordTyped()
  266. {
  267. RunTypeKeywordTest("if");
  268. }
  269. [Fact]
  270. public void ImplicitExpressionCorrectlyTriggersReparseIfSelectKeywordTyped()
  271. {
  272. RunTypeKeywordTest("select");
  273. }
  274. [Fact]
  275. public void ImplicitExpressionCorrectlyTriggersReparseIfForKeywordTyped()
  276. {
  277. RunTypeKeywordTest("for");
  278. }
  279. [Fact]
  280. public void ImplicitExpressionCorrectlyTriggersReparseIfTryKeywordTyped()
  281. {
  282. RunTypeKeywordTest("try");
  283. }
  284. [Fact]
  285. public void ImplicitExpressionCorrectlyTriggersReparseIfWithKeywordTyped()
  286. {
  287. RunTypeKeywordTest("with");
  288. }
  289. [Fact]
  290. public void ImplicitExpressionCorrectlyTriggersReparseIfSyncLockKeywordTyped()
  291. {
  292. RunTypeKeywordTest("synclock");
  293. }
  294. [Fact]
  295. public void ImplicitExpressionCorrectlyTriggersReparseIfUsingKeywordTyped()
  296. {
  297. RunTypeKeywordTest("using");
  298. }
  299. [Fact]
  300. public void ImplicitExpressionCorrectlyTriggersReparseIfImportsKeywordTyped()
  301. {
  302. RunTypeKeywordTest("imports");
  303. }
  304. [Fact]
  305. public void ImplicitExpressionCorrectlyTriggersReparseIfInheritsKeywordTyped()
  306. {
  307. RunTypeKeywordTest("inherits");
  308. }
  309. [Fact]
  310. public void ImplicitExpressionCorrectlyTriggersReparseIfOptionKeywordTyped()
  311. {
  312. RunTypeKeywordTest("option");
  313. }
  314. [Fact]
  315. public void ImplicitExpressionCorrectlyTriggersReparseIfHelperKeywordTyped()
  316. {
  317. RunTypeKeywordTest("helper");
  318. }
  319. [Fact]
  320. public void ImplicitExpressionCorrectlyTriggersReparseIfNamespaceKeywordTyped()
  321. {
  322. RunTypeKeywordTest("namespace");
  323. }
  324. [Fact]
  325. public void ImplicitExpressionCorrectlyTriggersReparseIfClassKeywordTyped()
  326. {
  327. RunTypeKeywordTest("class");
  328. }
  329. [Fact]
  330. public void ImplicitExpressionCorrectlyTriggersReparseIfLayoutKeywordTyped()
  331. {
  332. RunTypeKeywordTest("layout");
  333. }
  334. }
  335. }