Skip to content
Snippets Groups Projects
Commit f25d690a authored by Brian Terlson's avatar Brian Terlson
Browse files

Merge pull request #348 from bocoup/WeakMap

Add tests for WeakMap
parents 53be57f9 7f1f806f
No related branches found
No related tags found
No related merge requests found
Showing
with 596 additions and 0 deletions
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1
description: >
The WeakMap constructor is the %WeakMap% intrinsic object and the initial
value of the WeakMap property of the global object.
---*/
assert.sameValue(
typeof WeakMap, 'function',
'typeof WeakMap is "function"'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
If the iterable argument is empty, return new WeakMap object.
info: >
23.3.1.1 WeakMap ( [ iterable ] )
...
9. Repeat
a. Let next be IteratorStep(iter).
b. ReturnIfAbrupt(next).
c. If next is false, return map.
...
---*/
var counter = 0;
var set = WeakMap.prototype.set;
WeakMap.prototype.set = function(value) {
counter++;
return set.call(this, value);
};
var map = new WeakMap([]);
assert.sameValue(Object.getPrototypeOf(map), WeakMap.prototype);
assert(map instanceof WeakMap);
assert.sameValue(
counter, 0,
'empty iterable does not call WeakMap.prototype.set'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Return abrupt after getting `set` method.
info: >
23.3.1.1 WeakMap ( [ iterable ] )
...
5. If iterable is not present, let iterable be undefined.
6. If iterable is either undefined or null, let iter be undefined.
7. Else,
a. Let adder be Get(map, "set").
b. ReturnIfAbrupt(adder).
...
---*/
Object.defineProperty(WeakMap.prototype, 'set', {
get: function() {
throw new Test262Error();
}
});
new WeakMap();
new WeakMap(null);
assert.throws(Test262Error, function() {
new WeakMap([]);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
If the iterable argument is undefined, return new WeakMap object.
info: >
23.3.1.1 WeakMap ( [ iterable ] )
...
7. Else,
d. Let iter be GetIterator(iterable).
e. ReturnIfAbrupt(iter).
...
---*/
assert.throws(TypeError, function() {
new WeakMap({});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Returns the new WeakMap adding the objects from the iterable parameter.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
k. Let status be Call(adder, map, «k.[[value]], v.[[value]]»).
l. If status is an abrupt completion, return IteratorClose(iter, status).
includes: [compareArray.js]
---*/
var first = {};
var second = {};
var results = [];
var set = WeakMap.prototype.set;
WeakMap.prototype.set = function(key, value) {
results.push({
_this: this,
key: key,
value: value
});
return set.call(this, key, value);
};
var map = new WeakMap([[first, 42], [second, 43]]);
assert.sameValue(results.length, 2, 'Called WeakMap#set for each object');
assert.sameValue(results[0].key, first, 'Adds object in order - first key');
assert.sameValue(results[0].value, 42, 'Adds object in order - first value');
assert.sameValue(results[0]._this, map, 'Adds object in order - this');
assert.sameValue(results[1].key, second, 'Adds object in order - second key');
assert.sameValue(results[1].value, 43, 'Adds object in order - second value');
assert.sameValue(results[1]._this, map, 'Adds object in order - this');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Return IteratorClose(iter, status) if fail on adding value on constructing.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
k. Let status be Call(adder, map, «k.[[value]], v.[[value]]»).
l. If status is an abrupt completion, return IteratorClose(iter, status).
---*/
var count = 0;
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
return { value: [], done: false };
},
return: function() {
count += 1;
}
};
};
WeakMap.prototype.set = function() { throw new Test262Error(); };
assert.throws(Test262Error, function() {
new WeakMap(iterable);
});
assert.sameValue(
count, 1,
'The iterator is closed when `WeakMap.prototype.set` throws an error.'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Closes iterator if item first entry completes abruptly.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
...
d. Let nextItem be IteratorValue(next).
...
g. Let k be Get(nextItem, "0").
h. If k is an abrupt completion, return IteratorClose(iter, k).
...
features: [Symbol.iterator]
---*/
var count = 0;
var item = ['foo', 'bar'];
Object.defineProperty(item, 0, {
get: function() {
throw new Test262Error();
}
});
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
return {
value: item,
done: false
};
},
return: function() {
count++;
}
};
};
assert.throws(Test262Error, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 1, 'The get error closed the iterator');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Closes iterator if item second entry completes abruptly.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
...
d. Let nextItem be IteratorValue(next).
...
i. Let v be Get(nextItem, "1").
j. If v is an abrupt completion, return IteratorClose(iter, v).
...
features: [Symbol.iterator]
---*/
var count = 0;
var item = ['foo', 'bar'];
Object.defineProperty(item, 1, {
get: function() {
throw new Test262Error();
}
});
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
return {
value: item,
done: false
};
},
return: function() {
count++;
}
};
};
assert.throws(Test262Error, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 1, 'The get error closed the iterator');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Closes the iterator object after not object error on next item.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
...
d. Let nextItem be IteratorValue(next).
e. ReturnIfAbrupt(nextItem).
f. If Type(nextItem) is not Object,
i. Let error be Completion{[[type]]: throw, [[value]]: a newly created
TypeError object, [[target]]:empty}.
ii. Return IteratorClose(iter, error).
features:
- Symbol
- Symbol.iterator
---*/
var count = 0;
var nextItem;
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
return { value: nextItem, done: false };
},
return: function() {
count += 1;
}
};
};
nextItem = 1;
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 1);
nextItem = true;
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 2);
nextItem = '';
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 3);
nextItem = null;
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 4);
nextItem = undefined;
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 5);
nextItem = Symbol('a');
assert.throws(TypeError, function() {
new WeakMap(iterable);
});
assert.sameValue(count, 6);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Throws a TypeError if iterable itens are not Objects.
info: >
WeakMap ( [ iterable ] )
...
9. Repeat
...
d. Let nextItem be IteratorValue(next).
e. ReturnIfAbrupt(nextItem).
f. If Type(nextItem) is not Object,
i. Let error be Completion{[[type]]: throw, [[value]]: a newly created
TypeError object, [[target]]:empty}.
ii. Return IteratorClose(iter, error).
features: [Symbol]
---*/
assert.throws(TypeError, function() {
new WeakMap([1, 1]);
});
assert.throws(TypeError, function() {
new WeakMap(['', 1]);
});
assert.throws(TypeError, function() {
new WeakMap([true, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([null, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([Symbol('a'), 1]);
});
assert.throws(TypeError, function() {
new WeakMap([undefined, 1]);
});
assert.throws(TypeError, function() {
new WeakMap([['a', 1], 2]);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
Return abrupt from next iterator step.
info: >
23.3.1.1 WeakMap ( [ iterable ] )
...
9. Repeat
a. Let next be IteratorStep(iter).
b. ReturnIfAbrupt(next).
...
---*/
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
throw new Test262Error();
}
};
};
assert.throws(Test262Error, function() {
new WeakMap(iterable);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
If the iterable argument is empty, return new WeakMap object.
info: >
23.3.1.1 WeakMap ( [ iterable ] )
...
9. Repeat
...
d. Let nextItem be IteratorValue(next).
e. ReturnIfAbrupt(nextItem).
...
---*/
var iterable = {};
iterable[Symbol.iterator] = function() {
return {
next: function() {
return {
get value() {
throw new Test262Error();
},
done: false
};
}
};
};
assert.throws(Test262Error, function() {
new WeakMap(iterable);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.2
description: >
The length property of the WeakMap constructor is 0.
includes: [propertyHelper.js]
---*/
assert.sameValue(WeakMap.length, 0, 'The value of `WeakMap.length` is `0`');
verifyNotEnumerable(WeakMap, 'length');
verifyNotWritable(WeakMap, 'length');
verifyConfigurable(WeakMap, 'length');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
WeakMap ( [ iterable ] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
WeakMap.name, 'WeakMap',
'The value of `WeakMap.name` is "WeakMap"'
);
verifyNotEnumerable(WeakMap, 'name');
verifyNotWritable(WeakMap, 'name');
verifyConfigurable(WeakMap, 'name');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.1.1
description: >
If the iterable argument is undefined, return new WeakMap object.
info: >
WeakMap ( [ iterable ] )
...
5. If iterable is not present, let iterable be undefined.
6. If iterable is either undefined or null, let iter be undefined.
...
8. If iter is undefined, return map.
...
---*/
var a = new WeakMap();
var b = new WeakMap(undefined);
var c = new WeakMap(null);
assert.sameValue(Object.getPrototypeOf(a), WeakMap.prototype);
assert.sameValue(Object.getPrototypeOf(b), WeakMap.prototype);
assert.sameValue(Object.getPrototypeOf(c), WeakMap.prototype);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.4
description: >
WeakMap instances are ordinary objects that inherit properties from the
WeakMap prototype.
---*/
assert.sameValue(
Object.getPrototypeOf(new WeakMap()),
WeakMap.prototype,
'`Object.getPrototypeOf(new WeakMap())` returns `WeakMap.prototype`'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.3
description: >
The WeakMap.prototype's prototype is Object.prototype.
info: >
23.3.3 Properties of the WeakMap Prototype Object
The WeakMap prototype object is the intrinsic object %WeakMapPrototype%. The
value of the [[Prototype]] internal slot of the WeakMap prototype object is
the intrinsic object %ObjectPrototype% (19.1.3). The WeakMap prototype object
is an ordinary object. It does not have a [[WeakMapData]] internal slot.
---*/
assert.sameValue(
Object.getPrototypeOf(WeakMap.prototype),
Object.prototype,
'`Object.getPrototypeOf(WeakMap.prototype)` returns `Object.prototype`'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.2
description: >
The value of the [[Prototype]] internal slot of the WeakMap constructor is the
intrinsic object %FunctionPrototype% (19.2.3).
---*/
assert.sameValue(
Object.getPrototypeOf(WeakMap),
Function.prototype,
'`Object.getPrototypeOf(WeakMap)` returns `Function.prototype`'
);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.3.6
description: "WeakMap#@@toStringTag value and writability"
info: >
WeakMap.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "WeakMap".
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
var WeakMapProto = WeakMap.prototype;
assert.sameValue(
WeakMapProto[Symbol.toStringTag],
'WeakMap',
'The value of WeakMap.prototype[Symbol.toStringTag] is "WeakMap"'
);
verifyNotEnumerable(WeakMapProto, Symbol.toStringTag);
verifyNotWritable(WeakMapProto, Symbol.toStringTag);
verifyConfigurable(WeakMapProto, Symbol.toStringTag);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 23.3.3.1
description: >
WeakMap.prototype.constructor value and property descriptor
info: >
The initial value of WeakMap.prototype.constructor is the %WeakMap%
intrinsic object.
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(WeakMap.prototype.constructor, WeakMap);
assert.sameValue((new WeakMap()).constructor, WeakMap);
verifyNotEnumerable(WeakMap.prototype, 'constructor');
verifyWritable(WeakMap.prototype, 'constructor');
verifyConfigurable(WeakMap.prototype, 'constructor');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment