throw more detailed exceptions for type not found and interface mismatches

This commit is contained in:
Brendan Forster
2014-02-20 22:11:02 +11:00
parent 14de4a6edf
commit 87638fc41d
6 changed files with 96 additions and 11 deletions
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace Octokit.Tests.Conventions
{
public class InterfaceHasAdditionalMethodsException : Exception
{
public InterfaceHasAdditionalMethodsException(Type type, IEnumerable<string> methodsMissingOnReactiveClient)
: base(CreateMessage(type, methodsMissingOnReactiveClient)) { }
public InterfaceHasAdditionalMethodsException(Type type, IEnumerable<string> methodsMissingOnReactiveClient, Exception innerException)
: base(CreateMessage(type, methodsMissingOnReactiveClient), innerException) { }
protected InterfaceHasAdditionalMethodsException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
static string CreateMessage(Type type, IEnumerable<string> methods)
{
var methodsFormatted = String.Join("\r\n", methods.Select(m => String.Format(" - {0}", m)));
return String.Format("Methods found on type {0} which should be removed:\r\n{1}", type.Name, methodsFormatted);
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace Octokit.Tests.Conventions
{
public class InterfaceMissingMethodsException : Exception
{
public InterfaceMissingMethodsException(Type type, IEnumerable<string> methodsMissingOnReactiveClient)
: base(CreateMessage(type, methodsMissingOnReactiveClient)) { }
public InterfaceMissingMethodsException(Type type, IEnumerable<string> methodsMissingOnReactiveClient, Exception innerException)
: base(CreateMessage(type, methodsMissingOnReactiveClient), innerException) { }
protected InterfaceMissingMethodsException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
static string CreateMessage(Type type, IEnumerable<string> methods)
{
var methodsFormatted = String.Join("\r\n", methods.Select(m => String.Format(" - {0}", m)));
return String.Format("Methods not found on interface {0} which are required:\r\n{1}", type.Name, methodsFormatted);
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Runtime.Serialization;
namespace Octokit.Tests.Conventions
{
public class InterfaceNotFoundException : Exception
{
public InterfaceNotFoundException() { }
public InterfaceNotFoundException(string type)
: base(CreateMessage(type)) { }
public InterfaceNotFoundException(string type, Exception innerException)
: base(CreateMessage(type), innerException) { }
protected InterfaceNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
static string CreateMessage(string type)
{
return String.Format("Could not find the interface {0}. Add this to the Octokit.Reactive project", type);
}
}
}
@@ -50,6 +50,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DebuggerDisplayOnModels.cs" />
<Compile Include="Exception\InterfaceHasAdditionalMethodsException.cs" />
<Compile Include="Exception\InterfaceMissingMethodsException.cs" />
<Compile Include="Exception\InterfaceNotFoundException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SyncObservableClients.cs" />
<Compile Include="TypeExtensions.cs" />
@@ -13,23 +13,30 @@ namespace Octokit.Tests.Conventions
{
public class SyncObservableClients
{
[Fact]
private void CheckObservableClientExample()
{
CheckObservableClients(typeof(ISearchClient));
}
[Theory]
[ClassData(typeof(ClientInterfaces))]
private void CheckObservableClients(Type clientInterface)
public void CheckObservableClients(Type clientInterface)
{
var observableClient = clientInterface.GetObservableClientInterface();
var mainMethods = clientInterface.GetMethodsOrdered();
var observableMethods = observableClient.GetMethodsOrdered();
var mainNames = Array.ConvertAll(mainMethods, m => m.Name);
var observableNames = Array.ConvertAll(observableMethods, m => m.Name);
AssertEx.Empty(observableNames.Except(mainNames), "Extra observable methods");
AssertEx.Empty(mainNames.Except(observableNames), "Missing observable methods");
var methodsMissingOnReactiveClient = mainNames.Except(observableNames);
if (methodsMissingOnReactiveClient.Any())
{
throw new InterfaceMissingMethodsException(observableClient, methodsMissingOnReactiveClient);
}
var additionalMethodsOnReactiveClient = observableNames.Except(mainNames);
if (additionalMethodsOnReactiveClient.Any())
{
throw new InterfaceHasAdditionalMethodsException(observableClient, additionalMethodsOnReactiveClient);
}
int index = 0;
foreach(var mainMethod in mainMethods)
{
@@ -88,7 +95,7 @@ namespace Octokit.Tests.Conventions
{
var observableParameter = observableParameters[index];
Assert.Equal(mainParameter.Name, observableParameter.Name);
var mainType = mainParameter.ParameterType;
var mainType = mainParameter.ParameterType;
var typeInfo = mainType.GetTypeInfo();
var expectedType = GetObservableExpectedType(mainType);
Assert.Equal(expectedType, observableParameter.ParameterType);
+1 -1
View File
@@ -80,7 +80,7 @@ namespace Octokit.Tests.Conventions
var observableInterface = observableClient.Assembly.GetType(observableClientName);
if(observableInterface == null)
{
throw new Exception("Cannot find observable interface "+observableClientName);
throw new InterfaceNotFoundException(observableClientName);
}
return observableInterface;
}