michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { on, once, off, emit, count } = require("sdk/event/core"); michael@0: michael@0: function scenario(setup) { michael@0: return function(unit) { michael@0: return function(assert) { michael@0: let actual = []; michael@0: let input = {}; michael@0: unit(input, function(output, events, expected, message) { michael@0: let result = setup(output, expected, actual); michael@0: michael@0: events.forEach(function(event) emit(input, "data", event)); michael@0: michael@0: assert.deepEqual(actual, result, message); michael@0: }); michael@0: } michael@0: } michael@0: } michael@0: michael@0: exports.emits = scenario(function(output, expected, actual) { michael@0: on(output, "data", function(data) actual.push(this, data)); michael@0: michael@0: return expected.reduce(function($$, $) $$.concat(output, $), []); michael@0: }); michael@0: michael@0: exports.registerOnce = scenario(function(output, expected, actual) { michael@0: function listener(data) actual.push(data); michael@0: on(output, "data", listener); michael@0: on(output, "data", listener); michael@0: on(output, "data", listener); michael@0: michael@0: return expected; michael@0: }); michael@0: michael@0: exports.ignoreNew = scenario(function(output, expected, actual) { michael@0: on(output, "data", function(data) { michael@0: actual.push(data + "#1"); michael@0: on(output, "data", function(data) { michael@0: actual.push(data + "#2"); michael@0: }); michael@0: }); michael@0: michael@0: return expected.map(function($) $ + "#1"); michael@0: }); michael@0: michael@0: exports.FIFO = scenario(function(target, expected, actual) { michael@0: on(target, "data", function($) actual.push($ + "#1")); michael@0: on(target, "data", function($) actual.push($ + "#2")); michael@0: on(target, "data", function($) actual.push($ + "#3")); michael@0: michael@0: return expected.reduce(function(result, value) { michael@0: return result.concat(value + "#1", value + "#2", value + "#3"); michael@0: }, []); michael@0: });