|
From: Steve F. <sm...@us...> - 2005-05-26 20:51:58
|
Update of /cvsroot/nmock/nmock2/src/NMock2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2 Added Files: Mockery.cs .cvsignore Return.cs Stub.cs Expect.cs ISelfDescribing.cs AssemblyInfo.cs Is.cs Matcher.cs IAction.cs NMock2.csproj Has.cs IExpectation.cs Verify.cs Throw.cs Log Message: first upload of nmock2 --- NEW FILE: IExpectation.cs --- using System.IO; using NMock2.Monitoring; namespace NMock2 { public interface IExpectation { bool IsActive { get; } bool HasBeenMet { get; } bool Matches(Invocation invocation); void Perform(Invocation invocation); void DescribeActiveExpectationsTo(TextWriter writer); void DescribeUnmetExpectationsTo(TextWriter writer); } } --- NEW FILE: .cvsignore --- obj NMock2.csproj.user --- NEW FILE: Mockery.cs --- using System; using System.Reflection; using NMock2.Internal; using NMock2.Monitoring; namespace NMock2 { // Name inspired by Ivan Moore. public class Mockery : IDisposable { private readonly MultiInterfaceFactory facadeFactory = new MultiInterfaceFactory("Mocks"); private int depth; private IExpectationOrdering expectations; private IExpectationOrdering topOrdering; public Mockery() { depth = 1; expectations = new UnorderedExpectations(); topOrdering = expectations; } public object NewMock(Type mockedType) { return NewMock(mockedType, DefaultNameFor(mockedType)); } public object NewMock(Type mockedType, string name) { Type facadeType = facadeFactory.GetType(typeof(IMockObject), mockedType); MockObject mockObject = new MockObject(this, mockedType, name); ProxyInvokableAdapter adapter = new ProxyInvokableAdapter(facadeType, new ProxiedObjectIdentity(mockObject, new Invoker(typeof(IMockObject), mockObject, mockObject))); return adapter.GetTransparentProxy(); } public void AddExpectation(IExpectation expectation) { topOrdering.AddExpectation(expectation); } private void Dispatch(Invocation invocation) { if (expectations.Matches(invocation)) { expectations.Perform(invocation); } else { FailUnexpectedInvocation(invocation); } } private bool TypeHasMethodMatching(Type type, Matcher matcher) { foreach (MethodInfo method in type.GetMethods()) { if (matcher.Matches(method)) return true; } return false; } public IDisposable Ordered { get { return Push(new OrderedExpectations(depth)); } } public IDisposable Unordered { get { return Push(new UnorderedExpectations(depth)); } } private Popper Push(IExpectationOrdering newOrdering) { topOrdering.AddExpectation(newOrdering); IExpectationOrdering oldOrdering = topOrdering; topOrdering = newOrdering; depth++; return new Popper(this, oldOrdering); } private void Pop(IExpectationOrdering oldOrdering) { topOrdering = oldOrdering; depth--; } public void VerifyAllExpectationsHaveBeenMet() { if (!expectations.HasBeenMet) { FailUnmetExpectations(); } } private void FailUnmetExpectations() { DescriptionWriter writer = new DescriptionWriter(); writer.WriteLine("not all expected invocations were performed"); expectations.DescribeUnmetExpectationsTo(writer); throw new ExpectationException(writer.ToString()); } private void FailUnexpectedInvocation(Invocation invocation) { DescriptionWriter writer = new DescriptionWriter(); writer.Write("unexpected invocation of "); invocation.DescribeTo(writer); writer.WriteLine(); expectations.DescribeActiveExpectationsTo(writer); throw new ExpectationException(writer.ToString()); } public void Dispose() { VerifyAllExpectationsHaveBeenMet(); } protected virtual string DefaultNameFor(Type type) { string name = type.Name; int firstLower = FirstLowerCaseChar(name); if (firstLower == name.Length) { return name.ToLower(); } else { return name.Substring(firstLower-1,1).ToLower() + name.Substring(firstLower); } } private int FirstLowerCaseChar(string s) { int i = 0; while (i < s.Length && !Char.IsLower(s[i])) i++; return i; } private class MockObject : IInvokable, IMockObject { private readonly Mockery mockery; private readonly Type mockedType; private readonly string name; public MockObject(Mockery mockery, Type mockedType, string name) { this.mockery = mockery; this.mockedType = mockedType; this.name = name; } public override string ToString() { return name; } public void Invoke(Invocation invocation) { mockery.Dispatch(invocation); } public bool HasMethodMatching(Matcher methodMatcher) { return mockery.TypeHasMethodMatching(mockedType, methodMatcher); } public void AddExpectation(IExpectation expectation) { mockery.AddExpectation(expectation); } } private class Popper : IDisposable { private readonly Mockery mockery; private readonly IExpectationOrdering previous; public Popper( Mockery mockery, IExpectationOrdering previous ) { this.previous = previous; this.mockery = mockery; } public void Dispose() { mockery.Pop(previous); } } } } --- NEW FILE: Return.cs --- using System; using NMock2.Actions; namespace NMock2 { public class Return { public static IAction Value(object result) { return new ReturnAction(result); } public static IAction CloneOf(ICloneable prototype) { return new ReturnCloneAction(prototype); } } } --- NEW FILE: Verify.cs --- using NMock2.Internal; namespace NMock2 { public class Verify { public static void That( object actualValue, Matcher matcher, string message, params object[] formatArgs ) { if (!matcher.Matches(actualValue)) { DescriptionWriter writer = new DescriptionWriter(); writer.Write(message, formatArgs); WriteDescriptionOfFailedMatch(writer, actualValue, matcher); throw new ExpectationException(writer.ToString()); } } public static void That( object actualValue, Matcher matcher ) { if (!matcher.Matches(actualValue)) { DescriptionWriter writer = new DescriptionWriter(); WriteDescriptionOfFailedMatch(writer, actualValue, matcher); throw new ExpectationException(writer.ToString()); } } private static void WriteDescriptionOfFailedMatch(DescriptionWriter writer, object actualValue, Matcher matcher) { writer.WriteLine(); writer.Write("Expected: "); matcher.DescribeTo(writer); writer.WriteLine(); writer.Write("Actual: "); writer.Write(actualValue); } } } --- NEW FILE: Has.cs --- using NMock2.Matchers; namespace NMock2 { public class Has { public static Matcher ToString(Matcher stringMatcher) { return new ToStringMatcher(stringMatcher); } public static Matcher Property(string propertyName, Matcher valueMatcher) { return new PropertyMatcher(propertyName, valueMatcher); } } } --- NEW FILE: Stub.cs --- using NMock2.Internal; using NMock2.Syntax; namespace NMock2 { public class Stub { public static IMethodSyntax On(object mock) { ExpectationBuilder builder = new ExpectationBuilder("Stub", Is.Anything, Is.Anything); return builder.On(mock); } } } --- NEW FILE: Throw.cs --- using NMock2.Actions; namespace NMock2 { public class Throw { public static IAction Exception(System.Exception exception) { return new ThrowAction(exception); } } } --- NEW FILE: IAction.cs --- using NMock2.Monitoring; namespace NMock2 { public interface IAction : IInvokable, ISelfDescribing { } } --- NEW FILE: AssemblyInfo.cs --- using System.Reflection; // // 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("SharpMock")] [assembly: AssemblyDescription("Funky Mock Objects for C# and .NET")] [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("")] --- NEW FILE: Expect.cs --- using NMock2.Internal; using NMock2.Syntax; namespace NMock2 { public class Expect { public static IReceiverSyntax Never { get { return new ExpectationBuilder("never", Is.EqualTo(0), Is.EqualTo(0)); } } public static IReceiverSyntax Once { get { return Exactly(1); } } public static IReceiverSyntax AtLeastOnce { get { return AtLeast(1); } } public static IReceiverSyntax Exactly(int count) { return new ExpectationBuilder(Times(count), Is.EqualTo(count), Is.EqualTo(count)); } public static IReceiverSyntax AtLeast(int count) { return new ExpectationBuilder("at least "+Times(count), Is.AtLeast(count), Is.Anything); } public static IReceiverSyntax AtMost(int count) { return new ExpectationBuilder("at most "+Times(count) , Is.Anything, Is.AtMost(count)); } public static IReceiverSyntax Between(int minCount, int maxCount) { return new ExpectationBuilder(minCount+" to "+maxCount+" times", Is.AtLeast(minCount), Is.AtMost(maxCount)); } private static string Times(int n) { return n + ((n == 1) ? " time" : " times"); } } } --- NEW FILE: Matcher.cs --- using System.IO; using NMock2.Internal; using NMock2.Matchers; namespace NMock2 { public abstract class Matcher : ISelfDescribing { public abstract bool Matches(object o); public abstract void DescribeTo(TextWriter writer); public override string ToString() { DescriptionWriter writer = new DescriptionWriter(); DescribeTo(writer); return writer.ToString(); } public static Matcher operator& (Matcher m1, Matcher m2) { return new AndMatcher(m1,m2); } public static Matcher operator| (Matcher m1, Matcher m2) { return new OrMatcher(m1,m2); } public static Matcher operator! (Matcher m) { return new NotMatcher(m); } } } --- NEW FILE: Is.cs --- using System; using NMock2.Matchers; namespace NMock2 { public class Is { public static readonly Matcher Anything = new AlwaysMatcher(true, "anything"); public static readonly Matcher Nothing = new AlwaysMatcher(false, "nothing"); public static readonly Matcher Null = new NullMatcher(); public static readonly Matcher NotNull = new NotMatcher(Null); public static readonly Matcher Out = new ArgumentsMatcher.OutMatcher(); public static Matcher EqualTo(object expected) { return new EqualMatcher(expected); } public static Matcher Same(object expected) { return new SameMatcher(expected); } public static Matcher StringContaining(string substring) { return new StringContainsMatcher(substring); } public static Matcher GreaterThan(IComparable value) { return new ComparisonMatcher(value, 1, 1); } public static Matcher AtLeast(IComparable value) { return new ComparisonMatcher(value, 0, 1); } public static Matcher LessThan(IComparable value) { return new ComparisonMatcher(value, -1, -1); } public static Matcher AtMost(IComparable value) { return new ComparisonMatcher(value, -1, 0); } } } --- NEW FILE: ISelfDescribing.cs --- using System.IO; namespace NMock2 { public interface ISelfDescribing { void DescribeTo(TextWriter writer); } } --- NEW FILE: NMock2.csproj --- <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{CEE959FE-C3AF-4B51-8F1A-CCB32BAA1E98}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "NMock2" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "NMock2" 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 = "..\..\build\NMock2\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 = "..\..\build\NMock2\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Expect.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Has.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IExpectation.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Is.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ISelfDescribing.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Mockery.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Return.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Stub.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Throw.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Verify.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\ResultSynthesizer.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\ReturnAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\ReturnCloneAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\SetIndexedParameterAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\SetNamedParameterAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Actions\ThrowAction.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\BuildableExpectation.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\DescriptionWriter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\ExpectationBuilder.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\ExpectationException.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\IExpectationOrdering.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\IMockObject.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\OrderedExpectations.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Internal\UnorderedExpectations.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\AlwaysMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\AndMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\ArgumentsMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\BinaryOperator.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\ComparisonMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\DescriptionOverride.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\EqualMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\IndexGetterArgumentsMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\IndexSetterArgumentsMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\MethodNameMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\NotMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\NullMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\OrMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\PropertyMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\SameMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\StringContainsMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Matchers\ToStringMatcher.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\IInvokable.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\Invocation.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\Invoker.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\MultiInterfaceFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\ParameterList.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\ProxiedObjectIdentity.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Monitoring\ProxyInvokableAdapter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IActionSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IArgumentSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IGetIndexerSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IMatchSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IMethodSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IReceiverSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\ISetIndexerSyntax.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Syntax\IValueSyntax.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> |