From: Choy R. <ch...@us...> - 2005-01-25 06:21:03
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.TestFramework.Tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22568/DotNetMock.TestFramework.Tests Added Files: Tag: RFE_1098585 .cvsignore AssemblyInfo.cs DotNetMock.TestFramework.Tests.csproj MbUnitStubMakerTests.cs NUnitStubMakerTests.cs StubClassMakerTests.cs Log Message: Initial implementation of dynamically generating implementations of ITestFramework on startup. So far created impls for NUnit and MbUnit. Needs some cleanup. --- NEW FILE: .cvsignore --- bin obj *.csproj.user --- NEW FILE: NUnitStubMakerTests.cs --- #region License // Copyright (c) 2004 Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Reflection; using System.Reflection.Emit; using NUnit.Framework; using DotNetMock.TestFramework; using DotNetMock.Core; #endregion namespace DotNetMock.TestFramework.Tests { [TestFixture] public class NUnitStubMakerTests { [Test] public void Basic() { Assembly providerAssembly = Assembly.Load("nunit.framework"); NUnitStubMaker stubMaker = new NUnitStubMaker(providerAssembly); StubClassMaker scm = new StubClassMaker(); Type stubClass = scm.MakeStubClass(typeof(ITestFramework), stubMaker); ITestFramework tf = (ITestFramework) Activator.CreateInstance(stubClass); tf.Assert(true); tf.AssertEquals(1, 1); } } } --- NEW FILE: DotNetMock.TestFramework.Tests.csproj --- <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{6C2904D4-ADCC-4980-AED0-64A8C6F3AF69}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "DotNetMock.TestFramework.Tests" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "DotNetMock.TestFramework.Tests" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "bin\Debug\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> <Reference Name = "DotNetMock.Core" Project = "{5666DF57-5109-4C5F-967D-EEDC554B8E55}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "DotNetMock" Project = "{1AD0CD00-16FA-4456-B2ED-A47406957228}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "nunit.framework" AssemblyName = "nunit.framework" HintPath = "..\lib\nunit.framework.dll" Private = "True" /> </References> </Build> <Files> <Include> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "MbUnitStubMakerTests.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NUnitStubMakerTests.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "StubClassMakerTests.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> --- NEW FILE: MbUnitStubMakerTests.cs --- #region License // Copyright (c) 2004 Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Reflection; using System.Reflection.Emit; using NUnit.Framework; using DotNetMock.TestFramework; using DotNetMock.Core; #endregion namespace DotNetMock.TestFramework.Tests { [TestFixture] public class MbUnitStubMakerTests { static readonly Assembly providerAssembly = Assembly.GetExecutingAssembly(); const string EXPECTED_MESSAGE = "MESSAGE"; const string NOT_NULL = "NOT-NULL"; ITestFramework tf; static string MethodName { get { return MbUnit.Core.Framework.Assert.MethodName; } } static object[] Arguments { get { return MbUnit.Core.Framework.Assert.Arguments; } } [Test] public void CheckAssert() { tf.Assert(true); Assert.AreEqual("IsTrue", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.AreEqual(true, Arguments[0]); tf.Assert(false); Assert.AreEqual("IsTrue", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.AreEqual(false, Arguments[0]); } [Test] public void CheckAssertWithMessage() { tf.Assert(EXPECTED_MESSAGE, true); Assert.AreEqual("IsTrue", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.AreEqual(true, Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); tf.Assert(EXPECTED_MESSAGE, false); Assert.AreEqual("IsTrue", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.AreEqual(false, Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); } [Test] public void CheckAssertNotNull() { tf.AssertNotNull(NOT_NULL); Assert.AreEqual("IsNotNull", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.AreSame(NOT_NULL, Arguments[0]); tf.AssertNotNull(null); Assert.AreEqual("IsNotNull", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.IsNull(Arguments[0]); } [Test] public void CheckAssertNotNullWithMessage() { tf.AssertNotNull(EXPECTED_MESSAGE, NOT_NULL); Assert.AreEqual("IsNotNull", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.AreSame(NOT_NULL, Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); tf.AssertNotNull(EXPECTED_MESSAGE, null); Assert.AreEqual("IsNotNull", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.IsNull(Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); } [Test] public void CheckAssertNull() { tf.AssertNull(NOT_NULL); Assert.AreEqual("IsNull", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.AreSame(NOT_NULL, Arguments[0]); tf.AssertNull(null); Assert.AreEqual("IsNull", MethodName); Assert.AreEqual(1, Arguments.Length); Assert.IsNull(Arguments[0]); } [Test] public void CheckAssertNullWithMessage() { tf.AssertNull(EXPECTED_MESSAGE, NOT_NULL); Assert.AreEqual("IsNull", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.AreSame(NOT_NULL, Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); tf.AssertNull(EXPECTED_MESSAGE, null); Assert.AreEqual("IsNull", MethodName); Assert.AreEqual(3, Arguments.Length); Assert.IsNull(Arguments[0]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[1]); } [Test] public void CheckFail() { tf.Fail(); Assert.AreEqual("Fail", MethodName); Assert.AreEqual(0, Arguments.Length); } [Test] public void CheckFailWithMessage() { tf.Fail(EXPECTED_MESSAGE); Assert.AreEqual("Fail", MethodName); Assert.AreEqual(2, Arguments.Length); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[0]); } [Test] public void CheckAssertEquals() { tf.AssertEquals(1, 1); Assert.AreEqual("AreEqual", MethodName); Assert.AreEqual(2, Arguments.Length); Assert.AreEqual(1, Arguments[0]); Assert.AreEqual(1, Arguments[1]); } [Test] public void CheckAssertEqualsWithMessage() { tf.AssertEquals(EXPECTED_MESSAGE, 1, 1); Assert.AreEqual("AreEqual", MethodName); Assert.AreEqual(4, Arguments.Length); Assert.AreEqual(1, Arguments[0]); Assert.AreEqual(1, Arguments[1]); Assert.AreEqual(EXPECTED_MESSAGE, Arguments[2]); } [SetUp] public void BeforeEachTest() { // reset mock MbUnit implementation MbUnit.Core.Framework.Assert.Reset(); // make provider stub instance tf = MakeProviderStubInstance(); } ITestFramework MakeProviderStubInstance() { MbUnitStubMaker stubMaker = new MbUnitStubMaker(providerAssembly); StubClassMaker scm = new StubClassMaker(); Type stubClass = scm.MakeStubClass(typeof(ITestFramework), stubMaker); ITestFramework stub = (ITestFramework) Activator.CreateInstance(stubClass); return stub; } } } namespace MbUnit.Core.Framework { /// <summary> /// Mock implementation of MbUnit.Core.Framework.Assert for testing. /// </summary> public class Assert { static string _methodName; static object[] _args = null; public static string MethodName { get { return _methodName; } } public static object[] Arguments { get { return _args; } } public static void Reset() { _methodName = null; _args = null; } public static void IsTrue(bool assertion) { _methodName = "IsTrue"; _args = new object[] { assertion }; } public static void IsTrue(bool assertion, string format, params object[] args) { _methodName = "IsTrue"; _args = new object[] { assertion, format, args }; } public static void IsNotNull(object nullable) { _methodName = "IsNotNull"; _args = new object[] { nullable }; } public static void IsNotNull(object nullable, string format, params object[] args) { _methodName = "IsNotNull"; _args = new object[] { nullable, format, args }; } public static void AreEqual(object expected, object actual) { _methodName = "AreEqual"; _args = new object[] { expected, actual }; } public static void AreEqual( object expected, object actual, string format, params object[] args ) { _methodName = "AreEqual"; _args = new object[] { expected, actual, format, args }; } public static void Fail() { _methodName = "Fail"; _args = new object[0]; } public static void Fail(string format, params object[] args) { _methodName = "Fail"; _args = new object[] { format, args }; } public static void IsNull(object nullable) { _methodName = "IsNull"; _args = new object[] { nullable }; } public static void IsNull(object nullable, string format, params object[] args) { _methodName = "IsNull"; _args = new object[] { nullable, format, args }; } } } --- NEW FILE: StubClassMakerTests.cs --- #region License // Copyright (c) 2004 Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Reflection; using System.Reflection.Emit; using NUnit.Framework; using DotNetMock.TestFramework; #endregion namespace DotNetMock.TestFramework.Tests { [TestFixture] public class StubClassMakerTests { static bool CalledMethod1; interface ITrivialProvider { void Method1(); } class TrivialStubMaker : AbstractStubMaker { public TrivialStubMaker() : base(typeof(StubClassMakerTests)) { } public override void ImplementStubMethod(ILGenerator ilg, MethodInfo mi) { EmitProviderCall(ilg, "RealMethod1", new Type[0]); } } public static void RealMethod1() { CalledMethod1 = true; } [Test] public void TrivialProviderInterface() { StubClassMaker classBuilder = new StubClassMaker(); TrivialStubMaker tsm = new TrivialStubMaker(); Type stubClass = classBuilder.MakeStubClass( typeof(ITrivialProvider), tsm ); ITrivialProvider provider = (ITrivialProvider) Activator.CreateInstance(stubClass); provider.Method1(); Assert.IsTrue(CalledMethod1); } [SetUp] public void BeforeEachTest() { CalledMethod1 = false; } } } --- NEW FILE: AssemblyInfo.cs --- using System.Reflection; using System.Runtime.CompilerServices; // // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. // [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")] // // In order to sign your assembly you must specify a key to use. Refer to the // Microsoft .NET Framework documentation for more information on assembly signing. // // Use the attributes below to control which key is used for signing. // // Notes: // (*) If no key is specified, the assembly is not signed. // (*) KeyName refers to a key that has been installed in the Crypto Service // Provider (CSP) on your machine. KeyFile refers to a file which contains // a key. // (*) If the KeyFile and the KeyName values are both specified, the // following processing occurs: // (1) If the KeyName can be found in the CSP, that key is used. // (2) If the KeyName does not exist and the KeyFile does exist, the key // in the KeyFile is installed into the CSP and used. // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. // When specifying the KeyFile, the location of the KeyFile should be // relative to the project output directory which is // %Project Directory%\obj\<configuration>. For example, if your KeyFile is // located in the project directory, you would specify the AssemblyKeyFile // attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")] // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework // documentation for more information on this. // [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")] |