a_mock 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,5 +1,14 @@
1
1
  type AnyFunction = (...args: any[]) => any;
2
2
  type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
3
+ type FunctionProps<T> = T extends (...args: any[]) => any
4
+ ? { [K in Exclude<keyof T, keyof Function>]: Mocked<T[K]> }
5
+ : {};
6
+ type MockedFunction<T extends AnyFunction> = MockFunction<
7
+ Parameters<T>,
8
+ ReturnType<T>
9
+ > &
10
+ T &
11
+ FunctionProps<T>;
3
12
 
4
13
  interface RepeatControl {
5
14
  repeat(times: number): RepeatControl;
@@ -28,6 +37,8 @@ interface ExpectationChain<TArgs extends any[], R>
28
37
 
29
38
  interface MockFunction<TArgs extends any[], R> {
30
39
  (...args: TArgs): R;
40
+ // Allow any-args calls in addition to the original signature.
41
+ (...args: any[]): any;
31
42
  expect(): ExpectationTerminal<R, TArgs>;
32
43
  expect(...args: TArgs): ExpectationTerminal<R, TArgs>;
33
44
  expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
@@ -36,12 +47,14 @@ interface MockFunction<TArgs extends any[], R> {
36
47
  expectArray(
37
48
  value: TArgs[0] extends any[] ? TArgs[0] : any[]
38
49
  ): ExpectationChain<Tail<TArgs>, R>;
50
+ // Allow any-args expectations in addition to typed expectations.
51
+ expect(...args: any[]): ExpectationTerminal<any, any[]>;
39
52
  verify(): true;
40
53
  reset(): void;
41
54
  }
42
55
 
43
- type Mocked<T> = T extends (...args: infer A) => infer R
44
- ? MockFunction<A, R>
56
+ type Mocked<T> = T extends AnyFunction
57
+ ? MockedFunction<T>
45
58
  : T extends object
46
59
  ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
47
60
  : T;
@@ -69,7 +82,7 @@ declare function mock<TArgs extends any[] = any[], R = any>(): MockFunction<
69
82
  >;
70
83
  declare function mock<T extends AnyFunction>(
71
84
  original: T
72
- ): MockFunction<Parameters<T>, ReturnType<T>>;
85
+ ): MockedFunction<T>;
73
86
  declare function mock<T extends object>(subject: T): Mocked<T>;
74
87
 
75
88
  declare function expectRequire(moduleName: string): RequireExpectation;
@@ -1,16 +1,39 @@
1
- var propertyMock = require('./propertyMock');
2
-
3
- function execute(subject,mockObject) {
4
- if (!(subject instanceof Object))
5
- return;
6
- var mockFuncProperties = require('./mockFuncProperties');
7
- for(var propertyName in subject) {
8
- var property = subject[propertyName];
9
- var mockProperty = mockObject[propertyName];
10
- if (property instanceof Function)
11
- subject[propertyName] = propertyMock(subject,propertyName,mockProperty);
12
- mockFuncProperties(property,mockProperty);
13
- }
14
- }
1
+ var propertyMock = require('./propertyMock');
2
+
3
+ function collectPropertyNames(subject) {
4
+ var names = {};
5
+ for (var name in subject) {
6
+ names[name] = true;
7
+ }
8
+ var proto = Object.getPrototypeOf(subject);
9
+ while (proto && proto !== Object.prototype && proto !== Function.prototype) {
10
+ var props = Object.getOwnPropertyNames(proto);
11
+ for (var i = 0; i < props.length; i++) {
12
+ var prop = props[i];
13
+ if (prop === 'constructor' || names[prop])
14
+ continue;
15
+ var desc = Object.getOwnPropertyDescriptor(proto, prop);
16
+ if (desc && typeof desc.value === 'function')
17
+ names[prop] = true;
18
+ }
19
+ proto = Object.getPrototypeOf(proto);
20
+ }
21
+ return Object.keys(names);
22
+ }
23
+
24
+ function execute(subject,mockObject) {
25
+ if (!(subject instanceof Object))
26
+ return;
27
+ var mockFuncProperties = require('./mockFuncProperties');
28
+ var propertyNames = collectPropertyNames(subject);
29
+ for(var i = 0; i < propertyNames.length; i++) {
30
+ var propertyName = propertyNames[i];
31
+ var property = subject[propertyName];
32
+ var mockProperty = mockObject[propertyName];
33
+ if (property instanceof Function)
34
+ subject[propertyName] = propertyMock(subject,propertyName,mockProperty);
35
+ mockFuncProperties(property,mockProperty);
36
+ }
37
+ }
15
38
 
16
39
  module.exports = execute;
@@ -1,23 +1,48 @@
1
- var newMockContext = require('./mockContext');
2
- var newPartialMock = require('../partialMock');
3
-
4
- function _new(subject,mockContext) {
5
- var newObjectMock = require('./objectMock');
6
-
7
- var mockContext = newMockContext(mockContext);
8
- var mock = {};
9
- mock.verify = mockContext.verify;
10
- if (subject instanceof Function) {
11
- mock = newPartialMock(subject);
12
- mockContext.verify.add(mock.verify);
13
- }
14
- if (!(subject instanceof Object))
15
- return mock;
16
- for(var propertyName in subject) {
17
- var property = subject[propertyName];
18
- mock[propertyName] = newObjectMock(property,mockContext);
19
- }
20
- return mock;
21
- }
1
+ var newMockContext = require('./mockContext');
2
+ var newPartialMock = require('../partialMock');
3
+
4
+ function collectPropertyNames(subject) {
5
+ var names = {};
6
+ for (var name in subject) {
7
+ names[name] = true;
8
+ }
9
+ var proto = Object.getPrototypeOf(subject);
10
+ while (proto && proto !== Object.prototype && proto !== Function.prototype) {
11
+ var props = Object.getOwnPropertyNames(proto);
12
+ for (var i = 0; i < props.length; i++) {
13
+ var prop = props[i];
14
+ if (prop === 'constructor' || names[prop])
15
+ continue;
16
+ var desc = Object.getOwnPropertyDescriptor(proto, prop);
17
+ if (desc && typeof desc.value === 'function')
18
+ names[prop] = true;
19
+ }
20
+ proto = Object.getPrototypeOf(proto);
21
+ }
22
+ return Object.keys(names);
23
+ }
24
+
25
+ function _new(subject,mockContext,parent) {
26
+ var newObjectMock = require('./objectMock');
27
+
28
+ var mockContext = newMockContext(mockContext);
29
+ var mock = {};
30
+ mock.verify = mockContext.verify;
31
+ if (subject instanceof Function) {
32
+ mock = newPartialMock(subject, parent);
33
+ mockContext.verify.add(mock.verify);
34
+ }
35
+ if (!(subject instanceof Object))
36
+ return mock;
37
+ var propertyNames = collectPropertyNames(subject);
38
+ for(var i = 0; i < propertyNames.length; i++) {
39
+ var propertyName = propertyNames[i];
40
+ if (Object.prototype.hasOwnProperty.call(mock, propertyName))
41
+ continue;
42
+ var property = subject[propertyName];
43
+ mock[propertyName] = newObjectMock(property,mockContext,subject);
44
+ }
45
+ return mock;
46
+ }
22
47
 
23
48
  module.exports = _new
package/mock.js CHANGED
@@ -3,16 +3,14 @@ var newObjectMock = require('./mock/objectMock');
3
3
  var newPartialMock = require('./partialMock')
4
4
  var mockFuncProperties = require('./mock/mockFuncProperties')
5
5
 
6
- function create(subject) {
7
- if (subject == undefined)
8
- return newStrictMock();
9
- if (subject instanceof Function)
10
- return newPartialMock(subject);
11
- var mock = newObjectMock(subject);
12
- mockFuncProperties(subject,mock);
13
- return mock;
14
-
15
- }
6
+ function create(subject) {
7
+ if (subject == undefined)
8
+ return newStrictMock();
9
+ var mock = newObjectMock(subject);
10
+ mockFuncProperties(subject,mock);
11
+ return mock;
12
+
13
+ }
16
14
 
17
15
  module.exports = create;
18
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a_mock",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "title": "a_mock",
@@ -7,13 +7,14 @@ function execute(returnValue,fallback,hasCorrectArguments,mockContext,shouldDecr
7
7
  args.push(arguments[index]);
8
8
  index++;
9
9
  }
10
- if (hasCorrectArguments.apply(null,args)) {
11
- negotiateDecrementExpectCount(shouldDecrementExpectCount,mockContext);
12
- whenCalledEmitter.emit.apply(null,args);
13
- return returnValue;
14
- }
15
-
16
- return fallback.apply(null,args);
17
- }
10
+ if (hasCorrectArguments.apply(null,args)) {
11
+ negotiateDecrementExpectCount(shouldDecrementExpectCount,mockContext);
12
+ whenCalledEmitter.emit.apply(null,args);
13
+ return returnValue;
14
+ }
15
+
16
+ var receiver = mockContext && mockContext.thisArg != null ? mockContext.thisArg : this;
17
+ return fallback.apply(receiver,args);
18
+ }
18
19
 
19
- module.exports = execute;
20
+ module.exports = execute;
@@ -1,15 +1,16 @@
1
- var newFallbackWrapper = require('../newFallbackWrapper');
2
- var newAnd = require('../../newMutableAnd')
1
+ var newFallbackWrapper = require('../newFallbackWrapper');
2
+ var newAnd = require('../../newMutableAnd')
3
+
4
+ function _new(mockContext,originalFallback,thisArg) {
5
+ var and = newAnd();
6
+ mockContext.execute = newFallbackWrapper(originalFallback, thisArg);
7
+ mockContext.originalFallback = originalFallback;
8
+ mockContext.thisArg = thisArg;
9
+ mockContext.lastExecute = mockContext.execute;
10
+ mockContext.compositeAreCorrectArguments = and;
11
+ mockContext.expectCount = 0;
12
+
13
+ return mockContext;
14
+ }
3
15
 
4
- function _new(mockContext,originalFallback) {
5
- var and = newAnd();
6
- mockContext.execute = newFallbackWrapper(originalFallback);
7
- mockContext.originalFallback = originalFallback;
8
- mockContext.lastExecute = mockContext.execute;
9
- mockContext.compositeAreCorrectArguments = and;
10
- mockContext.expectCount = 0;
11
-
12
- return mockContext;
13
- }
14
-
15
- module.exports = _new;
16
+ module.exports = _new;
@@ -3,14 +3,14 @@ function newExecute(returnValue,hasCorrectArguments,mockContext,shouldDecrementE
3
3
  var _fallback = mockContext.originalFallback;
4
4
  var whenCalledEmitter = mockContext.whenCalledEmitter;
5
5
 
6
- function execute() {
7
- var args = [returnValue,_fallback,hasCorrectArguments,mockContext,shouldDecrementExpectCount,whenCalledEmitter];
8
- for(var index = 0; index < arguments.length; index++) {
9
- args.push(arguments[index]);
10
- }
11
- return _execute.apply(null,args);
12
- returnValue,fallback,hasCorrectArguments
13
- }
6
+ function execute() {
7
+ var args = [returnValue,_fallback,hasCorrectArguments,mockContext,shouldDecrementExpectCount,whenCalledEmitter];
8
+ for(var index = 0; index < arguments.length; index++) {
9
+ args.push(arguments[index]);
10
+ }
11
+ return _execute.apply(this,args);
12
+ returnValue,fallback,hasCorrectArguments
13
+ }
14
14
 
15
15
  execute.setFallback = function(fallback) {
16
16
  _fallback = fallback;
@@ -19,4 +19,4 @@ function newExecute(returnValue,hasCorrectArguments,mockContext,shouldDecrementE
19
19
  return execute;
20
20
  }
21
21
 
22
- module.exports = newExecute;
22
+ module.exports = newExecute;
@@ -1,31 +1,35 @@
1
- var getStackTrace = require('./fallbackWrapper/getStackTrace');
2
-
3
- function _new(originalFallback) {
4
-
5
- var fallback = originalFallback;
6
-
7
- function execute() {
8
- Error.stackTraceLimit = Error.stackTraceLimit + 2;
9
- try {
10
- return fallback.apply(null,arguments);
11
- }
12
- catch (e) {
13
- if (e.name == 'Mock Error') {
14
- e.stack = e.name + ': ' + e.message + '\n' + getStackTrace();
15
- }
1
+ var getStackTrace = require('./fallbackWrapper/getStackTrace');
2
+
3
+ function _new(originalFallback, thisArg) {
4
+
5
+ var fallback = originalFallback;
6
+ var fallbackThis = thisArg;
7
+
8
+ function execute() {
9
+ Error.stackTraceLimit = Error.stackTraceLimit + 2;
10
+ try {
11
+ var receiver = fallbackThis != null ? fallbackThis : this;
12
+ return fallback.apply(receiver,arguments);
13
+ }
14
+ catch (e) {
15
+ if (e.name == 'Mock Error') {
16
+ e.stack = e.name + ': ' + e.message + '\n' + getStackTrace();
17
+ }
16
18
  throw e;
17
19
  }
18
20
  finally {
19
21
  Error.stackTraceLimit = Error.stackTraceLimit - 2;
20
22
  }
21
-
22
- }
23
-
24
- execute.setFallback = function(fallback2) {
25
- fallback = fallback2;
26
- };
27
-
28
- return execute;
29
- }
23
+
24
+ }
25
+
26
+ execute.setFallback = function(fallback2, thisArg2) {
27
+ fallback = fallback2;
28
+ if (arguments.length > 1)
29
+ fallbackThis = thisArg2;
30
+ };
31
+
32
+ return execute;
33
+ }
30
34
 
31
- module.exports = _new;
35
+ module.exports = _new;
@@ -1,13 +1,13 @@
1
- var newObject = require('../newObject')
2
- var reset = require('./mockContext/reset');
1
+ var newObject = require('../newObject')
2
+ var reset = require('./mockContext/reset');
3
+
4
+ function _new(originalFallback, thisArg) {
5
+ var c = newObject();
6
+ c = reset(c,originalFallback,thisArg);
7
+ c.reset = function() {
8
+ return reset(c, originalFallback, thisArg);
9
+ };
10
+ return c;
11
+ }
3
12
 
4
- function _new(originalFallback) {
5
- var c = newObject();
6
- c = reset(c,originalFallback);
7
- c.reset = function() {
8
- return reset(c, originalFallback);
9
- };
10
- return c;
11
- }
12
-
13
- module.exports = _new;
13
+ module.exports = _new;
package/partialMock.js CHANGED
@@ -1,5 +1,5 @@
1
- function create(originalFunc) {
2
- var newMockContext = require('./partialMock/newMockContext');
1
+ function create(originalFunc, thisArg) {
2
+ var newMockContext = require('./partialMock/newMockContext');
3
3
  var expect = require('./partialMock/expect');
4
4
  var expectAnything = require('./partialMock/expectAnything');
5
5
  var ignore = require('./partialMock/ignore');
@@ -7,14 +7,14 @@ function create(originalFunc) {
7
7
  var verify = require('./partialMock/verify');
8
8
  var expectEmpty = require('./partialMock/expectEmpty');
9
9
  var newEmptyAnd = require('./newMutableAnd');
10
- var mockContext = newMockContext(originalFunc);
11
- var _negotiateEnd = require('./partialMock/negotiateEnd');
12
-
13
- function mock() {
14
- negotiateEnd();
15
- mockContext.arguments = arguments;
16
- return mockContext.execute.apply(null,arguments);
17
- }
10
+ var mockContext = newMockContext(originalFunc, thisArg);
11
+ var _negotiateEnd = require('./partialMock/negotiateEnd');
12
+
13
+ function mock() {
14
+ negotiateEnd();
15
+ mockContext.arguments = arguments;
16
+ return mockContext.execute.apply(this,arguments);
17
+ }
18
18
 
19
19
  mock.expect = function() {
20
20
  negotiateEnd();