Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Whats wrong here? #1

Closed
Cloud-Automation opened this issue Sep 28, 2014 · 6 comments
Closed

Whats wrong here? #1

Cloud-Automation opened this issue Sep 28, 2014 · 6 comments

Comments

@Cloud-Automation
Copy link

That is the output from gjstest. It is a simple array concatenation like arr = [1,2,3].concat[4,5,6] given to a callback with apply(foo, arr) and tested with expectCall(...)(1, 2, 3, 4, 5, 6).

events.js:17: Call matches no expectation.
(No arguments.)

Tried expectation at events.test.js:77, but number of arguments didn't match:
Arg 0: 1
Arg 1: 2
Arg 2: 3
Arg 3: 4
Arg 4: 5
Arg 5: 6

Unsatisfied expectation at events.test.js:77:
Arg 0: 1
Arg 1: 2
Arg 2: 3
Arg 3: 4
Arg 4: 5
Arg 5: 6

Expected 1 call; called 0 times.

@jacobsa
Copy link
Contributor

jacobsa commented Sep 28, 2014

That does look weird. But please provide a full, self-contained test to reproduce, and details about the version of gjstest and V8 that you are running.

@Cloud-Automation
Copy link
Author

There is a bit syntactic sugar in there but I hope that is enough:

events.js

Events.method('fireLater', function (name, args) {

if (args === undefined) {
    args = [];
}

return function () {

    var a = args.concat(arguments);

    this.fire(name, a===[]?a:undefined);

}.bind(this);

});

Events.method('on', function (name, func) {

if (!this._cbList[name]) {
    this._cbList[name] = [];
}

this._cbList[name].push(func);

return { name: name, index: this._cbList[name].length - 1 };

});

events.test.js

...EventsTest.prototype.fireEventLaterWithArgs = function () {

var subj = new Events();                                                              

expectCall(this.eventHandler)(1, 2, 3, 4, 5, 6);                                      

subj.on('test_event', this.eventHandler);
subj.fireLater('test_event', [1, 2, 3])([4, 5, 6]);                                   

};

@jacobsa
Copy link
Contributor

jacobsa commented Sep 28, 2014

A full and self-contained example, please. I'm looking one file that I can execute with gjstest without modification.

@Cloud-Automation
Copy link
Author

  • sugar.js

Function.prototype.method = function (name, func) {

this.prototype[name] = func;
return this;

};

Function.method('inherits', function (superCtor) {

this.super_ = superCtor;
this.prototype = Object.create(superCtor.prototype, {
    constructor: {
        value: this,
        enumerable: false,
        writable: true,
        configurable: true
    }
});

return this;

});

  • events.js

var Events = function () {

this._cbList = { };

};

Events.method('fire', function (name, args) {

if (!this._cbList[name]) {

    return;

}

for (var i in this._cbList[name]) {

    this._cbList[name][i].apply(this, args);

}

});

Events.method('fireLater', function (name, args) {

if (args === undefined) {
    args = [];
}

return function () {

    var a = args.concat(arguments);

    this.fire(name, a===[]?a:undefined);

}.bind(this);

});

Events.method('on', function (name, func) {

if (!this._cbList[name]) {
    this._cbList[name] = [];
}

this._cbList[name].push(func);

return { name: name, index: this._cbList[name].length - 1 };

});

Events.method('off', function (id) {

this._cbList[id.name].splice(id.index);

return this;

});

  • events.test.js

function EventsTest() {

this.eventHandler = createMockFunction();

}

registerTestSuite(EventsTest);

EventsTest.prototype.fireOneEvent = function () {

var subj = new Events();

expectCall(this.eventHandler)().times(1);

subj.on('test_event', this.eventHandler);
subj.fire('test_event');

};

EventsTest.prototype.fireTwoEvents = function () {

var subj = new Events();

expectCall(this.eventHandler)().times(2);

subj.on('test_event', this.eventHandler);
subj.fire('test_event');
subj.fire('test_event');
subj.fire('some_event');

};

EventsTest.prototype.fireOneEventWithParameters = function () {

var subj = new Events();

expectCall(this.eventHandler)(1, 2, 3);

subj.on('test_event', this.eventHandler);
subj.fire('test_event', [1, 2, 3]);

};

EventsTest.prototype.fireTwoEventsWithParameters = function () {

var subj = new Events();

expectCall(this.eventHandler)(1, 2, 3);
expectCall(this.eventHandler)(4, 5, 6);

subj.on('test_event', this.eventHandler);
subj.fire('test_event', [1, 2, 3]);
subj.fire('test_event', [4, 5, 6]);

};

EventsTest.prototype.fireEventLater = function () {

var subj = new Events();

expectCall(this.eventHandler)();

subj.on('test_event', this.eventHandler);
subj.fireLater('test_event')();

};

EventsTest.prototype.fireEventLaterWithArgs = function () {

var subj = new Events();

expectCall(this.eventHandler)(1, 2, 3, 4, 5, 6);

subj.on('test_event', this.eventHandler);
subj.fireLater('test_event', [1, 2, 3])([4, 5, 6]);

};

EventsTest.prototype.unsubscribeEventHandler = function () {

var subj = new Events(),
    hand = subj.on('test_event', this.eventHandler);

expectCall(this.eventHandler)().times(1);

subj.fire('test_event');
subj.off(hand);
subj.fire('test_event');

};

gjstest --js_files=sugar.js,events.js,events.test.js

google-js-test = latest
v8 = 3.25.30

@jacobsa
Copy link
Contributor

jacobsa commented Sep 30, 2014

Thanks. But there is a lot of JS magic there, and the way you have formatted your comment makes it exceedingly difficult to read. Please reduce this to a small test case—a single function on a single test suite that has no external dependencies, ideally.

@jacobsa
Copy link
Contributor

jacobsa commented Sep 30, 2014

I tried narrowing this down, and it seems to come down to this line:

this.fire(name, a===[]?a:undefined);

For the code in question, this causes fire to be called with undefined for
args, calling the mock callback with no arguments. Therefore the failure
message, which says "(No arguments.)", is correct.

So this is working as intended.

@jacobsa jacobsa closed this as completed Sep 30, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant