Skip to content

Instantly share code, notes, and snippets.

@kcleereman
Last active August 29, 2015 13:57
Show Gist options
  • Save kcleereman/9632252 to your computer and use it in GitHub Desktop.
Save kcleereman/9632252 to your computer and use it in GitHub Desktop.
def compareJsValue(mapped: JsValue, rendered: JsValue): Boolean = {
(mapped, rendered) match {
case (m: JsArray, r: JsArray) => compareJsArray(m, r)
case (JsBoolean(m), JsBoolean(r)) => m == r
case (JsNull, JsNull) => true
case (JsNumber(m), JsNumber(r)) => m == r
case (m: JsObject, r: JsObject) => compareJsObject(m, r)
case (JsString(m), JsString(r)) => m == r
case (m: JsUndefined, r: JsUndefined) => true
case _ => {
println("type mismatch: mapped = " + mapped, " ; rendered = " + rendered)
false
}
}
}
def compareJsObject(mapped: JsObject, rendered: JsObject): Boolean = {
var returnVal = true
val mappedFields = mapped.fields
val renderedMap = rendered.value
mappedFields.foreach(kv => {
renderedMap.get(kv._1) match {
case Some(v) => {
if(!compareJsValue(kv._2, v)) {
println("value mismatch on key " + kv._1 + " : mapped = " + kv._2 + ", rendered = " + v)
returnVal = false
}
}
case _ => {
println("missing key in rendered: " + kv._1)
returnVal = false
}
}
})
returnVal
}
def compareJsArray(mapped: JsArray, rendered: JsArray): Boolean = {
val diff = mapped.value.filterNot(v1 => rendered.value.exists(v2 => compareJsValue(v1, v2)))
if(diff.isEmpty) true else {
println("JsArray mismatch: " + diff)
false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment