1 module fuzzyd_test;
2 
3 import std.stdio;
4 import std.array;
5 import std.algorithm;
6 import std.algorithm.comparison : equal;
7 import std.container.binaryheap;
8 import std.range;
9 
10 import fuzzyd.core;
11 
12 private FuzzyResult[] prepare(string s)
13 {
14     string[] source = [
15         "cd Documents", "curl localhost/foo", "cp bar ../foo",
16         "rm -rf Downloads", "vi ~/Documents"
17     ];
18     FuzzyResult[] response = new FuzzyResult[source.length];
19     fuzzy(source)(s, response);
20     return heapify!"a.score < b.score"(response).take(source.length).array;
21 }
22 
23 @("Matches in expected order")
24 unittest
25 {
26     auto result = prepare("docts").map!(x => x.value);
27     const expected = [
28         "cd Documents", "vi ~/Documents", "curl localhost/foo",
29         "rm -rf Downloads", "cp bar ../foo"
30     ];
31     assert(equal(expected, result));
32 }
33 
34 @("Matches indexes")
35 unittest
36 {
37     auto result = prepare("docts")[0].matches;
38     const expected = [0, 1, 3, 4, 5, 10, 11];
39     assert(equal(expected, result.sort));
40 }
41 
42 @("Result is empty if provided db is empty")
43 unittest
44 {
45     string[] source = [];
46     FuzzyResult[] result = new FuzzyResult[0];
47     fuzzy(source)("f", result);
48     assert(result.empty);
49 }
50 
51 @("Unicode support")
52 unittest
53 {
54     string[] source = ["férias"];
55     auto result = new FuzzyResult[source.length];
56     fuzzy(source)("fé", result);
57     assert(equal([0, 1], result[0].matches.sort));
58 }
59 
60 @("Total amount of matches")
61 unittest
62 {
63     string[] source = [
64         "cd Documents", "curl localhost/foo", "cp bar ../foo",
65         "rm -rf Downloads", "vi ~/Documents"
66     ];
67     FuzzyResult[] response = new FuzzyResult[source.length];
68     const total = fuzzy(source)("doc", response);
69     assert(total == 4);
70 }