Author: jbevain
Date: 2007-05-11 21:14:41 -0400 (Fri, 11 May 2007)
New Revision: 77274
Added:
trunk/cecil/bat/Mono.Bat/CodeDomScript.cs
Modified:
trunk/cecil/bat/Mono.Bat.csproj
trunk/cecil/bat/Mono.Bat/AbstractAssemblyTestFixture.cs
trunk/cecil/bat/Mono.Bat/ScriptCompiler.cs
Log:
add round trip test possibility
Modified: trunk/cecil/bat/Mono.Bat/AbstractAssemblyTestFixture.cs
===================================================================
--- trunk/cecil/bat/Mono.Bat/AbstractAssemblyTestFixture.cs 2007-05-12
00:18:09 UTC (rev 77273)
+++ trunk/cecil/bat/Mono.Bat/AbstractAssemblyTestFixture.cs 2007-05-12
01:14:41 UTC (rev 77274)
@@ -27,6 +27,7 @@
//
using System;
+using System.Collections.Generic;
using System.IO;
using Boo.Lang.Parser;
using Mono.Cecil;
@@ -37,6 +38,31 @@
[TestFixture]
public abstract class AbstractAssemblyTestFixture {
+ IList<string> _tempFiles = new List<string> ();
+
+ [SetUp]
+ public virtual void SetUp ()
+ {
+ _tempFiles.Clear ();
+ }
+
+ [TearDown]
+ public virtual void TearDown ()
+ {
+ foreach (string file in _tempFiles) {
+ try {
+ File.Delete (file);
+ } catch {}
+ }
+ }
+
+ protected string GetTempFileName ()
+ {
+ string file = Path.GetTempFileName ();
+ _tempFiles.Add (file);
+ return file;
+ }
+
protected void RunReadAssemblyTestCase (string file)
{
string boo = GetBooFile (file);
@@ -45,6 +71,21 @@
RunAndAssertOutput (boo, delegate { ras.Run (); });
}
+ protected void RunCSharpRoundTripTestCase (string file)
+ {
+ string boo = GetBooFile (file);
+ CSharpScript cs = CompileBooFile<CSharpScript> (boo);
+ string asm = GetTempFileName ();
+ cs.Run ();
+ cs.CompileTo (asm);
+
+ AssemblyDefinition assembly =
AssemblyFactory.GetAssembly (asm);
+ string roundtrip = GetTempFileName ();
+ AssemblyFactory.SaveAssembly (assembly, roundtrip);
+
+ RunAndAssertOutput(boo, delegate {
AppDomain.CurrentDomain.ExecuteAssembly (roundtrip); });
+ }
+
string GetBooFile (string file)
{
return GetTestCasePath (file + ".boo");
@@ -53,7 +94,7 @@
protected AssemblyDefinition RunWriteAssemblyTestCase (string
file)
{
string boo = GetBooFile (file);
- string asm = Path.GetTempFileName ();
+ string asm = GetTempFileName ();
WriteAssemblyScript was =
CompileBooFile<WriteAssemblyScript> (boo);
was.DefineAssembly (file);
was.Run ();
@@ -62,9 +103,9 @@
return was.ASM;
}
- static TScript CompileBooFile<TScript> (string file) where
TScript : IScript
+ TScript CompileBooFile<TScript> (string file) where TScript :
IScript
{
- ScriptCompiler<TScript> compiler = new
ScriptCompiler<TScript> (file);
+ ScriptCompiler<TScript> compiler = new
ScriptCompiler<TScript> (file, GetTempFileName ());
return compiler.Compile ();
}
@@ -132,4 +173,4 @@
return Path.Combine (currentPath, "TestCases");
}
}
-}
\ No newline at end of file
+}
Added: trunk/cecil/bat/Mono.Bat/CodeDomScript.cs
===================================================================
--- trunk/cecil/bat/Mono.Bat/CodeDomScript.cs 2007-05-12 00:18:09 UTC (rev
77273)
+++ trunk/cecil/bat/Mono.Bat/CodeDomScript.cs 2007-05-12 01:14:41 UTC (rev
77274)
@@ -0,0 +1,99 @@
+//
+// CodeDomScript.cs
+//
+// Author:
+// Jb Evain ([EMAIL PROTECTED])
+//
+// (C) 2007 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.CodeDom.Compiler;
+using System.Text;
+using NUnit.Framework;
+
+namespace Mono.Bat {
+
+ public abstract class CSharpScript : CodeDomScript {
+
+ public override string Extension {
+ get { return ".cs"; }
+ }
+ }
+
+ public abstract class CodeDomScript : IScript {
+
+ string code;
+
+ public string CODE {
+ get { return code; }
+ set { code = value; }
+ }
+
+ public abstract string Extension { get; }
+
+ public abstract void Run ();
+
+ public void CompileTo (string file)
+ {
+ using (CodeDomProvider provider = GetProvider ()) {
+ CompilerParameters parameters =
GetDefaultParameters ();
+ parameters.IncludeDebugInformation = false;
+ parameters.GenerateExecutable = true;
+ parameters.OutputAssembly = file;
+
+ CompilerResults results =
provider.CompileAssemblyFromSource (parameters, code);
+ AssertCompilerResults (results);
+ }
+ }
+
+ static void AssertCompilerResults (CompilerResults results)
+ {
+ Assert.AreEqual (0, results.Errors.Count,
GetErrorMessage (results));
+ }
+
+ static string GetErrorMessage (CompilerResults results)
+ {
+ if (!results.Errors.HasErrors)
+ return string.Empty;
+
+ StringBuilder sb = new StringBuilder ();
+ foreach (CompilerError error in results.Errors)
+ sb.AppendLine (error.ToString ());
+ return sb.ToString ();
+ }
+
+ CompilerParameters GetDefaultParameters ()
+ {
+ return GetCompilerInfo
().CreateDefaultCompilerParameters ();
+ }
+
+ CodeDomProvider GetProvider ()
+ {
+ return GetCompilerInfo ().CreateProvider ();
+ }
+
+ CompilerInfo GetCompilerInfo ()
+ {
+ return CodeDomProvider.GetCompilerInfo
(CodeDomProvider.GetLanguageFromExtension (Extension));
+ }
+ }
+}
Property changes on: trunk/cecil/bat/Mono.Bat/CodeDomScript.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/cecil/bat/Mono.Bat/ScriptCompiler.cs
===================================================================
--- trunk/cecil/bat/Mono.Bat/ScriptCompiler.cs 2007-05-12 00:18:09 UTC (rev
77273)
+++ trunk/cecil/bat/Mono.Bat/ScriptCompiler.cs 2007-05-12 01:14:41 UTC (rev
77274)
@@ -26,7 +26,6 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-using System.IO;
using Boo.Lang.Compiler;
using Boo.Lang.Compiler.IO;
using Boo.Lang.Compiler.Pipelines;
@@ -37,12 +36,14 @@
public class ScriptCompiler<TScript> where TScript : IScript {
string _file;
+ string _outfile;
BooCompiler _compiler;
InstantiateScript<TScript> _instantiater;
- public ScriptCompiler (string file)
+ public ScriptCompiler (string file, string outfile)
{
_file = file;
+ _outfile = outfile;
SetupCompiler (new CompileToFile ());
}
@@ -63,7 +64,7 @@
_compiler = new BooCompiler ();
_compiler.Parameters.Input.Add (new FileInput (_file));
_compiler.Parameters.OutputType =
CompilerOutputType.Library;
- _compiler.Parameters.OutputAssembly =
Path.GetTempFileName ();
+ _compiler.Parameters.OutputAssembly = _outfile;
_compiler.Parameters.References.Add (typeof
(ScriptCompiler<>).Assembly);
_compiler.Parameters.References.Add (typeof
(Mono.Cecil.AssemblyFactory).Assembly);
_compiler.Parameters.References.Add (typeof
(NUnit.Framework.Assert).Assembly);
Modified: trunk/cecil/bat/Mono.Bat.csproj
===================================================================
--- trunk/cecil/bat/Mono.Bat.csproj 2007-05-12 00:18:09 UTC (rev 77273)
+++ trunk/cecil/bat/Mono.Bat.csproj 2007-05-12 01:14:41 UTC (rev 77274)
@@ -49,6 +49,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Mono.Bat\AbstractAssemblyTestFixture.cs" />
+ <Compile Include="Mono.Bat\CodeDomScript.cs" />
<Compile Include="Mono.Bat\ReadAssemblyScript.cs" />
<Compile Include="Mono.Bat\IScript.cs" />
<Compile Include="Mono.Bat\PrepareScript.cs" />
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches