Skip to content

Commit 3d1eb57

Browse files
Merge pull request #40 from OmniSharp/fix/HandlerDescriptor-equality
Added new equals and additional tests for HandlerDescriptor
2 parents c4f1b79 + 6aa4d66 commit 3d1eb57

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

src/Lsp/HandlerDescriptor.cs

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reflection;
34
using OmniSharp.Extensions.JsonRpc;
45
using OmniSharp.Extensions.LanguageServer.Abstractions;
@@ -7,7 +8,7 @@
78

89
namespace OmniSharp.Extensions.LanguageServer
910
{
10-
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable
11+
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable, IEquatable<HandlerDescriptor>
1112
{
1213
private readonly Action _disposeAction;
1314

@@ -96,16 +97,34 @@ private static void SetCapability<T>(ICapability<T> capability, T instance)
9697

9798
public override bool Equals(object obj)
9899
{
99-
if (obj is HandlerDescriptor handler)
100-
{
101-
return handler.HandlerType == HandlerType && handler.Key == Key;
102-
}
103-
return false;
100+
return Equals(obj as HandlerDescriptor);
101+
}
102+
103+
public bool Equals(HandlerDescriptor other)
104+
{
105+
return other != null &&
106+
EqualityComparer<Type>.Default.Equals(HandlerType, other.HandlerType) &&
107+
Method == other.Method &&
108+
Key == other.Key;
104109
}
105110

106111
public override int GetHashCode()
107112
{
108-
return Tuple.Create(HandlerType, Key).GetHashCode();
113+
var hashCode = -45133801;
114+
hashCode = hashCode * -1521134295 + EqualityComparer<Type>.Default.GetHashCode(HandlerType);
115+
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Method);
116+
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Key);
117+
return hashCode;
118+
}
119+
120+
public static bool operator ==(HandlerDescriptor descriptor1, HandlerDescriptor descriptor2)
121+
{
122+
return EqualityComparer<HandlerDescriptor>.Default.Equals(descriptor1, descriptor2);
123+
}
124+
125+
public static bool operator !=(HandlerDescriptor descriptor1, HandlerDescriptor descriptor2)
126+
{
127+
return !(descriptor1 == descriptor2);
109128
}
110129
}
111130
}

test/Lsp.Tests/HandlerResolverTests.cs

+67-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OmniSharp.Extensions.LanguageServer.Protocol;
88
using NSubstitute;
99
using OmniSharp.Extensions.JsonRpc;
10+
using OmniSharp.Extensions.LanguageServer;
1011
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
1112
using Xunit;
1213
using HandlerCollection = OmniSharp.Extensions.LanguageServer.HandlerCollection;
@@ -17,7 +18,6 @@ namespace Lsp.Tests
1718
{
1819
public class HandlerResolverTests
1920
{
20-
2121
[Theory]
2222
[InlineData(typeof(IInitializeHandler), "initialize", 1)]
2323
[InlineData(typeof(IInitializedHandler), "initialized", 1)]
@@ -35,11 +35,11 @@ public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, in
3535
}
3636

3737
[Theory]
38-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 4)]
39-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 4)]
40-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 4)]
41-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 4)]
42-
public void Should_Contain_AllDefinedTextDocumentSyncMethods(Type requestHandler, string key, int count)
38+
[InlineData("textDocument/didOpen", 4)]
39+
[InlineData("textDocument/didChange", 4)]
40+
[InlineData("textDocument/didClose", 4)]
41+
[InlineData("textDocument/didSave", 4)]
42+
public void Should_Contain_AllDefinedTextDocumentSyncMethods(string key, int count)
4343
{
4444
var handler = new HandlerCollection();
4545
var sub = (IJsonRpcHandler)TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.something"));
@@ -49,6 +49,50 @@ public void Should_Contain_AllDefinedTextDocumentSyncMethods(Type requestHandler
4949
handler._handlers.Count.Should().Be(count);
5050
}
5151

52+
[Theory]
53+
[InlineData("exit", 4)]
54+
[InlineData("shutdown", 4)]
55+
[InlineData("initialized", 4)]
56+
[InlineData("initialize", 4)]
57+
public void Should_Contain_AllDefinedLanguageServerMethods(string key, int count)
58+
{
59+
var handler = new HandlerCollection();
60+
handler.Add(
61+
Substitute.For<IInitializeHandler>(),
62+
Substitute.For<IInitializedHandler>(),
63+
Substitute.For<IExitHandler>(),
64+
Substitute.For<IShutdownHandler>()
65+
);
66+
handler._handlers.Should().Contain(x => x.Method == key);
67+
handler._handlers.Count.Should().Be(count);
68+
}
69+
70+
[Theory]
71+
[InlineData("exit", 4)]
72+
[InlineData("shutdown", 4)]
73+
[InlineData("initialized", 4)]
74+
[InlineData("initialize", 4)]
75+
public void Should_Contain_AllDefinedLanguageServerMethods_GivenDuplicates(string key, int count)
76+
{
77+
var handler = new HandlerCollection();
78+
handler.Add(
79+
Substitute.For<IInitializeHandler>(),
80+
Substitute.For<IInitializedHandler>(),
81+
Substitute.For<IExitHandler>(),
82+
Substitute.For<IShutdownHandler>(),
83+
Substitute.For<IInitializeHandler>(),
84+
Substitute.For<IInitializedHandler>(),
85+
Substitute.For<IExitHandler>(),
86+
Substitute.For<IShutdownHandler>(),
87+
Substitute.For<IInitializeHandler>(),
88+
Substitute.For<IInitializedHandler>(),
89+
Substitute.For<IExitHandler>(),
90+
Substitute.For<IShutdownHandler>()
91+
);
92+
handler._handlers.Should().Contain(x => x.Method == key);
93+
handler._handlers.Count.Should().Be(count);
94+
}
95+
5296
[Theory]
5397
[InlineData("textDocument/didOpen", 8)]
5498
[InlineData("textDocument/didChange", 8)]
@@ -107,5 +151,22 @@ public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(
107151
handler._handlers.Should().Contain(x => x.Method == key2);
108152
handler._handlers.Count.Should().Be(count);
109153
}
154+
155+
[Fact]
156+
public void Should_BeAwesome()
157+
{
158+
var handler = new HandlerCollection();
159+
160+
handler.Add(
161+
Substitute.For<IExitHandler>(),
162+
Substitute.For<IInitializeHandler>(),
163+
Substitute.For<IInitializedHandler>(),
164+
Substitute.For<IShutdownHandler>()
165+
);
166+
167+
handler._handlers.Should().Contain(x => x.Method == "exit");
168+
handler._handlers.Should().Contain(x => x.Method == "shutdown");
169+
handler._handlers.Count.Should().Be(4);
170+
}
110171
}
111172
}

0 commit comments

Comments
 (0)