From 1497f354b1c1aac0d0ee22ff00b901d22911841f Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Fri, 14 Aug 2020 22:42:27 -0400 Subject: [PATCH] Fixed bug in composite handler managera and added unit tests (#316) --- src/JsonRpc/CompositeHandlersManager.cs | 2 +- .../CompositeHandlersManagerTests.cs | 101 ++++++++++++++++++ .../TestLanguageServerRegistry.cs | 1 + 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 test/JsonRpc.Tests/CompositeHandlersManagerTests.cs diff --git a/src/JsonRpc/CompositeHandlersManager.cs b/src/JsonRpc/CompositeHandlersManager.cs index f4feb00bc..384cc8399 100644 --- a/src/JsonRpc/CompositeHandlersManager.cs +++ b/src/JsonRpc/CompositeHandlersManager.cs @@ -36,7 +36,7 @@ public IDisposable Add(JsonRpcHandlerFactory factory, JsonRpcHandlerOptions opti public IDisposable Add(string method, JsonRpcHandlerFactory factory, JsonRpcHandlerOptions options) { - var result = _parent.Add(factory, options); + var result = _parent.Add(method, factory, options); _compositeDisposable.Add(result); return result; } diff --git a/test/JsonRpc.Tests/CompositeHandlersManagerTests.cs b/test/JsonRpc.Tests/CompositeHandlersManagerTests.cs new file mode 100644 index 000000000..7ffe639b4 --- /dev/null +++ b/test/JsonRpc.Tests/CompositeHandlersManagerTests.cs @@ -0,0 +1,101 @@ +using System; +using System.Reactive.Disposables; +using FluentAssertions; +using NSubstitute; +using OmniSharp.Extensions.JsonRpc; +using Xunit; +using Xunit.Abstractions; + +namespace JsonRpc.Tests +{ + public class CompositeHandlersManagerTests : AutoTestBase + { + public CompositeHandlersManagerTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) + { + } + + [Fact] + public void Should_Add_Handler_Instance_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add(Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any(), Arg.Any()); + parent.Received(1).Add(Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + + [Fact] + public void Should_Add_Named_Handler_Instance_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add("mymethod", Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any()); + parent.Received(1).Add("mymethod", Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + + [Fact] + public void Should_Add_Handler_Factory_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add(Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any(), Arg.Any()); + parent.Received(1).Add(Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + + [Fact] + public void Should_Add_Named_Handler_Factory_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add("mymethod", Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any()); + parent.Received(1).Add("mymethod", Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + + [Fact] + public void Should_Add_Handler_Type_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add(Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any(), Arg.Any()); + parent.Received(1).Add(Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + + [Fact] + public void Should_Add_Named_Handler_Type_To_Parent() + { + var parent = Substitute.For(); + parent.Add(Arg.Any(), Arg.Any(), Arg.Any()).Returns(Disposable.Empty); + + var manager = new CompositeHandlersManager(parent); + manager.Add("mymethod", Substitute.For(), new JsonRpcHandlerOptions()); + + parent.Received(0).Add(Arg.Any(), Arg.Any()); + parent.Received(1).Add("mymethod", Arg.Any(), Arg.Any()); + manager.GetDisposable().Count.Should().Be(1); + } + } +} diff --git a/test/JsonRpc.Tests/TestLanguageServerRegistry.cs b/test/JsonRpc.Tests/TestLanguageServerRegistry.cs index fc2f46f76..589218062 100644 --- a/test/JsonRpc.Tests/TestLanguageServerRegistry.cs +++ b/test/JsonRpc.Tests/TestLanguageServerRegistry.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Composition; using OmniSharp.Extensions.JsonRpc; namespace JsonRpc.Tests