Skip to content

Commit

Permalink
Inspect the precious counterexamples with infinite depth so the detai…
Browse files Browse the repository at this point in the history
…ls don't get dotted out
  • Loading branch information
papandreou committed Sep 3, 2024
1 parent 1d1e24e commit f3667c1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/unexpected-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
output
.jsFunctionName('fuzz')
.text('(')
.appendItems(value.args, ', ')
.appendItemsWithInfiniteDepth(value.args, ', ')
.text(', ')
.appendInspected(value.mutatorFunction)
.text(')');
Expand All @@ -158,6 +158,20 @@
},
});

expect.addStyle(
'appendItemsWithInfiniteDepth',
function (items, separator) {
const that = this;
separator = separator || '';
items.forEach((item, index) => {
if (index > 0) {
that.append(separator);
}
that.appendInspected(item, Infinity);
});
}
);

const promiseLoop = function (condition, action) {
return expect.promise(function (resolve, reject) {
const loop = function () {
Expand Down Expand Up @@ -386,7 +400,7 @@
output.i().block(function (output) {
output
.text('Generated input: ')
.appendItems(bestFailure.args, ', ')
.appendItemsWithInfiniteDepth(bestFailure.args, ', ')
.nl()
.text('with: ')
.appendItems(options.generators, ', ')
Expand Down
52 changes: 51 additions & 1 deletion test/unexpected-check.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global describe, it, beforeEach */
const { array, integer, string } = require('chance-generators');
const { array, integer, string, shape } = require('chance-generators');
const assert = require('assert');
const expect = require('unexpected').clone();
expect.output.preferredWidth = 80;
Expand Down Expand Up @@ -62,6 +62,56 @@ describe('unexpected-check', function () {
);
});

it('does not dot out the failing example even when it is highly nested', function () {
expect(
function () {
expect(
function (input) {
const sorted = sort(input.numbers);

expect(sorted, 'to have length', input.numbers.length).and(
'to be sorted'
);
},
'to be valid for all',
{
generators: [
shape({
numbers: numbers,
a: {
bunch: {
of: {
deeply: {
nested: { but: { highly: { relevant: 'stuff' } } },
},
},
},
},
}),
],
maxIterations: 50,
maxErrorIterations: 200,
maxErrors: 30,
}
);
},
'to throw',
'Found an error after 1 iteration, 20 additional errors found.\n' +
'counterexample:\n' +
'\n' +
' Generated input: {\n' +
' numbers: [ -2, -1 ],\n' +
" a: { bunch: { of: { deeply: { nested: { but: { highly: { relevant: 'stuff' } } } } } } }\n" +
' }\n' +
' with: shape({\n' +
' numbers: array({ itemGenerator: integer({ min: -20, max: 20 }), min: 1, max: 20 }),\n' +
' a: { bunch: { of: ... } }\n' +
' })\n' +
'\n' +
' expected [ -1, -2 ] to be sorted'
);
});

describe('when an assertion error is caught', () => {
it('should not report a full stack trace', () => {
expect(
Expand Down

0 comments on commit f3667c1

Please sign in to comment.