Skip to content

Commit 7d02563

Browse files
Added tests
1 parent 0b7b6b4 commit 7d02563

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/Lsp/HandlerDescriptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace OmniSharp.Extensions.LanguageServer
1010
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable
1111
{
1212
private readonly Action _disposeAction;
13-
private readonly Guid _id = Guid.NewGuid();
1413

1514
public HandlerDescriptor(string method, IJsonRpcHandler handler, Type handlerType, Type @params, Type registrationType, Type capabilityType, Action disposeAction)
1615
{
@@ -95,14 +94,15 @@ public override bool Equals(object obj)
9594
{
9695
if (obj is HandlerDescriptor handler)
9796
{
98-
return handler._id == _id;
97+
return handler.HandlerType == HandlerType && handler.Handler.Key == Handler.Key;
9998
}
10099
return false;
101100
}
102101

103102
public override int GetHashCode()
104103
{
105-
return _id.GetHashCode();
104+
if (string.IsNullOrWhiteSpace(Handler.Key)) return HandlerType.GetHashCode();
105+
return Tuple.Create(HandlerType, Handler.Key).GetHashCode();
106106
}
107107
}
108108
}

test/Lsp.Tests/HandlerResolverTests.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,36 @@ public class HandlerResolverTests
1919
[Theory]
2020
[InlineData(typeof(IInitializeHandler), "initialize", 1)]
2121
[InlineData(typeof(IInitializedHandler), "initialized", 1)]
22-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 5)]
23-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 5)]
24-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 5)]
25-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 5)]
22+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 4)]
23+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 4)]
24+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 4)]
25+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 4)]
2626
public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, int count)
2727
{
2828
var handler = new HandlerCollection();
29-
handler.Add((IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]));
29+
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
30+
sub.Key.Returns("abcd");
31+
handler.Add(sub);
32+
handler._handlers.Should().Contain(x => x.Method == key);
33+
handler._handlers.Count.Should().Be(count);
34+
}
35+
36+
[Theory]
37+
[InlineData(typeof(IInitializeHandler), "initialize", 2)]
38+
[InlineData(typeof(IInitializedHandler), "initialized", 2)]
39+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 8)]
40+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 8)]
41+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 8)]
42+
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 8)]
43+
public void Should_Contain_AllDefinedMethods_ForDifferentKeys(Type requestHandler, string key, int count)
44+
{
45+
var handler = new HandlerCollection();
46+
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
47+
sub.Key.Returns("abcd");
48+
var sub2 = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
49+
sub2.Key.Returns("efgh");
50+
handler.Add(sub);
51+
handler.Add(sub2);
3052
handler._handlers.Should().Contain(x => x.Method == key);
3153
handler._handlers.Count.Should().Be(count);
3254
}
@@ -36,7 +58,25 @@ public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, in
3658
public void Should_Contain_AllDefinedMethods_OnLanguageServer(Type requestHandler, Type type2, string key, string key2, int count)
3759
{
3860
var handler = new HandlerCollection();
39-
handler.Add((IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]));
61+
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
62+
sub.Key.Returns("abd3");
63+
handler.Add(sub);
64+
handler._handlers.Should().Contain(x => x.Method == key);
65+
handler._handlers.Should().Contain(x => x.Method == key2);
66+
handler._handlers.Count.Should().Be(count);
67+
}
68+
69+
[Theory]
70+
[InlineData(typeof(IInitializeHandler), typeof(IInitializedHandler), "initialize", "initialized", 4)]
71+
public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(Type requestHandler, Type type2, string key, string key2, int count)
72+
{
73+
var handler = new HandlerCollection();
74+
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
75+
sub.Key.Returns("abd3");
76+
var sub2 = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
77+
sub2.Key.Returns("efgh");
78+
handler.Add(sub);
79+
handler.Add(sub2);
4080
handler._handlers.Should().Contain(x => x.Method == key);
4181
handler._handlers.Should().Contain(x => x.Method == key2);
4282
handler._handlers.Count.Should().Be(count);

0 commit comments

Comments
 (0)