Skip to content

Commit 1880faf

Browse files
committed
fix(packages/grpc-js/test/assert2): move assert2 into its own file
Moving from exporting a namespace to just putting assert2 functions into their own files Fixes #2464
1 parent 2a11a2c commit 1880faf

File tree

2 files changed

+96
-80
lines changed

2 files changed

+96
-80
lines changed

packages/grpc-js/test/assert2.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2019 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import * as assert from 'assert';
19+
20+
const toCall = new Map<() => void, number>();
21+
const afterCallsQueue: Array<() => void> = [];
22+
23+
/**
24+
* Assert that the given function doesn't throw an error, and then return
25+
* its value.
26+
* @param fn The function to evaluate.
27+
*/
28+
export function noThrowAndReturn<T>(fn: () => T): T {
29+
try {
30+
return fn();
31+
} catch (e) {
32+
assert.throws(() => {
33+
throw e;
34+
});
35+
throw e; // for type safety only
36+
}
37+
}
38+
39+
/**
40+
* Helper function that returns true when every function wrapped with
41+
* mustCall has been called.
42+
*/
43+
function mustCallsSatisfied(): boolean {
44+
let result = true;
45+
toCall.forEach(value => {
46+
result = result && value === 0;
47+
});
48+
return result;
49+
}
50+
51+
export function clearMustCalls(): void {
52+
afterCallsQueue.length = 0;
53+
}
54+
55+
/**
56+
* Wraps a function to keep track of whether it was called or not.
57+
* @param fn The function to wrap.
58+
*/
59+
// tslint:disable:no-any
60+
export function mustCall<T>(fn: (...args: any[]) => T): (...args: any[]) => T {
61+
const existingValue = toCall.get(fn);
62+
if (existingValue !== undefined) {
63+
toCall.set(fn, existingValue + 1);
64+
} else {
65+
toCall.set(fn, 1);
66+
}
67+
return (...args: any[]) => {
68+
const result = fn(...args);
69+
const existingValue = toCall.get(fn);
70+
if (existingValue !== undefined) {
71+
toCall.set(fn, existingValue - 1);
72+
}
73+
if (mustCallsSatisfied()) {
74+
afterCallsQueue.forEach(fn => fn());
75+
afterCallsQueue.length = 0;
76+
}
77+
return result;
78+
};
79+
}
80+
81+
/**
82+
* Calls the given function when every function that was wrapped with
83+
* mustCall has been called.
84+
* @param fn The function to call once all mustCall-wrapped functions have
85+
* been called.
86+
*/
87+
export function afterMustCallsSatisfied(fn: () => void): void {
88+
if (!mustCallsSatisfied()) {
89+
afterCallsQueue.push(fn);
90+
} else {
91+
fn();
92+
}
93+
}

packages/grpc-js/test/common.ts

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import * as loader from '@grpc/proto-loader';
19-
import * as assert from 'assert';
19+
import * as assert2 from './assert2';
2020

2121
import { GrpcObject, loadPackageDefinition } from '../src/make-client';
2222

@@ -32,86 +32,9 @@ export function mockFunction(): never {
3232
throw new Error('Not implemented');
3333
}
3434

35-
export namespace assert2 {
36-
const toCall = new Map<() => void, number>();
37-
const afterCallsQueue: Array<() => void> = [];
38-
39-
/**
40-
* Assert that the given function doesn't throw an error, and then return
41-
* its value.
42-
* @param fn The function to evaluate.
43-
*/
44-
export function noThrowAndReturn<T>(fn: () => T): T {
45-
try {
46-
return fn();
47-
} catch (e) {
48-
assert.throws(() => {
49-
throw e;
50-
});
51-
throw e; // for type safety only
52-
}
53-
}
54-
55-
/**
56-
* Helper function that returns true when every function wrapped with
57-
* mustCall has been called.
58-
*/
59-
function mustCallsSatisfied(): boolean {
60-
let result = true;
61-
toCall.forEach(value => {
62-
result = result && value === 0;
63-
});
64-
return result;
65-
}
66-
67-
export function clearMustCalls(): void {
68-
afterCallsQueue.length = 0;
69-
}
70-
71-
/**
72-
* Wraps a function to keep track of whether it was called or not.
73-
* @param fn The function to wrap.
74-
*/
75-
// tslint:disable:no-any
76-
export function mustCall<T>(
77-
fn: (...args: any[]) => T
78-
): (...args: any[]) => T {
79-
const existingValue = toCall.get(fn);
80-
if (existingValue !== undefined) {
81-
toCall.set(fn, existingValue + 1);
82-
} else {
83-
toCall.set(fn, 1);
84-
}
85-
return (...args: any[]) => {
86-
const result = fn(...args);
87-
const existingValue = toCall.get(fn);
88-
if (existingValue !== undefined) {
89-
toCall.set(fn, existingValue - 1);
90-
}
91-
if (mustCallsSatisfied()) {
92-
afterCallsQueue.forEach(fn => fn());
93-
afterCallsQueue.length = 0;
94-
}
95-
return result;
96-
};
97-
}
98-
99-
/**
100-
* Calls the given function when every function that was wrapped with
101-
* mustCall has been called.
102-
* @param fn The function to call once all mustCall-wrapped functions have
103-
* been called.
104-
*/
105-
export function afterMustCallsSatisfied(fn: () => void): void {
106-
if (!mustCallsSatisfied()) {
107-
afterCallsQueue.push(fn);
108-
} else {
109-
fn();
110-
}
111-
}
112-
}
113-
11435
export function loadProtoFile(file: string): GrpcObject {
11536
const packageDefinition = loader.loadSync(file, protoLoaderOptions);
11637
return loadPackageDefinition(packageDefinition);
11738
}
39+
40+
export { assert2 };

0 commit comments

Comments
 (0)