From: <gc...@us...> - 2002-10-12 03:29:56
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock/Dynamic In directory usw-pr-cvs1:/tmp/cvs-serv12370/DotNetMock/Dynamic Added Files: DynamicMock.cs IMock.cs IPredicate.cs Mock.cs Predicate.cs Log Message: Added the dynamic Mock Object Generation package --- NEW FILE: DynamicMock.cs --- using System; using DotNetMock.Dynamic.Generate; namespace DotNetMock.Dynamic { public class DynamicMock : Mock { private object obj; private Type type; public DynamicMock(Type type) : this(type, null) { string name = type.Name; if (name.StartsWith("I")) { name = name.Substring(1); } Name = "Mock" + name; } public DynamicMock(Type type, string name) : base(name) { this.type = type; } public override object Object { get { if (obj == null) { generate(); } return obj; } } private void generate() { ClassGenerator cg = new ClassGenerator(); obj = cg.Generate(type, this); } } } --- NEW FILE: IMock.cs --- using System; namespace DotNetMock.Dynamic { /// <summary> /// Interface for setting up and invoking a Mock object. The default implementation of /// this is <c>Mock</c> but users may choose to implement their own with custom needs. /// </summary> /// <see cref="Mock"/> public interface IMock { /// <summary> /// Name of this Mock - used for test failure readability only. /// </summary> string Name { get; } /// <summary> /// Get mocked version of object. /// </summary> object Object { get; } /// <summary> /// Expect a method to be called with the supplied parameters. /// </summary> void Expect(string methodName, params object[] args); /// <summary> /// Expect a method to be called with the supplied parameters and setup a /// value to be returned. /// </summary> void ExpectAndReturn(string methodName, object returnVal, params object[] args); /// <summary> /// Expect a method to be called with the supplied parameters and setup an /// exception to be thrown. /// </summary> void ExpectAndThrow(string methodName, Exception exceptionVal, params object[] args); /// <summary> /// Set a fixed return value for a method/property. This allows the method to be /// called multiple times in no particular sequence and have the same value returned /// each time. Useful for getter style methods. /// </summary> void SetValue(string methodName, object returnVal); /// <summary> /// Make a call to a mocked up method name to check it meets its expectations. /// </summary> object Call(string methodName, params object[] args); /// <summary> /// Verify that all expectations were met. /// </summary> void Verify(); } } --- NEW FILE: IPredicate.cs --- using System; namespace DotNetMock.Dynamic { /// <summary> /// Interface for all Predicates to implement. /// </summary> public interface IPredicate { bool eval(object val); } } --- NEW FILE: Mock.cs --- using System; using System.Collections; using NUnit.Framework; namespace DotNetMock.Dynamic { /// <summary> /// Summary description for Mock. /// </summary> public class Mock : IMock { private int currentExpectation; private string name; private object obj; private IList expectations; private IDictionary values; public Mock(string name) { this.name = name; expectations = new ArrayList(); values = new Hashtable(); currentExpectation = 0; } public virtual string Name { get { return name; } set { name = value; } } public virtual object Object { get { return obj; } set { obj = value; } } public virtual void Verify() { Assertion.AssertEquals(currentExpectation, expectations.Count); } public virtual void Expect(string methodName, params object[] args) { ExpectAndReturn(methodName, null, args); } public virtual void ExpectAndReturn(string methodName, object result, params object[] args) { expectations.Add(new Expectation(methodName, result, null, args)); } public virtual void ExpectAndThrow(string methodName, Exception e, params object[] args) { expectations.Add(new Expectation(methodName, null, e, args)); } public virtual void SetValue(string methodName, object returnVal) { values[methodName] = returnVal; } public virtual object Call(string methodName, params object[] args) { if (values.Contains(methodName)) { return values[methodName]; } if (currentExpectation == expectations.Count) { Assertion.Fail(methodName + "() called too many times"); } Expectation exp = (Expectation)expectations[currentExpectation]; currentExpectation++; return exp.Verify(methodName, args); } private class Expectation { private string methodName; private object[] args; private object returnValue; private Exception e; public Expectation(string methodName, object returnValue, Exception e, object[] args) { this.methodName = methodName; if (args == null) { this.args = new object[0]; } else { this.args = args; } this.returnValue = returnValue; this.e = e; } public object Verify(string methodName, object[] args) { Assertion.AssertEquals(this.methodName, methodName); // assert that each passed in arg is validated by the appropriate predicate. for (int i = 0; i < this.args.Length; i++) { object expectedArg = this.args[i]; object actualArg = args[i]; IPredicate predicate = expectedArg as IPredicate; // if expectedArg is not an IPredicate, use default // behavior of IsEqual or IsAnything if (predicate == null) { if (expectedArg == null) { predicate = new IsAnything(); } else { predicate = new IsEqual(expectedArg); } } Assertion.Assert(predicate.eval(actualArg)); } // if exception setup to be thrown, throw it if (e != null) { throw e; } // otherwise return desired value return returnValue; } } } } --- NEW FILE: Predicate.cs --- using System; using System.Text; using System.Text.RegularExpressions; namespace DotNetMock.Dynamic { /// <summary> /// Summary description for Predicate. /// </summary> public class IsNull : IPredicate { public bool eval(object val) { return val == null; } } public class IsAnything : IPredicate { public bool eval(object val) { return true; } } public class IsIn : IPredicate { private object[] inList; public IsIn(params object[] inList) { if (inList.Length == 1 && inList[0].GetType().IsArray) { Array arr = (Array)inList[0]; this.inList = new object[arr.Length]; arr.CopyTo(this.inList, 0); } else { this.inList = inList; } } public bool eval(object val) { foreach (object o in inList) { if (o.Equals(val)) { return true; } } return false; } } public class IsEqual : IPredicate { private object compare; public IsEqual(object compare) { this.compare = compare; } public bool eval(object val) { return compare.Equals(val); } } public class IsTypeOf : IPredicate { private Type type; public IsTypeOf(Type type) { this.type = type; } public bool eval(object val) { return val == null ? false : type.IsAssignableFrom(val.GetType()); } } public class Not : IPredicate { private IPredicate p; public Not(IPredicate p) { this.p = p; } public bool eval(object val) { return !p.eval(val); } } public class And : IPredicate { private IPredicate p1, p2; public And(IPredicate p1, IPredicate p2) { this.p1 = p1; this.p2 = p2; } public bool eval(object val) { return p1.eval(val) && p2.eval(val); } } public class Or : IPredicate { private IPredicate p1, p2; public Or(IPredicate p1, IPredicate p2) { this.p1 = p1; this.p2 = p2; } public bool eval(object val) { return p1.eval(val) || p2.eval(val); } } public class NotNull : IPredicate { public bool eval(object val) { return val != null; } } public class NotEqual : IPredicate { private IPredicate p; public NotEqual(object compare) { p = new Not(new IsEqual(compare)); } public bool eval(object val) { return p.eval(val); } } public class NotIn : IPredicate { private IPredicate p; public NotIn(params object[] inList) { p = new Not(new IsIn(inList)); } public bool eval(object val) { return p.eval(val); } } public class IsEqualIgnoreCase : IPredicate { private IPredicate p; public IsEqualIgnoreCase(object compare) { p = new IsEqual(compare.ToString().ToLower()); } public bool eval(object val) { return p.eval(val.ToString().ToLower()); } } public class IsEqualIgnoreWhiteSpace : IPredicate { private IPredicate p; public IsEqualIgnoreWhiteSpace(object compare) { p = new IsEqual(StripSpace(compare.ToString())); } public bool eval(object val) { return p.eval(StripSpace(val.ToString())); } public static string StripSpace(string s) { StringBuilder result = new StringBuilder(); bool lastWasSpace = true; foreach(char c in s) { if (Char.IsWhiteSpace(c)) { if (!lastWasSpace) { result.Append(' '); } lastWasSpace = true; } else { result.Append(c); lastWasSpace = false; } } return result.ToString().Trim(); } } public class IsMatch : IPredicate { private Regex regex; public IsMatch(Regex regex) { this.regex = regex; } public IsMatch(String regex) : this(new Regex(regex)) { } public IsMatch(String regex, bool ignoreCase) : this(new Regex(regex, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)) { } public bool eval(object val) { return val == null ? false : regex.IsMatch(val.ToString()); } } public class Predicate : IPredicate { public delegate bool Method(object val); private Method m; public Predicate(Method m) { this.m = m; } public bool eval(object val) { return m(val); } } } |