-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds in a Mustache template engine, tested against the official spec.
- Loading branch information
1 parent
2a0bc7c
commit 078c45e
Showing
24 changed files
with
2,350 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
branch/src/main/scala/dev/wishingtree/branch/mustachio/Delimiter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.wishingtree.branch.mustachio | ||
|
||
import java.util.regex.Pattern | ||
|
||
private[mustachio] case class Delimiter(open: String, close: String) { | ||
|
||
def closing(str: String): String = | ||
s"$open/$str$close" | ||
|
||
def section(str: String): String = | ||
s"$open#$str$close" | ||
|
||
def inversion(str: String): String = | ||
s"$open^$str$close" | ||
|
||
def partial(str: String): String = | ||
s"$open>$str$close" | ||
|
||
def comment(str: String): String = | ||
s"$open!$str$close" | ||
|
||
lazy val isDefault: Boolean = | ||
Delimiter.default.open.equals(open) && | ||
Delimiter.default.close.equals(close) | ||
} | ||
|
||
private[mustachio] object Delimiter { | ||
val default: Delimiter = Delimiter("{{", "}}") | ||
|
||
def replaceDefaultWith(content: String, newDelimiter: Delimiter): String = | ||
content | ||
.replaceAll(Pattern.quote(default.open), newDelimiter.open) | ||
.replaceAll(Pattern.quote(default.close), newDelimiter.close) | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
branch/src/main/scala/dev/wishingtree/branch/mustachio/MustacheSpecSuite.test.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package dev.wishingtree.branch.mustachio | ||
|
||
import dev.wishingtree.branch.friday.{Json, JsonDecoder} | ||
|
||
import scala.io.Source | ||
import scala.util.{Try, Using} | ||
|
||
case class Spec( | ||
name: String, | ||
desc: String, | ||
data: Json, | ||
template: String, | ||
expected: String, | ||
partials: Json | ||
) | ||
|
||
object Spec { | ||
|
||
// Because we're reading the tests from a file, | ||
// it will escape the newlines, tabs, and carriage returns. | ||
// We need to unescape them. | ||
extension (s: String) { | ||
def unescape: String = | ||
s | ||
.replaceAll("\\\\n", "\n") | ||
.replaceAll("\\\\r", "\r") | ||
.replaceAll("\\\\t", "\t") | ||
} | ||
|
||
given decoder: JsonDecoder[Spec] with { | ||
def decode(json: Json): Try[Spec] = Try { | ||
for { | ||
name <- json ? "name" | ||
desc <- json ? "desc" | ||
data <- json ? "data" | ||
template <- json ? "template" | ||
expected <- json ? "expected" | ||
partials <- (json ? "partials").orElse(Some(Json.obj())) | ||
} yield Spec( | ||
name.strVal.unescape, | ||
desc.strVal.unescape, | ||
data, | ||
template.strVal.unescape, | ||
expected.strVal.unescape, | ||
partials | ||
) | ||
}.map(_.get) | ||
} | ||
|
||
} | ||
|
||
case class SpecSuite(tests: IndexedSeq[Spec]) | ||
|
||
object SpecSuite { | ||
|
||
given decoder: JsonDecoder[SpecSuite] with { | ||
def decode(json: Json): Try[SpecSuite] = Try { | ||
for { | ||
tests <- json ? "tests" | ||
} yield SpecSuite(tests.arrVal.map(Spec.decoder.decode(_).get)) | ||
}.map(_.get) | ||
} | ||
|
||
} | ||
|
||
trait MustacheSpecSuite extends munit.FunSuite { | ||
|
||
def runSpec( | ||
spec: Spec | ||
)(implicit loc: munit.Location): Unit = { | ||
test(spec.name) { | ||
val context = Stache.fromJson(spec.data) | ||
val partials = Option(Stache.fromJson(spec.partials)) | ||
assertEquals( | ||
Mustachio.render( | ||
spec.template, | ||
context, | ||
partials | ||
), | ||
spec.expected | ||
) | ||
} | ||
} | ||
|
||
/** Attempts to load and parse a Moustache spec suite from a resource file. | ||
* @return | ||
*/ | ||
def specSuite(resource: String): SpecSuite = | ||
Using(Source.fromResource(resource)) { source => | ||
SpecSuite.decoder.decode(source.mkString) | ||
}.flatten | ||
.getOrElse(throw new Exception("Failed to parse json for specSuite")) | ||
} |
Oops, something went wrong.