Skip to content

Commit

Permalink
Fixed bug in composite handler managera and added unit tests (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll authored Aug 15, 2020
1 parent 0b9e85d commit 1497f35
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonRpc/CompositeHandlersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
101 changes: 101 additions & 0 deletions test/JsonRpc.Tests/CompositeHandlersManagerTests.cs
Original file line number Diff line number Diff line change
@@ -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<IHandlersManager>();
parent.Add(Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add(Substitute.For<IJsonRpcHandler>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<string>(), Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add(Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}

[Fact]
public void Should_Add_Named_Handler_Instance_To_Parent()
{
var parent = Substitute.For<IHandlersManager>();
parent.Add(Arg.Any<string>(), Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add("mymethod", Substitute.For<IJsonRpcHandler>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add("mymethod", Arg.Any<IJsonRpcHandler>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}

[Fact]
public void Should_Add_Handler_Factory_To_Parent()
{
var parent = Substitute.For<IHandlersManager>();
parent.Add(Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add(Substitute.For<JsonRpcHandlerFactory>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<string>(), Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add(Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}

[Fact]
public void Should_Add_Named_Handler_Factory_To_Parent()
{
var parent = Substitute.For<IHandlersManager>();
parent.Add(Arg.Any<string>(), Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add("mymethod", Substitute.For<JsonRpcHandlerFactory>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add("mymethod", Arg.Any<JsonRpcHandlerFactory>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}

[Fact]
public void Should_Add_Handler_Type_To_Parent()
{
var parent = Substitute.For<IHandlersManager>();
parent.Add(Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add(Substitute.For<Type>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<string>(), Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add(Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}

[Fact]
public void Should_Add_Named_Handler_Type_To_Parent()
{
var parent = Substitute.For<IHandlersManager>();
parent.Add(Arg.Any<string>(), Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>()).Returns(Disposable.Empty);

var manager = new CompositeHandlersManager(parent);
manager.Add("mymethod", Substitute.For<Type>(), new JsonRpcHandlerOptions());

parent.Received(0).Add(Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>());
parent.Received(1).Add("mymethod", Arg.Any<Type>(), Arg.Any<JsonRpcHandlerOptions>());
manager.GetDisposable().Count.Should().Be(1);
}
}
}
1 change: 1 addition & 0 deletions test/JsonRpc.Tests/TestLanguageServerRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Composition;
using OmniSharp.Extensions.JsonRpc;

namespace JsonRpc.Tests
Expand Down

0 comments on commit 1497f35

Please sign in to comment.