~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to .pc/port_to_nunit_2.5.patch/src/addins/NUnit/NUnitRunner/NUnitTestRunner.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-07-11 19:09:31 UTC
  • mfrom: (1.3.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20110711190931-1gfoqejryhrliuuq
Tags: 2.5.92+dfsg-2ubuntu1
* Merge from Debian experimental:
  + debian/patches/no_appmenu: Disable AppMenu proxy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ExternalTestRunner.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 
8
//
 
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:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
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.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Reflection;
 
32
using System.IO;
 
33
using System.Collections;
 
34
using System.Collections.Generic;
 
35
using System.Threading;
 
36
 
 
37
using NUnit.Core;
 
38
using NUnit.Framework;
 
39
using NUnit.Core.Filters;
 
40
 
 
41
namespace MonoDevelop.NUnit.External
 
42
{
 
43
        public class NUnitTestRunner: MarshalByRefObject
 
44
        {
 
45
                public void Initialize (string nunitPath, string nunitCorePath)
 
46
                {
 
47
                        // In some cases MS.NET can't properly resolve assemblies even if they
 
48
                        // are already loaded. For example, when deserializing objects from remoting.
 
49
                        AppDomain.CurrentDomain.AssemblyResolve += delegate (object s, ResolveEventArgs args) {
 
50
                                foreach (Assembly am in AppDomain.CurrentDomain.GetAssemblies ()) {
 
51
                                        if (am.GetName ().FullName == args.Name)
 
52
                                                return am;
 
53
                                }
 
54
                                return null;
 
55
                        };
 
56
 
 
57
                        // Force the loading of the NUnit.Framework assembly.
 
58
                        // It's needed since that dll is not located in the test dll directory.
 
59
                        Assembly.LoadFrom (nunitCorePath);
 
60
                        Assembly.LoadFrom (nunitPath);
 
61
 
 
62
                        // Initialize ExtensionHost if not already done
 
63
                        if ( !CoreExtensions.Host.Initialized )
 
64
                                CoreExtensions.Host.InitializeService();
 
65
                }
 
66
                
 
67
                public TestResult Run (EventListener listener, ITestFilter filter, string path, string suiteName, List<string> supportAssemblies)
 
68
                {
 
69
                        InitSupportAssemblies (supportAssemblies);
 
70
                        
 
71
                        if (filter == null)
 
72
                                filter = TestFilter.Empty;
 
73
                        
 
74
                        RemoteTestRunner tr = new RemoteTestRunner ();
 
75
                        TestPackage package = new TestPackage (path);
 
76
                        if (!string.IsNullOrEmpty (suiteName))
 
77
                                package.TestName = suiteName;
 
78
                        tr.Load (package);
 
79
                        return tr.Run (listener, filter);
 
80
                }
 
81
                
 
82
                public NunitTestInfo GetTestInfo (string path, List<string> supportAssemblies)
 
83
                {
 
84
                        InitSupportAssemblies (supportAssemblies);
 
85
                        
 
86
                        TestSuite rootTS = new TestSuiteBuilder ().Build (new TestPackage (path));
 
87
                        return BuildTestInfo (rootTS);
 
88
                }
 
89
                
 
90
                NunitTestInfo BuildTestInfo (Test test)
 
91
                {
 
92
                        NunitTestInfo ti = new NunitTestInfo ();
 
93
                        
 
94
                        // The name of inherited tests include the base class name as prefix.
 
95
                        // That prefix has to be removed
 
96
                        string tname = test.TestName.Name;
 
97
                        int i = tname.LastIndexOf ('.');
 
98
                        if (i != -1)
 
99
                                tname = tname.Substring (i + 1);
 
100
                        
 
101
                        ti.Name = tname;
 
102
 
 
103
                        // Trim short name from end of full name to get the path
 
104
                        string testNameWithDelimiter = "." + tname;
 
105
                        if (test.TestName.FullName.EndsWith (testNameWithDelimiter)) {
 
106
                                int pathLength = test.TestName.FullName.Length - testNameWithDelimiter.Length;
 
107
                                ti.PathName = test.TestName.FullName.Substring(0, pathLength );
 
108
                        }
 
109
                        else
 
110
                                ti.PathName = null;
 
111
                        
 
112
                        if (test.Tests != null && test.Tests.Count > 0) {
 
113
                                ti.Tests = new NunitTestInfo [test.Tests.Count];
 
114
                                for (int n=0; n<test.Tests.Count; n++)
 
115
                                        ti.Tests [n] = BuildTestInfo ((Test)test.Tests [n]);
 
116
                        }
 
117
                        return ti;
 
118
                }
 
119
                
 
120
                void InitSupportAssemblies (List<string> supportAssemblies)
 
121
                {
 
122
                        // Preload support assemblies (they may not be in the test assembly directory nor in the gac)
 
123
                        foreach (string asm in supportAssemblies)
 
124
                                Assembly.LoadFrom (asm);
 
125
                }
 
126
                
 
127
                public override object InitializeLifetimeService ()
 
128
                {
 
129
                        return null;
 
130
                }
 
131
 
 
132
        }
 
133
        
 
134
        [Serializable]
 
135
        public class NunitTestInfo
 
136
        {
 
137
                public string Name;
 
138
                public string PathName;
 
139
                public NunitTestInfo[] Tests;
 
140
        }
 
141
                
 
142
        [Serializable]
 
143
        public class TestNameFilter: ITestFilter
 
144
        {
 
145
                string name;
 
146
                
 
147
                public TestNameFilter (string name)
 
148
                {
 
149
                        this.name = name;
 
150
                }
 
151
                
 
152
                #region ITestFilter implementation 
 
153
                
 
154
                public bool Pass (ITest test)
 
155
                {
 
156
                        if ((test is global::NUnit.Core.TestCase) && test.TestName.FullName == name)
 
157
                                return true;
 
158
                        if (test.Tests != null) {
 
159
                                foreach (ITest ct in test.Tests) {
 
160
                                        if (Pass (ct))
 
161
                                                return true;
 
162
                                }
 
163
                        }
 
164
                        return false;
 
165
                }
 
166
                
 
167
                public bool Match (ITest test)
 
168
                {
 
169
                        return Pass (test);
 
170
                }
 
171
                
 
172
                public bool IsEmpty {
 
173
                        get {
 
174
                                return false;
 
175
                        }
 
176
                }
 
177
                
 
178
                #endregion 
 
179
        }
 
180
}
 
181