From eb497a8f5db949ea661042371fbbc908f6524ea4 Mon Sep 17 00:00:00 2001 From: Iurii Malchenko Date: Fri, 4 Jan 2019 18:32:57 +0200 Subject: adding support for customizable constructor annotations, codec and "inclusive dot" (#481) --- .../core/views/hello.scala.html | 8 +++ .../core/views/wrapper.scala.html | 5 ++ .../hello-world/core/views/hello.scala.html | 2 +- contrib/twirllib/test/src/HelloWorldTests.scala | 74 ++++++++++++++++++---- 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/hello.scala.html create mode 100644 contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/wrapper.scala.html (limited to 'contrib/twirllib/test') diff --git a/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/hello.scala.html b/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/hello.scala.html new file mode 100644 index 00000000..16053d72 --- /dev/null +++ b/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/hello.scala.html @@ -0,0 +1,8 @@ +@this(title: String) +@wrapper { + + +

@title

+ + +} \ No newline at end of file diff --git a/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/wrapper.scala.html b/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/wrapper.scala.html new file mode 100644 index 00000000..af1f5d8e --- /dev/null +++ b/contrib/twirllib/test/resources/hello-world-inclusive-dot/core/views/wrapper.scala.html @@ -0,0 +1,5 @@ +@(content: Html) + +@defining("test") { className => +
@content
+} \ No newline at end of file diff --git a/contrib/twirllib/test/resources/hello-world/core/views/hello.scala.html b/contrib/twirllib/test/resources/hello-world/core/views/hello.scala.html index acadf615..3603fe07 100644 --- a/contrib/twirllib/test/resources/hello-world/core/views/hello.scala.html +++ b/contrib/twirllib/test/resources/hello-world/core/views/hello.scala.html @@ -1,4 +1,4 @@ -@(title: String) +@this(title: String)

@title

diff --git a/contrib/twirllib/test/src/HelloWorldTests.scala b/contrib/twirllib/test/src/HelloWorldTests.scala index 71f25ff4..67344ea6 100644 --- a/contrib/twirllib/test/src/HelloWorldTests.scala +++ b/contrib/twirllib/test/src/HelloWorldTests.scala @@ -13,28 +13,40 @@ object HelloWorldTests extends TestSuite { } trait HelloWorldModule extends mill.twirllib.TwirlModule { - def twirlVersion = "1.0.0" - override def twirlAdditionalImports: Seq[String] = additionalImports + + def twirlVersion = "1.3.15" + } object HelloWorld extends HelloBase { object core extends HelloWorldModule { - override def twirlVersion = "1.3.15" + override def twirlAdditionalImports: Seq[String] = testAdditionalImports + override def twirlConstructorAnnotations: Seq[String] = testConstructorAnnotations } + } - val resourcePath: os.Path = os.pwd / 'contrib / 'twirllib / 'test / 'resources / "hello-world" + object HelloWorldWithInclusiveDot extends HelloBase { + + object core extends HelloWorldModule { + override def twirlInclusiveDot: Boolean = true + } + + } def workspaceTest[T]( m: TestUtil.BaseModule, - resourcePath: os.Path = resourcePath + resourcePathSuffix: String )(t: TestEvaluator => T)(implicit tp: TestPath): T = { val eval = new TestEvaluator(m) os.remove.all(m.millSourcePath) os.remove.all(eval.outPath) os.makeDir.all(m.millSourcePath / os.up) - os.copy(resourcePath, m.millSourcePath) + os.copy( + os.pwd / 'contrib / 'twirllib / 'test / 'resources / resourcePathSuffix, + m.millSourcePath + ) t(eval) } @@ -52,15 +64,20 @@ object HelloWorldTests extends TestSuite { "import _root_.play.twirl.api.Xml" ) - def additionalImports: Seq[String] = Seq( + def testAdditionalImports: Seq[String] = Seq( "mill.twirl.test.AdditionalImport1._", "mill.twirl.test.AdditionalImport2._" ) + def testConstructorAnnotations = Seq( + "@org.springframework.stereotype.Component()", + "@something.else.Thing()" + ) + def tests: Tests = Tests { 'twirlVersion - { - 'fromBuild - workspaceTest(HelloWorld) { eval => + 'fromBuild - workspaceTest(HelloWorld, "hello-world") { eval => val Right((result, evalCount)) = eval.apply(HelloWorld.core.twirlVersion) @@ -70,7 +87,7 @@ object HelloWorldTests extends TestSuite { ) } } - 'compileTwirl - workspaceTest(HelloWorld) { eval => + 'compileTwirl - workspaceTest(HelloWorld, "hello-world") { eval => val Right((result, evalCount)) = eval.apply(HelloWorld.core.compileTwirl) val outputFiles = os.walk(result.classes.path).filter(_.last.endsWith(".scala")) @@ -86,8 +103,43 @@ object HelloWorldTests extends TestSuite { evalCount > 0, outputFiles.forall { p => val lines = os.read.lines(p).map(_.trim) - (expectedDefaultImports ++ additionalImports.map(s => s"import $s")).forall(lines.contains) - } + (expectedDefaultImports ++ testAdditionalImports.map(s => s"import $s")).forall(lines.contains) + }, + outputFiles.filter(_.toString().contains("hello.template.scala")).forall { p => + val lines = os.read.lines(p).map(_.trim) + val expectedClassDeclaration = s"class hello ${testConstructorAnnotations.mkString}" + lines.exists(_.startsWith(expectedClassDeclaration)) + }, + + ) + + // don't recompile if nothing changed + val Right((_, unchangedEvalCount)) = + eval.apply(HelloWorld.core.compileTwirl) + + assert(unchangedEvalCount == 0) + } + 'compileTwirlInclusiveDot - workspaceTest(HelloWorldWithInclusiveDot, "hello-world-inclusive-dot") { eval => + val Right((result, evalCount)) = eval.apply(HelloWorldWithInclusiveDot.core.compileTwirl) + + val outputFiles = os.walk(result.classes.path).filter(_.last.endsWith(".scala")) + val expectedClassfiles = compileClassfiles.map( name => + eval.outPath / 'core / 'compileTwirl / 'dest / 'html / name.toString().replace(".template.scala", "$$TwirlInclusiveDot.template.scala") + ) + + println(s"outputFiles: $outputFiles") + + assert( + result.classes.path == eval.outPath / 'core / 'compileTwirl / 'dest, + outputFiles.nonEmpty, + outputFiles.forall(expectedClassfiles.contains), + outputFiles.size == 2, + evalCount > 0, + outputFiles.filter(_.toString().contains("hello.template.scala")).forall { p => + val lines = os.read.lines(p).map(_.trim) + lines.exists(_.contains("$$TwirlInclusiveDot")) + }, + ) // don't recompile if nothing changed -- cgit v1.2.3