Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent Filter #30

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lazy val lib = (project in file("."))
"com.github.mpilquist" %% "simulacrum" % "0.19.0",
"org.scalactic" %% "scalactic" % "3.0.8",
"org.scalatest" %% "scalatest" % "3.0.8" % Test,
"org.scalacheck" %% "scalacheck" % "1.14.0" % Test,
"org.webjars.npm" % "govuk-frontend" % "3.1.0" % Test
)
)
Expand Down
13 changes: 12 additions & 1 deletion src/main/scala/wolfendale/nunjucks/filters/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ package object filters {
_.toStr.copy(safe = true)
}

val indent: Filter = Filter {
(input, args) => {
val count : Int = Math.min(args.get(0).map(_.toNumeric) match {
case Some(Number(value)) => value.toInt
case _ => 2
}, 1000)

Str(input.toStr.value.replace("\n", "\n" + " " * count))
}
}

val noop: Filter = Filter {
a => a
}
Expand All @@ -103,7 +114,7 @@ package object filters {
"d" -> default,
"string" -> string,
"safe" -> safe,
"indent" -> noop
"indent" -> indent
)

private def mapString(value: Value, f: String => String): Value.Str = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ConcatSpec extends FreeSpec with MustMatchers {
tester.evaluate("(1/0) ~ 'world'") mustEqual Value.Str("Infinityworld")
}

"concatenation of strings with spaces is consistent" in {
tester.evaluate("'hello ' ~ 'world'") mustEqual tester.evaluate("'hello' ~ ' world'")
}

}

}
28 changes: 28 additions & 0 deletions src/test/scala/wolfendale/nunjucks/expression/StringSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package wolfendale.nunjucks.expression

import org.scalatest.{FreeSpec, MustMatchers}
import wolfendale.nunjucks.expression.runtime.Value

class StringSpec extends FreeSpec with MustMatchers {
val tester = new ExpressionTester()

"should evaluate simple string" in {
tester.evaluate("\"hello\"") mustEqual Value.Str("hello")
}

"should evaluate string that starts with tab" in {
tester.evaluate("\"\u0009hello\"") mustEqual Value.Str("\u0009hello")
}

"should evaluate string that ends with tab" in {
tester.evaluate("\"hello\u0009\"") mustEqual Value.Str("hello\u0009")
}

"should evaluate string that starts with space" in {
tester.evaluate("\" hello\"") mustEqual Value.Str(" hello")
}

"should evaluate string that ends with space" in {
tester.evaluate("\"hello \"") mustEqual Value.Str("hello ")
}
}
120 changes: 120 additions & 0 deletions src/test/scala/wolfendale/nunjucks/filters/IndentSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package wolfendale.nunjucks.filters

import org.scalatest.{FreeSpec, MustMatchers}
import wolfendale.nunjucks.expression.ExpressionTester
import wolfendale.nunjucks.expression.runtime.Value
import org.scalacheck.{Properties, Gen}
import org.scalacheck.Prop.forAll
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import wolfendale.nunjucks.Frame


object IndentProperties extends Properties("Indent filter") {
val tester = new ExpressionTester()

val singleLineStringLiteral: Gen[String] =
(Arbitrary.arbitrary[String].map(x =>
x.replace("\n", "")
.replace("\r", "")
.replace("\\", "\\\\")
.replace("\"", "\\\""))).suchThat(!_.contains("\n"))

val singleOrMultiLineString: Gen[String] = (for {
noOfLines <- Gen.choose(0, 100)
lines <- Gen.listOfN(noOfLines, singleLineStringLiteral)
} yield lines.mkString("\n"))

val multiLineString: Gen[String] = singleOrMultiLineString suchThat (_.contains("\n"))

def indentCallAsString(count: Int, indentFirst: Boolean) = s"indent(${count}, ${indentFirst})"

property("Should not change single line strings when indentFirstLine is false") =
forAll(arbitrary[Int], singleLineStringLiteral) {(count, input) =>
tester.evaluate("\"" + input + "\" | " + indentCallAsString(count, false)).toStr.value == input
}

property("When the input has multiple lines the output should contain more characters than the input") =
forAll(arbitrary[Int] suchThat(_ > 0), arbitrary[Boolean], multiLineString) {(count, indentFirstLine, input) =>
tester.evaluate("\"" + input + "\" | " + indentCallAsString(count, indentFirstLine)).toStr.value.length() > input.length()
}

val countPairGen = for {
a <- Gen.choose(0, 1000)
b <- Gen.choose(0, 1000 - a)
} yield (a, b)
property("subsequent applications are the same as addition") =
forAll(countPairGen, arbitrary[Boolean], singleOrMultiLineString) {(counts, indentFirstLine, input) =>
tester.evaluate("\"" + input + "\" | " +
indentCallAsString(counts._1, indentFirstLine) + " | " +
indentCallAsString(counts._2, indentFirstLine )).toStr.value ==
tester.evaluate("\"" + input + "\" | " +
indentCallAsString(counts._1 + counts._2, indentFirstLine)).toStr.value
}

property("Adds only spaces") =
forAll(arbitrary[Int], arbitrary[Boolean], singleOrMultiLineString suchThat(!_.contains(' '))) {(count, indentFirstLine, input) =>
tester.evaluate("\"" + input + "\" | " + indentCallAsString(count, indentFirstLine)).toStr.value.replace(" ", "") == input
}
property("orignal lines are preserved as tails of new lines") =
forAll(arbitrary[Int], arbitrary[Boolean], singleOrMultiLineString) {(count, indentFirstLine, input) => {
tester.evaluate(s""""${input}" | indent """)
.toStr.value.split('\n')
.zip(input.split('\n'))
.forall {
case (indented, original) => indented.takeRight(original.length) == original
}
}}

property("indenting by more than 1000 characters is the same as indenting by 1000") =
forAll(Gen.chooseNum(1001, Int.MaxValue), arbitrary[Boolean], singleOrMultiLineString) {(count, indentFirstLine, input) => {
val x = tester.evaluate(s""""${input}" | indent(${count}, ${indentFirstLine})""").toStr.value
val y = tester.evaluate(s""""${input}" | indent(1000, ${indentFirstLine})""").toStr.value
x == y
}}

property("indenting by less than 0 characters is the same as indenting by 0") =
forAll(Gen.chooseNum(Int.MinValue, -1), arbitrary[Boolean], singleOrMultiLineString) {(count, indentFirstLine, input) => {
val x = tester.evaluate(s""""${input}" | indent(${count}, ${indentFirstLine})""").toStr.value
val y = tester.evaluate(s""""${input}" | indent(0, ${indentFirstLine})""").toStr.value
x == y
}}

property("calling without parameters behaves exactly like calling with parameters (2, false)") =
forAll(singleLineStringLiteral) {(i:String) => {
val x = tester.evaluate("\"" + i + "\" | indent").toStr.value
val y = tester.evaluate("\"" + i + "\" | indent(2, false)").toStr.value
x == y
}}

property("calling without second parameter behaves exactly like calling with second parameter set to false") =
forAll(arbitrary[Int], singleLineStringLiteral) {(count : Int, i:String) =>
tester.evaluate("\"" + i + "\" | indent(" + count + ")").toStr.value ==
tester.evaluate("\"" + i + "\" | indent(" + count + ", false)").toStr.value
}
}

class IndentCaseTests extends FreeSpec with MustMatchers {
val tester = new ExpressionTester()

"Should indent second and subsequent lines" in {
tester.evaluate("\"aaaa\nbbbb\ncccc\" | indent") mustEqual Value.Str("aaaa\n bbbb\n cccc")
}

"Cope with empty input" in {
tester.evaluate("\"\" | indent") mustEqual Value.Str("")
}

"Can indent the first line" in {
tester.evaluate("\"aaaa\nbbbb\nccc\" | indent(4, true)") mustEqual Value.Str(" aaaa\n bbbb\n cccc")
}

"cope with lines that start with tabs" in {
tester.evaluate("\"\u0009boo\" | indent") mustEqual Value.Str("\u0009boo")
}

"cope with lines that start with tabs directly in the function" in {
val emptyParams = Value.Function.Parameters(Nil)
indent.apply(Frame.empty, Value.Str("\u0009boo"), emptyParams).toStr.value mustEqual "\u0009boo"
}
}