[System] Implement a helper function to create unique temporary directories and use...
[mono-project.git] / mcs / class / System / Test / Microsoft.CSharp / CSharpCodeProviderTest.cs
blobe03f59a5826f429486181c1d21f98c50c6b927f1
1 //
2 // Microsoft.CSharp.CSharpCodeProviderTest.cs
3 //
4 // Author:
5 // Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (C) 2005 Novell
8 //
10 using System;
11 using System.CodeDom;
12 using System.CodeDom.Compiler;
13 using System.Collections.Specialized;
14 using System.Globalization;
15 using System.IO;
16 using System.Reflection;
17 using Microsoft.CSharp;
18 using NUnit.Framework;
19 using System.Text;
20 using System.Linq;
21 using MonoTests.Helpers;
23 namespace MonoTests.Microsoft.CSharp
25 [TestFixture]
26 public class CSharpCodeProviderTest
28 private TempDirectory _tempDirectory;
29 private string _tempDir;
30 private CodeDomProvider _codeProvider;
32 private static readonly string _sourceLibrary1 = "public class Test1 {}";
33 private static readonly string _sourceLibrary2 = "public class Test2 {}";
34 private static readonly string _sourceLibrary3 =
35 @"public class Test3 { public void F() { } }
36 public class Test4 : Test3 { public void F() { } }";
37 private static readonly string _sourceExecutable = "public class Program { static void Main () { } }";
39 [SetUp]
40 public void SetUp ()
42 _codeProvider = new CSharpCodeProvider ();
43 _tempDirectory = new TempDirectory ();
44 _tempDir = _tempDirectory.Path;
47 [TearDown]
48 public void TearDown ()
50 _tempDirectory.Dispose ();
53 [Test]
54 public void FileExtension ()
56 Assert.AreEqual ("cs", _codeProvider.FileExtension);
59 [Test]
60 public void LanguageOptionsTest ()
62 Assert.AreEqual (LanguageOptions.None, _codeProvider.LanguageOptions);
65 [Test]
66 public void GeneratorSupports ()
68 ICodeGenerator codeGenerator = _codeProvider.CreateGenerator ();
69 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#1");
70 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ArraysOfArrays), "#2");
71 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.AssemblyAttributes), "#3");
72 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ChainedConstructorArguments), "#4");
73 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ComplexExpressions), "#5");
74 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareDelegates), "#6");
75 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#7");
76 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEvents), "#8");
77 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareInterfaces), "#9");
78 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareValueTypes), "#10");
79 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.EntryPointMethod), "#11");
80 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GotoStatements), "#12");
81 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultidimensionalArrays), "#13");
82 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultipleInterfaceMembers), "#14");
83 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.NestedTypes), "#15");
84 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ParameterAttributes), "#16");
85 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PublicStaticMembers), "#17");
86 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReferenceParameters), "#18");
87 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReturnTypeAttributes), "#19");
88 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.StaticConstructors), "#20");
89 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.TryCatchStatements), "#21");
90 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Win32Resources), "#22");
91 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareIndexerProperties), "#23");
92 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeDeclaration), "#24");
93 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeReference), "#25");
94 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PartialTypes), "#26");
95 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Resources), "#27");
98 [Test]
99 public void CompileFromFile_InMemory ()
101 // create source file
102 string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
103 using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {
104 using (StreamWriter s = new StreamWriter (f)) {
105 s.Write (_sourceLibrary1);
106 s.Close ();
108 f.Close ();
111 CompilerParameters options = new CompilerParameters ();
112 options.GenerateExecutable = false;
113 options.GenerateInMemory = true;
114 options.TempFiles = new TempFileCollection (_tempDir);
115 options.EmbeddedResources.Add (sourceFile);
117 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
118 CompilerResults results = compiler.CompileAssemblyFromFile (options,
119 sourceFile);
121 // verify compilation was successful
122 AssertCompileResults (results, true);
124 Assembly compiledAssembly = results.CompiledAssembly;
126 Assert.IsNotNull (compiledAssembly, "#1");
127 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#2");
128 Assert.IsNull (results.PathToAssembly, "#3");
129 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#4");
131 // verify we don't cleanup files in temp directory too agressively
132 string[] tempFiles = Directory.GetFiles (_tempDir);
133 Assert.AreEqual (1, tempFiles.Length, "#5");
134 Assert.AreEqual (sourceFile, tempFiles[0], "#6");
136 string[] resources = compiledAssembly.GetManifestResourceNames();
137 Assert.IsNotNull (resources, "#7");
138 Assert.AreEqual (1, resources.Length, "#8");
139 Assert.AreEqual ("file.cs", resources[0], "#9");
140 Assert.IsNull (compiledAssembly.GetFile ("file.cs"), "#10");
141 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file.cs"), "#11");
142 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file.cs");
143 Assert.IsNotNull (info, "#12");
144 Assert.IsNull (info.FileName, "#13");
145 Assert.IsNull (info.ReferencedAssembly, "#14");
146 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#15");
149 [Test]
150 public void CompileFromFileBatch_Executable_InMemory ()
152 // create source file
153 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
154 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
155 using (StreamWriter s = new StreamWriter (f)) {
156 s.Write (_sourceLibrary1);
157 s.Close ();
159 f.Close ();
162 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
163 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
164 using (StreamWriter s = new StreamWriter (f)) {
165 s.Write (_sourceExecutable);
166 s.Close ();
168 f.Close ();
171 CompilerParameters options = new CompilerParameters ();
172 options.GenerateExecutable = true;
173 options.GenerateInMemory = true;
174 options.OutputAssembly = string.Empty;
175 options.TempFiles = new TempFileCollection (_tempDir);
176 options.EmbeddedResources.Add (sourceFile1);
177 options.LinkedResources.Add (sourceFile2);
179 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
180 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
181 new string [] { sourceFile1, sourceFile2 });
183 // verify compilation was successful
184 AssertCompileResults (results, true);
186 Assembly compiledAssembly = results.CompiledAssembly;
188 Assert.IsNotNull (compiledAssembly, "#A1");
189 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
190 Assert.IsNull (results.PathToAssembly, "#A3");
191 Assert.IsNotNull (options.OutputAssembly, "#A4");
192 Assert.AreEqual (".exe", Path.GetExtension (options.OutputAssembly), "#A5");
193 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
194 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
196 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
197 Assert.IsNotNull (compiledAssembly.GetType ("Program"), "#B2");
199 // verify we don't cleanup files in temp directory too agressively
200 string [] tempFiles = Directory.GetFiles (_tempDir);
201 Assert.AreEqual (2, tempFiles.Length, "#C1");
202 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
203 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
205 string[] resources = compiledAssembly.GetManifestResourceNames();
206 Assert.IsNotNull (resources, "#D1");
207 Assert.AreEqual (2, resources.Length, "#D2");
209 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
210 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
211 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
212 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
213 Assert.IsNotNull (info, "#E4");
214 Assert.IsNull (info.FileName, "#E5");
215 Assert.IsNull (info.ReferencedAssembly, "#E6");
216 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
218 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
219 try {
220 compiledAssembly.GetFile ("file2.cs");
221 Assert.Fail ("#F2");
222 } catch (FileNotFoundException) {
224 try {
225 compiledAssembly.GetManifestResourceStream ("file2.cs");
226 Assert.Fail ("#F3");
227 } catch (FileNotFoundException) {
229 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
230 Assert.IsNotNull (info, "#F4");
231 Assert.IsNotNull (info.FileName, "#F5");
232 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
233 Assert.IsNull (info.ReferencedAssembly, "#F7");
234 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
237 [Test]
238 public void CompileFromFileBatch_Library_InMemory ()
240 // create source file
241 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
242 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
243 using (StreamWriter s = new StreamWriter (f)) {
244 s.Write (_sourceLibrary1);
245 s.Close ();
247 f.Close ();
250 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
251 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
252 using (StreamWriter s = new StreamWriter (f)) {
253 s.Write (_sourceLibrary2);
254 s.Close ();
256 f.Close ();
259 CompilerParameters options = new CompilerParameters ();
260 options.GenerateExecutable = false;
261 options.GenerateInMemory = true;
262 options.TempFiles = new TempFileCollection (_tempDir);
263 options.EmbeddedResources.Add (sourceFile1);
264 options.LinkedResources.Add (sourceFile2);
266 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
267 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
268 new string [] { sourceFile1, sourceFile2 });
270 // verify compilation was successful
271 AssertCompileResults (results, true);
273 Assembly compiledAssembly = results.CompiledAssembly;
275 Assert.IsNotNull (compiledAssembly, "#A1");
276 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
277 Assert.IsNull (results.PathToAssembly, "#A3");
278 Assert.IsNotNull (options.OutputAssembly, "#A4");
279 Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
280 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
281 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
283 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
284 Assert.IsNotNull (compiledAssembly.GetType ("Test2"), "#B2");
286 // verify we don't cleanup files in temp directory too agressively
287 string [] tempFiles = Directory.GetFiles (_tempDir);
288 Assert.AreEqual (2, tempFiles.Length, "#C1");
289 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
290 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
292 string[] resources = compiledAssembly.GetManifestResourceNames();
293 Assert.IsNotNull (resources, "#D1");
294 Assert.AreEqual (2, resources.Length, "#D2");
296 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
297 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
298 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
299 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
300 Assert.IsNotNull (info, "#E4");
301 Assert.IsNull (info.FileName, "#E5");
302 Assert.IsNull (info.ReferencedAssembly, "#E6");
303 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
305 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
306 try {
307 compiledAssembly.GetFile ("file2.cs");
308 Assert.Fail ("#F2");
309 } catch (FileNotFoundException) {
311 try {
312 compiledAssembly.GetManifestResourceStream ("file2.cs");
313 Assert.Fail ("#F3");
314 } catch (FileNotFoundException) {
316 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
317 Assert.IsNotNull (info, "#F4");
318 Assert.IsNotNull (info.FileName, "#F5");
319 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
320 Assert.IsNull (info.ReferencedAssembly, "#F7");
321 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
324 [Test]
325 public void CompileFromSource_InMemory ()
327 // create a file in temp directory to ensure that compiler is not removing
328 // too much (temporary) files
329 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
330 using (FileStream fs = File.Create (tempFile)) {
331 fs.Close ();
334 CompilerParameters options = new CompilerParameters ();
335 options.GenerateExecutable = false;
336 options.GenerateInMemory = true;
337 options.TempFiles = new TempFileCollection (_tempDir);
339 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
340 CompilerResults results = compiler.CompileAssemblyFromSource (options,
341 _sourceLibrary1);
343 // verify compilation was successful
344 AssertCompileResults (results, true);
346 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
347 Assert.IsNull (results.PathToAssembly, "#2");
348 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
350 // verify we don't cleanup files in temp directory too agressively
351 string[] tempFiles = Directory.GetFiles (_tempDir);
352 Assert.AreEqual (1, tempFiles.Length, "#4");
353 Assert.AreEqual (tempFile, tempFiles[0], "#5");
356 [Test]
357 public void CompileFromSource_InMemory_Twice ()
359 CompilerParameters options = new CompilerParameters ();
360 options.GenerateExecutable = false;
361 options.GenerateInMemory = true;
363 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
365 var src_1 = "class X { ";
367 CompilerResults results_1 = compiler.CompileAssemblyFromSource (options, src_1);
368 var output_1 = options.OutputAssembly;
370 var src_2 = "class X { }";
372 CompilerResults results_2 = compiler.CompileAssemblyFromSource (options, src_2);
373 var output_2 = options.OutputAssembly;
375 // verify compilation was successful
376 AssertCompileResults (results_2, true);
378 Assert.AreEqual (output_1, output_2, "#1");
382 [Test]
383 public void CompileFromSource_InMemory_With_Extra_Delete ()
385 CompilerParameters options = new CompilerParameters ();
386 options.GenerateExecutable = false;
387 options.GenerateInMemory = true;
389 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
391 var src_1 = "class X { ";
393 compiler.CompileAssemblyFromSource (options, src_1);
395 options.TempFiles.Delete ();
396 options.TempFiles.Delete ();
399 [Test]
400 public void CompileFromSourceBatch_InMemory ()
402 // create a file in temp directory to ensure that compiler is not removing
403 // too much (temporary) files
404 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
405 using (FileStream fs = File.Create (tempFile)) {
406 fs.Close ();
409 string outputAssembly = Path.Combine (_tempDir, "sourcebatch.dll");
411 CompilerParameters options = new CompilerParameters ();
412 options.GenerateExecutable = false;
413 options.GenerateInMemory = true;
414 options.OutputAssembly = outputAssembly;
415 options.TempFiles = new TempFileCollection (_tempDir);
417 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
418 CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,
419 new string [] { _sourceLibrary1, _sourceLibrary2 });
421 // verify compilation was successful
422 AssertCompileResults (results, true);
424 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#A1");
425 Assert.IsNull (results.PathToAssembly, "#A2");
426 Assert.IsNotNull (options.OutputAssembly, "#A3");
427 Assert.AreEqual (outputAssembly, options.OutputAssembly, "#A4");
428 Assert.IsTrue (File.Exists (outputAssembly), "#A5");
430 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#B1");
431 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#B2");
433 // verify we don't cleanup files in temp directory too agressively
434 string[] tempFiles = Directory.GetFiles (_tempDir);
435 Assert.AreEqual (2, tempFiles.Length, "#C1");
436 Assert.AreEqual (tempFile, tempFiles[0], "#C2");
437 Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
440 [Test]
441 public void CompileFromDom_NotInMemory ()
443 // create a file in temp directory to ensure that compiler is not removing
444 // too much (temporary) files
445 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
446 using (FileStream fs = File.Create (tempFile)) {
447 fs.Close ();
450 // compile and verify result in separate appdomain to avoid file locks
451 AppDomain testDomain = CreateTestDomain ();
452 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
454 string outputAssembly = null;
456 try {
457 outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);
458 } finally {
459 AppDomain.Unload (testDomain);
462 // there should be two files in temp dir: temp file and output assembly
463 string[] tempFiles = Directory.GetFiles (_tempDir);
464 Assert.AreEqual (2, tempFiles.Length, "#1");
465 Assert.IsTrue (File.Exists (outputAssembly), "#2");
466 Assert.IsTrue (File.Exists (tempFile), "#3");
469 [Test]
470 public void CompileFromDomBatch_NotInMemory ()
472 // create a file in temp directory to ensure that compiler is not removing
473 // too much (temporary) files
474 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
475 using (FileStream fs = File.Create (tempFile)) {
476 fs.Close ();
479 // compile and verify result in separate appdomain to avoid file locks
480 AppDomain testDomain = CreateTestDomain ();
481 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
483 string outputAssembly = null;
484 try {
485 outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);
486 } finally {
487 AppDomain.Unload (testDomain);
490 // there should be two files in temp dir: temp file and output assembly
491 string[] tempFiles = Directory.GetFiles (_tempDir);
492 Assert.AreEqual (2, tempFiles.Length, "#1");
493 Assert.IsTrue (File.Exists (outputAssembly), "#2");
494 Assert.IsTrue (File.Exists (tempFile), "#3");
497 [Test]
498 public void CompileFromDom_InMemory ()
500 // create a file in temp directory to ensure that compiler is not removing
501 // too much (temporary) files
502 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
503 using (FileStream fs = File.Create (tempFile)) {
504 fs.Close ();
507 CompilerParameters options = new CompilerParameters ();
508 options.GenerateExecutable = false;
509 options.GenerateInMemory = true;
510 options.TempFiles = new TempFileCollection (_tempDir);
512 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
513 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
515 // verify compilation was successful
516 AssertCompileResults (results, true);
518 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
519 Assert.IsNull (results.PathToAssembly, "#2");
521 // verify we don't cleanup files in temp directory too agressively
522 string[] tempFiles = Directory.GetFiles (_tempDir);
523 Assert.AreEqual (1, tempFiles.Length, "#3");
524 Assert.AreEqual (tempFile, tempFiles[0], "#4");
527 [Test]
528 public void CompileFromDomBatch_InMemory ()
530 // create a file in temp directory to ensure that compiler is not removing
531 // too much (temporary) files
532 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
533 using (FileStream fs = File.Create (tempFile)) {
534 fs.Close ();
537 CompilerParameters options = new CompilerParameters ();
538 options.GenerateExecutable = false;
539 options.GenerateInMemory = true;
540 options.TempFiles = new TempFileCollection (_tempDir);
542 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
543 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,
544 new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
546 // verify compilation was successful
547 AssertCompileResults (results, true);
549 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
550 Assert.IsNull (results.PathToAssembly, "#2");
552 // verify we don't cleanup files in temp directory too agressively
553 string[] tempFiles = Directory.GetFiles (_tempDir);
554 Assert.AreEqual (1, tempFiles.Length, "#3");
555 Assert.AreEqual (tempFile, tempFiles[0], "#4");
558 [Test]
559 public void MultiLineWarningIsReportedAsOneWarning()
561 CompilerParameters options = new CompilerParameters ();
562 options.GenerateExecutable = false;
563 options.GenerateInMemory = true;
564 options.TempFiles = new TempFileCollection (_tempDir);
566 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
567 CompilerResults results = compiler.CompileAssemblyFromSource (options,
568 _sourceLibrary3);
570 // verify compilation was successful
571 AssertCompileResults (results, true);
574 [Test]
575 public void EncodingMismatch ()
577 var source = @"
578 #warning Trigger Some Warning
579 public class MyClass {
580 public static string MyMethod () { return ""data""; }
583 var p = new CompilerParameters () {
584 GenerateInMemory = false,
585 GenerateExecutable = false,
586 IncludeDebugInformation = true,
587 TreatWarningsAsErrors = false,
588 TempFiles = new TempFileCollection (_tempDir, true),
591 var prov = new CSharpCodeProvider ();
592 CompilerResults results;
594 var prev = Console.OutputEncoding;
595 try {
596 Console.OutputEncoding = Encoding.Unicode;
598 results = prov.CompileAssemblyFromSource (p, source);
599 } finally {
600 Console.OutputEncoding = prev;
603 Assert.IsNotNull (results.Errors);
604 Assert.IsTrue (results.Output.Cast<string>().ToArray ()[1].Contains ("Trigger Some Warning"));
607 private static void AssertCompileResults (CompilerResults results, bool allowWarnings)
609 foreach (CompilerError compilerError in results.Errors) {
610 if (allowWarnings && compilerError.IsWarning) {
611 continue;
614 Assert.Fail (compilerError.ToString ());
618 private static AppDomain CreateTestDomain ()
620 return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
621 AppDomain.CurrentDomain.SetupInformation);
624 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
626 Type testerType = typeof (CrossDomainTester);
628 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
629 testerType.Assembly.FullName, testerType.FullName, false,
630 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
631 CultureInfo.InvariantCulture, new object[0], domain.Evidence);
634 // do not use the Assert class as this will introduce failures if the
635 // nunit.framework assembly is not in the GAC
636 private class CrossDomainTester : MarshalByRefObject
638 public string CompileAssemblyFromDom (string tempDir)
640 CompilerParameters options = new CompilerParameters ();
641 options.GenerateExecutable = false;
642 options.GenerateInMemory = false;
643 options.TempFiles = new TempFileCollection (tempDir);
645 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
646 ICodeCompiler compiler = codeProvider.CreateCompiler ();
647 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
649 // verify compilation was successful
650 AssertCompileResults (results, true);
652 if (results.CompiledAssembly.Location.Length == 0)
653 throw new Exception ("Location should not be empty string");
654 if (results.PathToAssembly == null)
655 throw new Exception ("PathToAssembly should not be null");
657 return results.PathToAssembly;
660 public string CompileAssemblyFromDomBatch (string tempDir)
662 CompilerParameters options = new CompilerParameters ();
663 options.GenerateExecutable = false;
664 options.GenerateInMemory = false;
665 options.TempFiles = new TempFileCollection (tempDir);
667 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
668 ICodeCompiler compiler = codeProvider.CreateCompiler ();
669 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
671 // verify compilation was successful
672 AssertCompileResults (results, true);
674 if (results.CompiledAssembly.Location.Length == 0)
675 throw new Exception ("Location should not be empty string");
676 if (results.PathToAssembly == null)
677 throw new Exception ("PathToAssembly should not be null");
679 return results.PathToAssembly;