|
1 | 1 | 'use strict'; |
| 2 | +require('../common'); |
2 | 3 | const assert = require('assert'); |
3 | | -const common = require('../common'); |
4 | 4 | const result = process.cpuUsage(); |
5 | 5 |
|
6 | 6 | // Validate the result of calling with no previous value argument. |
@@ -31,65 +31,80 @@ for (let i = 0; i < 10; i++) { |
31 | 31 | assert(diffUsage.user >= 0); |
32 | 32 | assert(diffUsage.system >= 0); |
33 | 33 | } |
34 | | -const invalidUserArgument = common.expectsError({ |
35 | | - code: 'ERR_INVALID_ARG_TYPE', |
36 | | - type: TypeError, |
37 | | - message: 'The "preValue.user" property must be of type number' |
38 | | -}, 8); |
39 | | - |
40 | | -const invalidSystemArgument = common.expectsError({ |
41 | | - code: 'ERR_INVALID_ARG_TYPE', |
42 | | - type: TypeError, |
43 | | - message: 'The "preValue.system" property must be of type number' |
44 | | -}, 2); |
45 | | - |
46 | 34 |
|
47 | 35 | // Ensure that an invalid shape for the previous value argument throws an error. |
48 | | -assert.throws(() => { |
49 | | - process.cpuUsage(1); |
50 | | -}, invalidUserArgument); |
51 | | - |
52 | | -assert.throws(() => { |
53 | | - process.cpuUsage({}); |
54 | | -}, invalidUserArgument); |
55 | | - |
56 | | -assert.throws(() => { |
57 | | - process.cpuUsage({ user: 'a' }); |
58 | | -}, invalidUserArgument); |
59 | | - |
60 | | -assert.throws(() => { |
61 | | - process.cpuUsage({ system: 'b' }); |
62 | | -}, invalidUserArgument); |
63 | | - |
64 | | -assert.throws(() => { |
65 | | - process.cpuUsage({ user: null, system: 'c' }); |
66 | | -}, invalidUserArgument); |
67 | | - |
68 | | -assert.throws(() => { |
69 | | - process.cpuUsage({ user: 'd', system: null }); |
70 | | -}, invalidUserArgument); |
71 | | - |
72 | | -assert.throws(() => { |
73 | | - process.cpuUsage({ user: -1, system: 2 }); |
74 | | -}, invalidUserArgument); |
75 | | - |
76 | | -assert.throws(() => { |
77 | | - process.cpuUsage({ user: 3, system: -2 }); |
78 | | -}, invalidSystemArgument); |
79 | | - |
80 | | -assert.throws(() => { |
81 | | - process.cpuUsage({ |
82 | | - user: Number.POSITIVE_INFINITY, |
83 | | - system: 4 |
84 | | - }); |
85 | | -}, invalidUserArgument); |
86 | | - |
87 | | -assert.throws(() => { |
88 | | - process.cpuUsage({ |
89 | | - user: 5, |
90 | | - system: Number.NEGATIVE_INFINITY |
91 | | - }); |
92 | | -}, invalidSystemArgument); |
| 36 | +assert.throws( |
| 37 | + () => process.cpuUsage(1), |
| 38 | + { |
| 39 | + code: 'ERR_INVALID_ARG_TYPE', |
| 40 | + name: 'TypeError [ERR_INVALID_ARG_TYPE]', |
| 41 | + message: 'The "prevValue" argument must be of type object. ' + |
| 42 | + 'Received type number' |
| 43 | + } |
| 44 | +); |
| 45 | + |
| 46 | +// Check invalid types. |
| 47 | +[ |
| 48 | + {}, |
| 49 | + { user: 'a' }, |
| 50 | + { user: null, system: 'c' }, |
| 51 | +].forEach((value) => { |
| 52 | + assert.throws( |
| 53 | + () => process.cpuUsage(value), |
| 54 | + { |
| 55 | + code: 'ERR_INVALID_ARG_TYPE', |
| 56 | + name: 'TypeError [ERR_INVALID_ARG_TYPE]', |
| 57 | + message: 'The "prevValue.user" property must be of type number. ' + |
| 58 | + `Received type ${typeof value.user}` |
| 59 | + } |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +[ |
| 64 | + { user: 3, system: 'b' }, |
| 65 | + { user: 3, system: null } |
| 66 | +].forEach((value) => { |
| 67 | + assert.throws( |
| 68 | + () => process.cpuUsage(value), |
| 69 | + { |
| 70 | + code: 'ERR_INVALID_ARG_TYPE', |
| 71 | + name: 'TypeError [ERR_INVALID_ARG_TYPE]', |
| 72 | + message: 'The "prevValue.system" property must be of type number. ' + |
| 73 | + `Received type ${typeof value.system}` |
| 74 | + } |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +// Check invalid values. |
| 79 | +[ |
| 80 | + { user: -1, system: 2 }, |
| 81 | + { user: Number.POSITIVE_INFINITY, system: 4 } |
| 82 | +].forEach((value) => { |
| 83 | + assert.throws( |
| 84 | + () => process.cpuUsage(value), |
| 85 | + { |
| 86 | + code: 'ERR_INVALID_OPT_VALUE', |
| 87 | + name: 'RangeError [ERR_INVALID_OPT_VALUE]', |
| 88 | + message: `The value "${value.user}" is invalid ` + |
| 89 | + 'for option "prevValue.user"' |
| 90 | + } |
| 91 | + ); |
| 92 | +}); |
| 93 | + |
| 94 | +[ |
| 95 | + { user: 3, system: -2 }, |
| 96 | + { user: 5, system: Number.NEGATIVE_INFINITY } |
| 97 | +].forEach((value) => { |
| 98 | + assert.throws( |
| 99 | + () => process.cpuUsage(value), |
| 100 | + { |
| 101 | + code: 'ERR_INVALID_OPT_VALUE', |
| 102 | + name: 'RangeError [ERR_INVALID_OPT_VALUE]', |
| 103 | + message: `The value "${value.system}" is invalid ` + |
| 104 | + 'for option "prevValue.system"' |
| 105 | + } |
| 106 | + ); |
| 107 | +}); |
93 | 108 |
|
94 | 109 | // Ensure that the return value is the expected shape. |
95 | 110 | function validateResult(result) { |
|
0 commit comments