summaryrefslogtreecommitdiff
path: root/pbt/ts/src/property.test.ts
blob: c47c964d8635c23fad11cc258d798b64990b6e16 (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
import {
  pairOf,
  positiveInt,
  property,
  shrinkPair,
  shrinkPositive,
} from './property.js';

// from https://github.com/jlink/shrinking-challenge/blob/main/challenges/difference.md
const predicate1: (pair: [number, number]) => Promise<boolean> = ([x, y]) =>
  Promise.resolve(x < 10 || x !== y);

describe('Find minimal counterexample', () => {
  test('do not shrink example if no shrinker given', async () => {
    const result = await property(pairOf(positiveInt), predicate1).then(p =>
      p(42, 1),
    );
    expect(result.result).toBe('Falsified');
    expect(result.counterexample).toBeNull;
  });

  test('shrink example if shrinker given', async () => {
    const result = await property(
      pairOf(positiveInt),
      predicate1,
      shrinkPair(shrinkPositive, shrinkPositive),
    ).then(p => p(42, 1));
    expect(result.result).toBe('Falsified');
    if (result.counterexample !== null) {
      expect(result.counterexample.counterexample).toStrictEqual([10, 10]);
    } else {
      expect.fail('no counterexample');
    }
  });
});