Path: blob/trunk/javascript/selenium-webdriver/test/lib/testutil.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const sinon = require('sinon')2122class StubError extends Error {23constructor(opt_msg) {24super(opt_msg)25this.name = this.constructor.name26}27}2829exports.StubError = StubError3031exports.throwStubError = function () {32throw new StubError()33}3435exports.assertIsStubError = function (value) {36assert.ok(value instanceof StubError, value + ' is not a ' + StubError.name)37}3839exports.assertIsInstance = function (ctor, value) {40assert.ok(value instanceof ctor, 'Not a ' + ctor.name + ': ' + value)41}4243function callbackPair(cb, eb) {44if (cb && eb) {45throw new TypeError('can only specify one of callback or errback')46}4748let callback = cb ? sinon.spy(cb) : sinon.spy()49let errback = eb ? sinon.spy(eb) : sinon.spy()5051function assertCallback() {52assert.ok(callback.called, 'callback not called')53assert.ok(!errback.called, 'errback called')54if (callback.threw()) {55throw callback.exceptions[0]56}57}5859function assertErrback() {60assert.ok(!callback.called, 'callback called')61assert.ok(errback.called, 'errback not called')62if (errback.threw()) {63throw errback.exceptions[0]64}65}6667function assertNeither() {68assert.ok(!callback.called, 'callback called')69assert.ok(!errback.called, 'errback called')70}7172return {73callback,74errback,75assertCallback,76assertErrback,77assertNeither,78}79}8081exports.callbackPair = callbackPair8283exports.callbackHelper = function (cb) {84let pair = callbackPair(cb)85let wrapped = pair.callback.bind(null)86wrapped.assertCalled = () => pair.assertCallback()87wrapped.assertNotCalled = () => pair.assertNeither()88return wrapped89}909192