• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {dingus} from 'dingusjs';
16
17interface VoidFunctionWithNumberArg {
18  (_: number): void;
19}
20
21class ExampleClass {
22  isEven(n: number): boolean {
23    return n % 2 === 0;
24  }
25}
26
27function* evenNumbers(n: number, math: ExampleClass) {
28  for (let i = 0; i < n; i++) {
29    if (math.isEven(i)) yield i;
30  }
31}
32
33/**
34 * Call |f| |n| times (once with each number [0, n)).
35 */
36function iterMap(f: VoidFunctionWithNumberArg, n: number): void {
37  for (let i = 0; i < n; i++) {
38    f(i);
39  }
40}
41
42test('example dingus test', () => {
43  const d = dingus<VoidFunctionWithNumberArg>();
44  iterMap(d, 3);
45  expect(d.calls.length).toBe(3);
46  expect(d.calls.filter(([_a, args, _b]) => args[0] === 1).length).toBe(1);
47});
48
49test('example dingus test class', () => {
50  const d = dingus<ExampleClass>('math');
51  // Dingus returns a truthy dingus in all situations - so bear that in mind!
52  expect([...evenNumbers(3, d)]).toEqual([0, 1, 2]);
53});
54