Skip to content

Commit

Permalink
Implement XmlDataSourceAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Nov 21, 2024
1 parent 8260bd6 commit 8f73a4a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions TestFx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Testing.Extension
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFramework.Extensions.Csv", "src\TestFramework\TestFramework.Extensions.Csv\TestFramework.Extensions.Csv.csproj", "{536A4485-F77F-4617-8449-2DCB387514AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFramework.Extensions.Xml", "src\TestFramework\TestFramework.Extensions.Xml\TestFramework.Extensions.Xml.csproj", "{7BF7BE9E-8D46-47ED-B45D-121C2252DF16}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -489,6 +491,10 @@ Global
{536A4485-F77F-4617-8449-2DCB387514AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{536A4485-F77F-4617-8449-2DCB387514AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{536A4485-F77F-4617-8449-2DCB387514AB}.Release|Any CPU.Build.0 = Release|Any CPU
{7BF7BE9E-8D46-47ED-B45D-121C2252DF16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BF7BE9E-8D46-47ED-B45D-121C2252DF16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BF7BE9E-8D46-47ED-B45D-121C2252DF16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BF7BE9E-8D46-47ED-B45D-121C2252DF16}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -574,6 +580,7 @@ Global
{F422398C-72CD-43EA-AC8E-E0DBD08E5563} = {BB874DF1-44FE-415A-B634-A6B829107890}
{8CE782A2-7374-4916-9C69-1F87E51A64A9} = {6AEE1440-FDF0-4729-8196-B24D0E333550}
{536A4485-F77F-4617-8449-2DCB387514AB} = {E48AC786-E150-4F41-9A16-32F02E4493D8}
{7BF7BE9E-8D46-47ED-B45D-121C2252DF16} = {E48AC786-E150-4F41-9A16-32F02E4493D8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9}
Expand Down
9 changes: 9 additions & 0 deletions src/TestFramework/TestFramework.Extensions.Xml/PACKAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MSTest.TestFramework

MSTest is Microsoft supported Test Framework.

This package includes the functionality needed for writing Csv-based data source unit tests.

Supported platforms:

- .NET Standard 2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#nullable enable
Microsoft.VisualStudio.TestTools.UnitTesting.XmlDataSourceAttribute
Microsoft.VisualStudio.TestTools.UnitTesting.XmlDataSourceAttribute.XmlDataSourceAttribute(string! fileName, string! tableName) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!-- TODO: (before merge) Is this hack relevant for a new project? -->
<UseAssemblyVersion14>true</UseAssemblyVersion14>
</PropertyGroup>

<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageId>MSTest.TestFramework.Xml</PackageId>
<PackageTags>MSTest TestFramework Unittest MSTestV2 Microsoft Test Testing TDD Framework</PackageTags>
<PackageDescription>
MSTest is Microsoft supported Test Framework.

This package includes the functionality needed for writing Xml-based data source unit tests.

Supported platforms:
- .NET Standard 2.0
</PackageDescription>
</PropertyGroup>

<PropertyGroup>
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace>
<!-- TODO: Discuss the assembly name before shipping/merging -->
<AssemblyName>Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.Xml</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework.Extensions\TestFramework.Extensions.csproj" />
</ItemGroup>

<ItemGroup>
<!-- API that is common to all frameworks that we build for. -->
<AdditionalFiles Include="PublicAPI\PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI\PublicAPI.Unshipped.txt" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Data;
using System.Globalization;
using System.Reflection;
using System.Xml;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Attribute to define dynamic data from an XML file for a test method.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false/*TODO: Do we want to allow multiple*/)]
public sealed class XmlDataSourceAttribute : Attribute, ITestDataSource
{
public XmlDataSourceAttribute(string fileName, string tableName)
{
FileName = fileName;
TableName = tableName;
}

internal string FileName { get; }

internal string TableName { get; }

IEnumerable<object?[]> ITestDataSource.GetData(MethodInfo methodInfo)
{
string fullPath = Path.GetFullPath(FileName);
if (!File.Exists(fullPath))
{
// TODO: Localize.
throw new FileNotFoundException($"Xml file '{fullPath}' cannot be found.", fullPath);
}

DataSet dataSet = new()
{
Locale = CultureInfo.CurrentCulture,
};

// ReadXml should use the overload with XmlReader to avoid DTD processing
dataSet.ReadXml(new XmlTextReader(fullPath));

DataTable table = dataSet.Tables[TableName];

object?[][] dataRows = new object?[table.Rows.Count][];
for (int i = 0; i < dataRows.Length; i++)
{
dataRows[i] = [table.Rows[i]];
}

return dataRows;
}

string? ITestDataSource.GetDisplayName(MethodInfo methodInfo, object?[]? data)
// TODO
=> null;
}

0 comments on commit 8f73a4a

Please sign in to comment.