helpers.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { on, once, off, emit, count } = require("sdk/event/core");
  6. function scenario(setup) {
  7. return function(unit) {
  8. return function(assert) {
  9. let actual = [];
  10. let input = {};
  11. unit(input, function(output, events, expected, message) {
  12. let result = setup(output, expected, actual);
  13. events.forEach(function(event) emit(input, "data", event));
  14. assert.deepEqual(actual, result, message);
  15. });
  16. }
  17. }
  18. }
  19. exports.emits = scenario(function(output, expected, actual) {
  20. on(output, "data", function(data) actual.push(this, data));
  21. return expected.reduce(function($$, $) $$.concat(output, $), []);
  22. });
  23. exports.registerOnce = scenario(function(output, expected, actual) {
  24. function listener(data) actual.push(data);
  25. on(output, "data", listener);
  26. on(output, "data", listener);
  27. on(output, "data", listener);
  28. return expected;
  29. });
  30. exports.ignoreNew = scenario(function(output, expected, actual) {
  31. on(output, "data", function(data) {
  32. actual.push(data + "#1");
  33. on(output, "data", function(data) {
  34. actual.push(data + "#2");
  35. });
  36. });
  37. return expected.map(function($) $ + "#1");
  38. });
  39. exports.FIFO = scenario(function(target, expected, actual) {
  40. on(target, "data", function($) actual.push($ + "#1"));
  41. on(target, "data", function($) actual.push($ + "#2"));
  42. on(target, "data", function($) actual.push($ + "#3"));
  43. return expected.reduce(function(result, value) {
  44. return result.concat(value + "#1", value + "#2", value + "#3");
  45. }, []);
  46. });