Update of /cvsroot/nmock/nmock2/src/NMock2/Actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2/Actions Added Files: SetIndexedParameterAction.cs ResultSynthesizer.cs SetNamedParameterAction.cs ReturnAction.cs ReturnCloneAction.cs ThrowAction.cs Log Message: first upload of nmock2 --- NEW FILE: ReturnAction.cs --- using System.IO; using NMock2.Monitoring; namespace NMock2.Actions { public class ReturnAction : IAction { private readonly object result; public ReturnAction(object result) { this.result = result; } public void Invoke(Invocation invocation) { invocation.Result = result; } public void DescribeTo(TextWriter writer) { writer.Write("return "); writer.Write(result); } } } --- NEW FILE: SetNamedParameterAction.cs --- using System; using System.IO; using System.Reflection; using NMock2.Monitoring; namespace NMock2.Actions { public class SetNamedParameterAction : IAction { private readonly string name; private readonly object value; public SetNamedParameterAction(string name, object value) { this.name = name; this.value = value; } public void Invoke(Invocation invocation) { ParameterInfo[] paramsInfo = invocation.Method.GetParameters(); for (int i = 0; i < paramsInfo.Length; i++) { if (paramsInfo[i].Name == name) { invocation.Parameters[i] = value; return; } } throw new ArgumentException("no such parameter", name); } public void DescribeTo(TextWriter writer) { writer.Write("set "); writer.Write(name); writer.Write("="); writer.Write(value); } } } --- NEW FILE: ResultSynthesizer.cs --- using System; using System.Collections; using System.IO; using NMock2.Monitoring; namespace NMock2.Actions { public class ResultSynthesizer : IAction { private static Hashtable DEFAULT_RESULTS = new Hashtable(); static ResultSynthesizer() { DEFAULT_RESULTS[typeof (string)] = new ReturnAction(""); DEFAULT_RESULTS[typeof (ArrayList)] = new ReturnCloneAction(new ArrayList()); DEFAULT_RESULTS[typeof (SortedList)] = new ReturnCloneAction(new SortedList()); DEFAULT_RESULTS[typeof (Hashtable)] = new ReturnCloneAction(new Hashtable()); DEFAULT_RESULTS[typeof (Queue)] = new ReturnCloneAction(new Queue()); DEFAULT_RESULTS[typeof (Stack)] = new ReturnCloneAction(new Stack()); } private Hashtable results = new Hashtable(); public void Invoke(Invocation invocation) { Type returnType = invocation.Method.ReturnType; if (returnType == typeof(void)) return; // sanity check if (results.ContainsKey(returnType)) { IAction action = GetAction(returnType, results); action.Invoke(invocation); } else if (returnType.IsArray) { invocation.Result = NewEmptyArray(returnType); } else if (returnType.IsValueType) { invocation.Result = Activator.CreateInstance(returnType); } else if(DEFAULT_RESULTS.ContainsKey(returnType)) { IAction action = GetAction(returnType, DEFAULT_RESULTS); action.Invoke(invocation); } else { throw new InvalidOperationException("No action registered for return type "+returnType); } } private IAction GetAction(Type returnType, Hashtable results) { return ((IAction)results[returnType]); } public void SetResult(Type type, object result) { SetAction(type, Return.Value(result)); } private object SetAction(Type type, IAction action) { return results[type] = action; } public void DescribeTo(TextWriter writer) { throw new NotImplementedException(); } private static object NewEmptyArray(Type arrayType) { int rank = arrayType.GetArrayRank(); int[] dimensions = new int[rank]; return Array.CreateInstance(arrayType.GetElementType(), dimensions); } } } --- NEW FILE: ThrowAction.cs --- using System; using System.IO; using NMock2.Monitoring; namespace NMock2.Actions { public class ThrowAction : IAction { private readonly Exception exception; public ThrowAction(Exception exception) { this.exception = exception; } public void Invoke(Invocation invocation) { invocation.Exception = exception; } public void DescribeTo(TextWriter writer) { writer.Write("throw "); writer.Write(exception); } } } --- NEW FILE: SetIndexedParameterAction.cs --- using System.IO; using NMock2.Monitoring; namespace NMock2.Actions { public class SetIndexedParameterAction : IAction { private readonly int index; private readonly object value; public SetIndexedParameterAction(int index, object value) { this.index = index; this.value = value; } public void Invoke(Invocation invocation) { invocation.Parameters[index] = value; } public void DescribeTo(TextWriter writer) { writer.Write("set arg "); writer.Write(index); writer.Write("="); writer.Write(value); } } } --- NEW FILE: ReturnCloneAction.cs --- using System; using System.IO; using NMock2.Monitoring; namespace NMock2.Actions { public class ReturnCloneAction : IAction { private readonly ICloneable prototype; public ReturnCloneAction(ICloneable prototype) { this.prototype = prototype; } public void Invoke(Invocation invocation) { invocation.Result = prototype.Clone(); } public void DescribeTo(TextWriter writer) { writer.Write("a clone of "); writer.Write(prototype); } } } |