From 6b927a211bbead9ae8cd1998a886c231a09e655d Mon Sep 17 00:00:00 2001 From: Edward A Maxedon Date: Fri, 27 Apr 2018 14:27:25 -0400 Subject: [PATCH] add bracket syntax support and unit tests --- README.md | 8 ++++---- build.sbt | 2 +- src/main/scala/Interpreter.scala | 10 ++++++++-- src/main/scala/Main.scala | 4 ++-- src/test/scala/TypesTests.scala | 15 +++++++++++++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 863a0c3..fca9489 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Liquescent [![Build Status](https://www.travis-ci.org/edadma/liquescent.svg?branch=master)](https://www.travis-ci.org/edadma/liquescent) [![Coverage Status](https://coveralls.io/repos/github/edadma/liquescent/badge.svg?branch=master)](https://coveralls.io/github/edadma/liquescent?branch=master) [![License](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/edadma/liquescent/blob/master/LICENSE) -[![Version](https://img.shields.io/badge/latest_release-v0.1.7-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.1.7) +[![Version](https://img.shields.io/badge/latest_release-v0.1.8-orange.svg)](https://github.com/edadma/liquescent/releases/tag/v0.1.8) *Liquescent* is an implementation of the [Liquid](https://shopify.github.io/liquid/) templating language for the [Scala](http://scala-lang.org) programming language. @@ -52,7 +52,7 @@ This program prints This next example shows how to use *Liquescent* as an executable on the command line. ```bash -echo "{{ v | join: \", \" }}" | java -jar liquescent-0.1.7.jar -j "{v: [\"one\", \"two\", \"three\"]}" -- +echo "{{ v | join: \", \" }}" | java -jar liquescent-0.1.8.jar -j "{v: [\"one\", \"two\", \"three\"]}" -- ``` The above command prints @@ -74,7 +74,7 @@ Use the following definition to use Liquescent in your Maven project: xyz.hyperreal liquescent - 0.1.7 + 0.1.8 ``` @@ -83,7 +83,7 @@ Add the following to your `build.sbt` file to use Liquescent in your SBT project ```sbt resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven" -libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.1.7" +libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.1.8" ``` Building diff --git a/build.sbt b/build.sbt index 61d6394..754631f 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "liquescent" -version := "0.1.7" +version := "0.1.8" scalaVersion := "2.12.5" diff --git a/src/main/scala/Interpreter.scala b/src/main/scala/Interpreter.scala index d2d8201..62d3f79 100644 --- a/src/main/scala/Interpreter.scala +++ b/src/main/scala/Interpreter.scala @@ -205,8 +205,14 @@ class Interpreter( filters: Map[String, Filter], tags: Map[String, Tag], assigns case (a: BigInt, b: BigInt) => a to b by 1 case (a, b) => sys.error( s"invalid range limits: $a, $b" ) }) toList - case DotExpressionAST( expr, name ) => - eval( expr ) match { + case ArrayExpressionAST( exp, index ) => + (eval( exp ), eval( index )) match { + case (m: collection.Map[_, _], i: String) => m.asInstanceOf[collection.Map[String, Any]](i) + case (s: Seq[_], i: Number) => s.asInstanceOf[Seq[Any]]( i.intValue ) + case (a, b) => sys.error( s"bracket syntax not applicable: $a, $b" ) + } + case DotExpressionAST( exp, name ) => + eval( exp ) match { case `nil` => nil case m: collection.Map[_, _] => m.asInstanceOf[collection.Map[String, Any]] get name match { diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index d5ac551..7422f70 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -15,9 +15,9 @@ object Main extends App { def usage { """ - |liquescent v0.1.7 + |liquescent v0.1.8 | - |Usage: java -jar liquescent-0.1.7.jar + |Usage: java -jar liquescent-0.1.8.jar | |Options: --help display this help and exit | -s assign to variable diff --git a/src/test/scala/TypesTests.scala b/src/test/scala/TypesTests.scala index 9db50e1..0671f41 100644 --- a/src/test/scala/TypesTests.scala +++ b/src/test/scala/TypesTests.scala @@ -11,4 +11,19 @@ class TypesTests extends FreeSpec with PropertyChecks with Matchers with Testing test( "The current user is {{ user.name }}", false ) shouldBe "The current user is " } + "bracket" in { + test( + """ + |{{ site.users[0] }} + |{{ site.users[1] }} + |{{ site.users[3] }} + """.stripMargin, true, "site" -> Map("users" -> List("Tobi", "Laura", "Tetsuro", "Adam")) + ) shouldBe "Tobi Laura Adam" + test( + """ + |{{ site['users'] | join: ", " }} + """.stripMargin, true, "site" -> Map("users" -> List("Tobi", "Laura", "Tetsuro", "Adam")) + ) shouldBe "Tobi, Laura, Tetsuro, Adam" + } + } \ No newline at end of file