PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 3ms app.codeStats 0ms

/test/fitnesse/wikitext/parser/IncludeTest.java

http://github.com/unclebob/fitnesse
Java | 252 lines | 208 code | 44 blank | 0 comment | 0 complexity | 63089b15177cdaa9d844adade399ab3b MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, GPL-2.0
  1. package fitnesse.wikitext.parser;
  2. import static fitnesse.wikitext.parser.ParserTestHelper.assertParses;
  3. import static org.junit.Assert.assertTrue;
  4. import fitnesse.wiki.PageData;
  5. import fitnesse.wiki.WikiPage;
  6. import org.junit.Test;
  7. public class IncludeTest {
  8. @Test
  9. public void scansIncludes() {
  10. ParserTestHelper.assertScansTokenType("!include name", "Include", true);
  11. }
  12. @Test
  13. public void parsesIncludes() throws Exception {
  14. assertParses("!include PageTwo\n", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  15. assertParses("|!include PageTwo|\n", "SymbolList[Table[TableRow[TableCell[Include[Text, WikiWord, Text, Style[Text]]]]]]");
  16. assertParses("!include PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  17. assertParses("!include -c PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  18. assertParses("!include <PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  19. assertParses("!include -setup PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  20. assertParses("!include -teardown PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  21. assertParses("!include -h PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  22. assertParses("!include -h .SuitePage.PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  23. assertParses("!include <PageTwo>", "SymbolList[Include[Text, Text, Text, Style[Text]]]");
  24. }
  25. @Test
  26. public void parsesIncludeNonWikiWordPages() throws Exception {
  27. assertParses("!include -h SuitePage.nonwikipage.PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  28. assertParses("!include -h nonwikipage.SuitePage.PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  29. assertParses("!include -h .nonwikipage.SuitePage.PageTwo", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  30. assertParses("!include -h SuitePage.PageTwo.nonwikipage", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  31. }
  32. @Test
  33. public void parsesIncludeSingleNonWikiWordPage() throws Exception {
  34. assertParses("!include -h nonwikipage", "SymbolList[Include[Text, WikiWord, Text, Style[Text]]]");
  35. }
  36. @Test
  37. public void translatesIncludedSibling() throws Exception {
  38. TestRoot root = new TestRoot();
  39. WikiPage currentPage = root.makePage("PageOne", "!include PageTwo");
  40. root.makePage("PageTwo", "page ''two''");
  41. String result = ParserTestHelper.translateTo(currentPage);
  42. assertContains(result, "class=\"collapsible\"");
  43. assertContains(result, "Included page: <a href=\"PageTwo\">PageTwo</a> <a href=\"PageTwo?edit&amp;redirectToReferer=true&amp;redirectAction=\" class=\"edit\">(edit)</a>");
  44. assertContains(result, "page <i>two</i>");
  45. }
  46. @Test
  47. public void translatesIncludeWithChildReference() throws Exception {
  48. TestRoot root = new TestRoot();
  49. WikiPage currentPage = root.makePage("PageOne", "!include PageTwo");
  50. WikiPage pageTwo = root.makePage("PageTwo", ">PageTwoChild");
  51. root.makePage(pageTwo, "PageTwoChild", "stuff");
  52. String result = ParserTestHelper.translateTo(currentPage);
  53. assertContains(result, "PageTwo.PageTwoChild");
  54. }
  55. @Test
  56. public void translatesRelativeInclude() throws Exception {
  57. TestRoot root = new TestRoot();
  58. WikiPage currentPage = root.makePage("PageOne", "!include >PageOneChild");
  59. root.makePage(currentPage, "PageOneChild", "stuff");
  60. String result = ParserTestHelper.translateTo(currentPage);
  61. assertContains(result, "stuff");
  62. }
  63. @Test
  64. public void translatesNestedRelativeInclude() throws Exception {
  65. TestRoot root = new TestRoot();
  66. WikiPage currentPage = root.makePage("PageOne", "!include >PageOneChild");
  67. WikiPage pageOneChild = root.makePage(currentPage, "PageOneChild", "!include >PageOneGrandChild");
  68. root.makePage(pageOneChild, "PageOneGrandChild", "stuff");
  69. String result = ParserTestHelper.translateTo(currentPage);
  70. assertContains(result, "stuff");
  71. }
  72. @Test
  73. public void translatesWithNonWikiWord() throws Exception {
  74. TestRoot root = new TestRoot();
  75. WikiPage currentPage = root.makePage("PageOne", "!include PageTwo.non_wiki");
  76. WikiPage pageTwo = root.makePage("PageTwo");
  77. root.makePage(pageTwo, "non_wiki", "page ''two''");
  78. String result = ParserTestHelper.translateTo(currentPage);
  79. assertContains(result, "class=\"collapsible\"");
  80. assertContains(result, "Included page: <a href=\"PageTwo.non_wiki\">PageTwo.non_wiki</a> <a href=\"PageTwo.non_wiki?edit&amp;redirectToReferer=true&amp;redirectAction=\" class=\"edit\">(edit)</a>");
  81. assertContains(result, "page <i>two</i>");
  82. }
  83. @Test
  84. public void translatesWithAllNonWikiWord() throws Exception {
  85. TestRoot root = new TestRoot();
  86. WikiPage currentPage = root.makePage("PageOne", "!include page_two.non_wiki");
  87. WikiPage pageTwo = root.makePage("page_two");
  88. root.makePage(pageTwo, "non_wiki", "page ''two''");
  89. String result = ParserTestHelper.translateTo(currentPage);
  90. assertContains(result, "class=\"collapsible\"");
  91. assertContains(result, "Included page: <a href=\"page_two.non_wiki\">page_two.non_wiki</a> <a href=\"page_two.non_wiki?edit&amp;redirectToReferer=true&amp;redirectAction=\" class=\"edit\">(edit)</a>");
  92. assertContains(result, "page <i>two</i>");
  93. }
  94. @Test
  95. public void setupsAreHidden() throws Exception {
  96. String result = ParserTestHelper.translateTo(makePageThatIncludesSetup());
  97. assertContains(result, "class=\"collapsible closed\"");
  98. assertContains(result, "<a href=\"PageTwo.SetUp\">");
  99. }
  100. @Test
  101. public void teardownsAreHiddenAndMarked() throws Exception {
  102. String result = ParserTestHelper.translateTo(makePageThatIncludesTeardown());
  103. assertContains(result, "class=\"collapsible closed teardown\"");
  104. assertContains(result, "<a href=\"PageTwo.TearDown\">");
  105. }
  106. private TestSourcePage makePageThatIncludesSetup() {
  107. return new TestSourcePage()
  108. .withContent("!include -setup >SetUp")
  109. .withTarget("PageTwo.SetUp")
  110. .withIncludedPage(new TestSourcePage().withContent("setup"));
  111. }
  112. private TestSourcePage makePageThatIncludesTeardown() {
  113. return new TestSourcePage()
  114. .withContent("!include -teardown >TearDown")
  115. .withTarget("PageTwo.TearDown")
  116. .withIncludedPage(new TestSourcePage().withContent("teardown"));
  117. }
  118. @Test
  119. public void shouldIncludePathWithNonWikiWordFollowedByNewLines() throws Exception {
  120. String result = ParserTestHelper.translateTo(makePageThatIncludesPageFromNonWikiWordPath("\n" +
  121. "\n" +
  122. "\n" +
  123. " "));
  124. assertContains(result, "class=\"collapsible\"");
  125. assertContains(result, "<a href=\"FrontPage.Tests.non_wiki_word\">");
  126. assertContains(result, "Hello world!");
  127. }
  128. @Test
  129. public void shouldIncludePathWithNonWikiWordFollowedBySpaces() throws Exception {
  130. String result = ParserTestHelper.translateTo(makePageThatIncludesPageFromNonWikiWordPath(" Some other text\n"));
  131. assertContains(result, "class=\"collapsible\"");
  132. assertContains(result, "<a href=\"FrontPage.Tests.non_wiki_word\">");
  133. assertContains(result, "Hello world!");
  134. }
  135. private TestSourcePage makePageThatIncludesPageFromNonWikiWordPath(String trailingContent) {
  136. return new TestSourcePage()
  137. .withContent("\n!include .FrontPage.Tests.non_wiki_word" + trailingContent)
  138. .withTarget("FrontPage.Tests.non_wiki_word")
  139. .withIncludedPage(new TestSourcePage().withContent("Hello world!"));
  140. }
  141. @Test
  142. public void translatesSetupWithoutCollapse() throws Exception {
  143. String result = ParserTestHelper.translateTo(makePageThatIncludesSetup(), new TestVariableSource("COLLAPSE_SETUP", "false"));
  144. assertContains(result, "class=\"collapsible\"");
  145. assertContains(result, "<a href=\"PageTwo.SetUp\">");
  146. }
  147. @Test
  148. public void translatesCollapsed() throws Exception {
  149. TestRoot root = new TestRoot();
  150. WikiPage includingPage = root.makePage("PageOne", "!include -c PageTwo");
  151. root.makePage("PageTwo", "two");
  152. String result = ParserTestHelper.translateTo(includingPage);
  153. assertContains(result, "class=\"collapsible closed\"");
  154. }
  155. @Test
  156. public void translatesSeamless() throws Exception {
  157. TestRoot root = new TestRoot();
  158. WikiPage includingPage = root.makePage("PageOne", "!include -seamless PageTwo");
  159. root.makePage("PageTwo", "two");
  160. ParserTestHelper.assertTranslatesTo(includingPage, "two");
  161. }
  162. @Test
  163. public void translatesHelp() throws Exception {
  164. TestRoot root = new TestRoot();
  165. WikiPage includingPage = root.makePage("PageOne", "!include -h PageTwo");
  166. WikiPage pageWithHelp = root.makePage("PageTwo", "two");
  167. PageData pageData = pageWithHelp.getData();
  168. pageData.setAttribute(PageData.PropertyHELP, "help me");
  169. pageWithHelp.commit(pageData);
  170. ParserTestHelper.assertTranslatesTo(includingPage, "help me");
  171. }
  172. private static final String NEW_LINE = System.getProperty("line.separator");
  173. private static final String HTML_ERR = ""
  174. + "<div class=\"collapsible\"><ul><li><a href='#' class='expandall'>Expand</a></li><li><a href='#' class='collapseall'>Collapse</a></li></ul>" + NEW_LINE //
  175. + "\t<p class=\"title\">Included page: %s</p>" + NEW_LINE
  176. + "\t<div><span class=\"error\">%s</span></div>" + NEW_LINE
  177. + "</div>" + NEW_LINE;
  178. @Test
  179. public void doesNotIncludeParent() throws Exception {
  180. TestRoot root = new TestRoot();
  181. WikiPage parent = root.makePage("ParentPage", "stuff");
  182. WikiPage currentPage = root.makePage(parent, "PageOne", "!include <ParentPage");
  183. ParserTestHelper.assertTranslatesTo(currentPage, String.format(HTML_ERR,
  184. "<a href=\"ParentPage\">&lt;ParentPage</a>",
  185. "Error! Cannot include parent page (&lt;ParentPage)."));
  186. }
  187. @Test
  188. public void doesNotIncludeInvalidPageName() throws Exception {
  189. TestRoot root = new TestRoot();
  190. WikiPage parent = root.makePage("ParentPage", "stuff");
  191. WikiPage currentPage = root.makePage(parent, "PageOne", "!include +not.a.+wiki.page");
  192. ParserTestHelper.assertTranslatesTo(currentPage, String.format(HTML_ERR,
  193. "+not.a.+wiki.page",
  194. "Page include failed because the page +not.a.+wiki.page does not have a valid wiki page name."));
  195. }
  196. @Test
  197. public void doesNotIncludeNotExistingPageName() throws Exception {
  198. TestRoot root = new TestRoot();
  199. WikiPage parent = root.makePage("ParentPage", "stuff");
  200. WikiPage currentPage = root.makePage(parent, "PageOne", "!include NotExistingPage");
  201. ParserTestHelper.assertTranslatesTo(currentPage, String.format(HTML_ERR,
  202. "NotExistingPage<a title=\"create page\" href=\"ParentPage.NotExistingPage?edit&amp;nonExistent=true\">[?]</a>",
  203. "Page include failed because the page NotExistingPage does not exist."));
  204. }
  205. private void assertContains(String result, String substring) {
  206. assertTrue(result, result.contains(substring));
  207. }
  208. }