Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-passiv committed Sep 12, 2016
1 parent a88d8ff commit 30f71f8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ One possible usecase is in Scala js projects to move html into their own file.
e.g.

```scala
import com.passivsystems.embed.Embed._
import org.scalajs.dom
def el[T <: dom.raw.Element](id: String) =
dom.document.getElementById(id).asInstanceOf[T]
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ version := "0.0.1"
scalaVersion := "2.11.7"

libraryDependencies <+= scalaVersion("org.scala-lang" % "scala-reflect" % _) // for scala macro

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % Test
5 changes: 3 additions & 2 deletions src/main/scala/Embed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ package object Embed {

def interpolateExpr(c: Context)(content: String): c.Expr[String] = {
import c.universe._
val parts = content.split("\\$\\{([^\\}]*)\\}").toSeq
val args = "\\$\\{([^\\}]*)\\}".r.findAllMatchIn(content).map(_.group(1)).map(c.parse(_)).toSeq
val regex = "\\$\\{([^\\}]*)\\}"
val parts = content.split(regex).toSeq
val args = regex.r.findAllMatchIn(content).map(_.group(1)).map(c.parse(_)).toSeq
c.Expr[String](q"scala.StringContext(..$parts).s(..$args)")
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/scala/EmbedTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import com.passivsystems.embed.Embed._

import org.scalatest._

class EmbedTest extends FlatSpec with Matchers {

private def contentOf(path: String) = {
val source = scala.io.Source.fromFile(path)
val content = source.mkString
source.close()
content
}

"Embed" should "include static file content" in {
val txt = embed("resource.txt")
val expectedTxt = contentOf("src/test/scala/resource.txt")
txt should be (expectedTxt)
}

it should "string interpolate static file content" in {
def test(guard: Boolean) = {
val txt = sEmbed("sResource.txt")
val expectedTxt = s"the guard is: ${if (guard) "TRUE" else "FALSE"}\n"
txt should be (expectedTxt)
}
test(true)
test(false)
}
}
3 changes: 3 additions & 0 deletions src/test/scala/resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce congue neque quam, in dignissim mi porttitor ut. Nunc pulvinar leo id commodo lobortis. Nunc magna felis, suscipit sed bibendum et, sodales iaculis nunc. Nunc eros velit, suscipit quis leo vitae, facilisis ultrices velit. Curabitur ultrices aliquam metus, id sagittis est semper id. Maecenas quam ex, lobortis at aliquet eget, lobortis eget est. Cras efficitur dolor vitae arcu aliquet, vel bibendum sem sollicitudin. Maecenas et tempus leo, eget aliquam quam. Aenean semper sollicitudin vestibulum.

Aliquam sed ex malesuada urna molestie pellentesque. In vel magna consectetur, efficitur massa id, aliquet risus. Integer quis ornare sem, non sollicitudin augue. Sed cursus tellus libero. Pellentesque imperdiet tempus aliquam. Proin efficitur, nulla sed tempus laoreet, odio velit imperdiet lectus, ut convallis augue metus eleifend nibh. Suspendisse egestas non augue eu dapibus. Phasellus quis viverra odio. Sed ut blandit ipsum. Integer scelerisque sollicitudin diam eu suscipit.
1 change: 1 addition & 0 deletions src/test/scala/sResource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the guard is: ${if (guard) "TRUE" else "FALSE"}

0 comments on commit 30f71f8

Please sign in to comment.