diff --git a/Octokit.Tests.Conventions/Exception/PaginationGetAllMethodNameMismatchException.cs b/Octokit.Tests.Conventions/Exception/PaginationGetAllMethodNameMismatchException.cs new file mode 100644 index 00000000..8c20067e --- /dev/null +++ b/Octokit.Tests.Conventions/Exception/PaginationGetAllMethodNameMismatchException.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit.Tests.Conventions +{ + public class PaginationGetAllMethodNameMismatchException : Exception + { + public PaginationGetAllMethodNameMismatchException(Type type, IEnumerable methods) + : base(CreateMessage(type, methods)) { } + + static string CreateMessage(Type type, IEnumerable methods) + { + var methodsFormatted = String.Join("\r\n", methods.Select(m => String.Format(" - {0}", m))); + return "Methods found on type {0} should follow the 'GetAll*' naming convention:\r\n{1}" + .FormatWithNewLine( + type.Name, + methodsFormatted); + } + } +} diff --git a/Octokit.Tests.Conventions/ModelTests.cs b/Octokit.Tests.Conventions/ModelTests.cs index 19e54cc5..df87d961 100644 --- a/Octokit.Tests.Conventions/ModelTests.cs +++ b/Octokit.Tests.Conventions/ModelTests.cs @@ -59,6 +59,31 @@ namespace Octokit.Tests.Conventions } } + //TODO: This should (probably) be moved to the PaginationTests class that is being introduced in PR #760 + [Theory] + [MemberData("GetClientInterfaces")] + public void CheckPaginationGetAllMethodNames(Type clientInterface) + { + var methodsOrdered = clientInterface.GetMethodsOrdered(); + + var methodsThatCanPaginate = methodsOrdered + .Where(x => x.ReturnType.GetTypeInfo().TypeCategory == TypeCategory.ReadOnlyList) + .Where(x => x.Name.StartsWith("Get")); + + var invalidMethods = methodsThatCanPaginate + .Where(x => !x.Name.StartsWith("GetAll")); + + if (invalidMethods.Any()) + { + throw new PaginationGetAllMethodNameMismatchException(clientInterface, invalidMethods); + } + } + + public static IEnumerable GetClientInterfaces() + { + return typeof(IEventsClient).Assembly.ExportedTypes.Where(TypeExtensions.IsClientInterface).Select(type => new[] { type }); + } + public static IEnumerable ModelTypes { get { return GetModelTypes(includeRequestModels: true).Select(type => new[] { type }); } diff --git a/Octokit.Tests.Conventions/Octokit.Tests.Conventions.csproj b/Octokit.Tests.Conventions/Octokit.Tests.Conventions.csproj index 0f4f7244..d7ec4332 100644 --- a/Octokit.Tests.Conventions/Octokit.Tests.Conventions.csproj +++ b/Octokit.Tests.Conventions/Octokit.Tests.Conventions.csproj @@ -57,6 +57,7 @@ +