using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; using Xunit; namespace Octokit.Tests.Helpers { public static class AssertEx { public static void WithMessage(Action assert, string message) { // TODO: we should just :fire: this to the ground assert(); } public static TAttribute HasAttribute(MemberInfo memberInfo, bool inherit = false) where TAttribute : Attribute { var attribute = memberInfo.GetCustomAttribute(inherit); Assert.NotNull(attribute); return attribute; } public async static Task Throws(Func testCode) where T : Exception { try { await testCode(); Assert.Throws(() => { }); // Use xUnit's default behavior. } catch (T exception) { return exception; } // We should never reach this line. It's here because the compiler doesn't know that // Assert.Throws above will always throw. return null; } static readonly string[] whitespaceArguments = { " ", "\t", "\n", "\n\r", " " }; public static async Task ThrowsWhenGivenWhitespaceArgument(Func action) { foreach (var argument in whitespaceArguments) { await Throws(async () => await action(argument)); } } public static void IsReadOnlyCollection(object instance) { var collection = instance as ICollection; // The collection == null case is for .NET 4.0 Assert.True(instance is IReadOnlyList && (collection == null || collection.IsReadOnly)); } public static void IsReadOnlyCollection(Type type) { var isReadOnlyList = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IReadOnlyList<>); var isReadOnlyDictionary = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>); Assert.True(isReadOnlyList || isReadOnlyDictionary); } } }