« get me outta code hell

generic-mock.js « lib « test - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/lib/generic-mock.js
blob: 28309ab0bbc96d2ac1710d00c67866dc5a28d93e (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import {same} from 'tcompare';

import {empty} from '#sugar';

export default function mock(callback) {
  const mocks = [];

  const track = callback => (...args) => {
    const {value, close} = callback(...args);
    mocks.push({close});
    return value;
  };

  const mock = {
    function: track(mockFunction),
  };

  return {
    value: callback(mock),
    close: () => {
      const errors = [];
      for (const mock of mocks) {
        try {
          mock.close();
        } catch (error) {
          errors.push(error);
        }
      }
      if (!empty(errors)) {
        throw new AggregateError(errors, `Errors closing sub-mocks`);
      }
    },
  };
}

export function mockFunction(...args) {
  let name = '(anonymous)';
  let behavior = null;

  if (args.length === 2) {
    if (
      typeof args[0] === 'string' &&
      typeof args[1] === 'function'
    ) {
      name = args[0];
      behavior = args[1];
    } else {
      throw new TypeError(`Expected name to be a string`);
    }
  } else if (args.length === 1) {
    if (typeof args[0] === 'string') {
      name = args[0];
    } else if (typeof args[0] === 'function') {
      behavior = args[0];
    } else if (args[0] !== null) {
      throw new TypeError(`Expected string (name), function (behavior), both, or null / no arguments`);
    }
  } else if (args.length > 2) {
    throw new TypeError(`Expected string (name), function (behavior), both, or null / no arguments`);
  }

  let currentCallDescription = newCallDescription();
  const allCallDescriptions = [currentCallDescription];

  const topLevelErrors = [];
  let runningCallCount = 0;
  let limitCallCount = false;
  let markedAsOnce = false;

  const fn = (...args) => {
    const description = processCall(...args);
    return description.behavior(...args);
  };

  fn.behavior = value => {
    if (!(value === null || (
      typeof value === 'function'
    ))) {
      throw new TypeError(`Expected function or null`);
    }

    currentCallDescription.behavior = behavior;
    currentCallDescription.described = true;

    return fn;
  }

  fn.argumentCount = value => {
    if (!(value === null || (
      typeof value === 'number' &&
      value === parseInt(value) &&
      value >= 0
    ))) {
      throw new TypeError(`Expected whole number or null`);
    }

    if (currentCallDescription.argsPattern) {
      throw new TypeError(`Unexpected .argumentCount() when .args() has been called`);
    }

    currentCallDescription.argsPattern = {length: value};
    currentCallDescription.described = true;

    return fn;
  };

  fn.args = (...args) => {
    const value = args[0];

    if (args.length > 1 || !(value === null || Array.isArray(value))) {
      throw new TypeError(`Expected one array or null`);
    }

    currentCallDescription.argsPattern = Object.fromEntries(
      value
        .map((v, i) => v === undefined ? false : [i, v])
        .filter(Boolean)
        .concat([['length', value.length]]));

    currentCallDescription.described = true;

    return fn;
  };

  fn.neverCalled = (...args) => {
    if (!empty(args)) {
      throw new TypeError(`Didn't expect any arguments`);
    }

    if (allCallDescriptions[0].described) {
      throw new TypeError(`Unexpected .neverCalled() when any descriptions provided`);
    }

    limitCallCount = true;
    allCallDescriptions.splice(0, allCallDescriptions.length);

    currentCallDescription = new Proxy({}, {
      set() {
        throw new Error(`Unexpected description when .neverCalled() has been called`);
      },
    });

    return fn;
  };

  fn.once = (...args) => {
    if (!empty(args)) {
      throw new TypeError(`Didn't expect any arguments`);
    }

    if (allCallDescriptions.length > 1) {
      throw new TypeError(`Unexpected .once() when providing multiple descriptions`);
    }

    currentCallDescription.described = true;
    limitCallCount = true;
    markedAsOnce = true;

    return fn;
  };

  fn.next = (...args) => {
    if (!empty(args)) {
      throw new TypeError(`Didn't expect any arguments`);
    }

    if (markedAsOnce) {
      throw new TypeError(`Unexpected .next() when .once() has been called`);
    }

    currentCallDescription = newCallDescription();
    allCallDescriptions.push(currentCallDescription);

    limitCallCount = true;

    return fn;
  };

  fn.repeat = times => {
    // Note: This function should be called AFTER filling out the
    // call description which is being repeated.

    if (!(
      typeof times === 'number' &&
      times === parseInt(times) &&
      times >= 2
    )) {
      throw new TypeError(`Expected whole number of at least 2`);
    }

    if (markedAsOnce) {
      throw new TypeError(`Unexpected .repeat() when .once() has been called`);
    }

    // The current call description is already in the full list,
    // so skip the first push.
    for (let i = 2; i <= times; i++) {
      allCallDescriptions.push(currentCallDescription);
    }

    // Prep a new description like when calling .next().
    currentCallDescription = newCallDescription();
    allCallDescriptions.push(currentCallDescription);

    limitCallCount = true;

    return fn;
  };

  return {
    value: fn,
    close: () => {
      const totalCallCount = runningCallCount;
      const expectedCallCount = countDescribedCalls();

      if (limitCallCount && totalCallCount !== expectedCallCount) {
        if (expectedCallCount > 1) {
          topLevelErrors.push(new Error(`Expected ${expectedCallCount} calls, got ${totalCallCount}`));
        } else if (expectedCallCount === 1) {
          topLevelErrors.push(new Error(`Expected 1 call, got ${totalCallCount}`));
        } else {
          topLevelErrors.push(new Error(`Expected no calls, got ${totalCallCount}`));
        }
      }

      if (topLevelErrors.length) {
        throw new AggregateError(topLevelErrors, `Errors in mock ${name}`);
      }
    },
  };

  function newCallDescription() {
    return {
      described: false,
      behavior: behavior ?? null,
      argumentCount: null,
      argsPattern: null,
    };
  }

  function processCall(...args) {
    const callErrors = [];

    runningCallCount++;

    // No further processing, this indicates the function shouldn't have been
    // called at all and there aren't any descriptions to match this call with.
    if (empty(allCallDescriptions)) {
      return newCallDescription();
    }

    const currentCallNumber = runningCallCount;
    const currentDescription = selectCallDescription(currentCallNumber);

    const {
      argumentCount,
      argsPattern,
    } = currentDescription;

    if (argumentCount !== null && args.length !== argumentCount) {
      callErrors.push(
        new Error(`Argument count mismatch: expected ${argumentCount}, got ${args.length}`));
    }

    if (argsPattern !== null) {
      const keysToCheck = Object.keys(argsPattern);
      const argsAsObject = Object.fromEntries(
        args
          .map((v, i) => [i.toString(), v])
          .filter(([i]) => keysToCheck.includes(i))
          .concat([['length', args.length]]));

      const {match, diff} = same(argsAsObject, argsPattern);
      if (!match) {
        callErrors.push(new Error(`Argument pattern mismatch:\n` + diff));
      }
    }

    if (!empty(callErrors)) {
      const aggregate = new AggregateError(callErrors, `Errors in call #${currentCallNumber}`);
      topLevelErrors.push(aggregate);
    }

    return currentDescription;
  }

  function selectCallDescription(currentCallNumber) {
    if (currentCallNumber > countDescribedCalls()) {
      const lastDescription = lastCallDescription();
      if (lastDescription.described) {
        return newCallDescription();
      } else {
        return lastDescription;
      }
    } else {
      return allCallDescriptions[currentCallNumber - 1];
    }
  }

  function countDescribedCalls() {
    if (empty(allCallDescriptions)) {
      return 0;
    }

    return (
      (lastCallDescription().described
        ? allCallDescriptions.length
        : allCallDescriptions.length - 1));
  }

  function lastCallDescription() {
    return allCallDescriptions[allCallDescriptions.length - 1];
  }
}