Created
November 10, 2017 23:58
-
-
Save patriksvensson/ef2018d6d94cca88ef59cb2913fbfefe to your computer and use it in GitHub Desktop.
EmbeddedResourceDataAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Reflection; | |
using Xunit.Sdk; | |
namespace MyProject.Tests | |
{ | |
public sealed class EmbeddedResourceDataAttribute : DataAttribute | |
{ | |
private readonly string[] _args; | |
public EmbeddedResourceDataAttribute(params string[] args) | |
{ | |
_args = args; | |
} | |
public override IEnumerable<object[]> GetData(MethodInfo testMethod) | |
{ | |
var result = new object[_args.Length]; | |
for (var index = 0; index < _args.Length; index++) | |
{ | |
result[index] = ReadManifestData(_args[index]); | |
} | |
return new[] { result }; | |
} | |
public static string ReadManifestData(string resourceName) | |
{ | |
var assembly = typeof(EmbeddedResourceDataAttribute).GetTypeInfo().Assembly; | |
resourceName = resourceName.Replace("/", "."); | |
using (var stream = assembly.GetManifestResourceStream(resourceName)) | |
{ | |
if (stream == null) | |
{ | |
throw new InvalidOperationException("Could not load manifest resource stream."); | |
} | |
using (var reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment