Skip to content

Commit

Permalink
Understanding category attribute from adapter (#1528)
Browse files Browse the repository at this point in the history
* Understanding category attribute from adapter

* review comments
  • Loading branch information
abhishkk committed Apr 5, 2018
1 parent 8d20bc7 commit 93ff962
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;

Expand Down Expand Up @@ -169,7 +170,7 @@ internal class TestDiscovererMetadata : ITestDiscovererCapabilities
/// </summary>
/// <param name="fileExtensions"> The file Extensions. </param>
/// <param name="defaultExecutorUri"> The default Executor Uri. </param>
public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string defaultExecutorUri)
public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string defaultExecutorUri, AssemblyType assemblyType = default(AssemblyType))
{
if (fileExtensions != null && fileExtensions.Count > 0)
{
Expand All @@ -180,6 +181,8 @@ public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string
{
this.DefaultExecutorUri = new Uri(defaultExecutorUri);
}

this.AssemblyType = assemblyType;
}

/// <summary>
Expand All @@ -199,5 +202,14 @@ public Uri DefaultExecutorUri
get;
private set;
}

/// <summary>
/// Gets assembly type supported by the discoverer.
/// </summary>
public AssemblyType AssemblyType
{
get;
private set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilitie
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using ObjectModel;

/// <summary>
Expand All @@ -26,6 +28,7 @@ public TestDiscovererPluginInformation(Type testDiscovererType)
{
this.FileExtensions = GetFileExtensions(testDiscovererType);
this.DefaultExecutorUri = GetDefaultExecutorUri(testDiscovererType);
this.AssemblyType = GetAssemblyType(testDiscovererType);
}
}

Expand All @@ -36,7 +39,7 @@ public override ICollection<Object> Metadata
{
get
{
return new object[] { this.FileExtensions, this.DefaultExecutorUri };
return new object[] { this.FileExtensions, this.DefaultExecutorUri, this.AssemblyType };
}
}

Expand All @@ -58,6 +61,15 @@ public string DefaultExecutorUri
private set;
}

/// <summary>
/// Gets the assembly type supported by discoverer plugin.
/// </summary>
public AssemblyType AssemblyType
{
get;
private set;
}

/// <summary>
/// Helper to get file extensions from the FileExtensionAttribute on the discover plugin.
/// </summary>
Expand Down Expand Up @@ -105,5 +117,23 @@ private static string GetDefaultExecutorUri(Type testDiscovererType)

return result;
}

/// <summary>
/// Helper to get the supported assembly type from the CategoryAttribute on the discover plugin.
/// </summary>
/// <param name="testDiscovererType"> The test discoverer Type. </param>
/// <returns> Supported assembly type. </returns>
private AssemblyType GetAssemblyType(Type testDiscovererType)
{
var assemblyType = default(AssemblyType);

// Get Category
var attribute = testDiscovererType.GetTypeInfo().GetCustomAttribute(typeof(CategoryAttribute));
var category = (attribute as CategoryAttribute)?.Category;

// Get assembly type from category.
Enum.TryParse(category, true, out assemblyType);
return assemblyType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Interfaces
using System;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestPlatform.Common.Utilities;

/// <summary>
/// Metadata that is available from Test Discoverers.
/// </summary>
Expand All @@ -20,5 +22,10 @@ public interface ITestDiscovererCapabilities
/// Default executor Uri for this discoverer
/// </summary>
Uri DefaultExecutorUri { get; }

/// <summary>
/// Assembly type that the test discoverer supports.
/// </summary>
AssemblyType AssemblyType { get; }
}
}
12 changes: 12 additions & 0 deletions src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities
{
public enum AssemblyType
{
None,
Native,
Managed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TestPlatform.Common.UnitTests.ExtensionFramework
using System.Reflection;

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand Down Expand Up @@ -124,5 +125,13 @@ public void TestDiscovererMetadataCtorSetsDefaultUri()

Assert.AreEqual("executor://helloworld/", metadata.DefaultExecutorUri.AbsoluteUri);
}

[TestMethod]
public void TestDiscovererMetadataCtorSetsAssemblyType()
{
var metadata = new TestDiscovererMetadata(null, "executor://helloworld", AssemblyType.Native);

Assert.AreEqual(AssemblyType.Native, metadata.AssemblyType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
namespace TestPlatform.Common.UnitTests.ExtensionFramework.Utilities
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Moq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[TestClass]
public class LazyExtensionTests
Expand Down Expand Up @@ -96,6 +98,7 @@ public void MetadataShouldCreateMetadataFromMetadataType()
Assert.AreEqual(typeof(DummyDiscovererCapability), metadata.GetType());
CollectionAssert.AreEqual(new List<string> { "csv" }, (metadata as ITestDiscovererCapabilities).FileExtension.ToArray());
Assert.AreEqual("executor://unittestexecutor/", (metadata as ITestDiscovererCapabilities).DefaultExecutorUri.AbsoluteUri);
Assert.AreEqual(AssemblyType.Native, (metadata as ITestDiscovererCapabilities).AssemblyType);
}

#endregion
Expand All @@ -116,16 +119,24 @@ public Uri DefaultExecutorUri
private set;
}

public DummyDiscovererCapability(List<string> fileExtensions, string executorURI)
public AssemblyType AssemblyType
{
get;
private set;
}

public DummyDiscovererCapability(List<string> fileExtensions, string executorURI, AssemblyType assemblyType)
{
this.FileExtension = fileExtensions;
this.DefaultExecutorUri = new Uri(executorURI);
this.AssemblyType = assemblyType;
}
}


[FileExtension("csv")]
[DefaultExecutorUri("executor://unittestexecutor")]
[Category("native")]
private class DummyExtension : ITestDiscoverer
{
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace TestPlatform.Common.UnitTests.ExtensionFramework.Utilities
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class TestDiscovererPluginInformationTests
{
Expand Down Expand Up @@ -52,6 +54,55 @@ public void FileExtensionsShouldReturnSupportedFileExtensionsForADiscoverer()
CollectionAssert.AreEqual(new List<string> {"csv", "docx"}, this.testPluginInformation.FileExtensions);
}

[TestMethod]
public void AssemblyTypeShouldReturnNoneIfDiscovererHasNoCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNoCategory));
Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnNoneIfDiscovererHasCategoryWithNoValue()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithCategoryHavingNoValue));
Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnNoneIfDiscovererHasCategoryWithEmptyValue()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithCategoryHavingEmptyValue));
Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnNativeIfDiscovererHasNativeCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNativeCategory));
Assert.AreEqual(AssemblyType.Native, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnManagedIfDiscovererHasManagedCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithManagedCategory));
Assert.AreEqual(AssemblyType.Managed, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnNoneIfDiscovererHasUnknownCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithUnknownCategory));
Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnAssemblyTypeIfDiscovererHasCategoryInArbitCasing()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithArbitCasedCategory));
Assert.AreEqual(AssemblyType.Native, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void DefaultExecutorUriShouldReturnEmptyListIfADiscovererDoesNotHaveOne()
{
Expand All @@ -68,7 +119,7 @@ public void DefaultExecutorUriShouldReturnDefaultExecutorUriOfADiscoverer()
}

[TestMethod]
public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri()
public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUriAndAssemblyType()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovererWithTwoFileExtensions));

Expand All @@ -77,6 +128,7 @@ public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri()

CollectionAssert.AreEqual(expectedFileExtensions, (testPluginMetada[0] as List<string>).ToArray());
Assert.AreEqual("csvexecutor", testPluginMetada[1] as string);
Assert.AreEqual(AssemblyType.Managed, Enum.Parse(typeof(AssemblyType), testPluginMetada[2].ToString()));
}
}

Expand All @@ -86,6 +138,47 @@ public class DummyTestDiscovererWithNoFileExtensions
{
}

[FileExtension(".dll")]
public class DummyTestDiscovereWithNoCategory
{
}

[FileExtension(".dll")]
[Category]
public class DummyTestDiscovereWithCategoryHavingNoValue
{
}

[FileExtension(".dll")]
[Category]
public class DummyTestDiscovereWithCategoryHavingEmptyValue
{
}

[FileExtension(".js")]
[Category("native")]
public class DummyTestDiscovereWithNativeCategory
{
}

[FileExtension(".dll")]
[Category("managed")]
public class DummyTestDiscovereWithManagedCategory
{
}

[FileExtension(".dll")]
[Category("arbitValue")]
public class DummyTestDiscovereWithUnknownCategory
{
}

[FileExtension(".dll")]
[Category("NatIVe")]
public class DummyTestDiscovereWithArbitCasedCategory
{
}

[FileExtension("csv")]
[DefaultExecutorUri("csvexecutor")]
public class DummyTestDiscovererWithOneFileExtensions
Expand All @@ -94,6 +187,7 @@ public class DummyTestDiscovererWithOneFileExtensions

[FileExtension("csv")]
[FileExtension("docx")]
[Category("managed")]
[DefaultExecutorUri("csvexecutor")]
public class DummyTestDiscovererWithTwoFileExtensions
{
Expand Down

0 comments on commit 93ff962

Please sign in to comment.