Skip to content

Commit de97a5b

Browse files
committed
Added FindBest for searching
Simplest implementation right now. Haven't hooked it into the cmdlet yet. Once I do that we should have our first usable version.
1 parent 4765e75 commit de97a5b

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

Jump.Location.Specs/CommandControllerSpec.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading;
33
using Moq;
4+
using Should;
45
using Xunit;
56

67
namespace Jump.Location.Specs
@@ -47,5 +48,69 @@ public void It_updates_weights()
4748
recordMock.Verify(x => x.AddTimeSpent(It.IsAny<TimeSpan>()), Times.Once());
4849
}
4950
}
51+
52+
public class DescribeFindBest
53+
{
54+
private readonly Mock<IDatabase> dbMock;
55+
private readonly Mock<IFileStoreProvider> fsMock;
56+
private readonly CommandController controller;
57+
58+
public DescribeFindBest()
59+
{
60+
dbMock = new Mock<IDatabase>();
61+
fsMock = new Mock<IFileStoreProvider>();
62+
controller = new CommandController(dbMock.Object, fsMock.Object);
63+
}
64+
65+
[Fact]
66+
public void Exact_match_on_last_segment_from_a_DB_of_one()
67+
{
68+
dbMock.Setup(x => x.Records).Returns(new[]
69+
{
70+
new Record(@"FS::C:\Users\tkellogg", 10M),
71+
});
72+
73+
var record = controller.FindBest("tkellogg");
74+
record.Path.ShouldEqual(@"C:\Users\tkellogg");
75+
}
76+
77+
[Fact]
78+
public void Exact_match_on_last_segment_from_a_DB_of_many()
79+
{
80+
dbMock.Setup(x => x.Records).Returns(new[]
81+
{
82+
new Record(@"FS::C:\Users\Kerianne", 10M),
83+
new Record(@"FS::C:\Users\lthompson", 10M),
84+
new Record(@"FS::C:\Users\tkellogg", 10M),
85+
});
86+
87+
var record = controller.FindBest("tkellogg");
88+
record.Path.ShouldEqual(@"C:\Users\tkellogg");
89+
}
90+
91+
[Fact]
92+
public void No_match_returns_null()
93+
{
94+
dbMock.Setup(x => x.Records).Returns(new[]
95+
{
96+
new Record(@"FS::C:\Users\tkellogg", 10M),
97+
});
98+
99+
var record = controller.FindBest("notfound");
100+
record.ShouldBeNull();
101+
}
102+
103+
[Fact]
104+
public void Exact_match_on_last_segment_is_case_insensitive()
105+
{
106+
dbMock.Setup(x => x.Records).Returns(new[]
107+
{
108+
new Record(@"FS::C:\Users\TKELLOGG", 10M),
109+
});
110+
111+
var record = controller.FindBest("tkellogg");
112+
record.Path.ShouldEqual(@"C:\Users\TKELLOGG");
113+
}
114+
}
50115
}
51116
}

Jump.Location.Specs/RecordSpec.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,15 @@ public void It_throws_ArgumentException_when_path_is_invalid(string path)
2828
Assert.Throws<ArgumentException>(() => new Record(path, 0));
2929
}
3030
}
31+
32+
public class DescribePathSegments
33+
{
34+
[Fact]
35+
public void It_splits_Path_into_segments_separated_by_backslash_and_lowercased()
36+
{
37+
var record = new Record(@"Provider::C:\Program Files (x86)\Alteryx\Engine");
38+
record.PathSegments.ShouldEqual(new[] {"c:", "program files (x86)", "alteryx", "engine"});
39+
}
40+
}
3141
}
3242
}

Jump.Location/CommandController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading;
34

45
namespace Jump.Location
@@ -59,5 +60,11 @@ private void SaveLoop()
5960
else Thread.Sleep(0);
6061
}
6162
}
63+
64+
public IRecord FindBest(string search)
65+
{
66+
search = search.ToLower();
67+
return database.Records.FirstOrDefault(x => x.PathSegments.Last() == search);
68+
}
6269
}
6370
}

Jump.Location/JumpLocationCommand.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ public class JumpLocationCommand : PSCmdlet
1111
private static readonly CommandController Controller = new CommandController(@"C:\Users\Kerianne\jump-location.txt");
1212

1313
/*
14-
* 1. Figure out how long they stay in the directory
15-
* 2. Log occurences of filename / weight
14+
* x1. Figure out how long they stay in the directory
15+
* x2. Log occurences of filename / weight
1616
* 3. Tail matches - search matches beginning of last segment of path
17-
* 4. Multiple args - last arg is a tail match, previous args match previous segments
17+
* 4. Weighting algorithm - match what Autojump does to increase weights
18+
* 5. Match what Autojump does to degrade weights
19+
* 6. Multiple args - last arg is a tail match, previous args match previous segments
20+
* 7. Tab completion - list 5 best matches
1821
*/
1922

2023
[Parameter(Position = 0)]

Jump.Location/Record.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace Jump.Location
45
{
@@ -8,11 +9,14 @@ public interface IRecord
89
string Path { get; }
910
string FullName { get; set; }
1011
decimal Weight { get; }
12+
string[] PathSegments { get; }
1113
void AddTimeSpent(TimeSpan timeSpan);
1214
}
1315

1416
class Record : IRecord
1517
{
18+
private string[] pathSegments;
19+
1620
public Record(string fullName, decimal weight)
1721
{
1822
FullName = fullName;
@@ -44,6 +48,16 @@ public string FullName
4448

4549
public decimal Weight { get; private set; }
4650

51+
public string[] PathSegments
52+
{
53+
get { return pathSegments ?? (pathSegments = GetPathSegments()); }
54+
}
55+
56+
private string[] GetPathSegments()
57+
{
58+
return Path.Split('\\').Select(x => x.ToLower()).ToArray();
59+
}
60+
4761
public void AddTimeSpent(TimeSpan timeSpan)
4862
{
4963
Weight += (decimal) timeSpan.TotalSeconds;

0 commit comments

Comments
 (0)