~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.Decompiler/Tests/TestRunner.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.CodeDom.Compiler;
 
21
using System.Collections.Generic;
 
22
using System.IO;
 
23
using System.Linq;
 
24
using System.Text;
 
25
using DiffLib;
 
26
using ICSharpCode.Decompiler.Ast;
 
27
using ICSharpCode.Decompiler.Tests.Helpers;
 
28
using Microsoft.CSharp;
 
29
using Mono.Cecil;
 
30
using NUnit.Framework;
 
31
 
 
32
namespace ICSharpCode.Decompiler.Tests
 
33
{
 
34
        [TestFixture]
 
35
        public class TestRunner
 
36
        {
 
37
                [Test]
 
38
                public void Async()
 
39
                {
 
40
                        TestFile(@"..\..\Tests\Async.cs");
 
41
                }
 
42
                
 
43
                [Test, Ignore("disambiguating overloads is not yet implemented")]
 
44
                public void CallOverloadedMethod()
 
45
                {
 
46
                        TestFile(@"..\..\Tests\CallOverloadedMethod.cs");
 
47
                }
 
48
                
 
49
                [Test, Ignore("unncessary primitive casts")]
 
50
                public void CheckedUnchecked()
 
51
                {
 
52
                        TestFile(@"..\..\Tests\CheckedUnchecked.cs");
 
53
                }
 
54
                
 
55
                [Test, Ignore("Missing cast on null")]
 
56
                public void DelegateConstruction()
 
57
                {
 
58
                        TestFile(@"..\..\Tests\DelegateConstruction.cs");
 
59
                }
 
60
                
 
61
                [Test, Ignore("Not yet implemented")]
 
62
                public void ExpressionTrees()
 
63
                {
 
64
                        TestFile(@"..\..\Tests\ExpressionTrees.cs");
 
65
                }
 
66
                
 
67
                [Test]
 
68
                public void ExceptionHandling()
 
69
                {
 
70
                        TestFile(@"..\..\Tests\ExceptionHandling.cs", optimize: false);
 
71
                }
 
72
                
 
73
                [Test]
 
74
                public void Generics()
 
75
                {
 
76
                        TestFile(@"..\..\Tests\Generics.cs");
 
77
                }
 
78
                
 
79
                [Test]
 
80
                public void CustomShortCircuitOperators()
 
81
                {
 
82
                        TestFile(@"..\..\Tests\CustomShortCircuitOperators.cs");
 
83
                }
 
84
                
 
85
                [Test]
 
86
                public void ControlFlowWithDebug()
 
87
                {
 
88
                        TestFile(@"..\..\Tests\ControlFlow.cs", optimize: false, useDebug: true);
 
89
                }
 
90
                
 
91
                [Test]
 
92
                public void IncrementDecrement()
 
93
                {
 
94
                        TestFile(@"..\..\Tests\IncrementDecrement.cs");
 
95
                }
 
96
                
 
97
                [Test]
 
98
                public void InitializerTests()
 
99
                {
 
100
                        TestFile(@"..\..\Tests\InitializerTests.cs");
 
101
                }
 
102
 
 
103
                [Test]
 
104
                public void LiftedOperators()
 
105
                {
 
106
                        TestFile(@"..\..\Tests\LiftedOperators.cs");
 
107
                }
 
108
                
 
109
                [Test]
 
110
                public void Loops()
 
111
                {
 
112
                        TestFile(@"..\..\Tests\Loops.cs");
 
113
                }
 
114
                
 
115
                [Test]
 
116
                public void MultidimensionalArray()
 
117
                {
 
118
                        TestFile(@"..\..\Tests\MultidimensionalArray.cs");
 
119
                }
 
120
                
 
121
                [Test]
 
122
                public void PInvoke()
 
123
                {
 
124
                        TestFile(@"..\..\Tests\PInvoke.cs");
 
125
                }
 
126
                
 
127
                [Test]
 
128
                public void PropertiesAndEvents()
 
129
                {
 
130
                        TestFile(@"..\..\Tests\PropertiesAndEvents.cs");
 
131
                }
 
132
                
 
133
                [Test]
 
134
                public void QueryExpressions()
 
135
                {
 
136
                        TestFile(@"..\..\Tests\QueryExpressions.cs");
 
137
                }
 
138
                
 
139
                [Test, Ignore("switch transform doesn't recreate the exact original switch")]
 
140
                public void Switch()
 
141
                {
 
142
                        TestFile(@"..\..\Tests\Switch.cs");
 
143
                }
 
144
                
 
145
                [Test]
 
146
                public void UndocumentedExpressions()
 
147
                {
 
148
                        TestFile(@"..\..\Tests\UndocumentedExpressions.cs");
 
149
                }
 
150
                
 
151
                [Test, Ignore("has incorrect casts to IntPtr")]
 
152
                public void UnsafeCode()
 
153
                {
 
154
                        TestFile(@"..\..\Tests\UnsafeCode.cs");
 
155
                }
 
156
                
 
157
                [Test]
 
158
                public void ValueTypes()
 
159
                {
 
160
                        TestFile(@"..\..\Tests\ValueTypes.cs");
 
161
                }
 
162
                
 
163
                [Test, Ignore("Redundant yield break; not removed")]
 
164
                public void YieldReturn()
 
165
                {
 
166
                        TestFile(@"..\..\Tests\YieldReturn.cs");
 
167
                }
 
168
                
 
169
                [Test]
 
170
                public void TypeAnalysis()
 
171
                {
 
172
                        TestFile(@"..\..\Tests\TypeAnalysisTests.cs");
 
173
                }
 
174
                
 
175
                static void TestFile(string fileName, bool useDebug = false)
 
176
                {
 
177
                        TestFile(fileName, false, useDebug);
 
178
                        TestFile(fileName, true, useDebug);
 
179
                }
 
180
 
 
181
                static void TestFile(string fileName, bool optimize, bool useDebug = false)
 
182
                {
 
183
                        string code = File.ReadAllText(fileName);
 
184
                        AssemblyDefinition assembly = Compile(code, optimize, useDebug);
 
185
                        AstBuilder decompiler = new AstBuilder(new DecompilerContext(assembly.MainModule));
 
186
                        decompiler.AddAssembly(assembly);
 
187
                        new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
 
188
                        StringWriter output = new StringWriter();
 
189
                        decompiler.GenerateCode(new PlainTextOutput(output));
 
190
                        CodeAssert.AreEqual(code, output.ToString());
 
191
                }
 
192
 
 
193
                static AssemblyDefinition Compile(string code, bool optimize, bool useDebug)
 
194
                {
 
195
                        CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
 
196
                        CompilerParameters options = new CompilerParameters();
 
197
                        options.CompilerOptions = "/unsafe /o" + (optimize ? "+" : "-") + (useDebug ? " /debug": "");
 
198
                        options.ReferencedAssemblies.Add("System.Core.dll");
 
199
                        CompilerResults results = provider.CompileAssemblyFromSource(options, code);
 
200
                        try {
 
201
                                if (results.Errors.Count > 0) {
 
202
                                        StringBuilder b = new StringBuilder("Compiler error:");
 
203
                                        foreach (var error in results.Errors) {
 
204
                                                b.AppendLine(error.ToString());
 
205
                                        }
 
206
                                        throw new Exception(b.ToString());
 
207
                                }
 
208
                                return AssemblyDefinition.ReadAssembly(results.PathToAssembly);
 
209
                        } finally {
 
210
                                File.Delete(results.PathToAssembly);
 
211
                                results.TempFiles.Delete();
 
212
                        }
 
213
                }
 
214
        }
 
215
}