Skip to content

Instantly share code, notes, and snippets.

@MoaidHathot
Created February 8, 2017 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoaidHathot/b599ea95cf3a70b8f99f1254ece6a967 to your computer and use it in GitHub Desktop.
Save MoaidHathot/b599ea95cf3a70b8f99f1254ece6a967 to your computer and use it in GitHub Desktop.
KnownType.Fody Unit Tests for Blog Post
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using KnownTypes.Fody;
using Mono.Cecil;
using NUnit.Framework;
[TestFixture]
public class WeaverTests
{
private Assembly _assembly;
private string _newAssemblyPath;
private string _assemblyPath;
[TestFixtureSetUp]
public void Setup()
{
var projectPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\AssemblyToProcess\AssemblyToProcess.csproj"));
_assemblyPath = Path.Combine(Path.GetDirectoryName(projectPath), @"bin\Debug\AssemblyToProcess.dll");
_newAssemblyPath = _assemblyPath.Replace(".dll", "2.dll");
File.Copy(_assemblyPath, _newAssemblyPath, true);
var moduleDefinition = ModuleDefinition.ReadModule(_newAssemblyPath);
var weavingTask = new ModuleWeaver
{
ModuleDefinition = moduleDefinition
};
weavingTask.Execute();
moduleDefinition.Write(_newAssemblyPath);
_assembly = Assembly.LoadFile(_newAssemblyPath);
}
[Test]
public void ValidateKnownTypeAttributesAreInjected()
{
var type = _assembly.GetType("AssemblyToProcess.A");
var attributes = type.GetCustomAttributes(typeof(KnownTypeAttribute), false);
Assert.AreEqual(2, attributes.Length);
}
[Test]
public void ValidateKnowsDeriveTypesAttributeRemoved()
{
var type = _assembly.GetType("AssemblyToProcess.A");
var attributes = type.GetCustomAttributes(typeof(KnowsDeriveTypesAttribute), false);
Assert.AreEqual(0, attributes.Length);
}
#if(DEBUG)
[Test]
public void PeVerify()
{
Verifier.Verify(_assemblyPath,_newAssemblyPath);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment