-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
130 lines (104 loc) · 4.53 KB
/
test.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var fzy = require('.');
var score = fzy.score;
var positions = fzy.positions;
var SCORE_MIN = fzy.SCORE_MIN;
var SCORE_MAX = fzy.SCORE_MAX;
var SCORE_GAP_LEADING = fzy.SCORE_GAP_LEADING;
var SCORE_GAP_TRAILING = fzy.SCORE_GAP_TRAILING;
var SCORE_GAP_INNER = fzy.SCORE_GAP_INNER;
var SCORE_MATCH_CONSECUTIVE = fzy.SCORE_MATCH_CONSECUTIVE;
var SCORE_MATCH_SLASH = fzy.SCORE_MATCH_SLASH;
var SCORE_MATCH_WORD = fzy.SCORE_MATCH_WORD;
var SCORE_MATCH_CAPITAL = fzy.SCORE_MATCH_CAPITAL;
var SCORE_MATCH_DOT = fzy.SCORE_MATCH_DOT;
/* score(needle, haystack) */
test("should_prefer_starts_of_words", function() {
/* App/Models/Order is better than App/MOdels/zRder */
expect(score("amor", "app/models/order")).toBeGreaterThan(score("amor", "app/models/zrder"));
});
test("should_prefer_consecutive_letters", function() {
/* App/MOdels/foo is better than App/M/fOo */
expect(score("amo", "app/m/foo")).toBeLessThan(score("amo", "app/models/foo"));
});
test("should_prefer_contiguous_over_letter_following_period", function() {
/* GEMFIle.Lock < GEMFILe */
expect(score("gemfil", "Gemfile.lock")).toBeLessThan(score("gemfil", "Gemfile"));
});
test("should_prefer_shorter_matches", function() {
expect(score("abce", "abcdef")).toBeGreaterThan(score("abce", "abc de"));
expect(score("abc", " a b c ")).toBeGreaterThan(score("abc", " a b c "));
expect(score("abc", " a b c ")).toBeGreaterThan(score("abc", " a b c "));
});
test("should_prefer_shorter_candidates", function() {
expect(score("test", "tests")).toBeGreaterThan(score("test", "testing"));
});
test("should_prefer_start_of_candidate", function() {
/* Scores first letter highly */
expect(score("test", "testing")).toBeGreaterThan(score("test", "/testing"));
});
test("score_exact_score", function() {
/* Exact match is SCORE_MAX */
expect(score("abc", "abc")).toBe(SCORE_MAX);
expect(score("aBc", "abC")).toBe(SCORE_MAX);
});
test("score_empty_query", function() {
/* Empty query always results in SCORE_MIN */
expect(score("", "")).toBe(SCORE_MIN);
expect(score("", "a")).toBe(SCORE_MIN);
expect(score("", "bb")).toBe(SCORE_MIN);
});
test("score_gaps", function() {
expect(score("a", "*a")).toBe(SCORE_GAP_LEADING);
expect(score("a", "*ba")).toBe(SCORE_GAP_LEADING*2);
expect(score("a", "**a*")).toBe(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING);
expect(score("a", "**a**")).toBe(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2);
expect(score("aa", "**aa**")).toBe(SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2);
expect(score("aa", "**a*a**")).toBe(SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING);
});
test("score_consecutive", function() {
expect(score("aa", "*aa")).toBe(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE);
expect(score("aaa", "*aaa")).toBe(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2);
expect(score("aaa", "*a*aa")).toBe(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE);
});
test("score_slash", function() {
expect(score("a", "/a")).toBe(SCORE_GAP_LEADING + SCORE_MATCH_SLASH);
expect(score("a", "*/a")).toBe(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH);
expect(score("aa", "a/aa")).toBe(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE);
});
test("score_capital", function() {
expect(score("a", "bA")).toBe(SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL);
expect(score("a", "baA")).toBe(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL);
expect(score("aa", "baAa")).toBe(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE);
});
test("score_dot", function() {
expect(score("a", ".a")).toBe(SCORE_GAP_LEADING + SCORE_MATCH_DOT);
expect(score("a", "*a.a")).toBe(SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT);
expect(score("a", "*a.a")).toBe(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT);
});
/* positions(needle, haystack) */
test("positions_consecutive", function() {
var p = positions("amo", "app/models/foo");
expect(p).toEqual([0,4,5]);
});
test("positions_start_of_word", function() {
/*
* We should prefer matching the 'o' in order, since it's the beginning
* of a word.
*/
var p = positions("amor", "app/models/order");
expect(p).toEqual([0,4,11,12]);
});
test("positions_no_bonuses", function() {
var p = positions("as", "tags");
expect(p).toEqual([1,3]);
var p = positions("as", "examples.txt");
expect(p).toEqual([2,7]);
});
test("positions_multiple_candidates_start_of_words", function() {
var p = positions("abc", "a/a/b/c/c");
expect(p).toEqual([2,4,6]);
});
test("positions_exact_match", function() {
var p = positions("foo", "foo");
expect(p).toEqual([0,1,2]);
});