This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspec.html
68 lines (59 loc) · 3.82 KB
/
spec.html
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
<!DOCTYPE html>
<title>Jasmine Specs</title>
<script src="http://jasmine.jelzo.com/1.0.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="jquery.fuzzymatch.js"></script>
<script>
describe("$.fuzzyMatch", function () {
it("should give a score of 1 for an exact match", function () {
expect($.fuzzyMatch("a", "a").score).toEqual(1);
expect($.fuzzyMatch("abcdef", "abcdef").score).toEqual(1);
expect($.fuzzyMatch("abCdef", "abCdef").score).toEqual(1);
});
it("should return 0 for abbreviations that don't match", function () {
expect($.fuzzyMatch("hello", "bye").score).toEqual(0);
expect($.fuzzyMatch("hello", "le").score).toEqual(0);
expect($.fuzzyMatch("hello world", "hell oworld").score).toEqual(0);
});
it("should prefer exact matches over prefix matches", function () {
expect($.fuzzyMatch("grab", "grab").score).toBeGreaterThan($.fuzzyMatch("grabbed", "grab").score);
expect($.fuzzyMatch("grabbed", "grab").score).toEqual($.fuzzyMatch("grabbing", "grab").score);
});
it("should prefer closer-together matches over further-apart matches", function () {
expect($.fuzzyMatch("garden", "ga").score).toBeGreaterThan($.fuzzyMatch("gray", "ga").score);
expect($.fuzzyMatch("aggravate", "ga").score).toBeGreaterThan($.fuzzyMatch("aglutamate", "ga").score);
});
it("should prefer case-sensitive matches over case-insensitivei matches", function () {
expect($.fuzzyMatch("CSS", "CS").score).toBeGreaterThan($.fuzzyMatch("css", "CS").score);
expect($.fuzzyMatch("arm", "ar").score).toBeGreaterThan($.fuzzyMatch("ARM", "ar").score);
});
it("should prefer things that starts of words more", function () {
expect($.fuzzyMatch("outlook-user", "ous").score).toBeGreaterThan($.fuzzyMatch("zoho-user", "ous").score);
expect($.fuzzyMatch("outlook-the-user", "ous").score).toBeGreaterThan($.fuzzyMatch("the-outlook-user", "ous").score);
expect($.fuzzyMatch("lots-of-outlook-users", "ous").score).toBeGreaterThan($.fuzzyMatch("millions-of-outlook-users", "ous").score);
expect($.fuzzyMatch("camelCaseStuff", "css").score).toBeGreaterThan($.fuzzyMatch("CAMELCASESTUFF", "css").score);
expect($.fuzzyMatch("CSS", "css").score).toBeGreaterThan($.fuzzyMatch("camelCaseStuff", "css").score);
});
it("should return the html of the best match", function () {
expect($.fuzzyMatch("outlook-user", "ous").html).toEqual("<b>o</b>utlook-<b>u</b><b>s</b>er");
// Nom — should put the suggestions through a hyphenator first? :p
expect($.fuzzyMatch("outsourced user", "ous").html).toEqual("<b>o</b>utsourced <b>u</b><b>s</b>er");
expect($.fuzzyMatch("out-sourced user", "ous").html).toEqual("<b>o</b><b>u</b>t-<b>s</b>ourced user");
});
it("should escape HTML", function () {
expect($.fuzzyMatch("<foo> b&r <bar>", "fob&").html).toEqual("<<b>f</b><b>o</b>o> <b>b</b><b>&</b>r <bar>");
});
it("should not take exponential time in pathological cases", function () {
var t = (new Date()).getTime();
$.fuzzyMatch("aaaaaaaaaaaaaaaaaaaa", "aaaaaaaaa");
// On my laptop the un-optimized algorithm takes about 10 seconds to run the above,
// so I'm going out on a limb and saying that there's no computer 100 times faster than
// mine that we're likely to run the specs on. [I'm also hoping there's no computer
// 10 times slower, so the test should fail in under 2 minutes :p]
//
// 100ms is just as generous, on my machine it's about 10ms with a proper cache in place.
expect((new Date()).getTime() - t).toBeLessThan(100);
});
});
</script>