From dbbb7a3d9a668bbb8b62bec38f065f2444dacb91 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 1 Feb 2017 14:02:45 +0100 Subject: Refactor templates and pages to deal with `SourceFile` This commit is the first step towards having reportable errors in the template files --- doc-tool/test/SourceFileOps.scala | 19 ++++++ .../tools/dottydoc/staticsite/PageTests.scala | 70 +++++++++++++--------- .../tools/dottydoc/staticsite/SiteTests.scala | 7 ++- 3 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 doc-tool/test/SourceFileOps.scala (limited to 'doc-tool/test') diff --git a/doc-tool/test/SourceFileOps.scala b/doc-tool/test/SourceFileOps.scala new file mode 100644 index 000000000..7b0c2e807 --- /dev/null +++ b/doc-tool/test/SourceFileOps.scala @@ -0,0 +1,19 @@ +package dotty.tools +package dottydoc +package staticsite + +import dotc.util.SourceFile +import java.io.{ BufferedWriter, OutputStreamWriter } +import io.VirtualFile +import scala.io.Codec + +trait SourceFileOps { + def stringToSource(path: String, sourceCode: String): SourceFile = { + val virtualFile = new VirtualFile(path, path) + val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) + writer.write(sourceCode) + writer.close() + + new SourceFile(virtualFile, Codec.UTF8) + } +} diff --git a/doc-tool/test/dotty/tools/dottydoc/staticsite/PageTests.scala b/doc-tool/test/dotty/tools/dottydoc/staticsite/PageTests.scala index 14886b681..20a41e70b 100644 --- a/doc-tool/test/dotty/tools/dottydoc/staticsite/PageTests.scala +++ b/doc-tool/test/dotty/tools/dottydoc/staticsite/PageTests.scala @@ -5,19 +5,45 @@ package staticsite import org.junit.Test import org.junit.Assert._ -class PageTests extends DottyDocTest { +import model.Package + +class PageTests extends DottyDocTest with SourceFileOps { import scala.collection.JavaConverters._ + private def markdownPage( + sourceCode: String, + path: String = "test-page", + params: Map[String, AnyRef] = Map.empty, + includes: Map[String, Include] = Map.empty, + docs: Map[String, Package] = Map.empty + ) = new MarkdownPage( + path, + stringToSource(path, sourceCode), + params, + includes, + docs + ) + + private def htmlPage( + sourceCode: String, + path: String = "test-page", + params: Map[String, AnyRef] = Map.empty, + includes: Map[String, Include] = Map.empty, + docs: Map[String, Package] = Map.empty + ) = new HtmlPage( + path, + stringToSource(path, sourceCode), + params, + includes + ) + @Test def mdHas1Key = { - val page = new MarkdownPage( + val page = markdownPage( """|--- |key: |--- | - |great""".stripMargin, - Map.empty, - Map.empty, - Map.empty + |great""".stripMargin ) assert( @@ -29,15 +55,13 @@ class PageTests extends DottyDocTest { } @Test def yamlPreservesLiquidTags = { - val page1 = new MarkdownPage( + val page1 = markdownPage( """|--- |key: |--- | |{{ content }}""".stripMargin, - Map("content" -> "Hello, world!"), - Map.empty, - Map.empty + params = Map("content" -> "Hello, world!") ) assert( @@ -47,11 +71,9 @@ class PageTests extends DottyDocTest { assertEquals("

Hello, world!

\n", page1.html) - val page2 = new MarkdownPage( + val page2 = markdownPage( """|{{ content }}""".stripMargin, - Map("content" -> "hello"), - Map.empty, - Map.empty + params = Map("content" -> "hello") ) assert( page2.yaml == Map(), @@ -59,13 +81,11 @@ class PageTests extends DottyDocTest { ) assertEquals("

hello

\n", page2.html) - val page3 = new MarkdownPage( + val page3 = markdownPage( """|{% if product.title == "Awesome Shoes" %} |These shoes are awesome! |{% endif %}""".stripMargin, - Map("product" -> Map("title" -> "Awesome Shoes").asJava), - Map.empty, - Map.empty + params = Map("product" -> Map("title" -> "Awesome Shoes").asJava) ) assertEquals( @@ -75,20 +95,18 @@ class PageTests extends DottyDocTest { } @Test def simpleHtmlPage = { - val p1 = new HtmlPage("""

{{ "hello, world!" }}

""", Map.empty, Map.empty) + val p1 = htmlPage("""

{{ "hello, world!" }}

""") assert(p1.yaml == Map(), "non-empty yaml found") assertEquals("

hello, world!

", p1.html) } @Test def htmlPageHasNoYaml = { - val page = new HtmlPage( + val page = htmlPage( """|--- |layout: main |--- | - |Hello, world!""".stripMargin, - Map.empty, - Map.empty + |Hello, world!""".stripMargin ) assert(!page.html.contains("---\nlayout: main\n---"), @@ -96,14 +114,12 @@ class PageTests extends DottyDocTest { } @Test def illegalYamlFrontMatter = try { - val page = new HtmlPage( + val page = htmlPage( """|--- |layout: main | | - |Hello, world!""".stripMargin, - Map.empty, - Map.empty + |Hello, world!""".stripMargin ) page.html diff --git a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala index ba431a5c9..77b49700c 100644 --- a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala +++ b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala @@ -5,15 +5,16 @@ package staticsite import org.junit.Test import org.junit.Assert._ -class SiteTests extends DottyDocTest { +class SiteTests extends DottyDocTest with SourceFileOps { import scala.collection.JavaConverters._ val site = new Site(new java.io.File("../doc-tool/resources/"), "test-site", Map.empty) private def html( str: String, + path: String = "test-page", params: Map[String, AnyRef] = Map("docs" -> List.empty.asJava), - includes: Map[String, String] = Map.empty - ) = new HtmlPage(str, params, includes) + includes: Map[String, Include] = Map.empty + ) = new HtmlPage(path, stringToSource(path, str), params, includes) @Test def hasCorrectLayoutFiles = { assert(site.root.exists && site.root.isDirectory, -- cgit v1.2.3