From: <gc...@us...> - 2002-10-12 03:29:56
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Tests/Dynamic/Generate In directory usw-pr-cvs1:/tmp/cvs-serv12370/DotNetMock.Tests/Dynamic/Generate Added Files: ClassGeneratorTests.cs Log Message: Added the dynamic Mock Object Generation package --- NEW FILE: ClassGeneratorTests.cs --- using System; using NUnit.Framework; using DotNetMock.Dynamic; using DotNetMock.Dynamic.Generate; using System.Collections; using System.Reflection; using System.Reflection.Emit; namespace DotNetMock.tests.Dynamic.Generate { public interface IThingy { void NoArgs(); void Another(); void WithSimpleArg(string s); void WithTwoArgs(string a, string b); void WithThreeArgs(string a, string b, string c); void WithLotsOfArgs(string a, string b, string c, string d, string e, string f); void WithOtherArgs(int x, bool y, object o, IList list); void WithParams(int i, params string[] extra); object simpleReturn(); string stringReturn(); int intReturn(); bool boolReturn(); double doubleReturn(); IThingy AThingy(); string ReadProperty { get; } string WriteProperty { set; } string AProperty { get; set; } } public abstract class SolidThingy { public virtual string VirtualMethod() { return "xx"; } public override string ToString() { return "xx"; } public abstract string AbstractMethod(); // cannot override public static string StaticMethod() { return "xx"; } public string NonVirtualMethod() { return "xx"; } private string privateMethod() { return "xx"; } internal string internalMethod() { return "xx"; } protected virtual string protectedMethod() { return "xx"; } protected internal virtual string protectedInternalMethod() { return "xx"; } string defaultInternalMethod() { return "xx"; } } [TestFixture] public class ClassGeneratorTest { private ClassGenerator cg; private IMock mock; private IThingy thingy; [SetUp] public void SetUp() { cg = new ClassGenerator(); mock = new Mock("Test Mock"); thingy = (IThingy)cg.Generate(typeof(IThingy), mock); } [Test] public void CallMethodIsCalled() { mock.Expect("NoArgs"); thingy.NoArgs(); mock.Verify(); } [Test] public void CallMethodWithReturn() { object x = "sdfs"; mock.ExpectAndReturn("simpleReturn", x); object result = thingy.simpleReturn(); Assertion.AssertEquals(x, result); mock.Verify(); } [Test] public void CallMethodWithReturnAndCast() { string x = "sdfs"; mock.ExpectAndReturn("stringReturn", x); string result = thingy.stringReturn(); Assertion.AssertEquals(x, result); mock.Verify(); } [Test] public void CallMethodWithWeirdObjectReturn() { IThingy t = thingy; mock.ExpectAndReturn("AThingy", t); IThingy result = thingy.AThingy(); Assertion.AssertEquals(thingy, result); mock.Verify(); } [Test] public void CallMethodWithReturnInt() { mock.ExpectAndReturn("intReturn", 7); int result = thingy.intReturn(); Assertion.AssertEquals(7, result); mock.Verify(); } [Test] public void CallMethodWithReturnBoxings() { mock.ExpectAndReturn("boolReturn", true); mock.ExpectAndReturn("doubleReturn", 1234567891234E+10); Assertion.Assert(thingy.boolReturn()); Assertion.AssertEquals(1234567891234E+10, thingy.doubleReturn()); mock.Verify(); } [Test] [ExpectedException(typeof(System.IO.IOException))] public void CallMethodTheThrowsException() { mock.ExpectAndThrow("boolReturn", new System.IO.IOException()); thingy.boolReturn(); } [Test] public void CallMethodWithParamExpectations() { mock.Expect("WithSimpleArg", new IsEqual("hello")); thingy.WithSimpleArg("hello"); mock.Verify(); } [Test] [ExpectedException(typeof(AssertionException))] public void CallMethodWithParamExpectationsThatFails() { mock.Expect("WithSimpleArg", new IsEqual("hello")); thingy.WithSimpleArg("goodbye"); mock.Verify(); } [Test] public void CallMethodWithTwoParamExpectations() { mock.Expect("WithTwoArgs", new IsEqual("hello"), new IsEqual("world")); thingy.WithTwoArgs("hello", "world"); mock.Verify(); } [Test] [ExpectedException(typeof(AssertionException))] public void CallMethodWithTwoParamExpectationsThatFails() { mock.Expect("WithTwoArgs", new IsEqual("hello"), new IsEqual("world")); thingy.WithTwoArgs("hello", "moon"); mock.Verify(); } [Test] public void CallMethodWithThreeParamExpectations() { mock.Expect("WithThreeArgs", new IsEqual("hello"), new IsEqual("the"), new IsEqual("world")); thingy.WithThreeArgs("hello", "the", "world"); mock.Verify(); } [Test] public void CallMethodWithLoadsOfParamExpectations() { mock.Expect("WithLotsOfArgs", new IsEqual("hello"), new IsEqual("world"), new IsEqual("is"), new IsEqual("this"), new IsEqual("the"), new IsEqual("end")); thingy.WithLotsOfArgs("hello", "world", "is", "this", "the", "end"); mock.Verify(); } [Test] public void CallMethodWithOtherArgs() { IList l = new ArrayList(); mock.Expect("WithOtherArgs", new IsEqual(6), new IsEqual(true), new IsNull(), new IsEqual(l)); thingy.WithOtherArgs(6, true, null, l); mock.Verify(); } [Test] public void CallReadOnlyProperty() { mock.ExpectAndReturn("ReadProperty", "hello"); mock.ExpectAndReturn("ReadProperty", "world"); Assertion.AssertEquals("hello", thingy.ReadProperty); Assertion.AssertEquals("world", thingy.ReadProperty); mock.Verify(); } [Test] public void WriteOnlyPropertyExpectations() { mock.Expect("WriteProperty", "hello"); mock.Expect("WriteProperty", "world"); thingy.WriteProperty = "hello"; thingy.WriteProperty = "world"; mock.Verify(); } [Test] public void ReadAndWriteProperty() { mock.Expect("AProperty", "hello"); mock.Expect("AProperty", "world"); mock.ExpectAndReturn("AProperty", "good"); mock.ExpectAndReturn("AProperty", "bye"); thingy.AProperty = "hello"; thingy.AProperty = "world"; Assertion.AssertEquals("good", thingy.AProperty); Assertion.AssertEquals("bye", thingy.AProperty); mock.Verify(); } [Test] public void ExtendClass() { cg = new ClassGenerator(); SolidThingy s = (SolidThingy)cg.Generate(typeof(SolidThingy), mock); mock.ExpectAndReturn("VirtualMethod", "hello"); mock.ExpectAndReturn("ToString", "STRING"); mock.ExpectAndReturn("GetHashCode", 123); mock.ExpectAndReturn("AbstractMethod", "fish"); Assertion.AssertEquals("hello", s.VirtualMethod()); Assertion.AssertEquals("STRING", s.ToString()); Assertion.AssertEquals(123, s.GetHashCode()); Assertion.AssertEquals("fish", s.AbstractMethod()); Assertion.AssertEquals("xx", s.NonVirtualMethod()); mock.Verify(); } [Test] public void BoxingOpCodes() { Assertion.AssertEquals( OpCodes.Ldind_I1, cg.GetBoxingOpCode(typeof(sbyte)) ); Assertion.AssertEquals( OpCodes.Ldind_I2, cg.GetBoxingOpCode(typeof(short)) ); Assertion.AssertEquals( OpCodes.Ldind_I4, cg.GetBoxingOpCode(typeof(int)) ); Assertion.AssertEquals( OpCodes.Ldind_I8, cg.GetBoxingOpCode(typeof(long)) ); Assertion.AssertEquals( OpCodes.Ldind_U1, cg.GetBoxingOpCode(typeof(byte)) ); Assertion.AssertEquals( OpCodes.Ldind_U2, cg.GetBoxingOpCode(typeof(ushort)) ); Assertion.AssertEquals( OpCodes.Ldind_U4, cg.GetBoxingOpCode(typeof(uint)) ); Assertion.AssertEquals( OpCodes.Ldind_I8, cg.GetBoxingOpCode(typeof(ulong)) ); Assertion.AssertEquals( OpCodes.Ldind_R4, cg.GetBoxingOpCode(typeof(float)) ); Assertion.AssertEquals( OpCodes.Ldind_R8, cg.GetBoxingOpCode(typeof(double)) ); Assertion.AssertEquals( OpCodes.Ldind_U2, cg.GetBoxingOpCode(typeof(char)) ); Assertion.AssertEquals( OpCodes.Ldind_I1, cg.GetBoxingOpCode(typeof(bool)) ); } } } |