|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const { strict: assert } = require('assert'); |
| 5 | +const { |
| 6 | + isJsonRpcFailure, |
| 7 | + isJsonRpcSuccess, |
| 8 | + getJsonRpcIdValidator, |
| 9 | +} = require('../dist'); |
| 10 | + |
| 11 | +describe('isJsonRpcSuccess', function () { |
| 12 | + it('correctly identifies JSON-RPC response objects', function () { |
| 13 | + assert.equal(isJsonRpcSuccess({ result: 'success' }), true); |
| 14 | + assert.equal(isJsonRpcSuccess({ result: undefined }), true); |
| 15 | + assert.equal(isJsonRpcSuccess({ error: new Error('foo') }), false); |
| 16 | + assert.equal(isJsonRpcSuccess({}), false); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('isJsonRpcFailure', function () { |
| 21 | + it('correctly identifies JSON-RPC response objects', function () { |
| 22 | + assert.equal(isJsonRpcFailure({ error: 'failure' }), true); |
| 23 | + assert.equal(isJsonRpcFailure({ error: undefined }), true); |
| 24 | + assert.equal(isJsonRpcFailure({ result: 'success' }), false); |
| 25 | + assert.equal(isJsonRpcFailure({}), false); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('getJsonRpcIdValidator', function () { |
| 30 | + const getInputs = () => { |
| 31 | + return { |
| 32 | + // invariant with respect to options |
| 33 | + fractionString: { value: '1.2', expected: true }, |
| 34 | + negativeInteger: { value: -1, expected: true }, |
| 35 | + object: { value: {}, expected: false }, |
| 36 | + positiveInteger: { value: 1, expected: true }, |
| 37 | + string: { value: 'foo', expected: true }, |
| 38 | + undefined: { value: undefined, expected: false }, |
| 39 | + zero: { value: 0, expected: true }, |
| 40 | + // variant with respect to options |
| 41 | + emptyString: { value: '', expected: true }, |
| 42 | + fraction: { value: 1.2, expected: false }, |
| 43 | + null: { value: null, expected: true }, |
| 44 | + }; |
| 45 | + }; |
| 46 | + |
| 47 | + const validateAll = (validate, inputs) => { |
| 48 | + for (const input of Object.values(inputs)) { |
| 49 | + assert.equal( |
| 50 | + validate(input.value), |
| 51 | + input.expected, |
| 52 | + `should output "${input.expected}" for "${input.value}"`, |
| 53 | + ); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + it('performs as expected with default options', function () { |
| 58 | + const inputs = getInputs(); |
| 59 | + |
| 60 | + // The default options are: |
| 61 | + // permitEmptyString: true, |
| 62 | + // permitFractions: false, |
| 63 | + // permitNull: true, |
| 64 | + validateAll(getJsonRpcIdValidator(), inputs); |
| 65 | + }); |
| 66 | + |
| 67 | + it('performs as expected with "permitEmptyString: false"', function () { |
| 68 | + const inputs = getInputs(); |
| 69 | + inputs.emptyString.expected = false; |
| 70 | + |
| 71 | + validateAll( |
| 72 | + getJsonRpcIdValidator({ |
| 73 | + permitEmptyString: false, |
| 74 | + permitFractions: false, |
| 75 | + permitNull: true, |
| 76 | + }), |
| 77 | + inputs, |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('performs as expected with "permitFractions: true"', function () { |
| 82 | + const inputs = getInputs(); |
| 83 | + inputs.fraction.expected = true; |
| 84 | + |
| 85 | + validateAll( |
| 86 | + getJsonRpcIdValidator({ |
| 87 | + permitEmptyString: true, |
| 88 | + permitFractions: true, |
| 89 | + permitNull: true, |
| 90 | + }), |
| 91 | + inputs, |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('performs as expected with "permitNull: false"', function () { |
| 96 | + const inputs = getInputs(); |
| 97 | + inputs.null.expected = false; |
| 98 | + |
| 99 | + validateAll( |
| 100 | + getJsonRpcIdValidator({ |
| 101 | + permitEmptyString: true, |
| 102 | + permitFractions: false, |
| 103 | + permitNull: false, |
| 104 | + }), |
| 105 | + inputs, |
| 106 | + ); |
| 107 | + }); |
| 108 | +}); |
0 commit comments