This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01-parse-argument.js
90 lines (76 loc) · 2.36 KB
/
01-parse-argument.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
describe('parseArgument', function () {
const $parseArg = require('../lib/parse-argument');
let concatTask = (gulp, concat) => {
gulp.task('concat', () => {
return gulp
.src('src/**/*.txt')
.pipe(gulp.dest('docs'));
});
};
it('parses the function notation', () => {
let info = $parseArg(concatTask);
assert.deepEqual(info.params, ['gulp', 'concat']);
assert.equal(info.fn.toString(), concatTask.toString());
});
it('parses the minification-safe notation', () => {
let info = $parseArg(['gulp', 'concat', concatTask]);
assert.deepEqual(info.params, ['gulp', 'concat']);
assert.equal(info.fn.toString(), concatTask.toString());
});
it('parses a function without any arguments', () => {
let fn = function () {
console.log(new Date() + '');
};
let info = $parseArg(fn);
assert.deepEqual(info.params, []);
assert.equal(info.fn.toString(), fn.toString());
});
it('parses a function without any arguments (minification-safe notation)', () => {
let info = $parseArg([function () {}]);
assert.deepEqual(info.params, []);
assert.equal(typeof info.fn, 'function');
});
it('throws an error when no valid function was declared', () => {
let tests = [
'',
0,
1,
null,
void 0,
{},
['test'],
[null, function () {}],
[0],
[' ', function () {}],
[' ', function () {}],
['', function () {}],
[' ', function (space) {}]
];
tests.forEach((test) => assert.throws(() => {
let info = $parseArg(test);
assert.ok(Array.isArray(info));
}));
});
it('can be used with functions and dependencies', () => {
let dependencies = {
A: 'a',
B: 'b',
C: 'c'
};
let abc = ['A', 'B', 'C', (A, B, C) => `${A}${B}${C}`];
// Solely changing the order in the minification-safe dependency notation
let cab0 = ['C', 'A', 'B', (A, B, C) => `${A}${B}${C}`];
// Using function parameters
let cab1 = (C, A, B) => `${C}${A}${B}`;
let getDependencies = (params) => params.map((key) => dependencies[key]);
let check = (arg, expected) => {
let info = $parseArg(arg);
let args = getDependencies(info.params);
assert.equal(expected, info.fn.apply(null, args));
};
check(abc, 'abc');
check(cab0, 'cab');
check(cab1, 'cab');
});
});