Skip to content
Permalink
Browse files

Tweak the levenshtein suggestions

  • Loading branch information...
sunesimonsen committed Sep 30, 2015
1 parent 18d418d commit a97adb9f506c8021105b7931241dc10dc5456f73
Showing with 16 additions and 10 deletions.
  1. +1 −1 documentation/api/clone.md
  2. +14 −8 lib/Unexpected.js
  3. +1 −1 test/documentation.spec.js
@@ -25,5 +25,5 @@ originalExpect(42, 'to be an integer');
```

```output
Unknown assertion 'to be an integer', did you mean: 'to be a number'
Unknown assertion 'to be an integer', did you mean: 'to be infinite'
```
@@ -786,43 +786,49 @@ Unexpected.prototype.throwAssertionNotFoundError = function (subject, testDescri
try {
var assertion = that.lookupAssertionRule(subject, assertionString, args);
// Give some points for matching and for being specific
return 5 + assertion.specificity;
return assertion.specificity;
} catch (e) {
return 0;
}
}

assertionStrings.forEach(function (assertionString) {
var score = leven(testDescriptionString, assertionString);
score -= calculateMatchingScore(assertionString);

assertionsWithScore.push({
assertion: assertionString,
score: score
});
}, this);

assertionsWithScore.sort(function (a, b) {
var bestMatch = assertionsWithScore.sort(function (a, b) {
var c = a.score - b.score;
if (c !== 0) {
return c;
}

if (a.assertion < b.assertion) {
return -1;
} else if (a.assertion > b.assertion) {
return 1;
} else {
return 0;
return 1;
}
});
}).slice(0, 10).filter(function (assertionsWithScore, i, arr) {
return Math.abs(assertionsWithScore.score - arr[0].score) <= 2;
}).sort(function (a, b) {
var c = calculateMatchingScore(b.assertion) - calculateMatchingScore(a.assertion);
if (c !== 0) {
return c;
}

return a.score - b.score;
})[0];

this.fail({
errorMode: 'bubbleThrough',
message: function (output) {

output.error("Unknown assertion '").jsString(testDescriptionString)
.error("', did you mean: '").jsString(assertionsWithScore[0].assertion).error("'");
.error("', did you mean: '").jsString(bestMatch.assertion).error("'");
}
});
};
@@ -741,7 +741,7 @@ describe("documentation tests", function () {
});
} catch (e) {
expect(e, "to have message",
"Unknown assertion 'to be an integer', did you mean: 'to be a number'"
"Unknown assertion 'to be an integer', did you mean: 'to be infinite'"
);
}
return expect.promise.all(testPromises);

0 comments on commit a97adb9

Please sign in to comment.
You can’t perform that action at this time.