|
| 1 | +/** |
| 2 | + * @author Victor Giovanni Beltrán Rodríguez |
| 3 | + * @file This file contains the test for the `argv2Object` function. |
| 4 | + */ |
| 5 | + |
| 6 | +// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 7 | +// » IMPORT NATIVE NODE MODULES |
| 8 | +const { describe, it, afterEach, beforeEach } = require('node:test'); |
| 9 | +const assert = require('node:assert'); |
| 10 | + |
| 11 | +// » IMPORT MODULES |
| 12 | +const argv2Object = require('..'); |
| 13 | + |
| 14 | +// ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 15 | +/** |
| 16 | + * This constant defines the errors that can be thrown by |
| 17 | + * the `argv2Object` function. |
| 18 | + * |
| 19 | + * @private |
| 20 | + * @constant {object} THROWS |
| 21 | + */ |
| 22 | +const THROWS = { |
| 23 | + NO_ARGS: { |
| 24 | + name: 'Error', |
| 25 | + message: 'No arguments added', |
| 26 | + }, |
| 27 | + NO_MATCH_SIMPLE: { |
| 28 | + name: 'Error', |
| 29 | + message: `Some argument(s) do not follow the 'key=value' format.`, |
| 30 | + }, |
| 31 | + NO_MATCH_UNIXMODE: { |
| 32 | + name: 'Error', |
| 33 | + message: `Some argument(s) do not follow the Unix-style command-line format.`, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +// ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 38 | + |
| 39 | +// ━━ TEST ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 40 | +describe('CryptoDriver', () => { |
| 41 | + const original = process.argv; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + process.argv = [...original]; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + process.argv = original; |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw an error when no arguments are provided', () => { |
| 52 | + process.argv = ['node', 'script.js']; |
| 53 | + assert.throws(() => argv2Object(), THROWS.NO_ARGS); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should throw an error when an argument does not match the expected format', () => { |
| 57 | + Object.assign(process.argv, { |
| 58 | + 2: '-task', |
| 59 | + 3: 'invalid-arg', |
| 60 | + }); |
| 61 | + assert.throws(() => argv2Object(), THROWS.NO_MATCH_SIMPLE); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should throw an error when a Unix-style argument does not match the expected format', () => { |
| 65 | + Object.assign(process.argv, { |
| 66 | + 2: 'task', |
| 67 | + 3: 'invalid-arg', |
| 68 | + }); |
| 69 | + assert.throws(() => argv2Object(true), THROWS.NO_MATCH_UNIXMODE); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should convert simple key-value pairs to an object', () => { |
| 73 | + Object.assign(process.argv, { |
| 74 | + 2: 'name=John', |
| 75 | + 3: 'age=30', |
| 76 | + 4: 'level=0', |
| 77 | + }); |
| 78 | + const result = argv2Object(); |
| 79 | + assert.deepStrictEqual(result, { name: 'John', age: '30', level: '0' }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should convert Unix-style options to an object', () => { |
| 83 | + Object.assign(process.argv, { |
| 84 | + 2: '-a', |
| 85 | + 3: '--name=John', |
| 86 | + 4: '--is-admin', |
| 87 | + }); |
| 88 | + const result = argv2Object(true); |
| 89 | + assert.deepStrictEqual(result, { a: true, name: 'John', is_admin: true }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments