16
16
*/
17
17
18
18
import * as loader from '@grpc/proto-loader' ;
19
- import * as assert from 'assert ' ;
19
+ import * as assert2 from './assert2 ' ;
20
20
21
21
import { GrpcObject , loadPackageDefinition } from '../src/make-client' ;
22
22
@@ -32,86 +32,9 @@ export function mockFunction(): never {
32
32
throw new Error ( 'Not implemented' ) ;
33
33
}
34
34
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
-
114
35
export function loadProtoFile ( file : string ) : GrpcObject {
115
36
const packageDefinition = loader . loadSync ( file , protoLoaderOptions ) ;
116
37
return loadPackageDefinition ( packageDefinition ) ;
117
38
}
39
+
40
+ export { assert2 } ;
0 commit comments