Permalink
Browse files

Run Time CodeGenerator can now generate classes that contain methods …

…with arguments
  • Loading branch information...
1 parent f102e51 commit ddbeea3dd795571369aca6365b36bff1cadc89f5 @shashankshetty shashankshetty committed Oct 7, 2009
Binary file not shown.
@@ -1,4 +1,3 @@
-using System;
using System.IO;
using NUnit.Framework;
@@ -7,105 +6,120 @@
namespace RunTimeCodeGenerator.Tests.ClassGeneration
{
- public class ClassFileGeneratorTests
- {
- [TestFixture]
- public class When_asked_to_generate_a_class_file_with_only_properties
- {
- private IClassGenerator _classGenerator;
- private ClassAttributes _classAttributes;
-
- [SetUp]
- public void SetUp()
- {
- _classAttributes = new ClassAttributes("TestClass");
- _classAttributes.AddUsingNamespaces("System");
- _classAttributes.Properties.Add(new Property("Double", "TestItem1"));
- _classAttributes.Properties.Add(new Property("string", "TestItem2"));
-
- _classGenerator = new ClassFileGenerator();
- }
-
- [TearDown]
- public void TearDown()
- {
- File.Delete(_classAttributes.FullName);
- }
-
- [Test]
- public void Should_create_a_class_file()
- {
- _classGenerator.Create(_classAttributes);
-
- string fileName = _classAttributes.FullName;
- Assert.IsTrue(File.Exists(fileName));
- Assert.IsTrue(new FileComparer().Compare(fileName, TestData.ClassWithProperties));
- }
- }
-
- [TestFixture]
- public class When_asked_to_generate_a_class_file
- {
- private IClassGenerator _classGenerator;
- private ClassAttributes _classAttributes;
-
- [SetUp]
- public void SetUp()
- {
- _classAttributes = new ClassAttributes("TestClass");
- _classAttributes.AddUsingNamespaces("System");
- _classAttributes.Namespace = "TestNamespace";
- Method method = new Method
- {
- Name = "TestMethod",
- ReturnType = "string",
- AccessLevel = AccessLevel.Public
- };
- method.Body.Add("string variable = \"Test\";");
- method.Body.Add("Console.WriteLine(\"Hello\");");
- method.Body.Add("return variable;");
- _classAttributes.Methods.Add(method);
-
- _classGenerator = new ClassFileGenerator();
- }
-
- [TearDown]
- public void TearDown()
- {
- File.Delete(_classAttributes.FullName);
- }
-
- [Test]
- public void Should_create_a_class_file()
- {
- _classGenerator.Create(_classAttributes);
-
- string fileName = _classAttributes.FullName;
- Assert.IsTrue(File.Exists(fileName));
- Assert.IsTrue(new FileComparer().Compare(fileName, TestData.ClassWithMethods));
- }
- }
-
- internal class FileComparer
- {
- public bool Compare(string file1, string file2)
- {
- using (FileStream fileStream1 = File.OpenRead(file1))
- {
- using (FileStream fileStream2 = File.OpenRead(file2))
- {
- if (fileStream1.Length != fileStream2.Length)
- {
- return false;
- }
- while (fileStream1.ReadByte() != fileStream2.ReadByte())
- {
- return false;
- }
- return true;
- }
- }
- }
- }
- }
+ public class ClassFileGeneratorTests
+ {
+ internal class FileComparer
+ {
+ public bool Compare(string file1, string file2)
+ {
+ using (FileStream fileStream1 = File.OpenRead(file1))
+ {
+ using (FileStream fileStream2 = File.OpenRead(file2))
+ {
+ if (fileStream1.Length != fileStream2.Length)
+ {
+ return false;
+ }
+ while (fileStream1.ReadByte() != fileStream2.ReadByte())
+ {
+ return false;
+ }
+ return true;
+ }
+ }
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_to_generate_a_class_file
+ {
+ private ClassAttributes _classAttributes;
+ private IClassGenerator _classGenerator;
+ private Method _method;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _classAttributes = new ClassAttributes("TestClass");
+ _classAttributes.AddUsingNamespaces("System");
+ _classAttributes.Namespace = "TestNamespace";
+
+ _method = new Method
+ {
+ Name = "TestMethod",
+ ReturnType = "string",
+ AccessLevel = AccessLevel.Public
+ };
+ _method.Body.Add("string variable = \"Test\";");
+ _method.Body.Add("Console.WriteLine(\"Hello\");");
+ _method.Body.Add("return variable;");
+ _classAttributes.Methods.Add(_method);
+
+ _classGenerator = new ClassFileGenerator();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ File.Delete(_classAttributes.FullName);
+ }
+
+ [Test]
+ public void Should_create_a_class_file()
+ {
+ _classGenerator.Create(_classAttributes);
+
+ string fileName = _classAttributes.FullName;
+ Assert.IsTrue(File.Exists(fileName));
+ Assert.IsTrue(new FileComparer().Compare(fileName, TestData.ClassWithMethods));
+ }
+
+ [Test]
+ public void Should_create_a_class_file_with_method_parameters()
+ {
+ _method.Parameters.Add(new Parameter("string", "param1"));
+ _method.Parameters.Add(new Parameter("int", "param2"));
+
+ _classGenerator.Create(_classAttributes);
+
+ string fileName = _classAttributes.FullName;
+ Assert.IsTrue(File.Exists(fileName));
+ Assert.IsTrue(new FileComparer().Compare(fileName, TestData.ClassWithMethodsWithParameters));
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_to_generate_a_class_file_with_only_properties
+ {
+ private ClassAttributes _classAttributes;
+ private IClassGenerator _classGenerator;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _classAttributes = new ClassAttributes("TestClass");
+ _classAttributes.AddUsingNamespaces("System");
+ _classAttributes.Properties.Add(new Property("Double", "TestItem1"));
+ _classAttributes.Properties.Add(new Property("string", "TestItem2"));
+
+ _classGenerator = new ClassFileGenerator();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ File.Delete(_classAttributes.FullName);
+ }
+
+ [Test]
+ public void Should_create_a_class_file()
+ {
+ _classGenerator.Create(_classAttributes);
+
+ string fileName = _classAttributes.FullName;
+ Assert.IsTrue(File.Exists(fileName));
+ Assert.IsTrue(new FileComparer().Compare(fileName, TestData.ClassWithProperties));
+ }
+ }
+ }
}
@@ -0,0 +1,14 @@
+using System;
+
+namespace TestNamespace
+{
+ public class TestClass
+ {
+ public string TestMethod(string param1,int param2)
+ {
+ string variable = "Test";
+ Console.WriteLine("Hello");
+ return variable;
+ }
+ }
+}
@@ -56,6 +56,9 @@
<Compile Include="AssemblyGeneration\AssemblyGeneratorTests.cs" />
<Compile Include="ClassGeneration\ClassFileGeneratorTests.cs" />
<Compile Include="ClassGeneration\ClassFileWriterTests.cs" />
+ <Compile Include="ClassGeneration\ClassWithMethodsWithParameters.cs">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Compile>
<Compile Include="Logging\FileListenerTests.cs" />
<Compile Include="Logging\LoggerTests.cs" />
<Compile Include="Logging\LogTests.cs" />
@@ -5,6 +5,7 @@ public class TestData
public static string Directory = "TestDirectory";
public static string ClassWithProperties = "ClassGeneration\\ClassWithProperties.cs";
public static string ClassWithMethods = "ClassGeneration\\ClassWithMethods.cs";
+ public static string ClassWithMethodsWithParameters = "ClassGeneration\\ClassWithMethodsWithParameters.cs";
public static string LogFile = "RunTimeCodeGeneratorLogs.log";
}
}
Oops, something went wrong.

0 comments on commit ddbeea3

Please sign in to comment.