Atlas › Test

argsert.mjs|title=Argsert configures function to accept 0 parameters, if only arguments object is provided|occurrence=1

Exact test identity: mocha:v1|namespace=yargs@4153e0f097aeaf43a71a2530db6dda51dff2c544:main|file=test/argsert.mjs|title=Argsert configures function to accept 0 parameters, if only arguments object is provided|occurrence=1

Package
mocha:v1|namespace=yargs@4153e0f097aeaf43a71a2530db6dda51dff2c544:main|file=test
Suite / test hierarchy
argsert.mjs|title=Argsert configures function to accept 0 parameters, if only arguments object is provided|occurrence=1
Test
argsert.mjs|title=Argsert configures function to accept 0 parameters, if only arguments object is provided|occurrence=1
Introduced at
argsert.ts ×1 Frontier kind: Joint frontier
Covered ranges
15
Covered lines
72
Covered files
3

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

lib/argsert.ts 49 covered LOC · 10 ranges

Open complete file

1 > import {YError} from './yerror.js'; argsert.ts
2 > import {parseCommand, ParsedCommand} from './parse-command.js';
3 >
4 > const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
5 > export function argsert(callerArguments: any[], length?: number): void;
6 > export function argsert(
7 > expected: string,
8 > callerArguments: any[],
9 > length?: number
10 > ): void;
11 > export function argsert(
12 > arg1: string | any[], argsert.ts
13 > arg2?: any[] | number,
14 > arg3?: number
15 > ): void {
16 > function parseArgs(): [
17 > Pick<ParsedCommand, 'demanded' | 'optional'>,
18 > any[],
19 > number?,
20 > ] {
21 > return typeof arg1 === 'object'
22 > ? [{demanded: [], optional: []}, arg1, arg2 as number | undefined]
23 > : [
24 parseCommand(`cmd ${arg1}`),
25 arg2 as any[],
26 arg3 as number | undefined,
27 ];
28 > } argsert.ts
29 > // TODO: should this eventually raise an exception.
30 > try {
31 > // preface the argument description with "cmd", so
32 > // that we can run it through yargs' command parser.
33 > let position = 0;
34 > const [parsed, callerArguments, _length] = parseArgs();
35 > const args = [].slice.call(callerArguments);
36 >
37 > while (args.length && args[args.length - 1] === undefined) args.pop();
38 > const length = _length || args.length;
39 >
40 > if (length < parsed.demanded.length) {
41 throw new YError(
42 `Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`
43 );
44 }
45 > argsert.ts
46 > const totalCommands = parsed.demanded.length + parsed.optional.length;
47 > if (length > totalCommands) {
48 > throw new YError( argsert.ts
49 > `Too many arguments provided. Expected max ${totalCommands} but received ${length}.`
50 > );
51 > }
52
53 parsed.demanded.forEach(demanded => {
73 position += 1;
74 });
75 > } catch (err) { argsert.ts
76 > console.warn((err as Error).stack); argsert.ts
77 > }
78 > } argsert.ts
79 > argsert.ts
80 function guessType(arg: any) {
81 if (Array.isArray(arg)) {
86 return typeof arg;
87 }
88 > argsert.ts
89 function argumentTypeError(
90 observedType: string,
lib/parse-command.ts 14 covered LOC · 2 ranges

Open complete file

1 > import {NotEmptyArray} from './typings/common-types.js'; parse-command.ts
2 >
3 > export function parseCommand(cmd: string) {
4 const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
5 const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
32 return parsedCommand;
33 }
35 > export interface ParsedCommand {
36 > cmd: string;
37 > demanded: Positional[];
38 > optional: Positional[];
39 > }
40 >
41 > export interface Positional {
42 > cmd: NotEmptyArray<string>;
43 > variadic: boolean;
44 > }
lib/yerror.ts 9 covered LOC · 3 ranges

Open complete file

1 > export class YError extends Error { yerror.ts
2 > name = 'YError';
3 > constructor(msg?: string | null) {
4 > super(msg || 'yargs error'); yerror.ts
5 > if (Error.captureStackTrace) {
6 > Error.captureStackTrace(this, YError);
7 > }
8 > }
9 > } yerror.ts