Skip to content

Instantly share code, notes, and snippets.

@koola
Created March 1, 2015 02:23
Show Gist options
  • Save koola/44365799933cc30bd9bd to your computer and use it in GitHub Desktop.
Save koola/44365799933cc30bd9bd to your computer and use it in GitHub Desktop.
Jasmine toDeepPartiallyEqual custom matcher
'use strict';
var customMatchers = {
toDeepPartiallyEqual: function (expected) {
var compare = function(obj1, obj2) {
for(var i in obj2) {
if(typeof obj2[i] !== typeof obj1[i]) {
return false
}
switch (typeof obj2[i]) {
case 'undefined':
return false;
case 'object':
if(!compare(obj1[i], obj2[i])) {
return false;
}
break;
default:
if(obj1[i] !== obj2[i]) {
return false;
}
}
}
return true;
};
var notText = this.isNot ? "not" : "";
this.message = function () {
return "Deep partially equal: \n\t" + JSON.stringify(this.actual) + "\nis " + notText + " equal to\n\t" + JSON.stringify(expected)
};
return compare(this.actual, expected)
}
};
ddescribe("Deep partially equal", function() {
beforeEach(function() {
this.addMatchers(customMatchers);
// https://api.github.com/gists/public
this.a = [
{
"url": "https://api.github.com/gists/1105b4801915a5b07139",
"forks_url": "https://api.github.com/gists/1105b4801915a5b07139/forks",
"commits_url": "https://api.github.com/gists/1105b4801915a5b07139/commits",
"id": "1105b4801915a5b07139",
"git_pull_url": "https://gist.github.com/1105b4801915a5b07139.git",
"git_push_url": "https://gist.github.com/1105b4801915a5b07139.git",
"html_url": "https://gist.github.com/1105b4801915a5b07139",
"files": {
"gistfile1.sh": {
"filename": "gistfile1.sh",
"type": "application/x-sh",
"language": [
{
"type": "Shell"
},
{
"type": "C"
}
],
"size": 329
}
}
}
];
});
it("should exactly equal JSON", function() {
expect(this.a).toDeepPartiallyEqual(this.a);
});
it("should deep partially equal JSON", function() {
var b = [
{
"files": {
"gistfile1.sh": {
"type": "application/x-sh",
"language": [
{
"type": "Shell"
},
{
"type": "C"
}
],
"size": 329
}
}
}
];
expect(this.a).toDeepPartiallyEqual(b);
});
it("should not deep partially equal JSON", function() {
var b = [
{
"files": {
"gistfile1.sh": {
"language": [
{
"type": "Shelll"
}
]
}
}
}
];
expect(this.a).not.toDeepPartiallyEqual(b);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment