addon-sdk/source/test/event/helpers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/event/helpers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const { on, once, off, emit, count } = require("sdk/event/core");
    1.11 +
    1.12 +function scenario(setup) {
    1.13 +  return function(unit) {
    1.14 +    return function(assert) {
    1.15 +      let actual = [];
    1.16 +      let input = {};
    1.17 +      unit(input, function(output, events, expected, message) {
    1.18 +        let result = setup(output, expected, actual);
    1.19 +
    1.20 +        events.forEach(function(event) emit(input, "data", event));
    1.21 +
    1.22 +        assert.deepEqual(actual, result, message);
    1.23 +      });
    1.24 +    }
    1.25 +  }
    1.26 +}
    1.27 +
    1.28 +exports.emits = scenario(function(output, expected, actual) {
    1.29 +  on(output, "data", function(data) actual.push(this, data));
    1.30 +
    1.31 +  return expected.reduce(function($$, $) $$.concat(output, $), []);
    1.32 +});
    1.33 +
    1.34 +exports.registerOnce = scenario(function(output, expected, actual) {
    1.35 +  function listener(data) actual.push(data);
    1.36 +  on(output, "data", listener);
    1.37 +  on(output, "data", listener);
    1.38 +  on(output, "data", listener);
    1.39 +
    1.40 +  return expected;
    1.41 +});
    1.42 +
    1.43 +exports.ignoreNew = scenario(function(output, expected, actual) {
    1.44 +  on(output, "data", function(data) {
    1.45 +    actual.push(data + "#1");
    1.46 +    on(output, "data", function(data) {
    1.47 +      actual.push(data + "#2");
    1.48 +    });
    1.49 +  });
    1.50 +
    1.51 +  return expected.map(function($) $ + "#1");
    1.52 +});
    1.53 +
    1.54 +exports.FIFO = scenario(function(target, expected, actual) {
    1.55 +  on(target, "data", function($) actual.push($ + "#1"));
    1.56 +  on(target, "data", function($) actual.push($ + "#2"));
    1.57 +  on(target, "data", function($) actual.push($ + "#3"));
    1.58 +
    1.59 +  return expected.reduce(function(result, value) {
    1.60 +    return result.concat(value + "#1", value + "#2", value + "#3");
    1.61 +  }, []);
    1.62 +});

mercurial