testutil.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const assert = require('assert');
  19. const sinon = require('sinon');
  20. class StubError extends Error {
  21. constructor(opt_msg) {
  22. super(opt_msg);
  23. this.name = this.constructor.name;
  24. }
  25. }
  26. exports.StubError = StubError;
  27. exports.throwStubError = function() {
  28. throw new StubError;
  29. };
  30. exports.assertIsStubError = function(value) {
  31. assert.ok(value instanceof StubError, value + ' is not a ' + StubError.name);
  32. };
  33. exports.assertIsInstance = function(ctor, value) {
  34. assert.ok(value instanceof ctor, 'Not a ' + ctor.name + ': ' + value);
  35. };
  36. function callbackPair(cb, eb) {
  37. if (cb && eb) {
  38. throw new TypeError('can only specify one of callback or errback');
  39. }
  40. let callback = cb ? sinon.spy(cb) : sinon.spy();
  41. let errback = eb ? sinon.spy(eb) : sinon.spy();
  42. function assertCallback() {
  43. assert.ok(callback.called, 'callback not called');
  44. assert.ok(!errback.called, 'errback called');
  45. if (callback.threw()) {
  46. throw callback.exceptions[0];
  47. }
  48. }
  49. function assertErrback() {
  50. assert.ok(!callback.called, 'callback called');
  51. assert.ok(errback.called, 'errback not called');
  52. if (errback.threw()) {
  53. throw errback.exceptions[0];
  54. }
  55. }
  56. function assertNeither() {
  57. assert.ok(!callback.called, 'callback called');
  58. assert.ok(!errback.called, 'errback called');
  59. }
  60. return {
  61. callback,
  62. errback,
  63. assertCallback,
  64. assertErrback,
  65. assertNeither
  66. };
  67. }
  68. exports.callbackPair = callbackPair;
  69. exports.callbackHelper = function(cb) {
  70. let pair = callbackPair(cb);
  71. let wrapped = pair.callback.bind(null);
  72. wrapped.assertCalled = () => pair.assertCallback();
  73. wrapped.assertNotCalled = () => pair.assertNeither();
  74. return wrapped;
  75. };