Commit b7faac57 authored by hongchan's avatar hongchan Committed by Commit bot

Implement ownProperty() and inheritFrom() assertions in should()

This CL add two assertions that abstracts the checks for JavaScript
traits.

 - should().ownProperty()
 - should().inheritFrom()

BUG=702708
TEST=webaudio/unit-tests/audit.html

Review-Url: https://codereview.chromium.org/2805773002
Cr-Commit-Position: refs/heads/master@{#462970}
parent 958c1c97
......@@ -36,22 +36,17 @@
// AudioScheduledSourceNode must have these properties.
for (p in expectedProperties) {
should(
AudioScheduledSourceNode.prototype.hasOwnProperty(
expectedProperties[p]),
'AudioScheduledSourceNode.' + expectedProperties[p])
.beTrue();
should(AudioScheduledSourceNode.prototype,
'AudioScheduledSourceNode.prototype')
.haveOwnProperty(expectedProperties[p]);
}
// ConstantSource and Oscillator must not
var nodes = ['ConstantSourceNode', 'OscillatorNode'];
for (n in nodes) {
for (p in expectedProperties) {
should(
window[nodes[n]].prototype.hasOwnProperty(
expectedProperties[p]),
nodes[n] + '.' + expectedProperties[p])
.beFalse();
should(window[nodes[n]].prototype, nodes[n] + '.prototype')
.notHaveOwnProperty(expectedProperties[p]);
}
}
......@@ -59,18 +54,15 @@
// have the others.
for (p in expectedProperties) {
if (expectedProperties[p] !== 'start') {
should(
AudioBufferSourceNode.prototype.hasOwnProperty(
expectedProperties[p]),
'AudioBufferSourceNode.' + expectedProperties[p])
.beFalse();
should(AudioBufferSourceNode.prototype,
'AudioBufferSourceNode.prototype')
.notHaveOwnProperty(expectedProperties[p]);
}
}
should(
AudioBufferSourceNode.prototype.hasOwnProperty('start'),
'AudioBufferSourceNode.start')
.beTrue();
should(AudioBufferSourceNode.prototype,
'AudioBufferSourceNode.prototype')
.haveOwnProperty('start');
task.done();
});
......
......@@ -977,6 +977,76 @@ window.Audit = (function () {
'${actual} ' + passDetail,
'${actual} ' + failDetail);
}
/**
* Check if |expected| property is truly owned by |actual| object.
*
* @example
* should(BaseAudioContext.prototype,
* 'BaseAudioContext.prototype').haveOwnProperty('createGain');
*
* @result
* "PASS BaseAudioContext.prototype has an own property of
* 'createGain'."
*/
haveOwnProperty () {
this._processArguments(arguments);
return this._assert(
this._actual.hasOwnProperty(this._expected),
'${actual} has an own property of "${expected}".',
'${actual} does not own the property of "${expected}".');
}
/**
* Check if |expected| property is not owned by |actual| object.
*
* @example
* should(BaseAudioContext.prototype,
* 'BaseAudioContext.prototype')
* .notHaveOwnProperty('startRendering');
*
* @result
* "PASS BaseAudioContext.prototype does not have an own property of
* 'startRendering'."
*/
notHaveOwnProperty () {
this._processArguments(arguments);
return this._assert(
!this._actual.hasOwnProperty(this._expected),
'${actual} does not have an own property of "${expected}".',
'${actual} has an own the property of "${expected}".')
}
/**
* Check if an object is inherited from a class. This looks up the entire
* prototype chain of a given object and tries to find a match.
*
* @example
* should(sourceNode, 'A buffer source node')
* .inheritFrom('AudioScheduledSourceNode');
*
* @result
* "PASS A buffer source node inherits from 'AudioScheduledSourceNode'."
*/
inheritFrom () {
this._processArguments(arguments);
let prototypes = [];
let currentPrototype = Object.getPrototypeOf(this._actual);
while (currentPrototype) {
prototypes.push(currentPrototype.constructor.name);
currentPrototype = Object.getPrototypeOf(currentPrototype);
}
return this._assert(
prototypes.includes(this._expected),
'${actual} inherits from "${expected}".',
'${actual} does not inherit from "${expected}".');
}
}
......
......@@ -36,10 +36,14 @@ PASS 1 is less than or equal to 1.
PASS 1 is equal to 1.
PASS should(1).beEqualTo(1) is true.
PASS The message is truthful!
PASS BaseAudioContext.prototype has an own property of "createGain".
PASS BaseAudioContext.prototype does not have an own property of "startRendering".
PASS An AudioBufferSourceNode inherits from "AudioScheduledSourceNode".
PASS An AudioBufferSourceNode inherits from "AudioNode".
PASS Decoding audio data with no argument rejected correctly with TypeError: Failed to execute 'decodeAudioData' on 'BaseAudioContext': 1 argument required, but only 0 present..
PASS Suspending OAC with no argument rejected correctly with TypeError.
PASS Start OAC rendering resolved correctly.
PASS < [basic] All assertions passed. (total 22 assertions)
PASS < [basic] All assertions passed. (total 26 assertions)
PASS > [load-file-in-should] Test Audit.loadFileFromUrl() within |should| assertion.
PASS Loading file within should().beResolved() resolved correctly.
PASS < [load-file-in-should] All assertions passed. (total 1 assertions)
......
......@@ -36,6 +36,18 @@
should(should(1).beEqualTo(1), 'should(1).beEqualTo(1)').beTrue();
should(true, 'The message is').message('truthful!', 'false!');
should(BaseAudioContext.prototype, 'BaseAudioContext.prototype')
.haveOwnProperty('createGain');
should(BaseAudioContext.prototype, 'BaseAudioContext.prototype')
.notHaveOwnProperty('startRendering');
let ac = new AudioContext();
let sourceNode = new AudioBufferSourceNode(ac);
should(sourceNode, 'An AudioBufferSourceNode')
.inheritFrom('AudioScheduledSourceNode');
should(sourceNode, 'An AudioBufferSourceNode')
.inheritFrom('AudioNode');
let oac = new OfflineAudioContext(1, 128, 44100);
Promise.all([
should(oac.startRendering(), 'Start OAC rendering').beResolved(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment