Last active
August 29, 2015 13:57
-
-
Save kcleereman/9632252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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