Added support for ES6 sets to toContain and toEqual.
This commit is contained in:
@@ -14,6 +14,25 @@ describe("jasmineUnderTest.pp", function () {
|
|||||||
expect(jasmineUnderTest.pp(-0)).toEqual("-0");
|
expect(jasmineUnderTest.pp(-0)).toEqual("-0");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('stringify sets', function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
|
||||||
|
it("should stringify sets properly", function() {
|
||||||
|
expect(jasmineUnderTest.pp(new Set([1, 2]))).toEqual("Set( 1, 2 )");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should truncate sets with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE", function() {
|
||||||
|
var originalMaxSize = jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE = 2;
|
||||||
|
expect(jasmineUnderTest.pp(new Set(["a", "b", "c"]))).toEqual("Set( 'a', 'b', ... )");
|
||||||
|
} finally {
|
||||||
|
jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE = originalMaxSize;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
describe('stringify arrays', function() {
|
describe('stringify arrays', function() {
|
||||||
it("should stringify arrays properly", function() {
|
it("should stringify arrays properly", function() {
|
||||||
expect(jasmineUnderTest.pp([1, 2])).toEqual("[ 1, 2 ]");
|
expect(jasmineUnderTest.pp([1, 2])).toEqual("[ 1, 2 ]");
|
||||||
|
|||||||
@@ -314,6 +314,42 @@ describe("matchersUtil", function() {
|
|||||||
|
|
||||||
expect(jasmineUnderTest.matchersUtil.equals(objA, objB)).toBe(false);
|
expect(jasmineUnderTest.matchersUtil.equals(objA, objB)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes when comparing two empty sets", function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(new Set(), new Set())).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes when comparing identical sets", function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
var setA = new Set([6, 5]);
|
||||||
|
var setB = new Set();
|
||||||
|
setB.add(6);
|
||||||
|
setB.add(5);
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails for sets with different elements", function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
var setA = new Set([6, 3, 5]);
|
||||||
|
var setB = new Set([6, 4, 5]);
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails for sets of different size", function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
var setA = new Set([6, 3]);
|
||||||
|
var setB = new Set([6, 4, 5]);
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails for sets with different insertion order", function() {
|
||||||
|
if (typeof Set === 'undefined') { return; }
|
||||||
|
var setA = new Set([3, 6]);
|
||||||
|
var setB = new Set([6, 3]);
|
||||||
|
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("contains", function() {
|
describe("contains", function() {
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
this.emitScalar('HTMLNode');
|
this.emitScalar('HTMLNode');
|
||||||
} else if (value instanceof Date) {
|
} else if (value instanceof Date) {
|
||||||
this.emitScalar('Date(' + value + ')');
|
this.emitScalar('Date(' + value + ')');
|
||||||
|
} else if (value.toString && value.toString() == '[object Set]') {
|
||||||
|
this.emitSet(value);
|
||||||
} else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) {
|
} else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) {
|
||||||
this.emitScalar(value.toString());
|
this.emitScalar(value.toString());
|
||||||
} else if (j$.util.arrayContains(this.seen, value)) {
|
} else if (j$.util.arrayContains(this.seen, value)) {
|
||||||
@@ -59,6 +61,7 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
||||||
|
PrettyPrinter.prototype.emitSet = j$.unimplementedMethod_;
|
||||||
PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_;
|
||||||
PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_;
|
||||||
PrettyPrinter.prototype.emitString = j$.unimplementedMethod_;
|
PrettyPrinter.prototype.emitString = j$.unimplementedMethod_;
|
||||||
@@ -115,6 +118,26 @@ getJasmineRequireObj().pp = function(j$) {
|
|||||||
this.append(' ]');
|
this.append(' ]');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
StringPrettyPrinter.prototype.emitSet = function(set) {
|
||||||
|
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||||
|
this.append('Set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.append('Set( ');
|
||||||
|
var size = Math.min(set.size, j$.MAX_PRETTY_PRINT_SET_SIZE);
|
||||||
|
var iter = set.values();
|
||||||
|
for (var i = 0; i < size; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
this.append(', ');
|
||||||
|
}
|
||||||
|
this.format(iter.next().value);
|
||||||
|
}
|
||||||
|
if (set.size > size){
|
||||||
|
this.append(', ...');
|
||||||
|
}
|
||||||
|
this.append(' )');
|
||||||
|
};
|
||||||
|
|
||||||
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
||||||
var constructorName = obj.constructor ? j$.fnNameFor(obj.constructor) : 'null';
|
var constructorName = obj.constructor ? j$.fnNameFor(obj.constructor) : 'null';
|
||||||
this.append(constructorName);
|
this.append(constructorName);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
|||||||
|
|
||||||
j$.MAX_PRETTY_PRINT_DEPTH = 40;
|
j$.MAX_PRETTY_PRINT_DEPTH = 40;
|
||||||
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 100;
|
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 100;
|
||||||
|
j$.MAX_PRETTY_PRINT_SET_SIZE = 100;
|
||||||
j$.DEFAULT_TIMEOUT_INTERVAL = 5000;
|
j$.DEFAULT_TIMEOUT_INTERVAL = 5000;
|
||||||
|
|
||||||
j$.getGlobal = function() {
|
j$.getGlobal = function() {
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
contains: function(haystack, needle, customTesters) {
|
contains: function(haystack, needle, customTesters) {
|
||||||
customTesters = customTesters || [];
|
customTesters = customTesters || [];
|
||||||
|
|
||||||
|
if ((Object.prototype.toString.apply(haystack) === '[object Set]')) {
|
||||||
|
return haystack.has(needle);
|
||||||
|
}
|
||||||
|
|
||||||
if ((Object.prototype.toString.apply(haystack) === '[object Array]') ||
|
if ((Object.prototype.toString.apply(haystack) === '[object Array]') ||
|
||||||
(!!haystack && !haystack.indexOf))
|
(!!haystack && !haystack.indexOf))
|
||||||
{
|
{
|
||||||
@@ -173,6 +177,19 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (className == '[object Set]') {
|
||||||
|
if (a.size != b.size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var iterA = a.values(), iterB = b.values();
|
||||||
|
var valA, valB;
|
||||||
|
do {
|
||||||
|
valA = iterA.next();
|
||||||
|
valB = iterB.next();
|
||||||
|
if (!eq(valA.value, valB.value, aStack, bStack, customTesters)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} while (!valA.done && !valB.done);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Objects with different constructors are not equivalent, but `Object`s
|
// Objects with different constructors are not equivalent, but `Object`s
|
||||||
|
|||||||
Reference in New Issue
Block a user