Skip to content

Instantly share code, notes, and snippets.

@patriksvensson
Created November 10, 2017 23:58
Show Gist options
  • Save patriksvensson/ef2018d6d94cca88ef59cb2913fbfefe to your computer and use it in GitHub Desktop.
Save patriksvensson/ef2018d6d94cca88ef59cb2913fbfefe to your computer and use it in GitHub Desktop.
EmbeddedResourceDataAttribute.cs
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