Last active
December 17, 2015 04:28
-
-
Save antonmedv/5550597 to your computer and use it in GitHub Desktop.
Check json structure: expect json, {mustBe: yes, sub: {weNeedThis: yes, butNotThis: no}}
This file contains hidden or 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
exports.expect = expect = (json, pattern, throws = false, current = []) -> | |
condition = false | |
for own key, value of pattern | |
at = current.concat [key] | |
if typeof value is 'boolean' | |
condition = if value is true then json[key]? else not json[key]? | |
if not condition and throws | |
throw (if value is true then 'expected' else 'unexpected') + ' ' + at.join(' ') | |
else | |
if json[key]? | |
condition = expect json[key], value, throws, at | |
else | |
condition = false | |
if throws | |
throw 'expected ' + at.join(' ') | |
return condition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment