Skip to content

Commit

Permalink
Compilation works for Scala 2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp committed May 17, 2011
1 parent 8cf3d35 commit 95bbf60
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 133 deletions.
Expand Up @@ -35,19 +35,21 @@ object JsonBoxSerializerSpec extends Specification("JsonBoxSerializer Specificat
parse("""{"name":"joe"}""").extract[Person] mustEqual Person("joe", Empty, Empty)
}

"Extract boxed age" in {
parse("""{"name":"joe", "age":12}""").extract[Person] mustEqual Person("joe", Full(12), Empty)
"Extract boxed thing" in {
parse("""{"name":"joe", "thing": "rog", "age":12}""").extract[Person] mustEqual Person("joe", Full(12), Empty, Full("rog"))
}



"Extract boxed mother" in {
val json = """{"name":"joe", "age":12, "mother": {"name":"ann", "age":53}}"""
val p = parse(json).extract[Person]
p mustEqual Person("joe", Full(12), Full(Person("ann", Full(53), Empty)))
(for { a1 <- p.age; m <-p.mother; a2 <- m.age } yield a1+a2) mustEqual Full(65)
(for { a1 <- p.age; m <-p.mother; a2 <- m.age } yield a1+a2) mustEqual Some(65)
}

"Render with age" in {
swrite(Person("joe", Full(12), Empty)) mustEqual """{"name":"joe","age":12,"mother":null}"""
swrite(Person("joe", Full(12), Empty)) mustEqual """{"name":"joe","age":12,"mother":null,"thing":null}"""
}

"Serialize failure" in {
Expand All @@ -68,5 +70,5 @@ object JsonBoxSerializerSpec extends Specification("JsonBoxSerializer Specificat

case class SomeException(msg: String) extends Exception

case class Person(name: String, age: Box[Int], mother: Box[Person])
case class Person(name: String, age: Option[Int], mother: Box[Person], thing: Box[String] = Empty)

80 changes: 80 additions & 0 deletions core/util/src/main/scala/net/liftweb/util/BasicTypesHelpers.scala
Expand Up @@ -77,6 +77,86 @@ trait BasicTypesHelpers { self: StringHelpers with ControlHelpers =>
}
}

/**
* Compare two NodeSeq and return true if they are equal, even if
* attribute order of Elems is different
*/
def compareXml(left: NodeSeq, right: NodeSeq): Boolean = {
val ls: Seq[Node] = left.toSeq
val rs: Seq[Node] = right.toSeq
if (ls.length == rs.length) {
ls.zip(rs).foldLeft(true){case (b, (l, r)) => b && compareNode(l, r)}
} else {
false
}
}

/**
* Compare two Elems
*/
def compareElem(left: Elem, right: Elem): Boolean =
compareXml(left.child, right.child) &&
left.label == right.label &&
(((null eq left.prefix) && (null eq right.prefix)) || left.prefix == right.prefix) &&
left.scope == right.scope &&
compareMetaData(left.attributes.toList, right.attributes.toList)

private def findFilter(m: MetaData, lst: List[MetaData]): Box[List[MetaData]] = {
var found = false
val ret = lst.filter {
case PrefixedAttribute(pre, label, value, _) if !found =>
m match {
case PrefixedAttribute(p2, l2, v2, _) if p2 == pre && l2 == label && v2.text == value.text =>
found = true
false
case _ => true
}
case UnprefixedAttribute(label, value, _) if !found =>
m match {
case UnprefixedAttribute(l2, v2, _) if l2 == label && v2.text == value.text =>
found = true
false
case _ => true
}
case _ => true
}
if (found) Full(ret) else Empty
}

/**
* Compare the metadata of two attributes
*/
def compareMetaData(left: List[MetaData], right: List[MetaData]): Boolean =
(left, right) match {
case (Nil, Nil) => true
case (_, Nil) => false
case (Nil, _) => false
case (attr :: rl, right) => findFilter(attr, right) match {
case Full(rr) => compareMetaData(rl, rr)
case _ => false
}
case _ => false
}

/**
* Comparse two XML nodes
*/
def compareNode(left: Node, right: Node): Boolean = {
(left, right) match {
case (Group(gl), Group(gr)) => compareXml(gl, gr)
case (el: Elem, er: Elem) => compareElem(el, er)
case (Unparsed(tl), Unparsed(tr)) => tl == tr
case (Text(tl), Text(tr)) => tl == tr

case (el: EntityRef, er: EntityRef) => el === er
case (Comment(cl), Comment(cr)) => cl == cr
case (PCData(dl), PCData(dr)) => dl == dr
case (pl: ProcInstr, pr: ProcInstr) => pl === pr
case (a, b) => a.toString == b.toString
}

}

/**
* Implicit transformation from a Boolean expression to an OptionalCons object so
* that an element can be added to a list if the expression is true
Expand Down

0 comments on commit 95bbf60

Please sign in to comment.