2 // AbstractLinkingTestFixture.cs
5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using Mono.Linker.Steps;
32 using NUnit.Framework;
34 namespace Mono.Linker.Tests {
36 public abstract class AbstractLinkingTestFixture : AbstractTestFixture {
39 public override void SetUp ()
43 TestCasesRoot = "Linker";
47 public override void TearDown ()
51 string output = GetOutputPath ();
52 if (Directory.Exists (output))
53 Directory.Delete (output, true);
56 protected override Pipeline GetPipeline ()
58 Pipeline p = new Pipeline ();
59 p.AppendStep (new LoadReferencesStep ());
60 p.AppendStep (new BlacklistStep ());
61 p.AppendStep (new TypeMapStep ());
62 p.AppendStep (new MarkStep ());
63 p.AppendStep (new SweepStep ());
64 p.AppendStep (new CleanStep ());
65 p.AppendStep (new OutputStep ());
69 protected override void RunTest (string testCase)
71 base.RunTest (testCase);
78 Context = GetContext ();
79 string output = GetOutputPath ();
80 if (Directory.Exists (output))
81 Directory.Delete (output, true);
82 Directory.CreateDirectory (output);
85 protected override void Run ()
93 foreach (AssemblyDefinition assembly in Context.GetAssemblies ()) {
94 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
97 string fileName = GetAssemblyFileName (assembly);
100 AssemblyFactory.GetAssembly (
101 Path.Combine (GetTestCasePath (), fileName)),
102 AssemblyFactory.GetAssembly (
103 Path.Combine (GetOutputPath (), fileName)));
107 static void CompareAssemblies (AssemblyDefinition original, AssemblyDefinition linked)
109 foreach (TypeDefinition originalType in original.MainModule.Types) {
110 TypeDefinition linkedType = linked.MainModule.Types [originalType.FullName];
111 if (NotLinked (originalType)) {
112 Assert.IsNull (linkedType, string.Format ("Type `{0}' should not have been linked", originalType));
116 Assert.IsNotNull (linkedType, string.Format ("Type `{0}' should have been linked", originalType));
117 CompareTypes (originalType, linkedType);
121 static void CompareTypes (TypeDefinition type, TypeDefinition linkedType)
123 foreach (FieldDefinition originalField in type.Fields) {
124 FieldDefinition linkedField = linkedType.Fields.GetField (originalField.Name);// TODO: also get with the type!
125 if (NotLinked (originalField)) {
126 Assert.IsNull (linkedField, string.Format ("Field `{0}' should not have been linked", originalField));
130 Assert.IsNotNull (linkedField, string.Format ("Field `{0}' should have been linked", originalField));
133 foreach (MethodDefinition originalCtor in type.Constructors) {
134 MethodDefinition linkedCtor = linkedType.Constructors.GetConstructor (originalCtor.IsStatic, originalCtor.Parameters);
135 if (NotLinked (originalCtor)) {
136 Assert.IsNull (linkedCtor, string.Format ("Constructor `{0}' should not have been linked", originalCtor));
140 Assert.IsNotNull (linkedCtor, string.Format ("Constructor `{0}' should have been linked", originalCtor));
143 foreach (MethodDefinition originalMethod in type.Methods) {
144 MethodDefinition linkedMethod = linkedType.Methods.GetMethod (originalMethod.Name, originalMethod.Parameters);
145 if (NotLinked (originalMethod)) {
146 Assert.IsNull (linkedMethod, string.Format ("Method `{0}' should not have been linked", originalMethod));
150 Assert.IsNotNull (linkedMethod, string.Format ("Method `{0}' should have been linked", originalMethod));
154 static bool NotLinked(ICustomAttributeProvider provider)
156 foreach (CustomAttribute ca in provider.CustomAttributes)
157 if (ca.Constructor.DeclaringType.Name == "NotLinkedAttribute")