Skip to content

Instantly share code, notes, and snippets.

@tstone
Created December 20, 2013 15:52
Show Gist options
  • Save tstone/8056696 to your computer and use it in GitHub Desktop.
Save tstone/8056696 to your computer and use it in GitHub Desktop.
// Class
package presenters.poiattributes
import scala.language.implicitConversions
import play.api.templates.Html
trait AttributePresenter {
val identifier: String
val value: Any
def toHtml : Html
def isDefined : Boolean = {
value match {
case value: Seq[_] if value.isEmpty => false
case o: Option[_] => o match {
case None => false
case Some(_) => true
}
case _ => true
}
}
}
// SPEC
package presenters.poiattributes
import test.LoMSpec
import play.api.templates.Html
class AttributePresenterSpec extends LoMSpec {
"attribute presenter" should {
"require identifier and value" in {
GeneralAttribute("Test", None) must throwAn [IllegalArgumentException]
GeneralAttribute(None, "Test") must throwAn [IllegalArgumentException]
}
"have method toHtml" in {
GeneralAttribute("Test", "Test").toHtml.body must equalTo("Test")
}
"have method isDefined that returns a boolean if a valid value or non-empty seq" in {
GeneralAttribute("Test", Some(List(1, 2, 3))).isDefined must beTrue
GeneralAttribute("Test", "").isDefined must beTrue
GeneralAttribute("Test", None).isDefined must beFalse
GeneralAttribute("Test", Some(List())).isDefined must beFalse
}
}
}
case class GeneralAttribute(identifier: String, value: Option[Any]) extends AttributePresenter {
def toHtml : Html = Html(value.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment