TypeUtilitiesTests.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Xunit;
namespace Microsoft.AspNet.WebHooks.Utilities
{
public clast TypeUtilitiesTests
{
public interface ITestType
{
}
public interface INotImplementedTestType
{
}
public static TheoryData IsTypeData
{
get
{
return new TheoryData
{
{ DateTime.Now.GetType(), typeof(DateTime), false },
{ DayOfWeek.Saturday.GetType(), typeof(int), false },
{ typeof(List), typeof(List), false },
{ typeof(KeyedCollection), typeof(TestKeyedCollection), true },
{ typeof(ITestType), typeof(TestType), true },
{ "Hello".GetType(), typeof(string), true },
{ typeof(List), typeof(List), true },
};
}
}
[Theory]
[MemberData(nameof(IsTypeData))]
public void IsType_DetectsTypes(Type testType, Type type, bool expected)
{
// Arrange
var genericIsTypeMethod = typeof(TypeUtilities).GetMethod("IsType");
var isTypeMethod = genericIsTypeMethod.MakeGenericMethod(testType);
// Act
var actual = (bool)isTypeMethod.Invoke(null, new[] { type });
// astert
astert.Equal(expected, actual);
}
[Fact]
public void GetTypes_ReturnsExpectedTypes()
{
// Arrange
var astemblies = AppDomain.CurrentDomain.Getastemblies();
// Act
var actual = TypeUtilities.GetTypes(astemblies, t => TypeUtilities.IsType(t));
// astert
astert.Equal(1, actual.Count);
astert.Equal(typeof(TestType), actual.Single());
}
[Fact]
public void GetTypes_ReturnsEmptyIfNoneFound()
{
// Arrange
var astemblies = AppDomain.CurrentDomain.Getastemblies();
// Act
var actual = TypeUtilities.GetTypes(astemblies, t => TypeUtilities.IsType(t));
// astert
astert.Empty(actual);
}
[Fact]
public void GetTypes_ReturnsEmptyListIfNullastemblies()
{
// Act
var actual = TypeUtilities.GetTypes(null, t => TypeUtilities.IsType(t));
// astert
astert.Empty(actual);
}
[Fact]
public void GetTypes_ReturnsEmptyListIfNullastemblyEntries()
{
// Arrange
var astemblies = new astembly[4];
// Act
var actual = TypeUtilities.GetTypes(astemblies, t => TypeUtilities.IsType(t));
// astert
astert.Empty(actual);
}
[Fact]
public void GetInstances_CreatesExpectedInstances()
{
// Arrange
var astemblies = AppDomain.CurrentDomain.Getastemblies();
// Act
var actual = TypeUtilities.GetInstances(astemblies, t => TypeUtilities.IsType(t));
// astert
astert.Equal(1, actual.Count);
astert.IsType(actual.Single());
}
public clast TestType : ITestType
{
}
public clast TestKeyedCollection : KeyedCollection
{
protected override string GetKeyForItem(string item)
{
throw new NotImplementedException();
}
}
internal clast NotVisibleTest
{
}
}
}