├── .gitignore ├── Build ├── Build.ps1 ├── Common.ps1 ├── Package.ps1 ├── Smocks.nuspec └── nuget.exe ├── LICENSE.md ├── README.md └── Source ├── Settings.StyleCop ├── Smocks.Dummy ├── Properties │ └── AssemblyInfo.cs ├── Smocks.Dummy.Net40.csproj ├── Smocks.Dummy.Net45.csproj └── StaticClass.cs ├── Smocks.Net40.sln ├── Smocks.Net45.sln ├── Smocks.Tests.NUnit3 ├── Issues │ └── IssueNineteenTests.cs ├── Properties │ └── AssemblyInfo.cs ├── Smocks.Tests.NUnit3.Net40.csproj ├── Smocks.Tests.NUnit3.Net45.csproj └── packages.config ├── Smocks.Tests ├── AppDomains │ ├── AppDomainContextTests.cs │ ├── AssemblyLoaderFactoryTests.cs │ ├── AssemblyLoaderTests.cs │ ├── AssemblyResolverTests.cs │ ├── CecilAssemblyResolverTests.cs │ ├── EnumerableReturnValueTransformerTests.cs │ ├── GenericEnumerableReturnValueTransformerTests.cs │ ├── LoadedAssemblyFinderTests.cs │ └── SerializableLambdaTests.cs ├── ArgumentMatchingTests.cs ├── CallbackExceptionTests.cs ├── CallbackExceptionTests.tt ├── CallbackTests.Generated.cs ├── CallbackTests.Generated.tt ├── CallbackTests.cs ├── EventsTests.cs ├── FuncTests.cs ├── IL │ ├── ArgumentGeneratorTests.cs │ ├── CompiledDynamicMethodTests.cs │ ├── Dependencies │ │ ├── DependencyGraphBuilderTests.cs │ │ ├── DependencyGraphNodeTests.cs │ │ └── DependencyGraphTests.cs │ ├── Filters │ │ ├── AllowAllModuleFilterTests.cs │ │ └── DirectReferencesModuleFilterTests.cs │ ├── ILGeneratorInstructionVisitorTests.cs │ ├── ILGeneratorWrapperTests.cs │ ├── InstructionExtensionsTests.cs │ ├── InstructionHelperTests.cs │ ├── ParameterDeducerTests.cs │ ├── ParameterTests.cs │ ├── Resolvers │ │ ├── AssemblyTypeContainerTests.cs │ │ └── FieldResolverTests.cs │ └── SetupExtractorTests.cs ├── Injection │ ├── ServiceCreatorTests.cs │ ├── ServiceLocatorContainerTests.cs │ └── ServiceLocatorTests.cs ├── Issues │ ├── IssueEightTests.cs │ ├── IssueElevenTests.cs │ ├── IssueTwelveTests.cs │ └── IssueTwentySixTests.cs ├── Logging │ └── LoggerExtensionsTests.cs ├── MethodTests.cs ├── OutParameterTests.cs ├── Properties │ └── AssemblyInfo.cs ├── RefParameterTests.cs ├── ReturnsCallbackTests.cs ├── Serialization │ └── SerializerTests.cs ├── Settings.StyleCop ├── Setups │ ├── ArgumentMatcherTests.cs │ ├── GenericSetupTests.cs │ ├── InvocationTrackerTests.cs │ ├── RewriteTargetMatcherTests.cs │ ├── SetupManagerTests.cs │ ├── SetupMatcherTests.cs │ ├── SetupTargetCollectionTests.cs │ ├── SetupTargetTests.cs │ ├── SetupTests.cs │ └── TargetMatcherTests.cs ├── Smocks.Tests.Net40.csproj ├── Smocks.Tests.Net45.csproj ├── TargetMatchingTests.cs ├── TestUtility │ ├── AppDomainUtility.cs │ ├── AssemblyUtility.cs │ ├── CecilUtility.cs │ ├── ReflectionUtility.cs │ ├── TestDataFactory.cs │ └── TestFunctions.cs ├── ThrowsTests.cs ├── Utility │ ├── ArgumentCheckerTests.cs │ ├── DiscovererTests.cs │ ├── ImmutableListTests.cs │ └── MethodCallInfoTests.cs ├── VerifiableTests.cs └── packages.config ├── Smocks.licenseheader ├── Smocks.snk ├── Smocks ├── .gitignore ├── AppDomains │ ├── AppDomainContext.cs │ ├── AssemblyLoader.cs │ ├── AssemblyLoaderFactory.cs │ ├── AssemblyResolver.cs │ ├── CecilAssemblyResolver.cs │ ├── EnumerableReturnValueTransformer.cs │ ├── GenericEnumerableReturnValueTransformer.cs │ ├── IAppDomainContext.cs │ ├── IAssemblyLoader.cs │ ├── IAssemblyLoaderFactory.cs │ ├── ILoadedAssemblyFinder.cs │ ├── IReturnValueTransformer.cs │ ├── ISerializableLambdaFilter.cs │ ├── InvocationResult.cs │ ├── LoadedAssemblyFinder.cs │ ├── SerializableLambda.cs │ └── SerializableLambdaInvoker.cs ├── Configuration.cs ├── Exceptions │ ├── SetupExtractionException.cs │ └── VerificationException.cs ├── IL │ ├── ArgumentGenerator.cs │ ├── AssemblyRewriter.cs │ ├── CompiledDynamicMethod.cs │ ├── Dependencies │ │ ├── DependencyGraph.cs │ │ ├── DependencyGraphBuilder.cs │ │ ├── DependencyGraphNode.cs │ │ ├── IDependencyGraph.cs │ │ ├── IDependencyGraphBuilder.cs │ │ └── IDependencyNodeContainer.cs │ ├── DisassembleResult.cs │ ├── DynamicMethodCompiler.cs │ ├── EventAccessorExtractor.cs │ ├── EventRewriteTarget.cs │ ├── EventTargetExtractor.cs │ ├── ExpressionDecompiler.cs │ ├── Filters │ │ ├── AllowAllModuleFilter.cs │ │ ├── DirectReferencesModuleFilter.cs │ │ ├── IModuleFilter.cs │ │ ├── IModuleFilterFactory.cs │ │ └── ModuleFilterFactory.cs │ ├── FixedPdbReaderProvider.cs │ ├── IArgumentGenerator.cs │ ├── IAssemblyPostProcessor.cs │ ├── IAssemblyRewriter.cs │ ├── ICompiledMethod.cs │ ├── IEventAccessorExtractor.cs │ ├── IEventTargetExtractor.cs │ ├── IExpressionDecompiler.cs │ ├── IILGenerator.cs │ ├── IInstructionHelper.cs │ ├── IInstructionsCompiler.cs │ ├── ILGeneratorInstructionVisitor.cs │ ├── ILGeneratorWrapper.cs │ ├── IMethodDisassembler.cs │ ├── IMethodImporter.cs │ ├── IMethodRewriter.cs │ ├── IOpCodeMapper.cs │ ├── IParameterDeducer.cs │ ├── ISetupExtractor.cs │ ├── InstructionExtensions.cs │ ├── InstructionHelper.cs │ ├── MethodDisassembler.cs │ ├── MethodRewriter.cs │ ├── ModuleDefinitionMethodImporter.cs │ ├── ModuleReferenceComparer.cs │ ├── OpCodeMapper.cs │ ├── Parameter.cs │ ├── ParameterDeducer.cs │ ├── PostProcessors │ │ ├── AssemblyAttributesFilter.cs │ │ └── ModuleMvidPostProcessor.cs │ ├── Resolvers │ │ ├── AssemblyTypeContainer.cs │ │ ├── FieldResolver.cs │ │ ├── GenericBindingContext.cs │ │ ├── IFieldResolver.cs │ │ ├── IMethodResolver.cs │ │ ├── IModuleResolver.cs │ │ ├── ITypeContainer.cs │ │ ├── ITypeResolver.cs │ │ ├── MethodResolver.cs │ │ ├── ModuleResolver.cs │ │ └── TypeResolver.cs │ ├── RewriteContext.cs │ ├── SetupExtractor.cs │ ├── VariableOperation.cs │ ├── VariableUsage.cs │ └── Visitors │ │ ├── IInstructionVisitor.cs │ │ ├── InstructionVisitorBase.cs │ │ ├── NumberPoppedVisitor.cs │ │ └── NumberPushedVisitor.cs ├── ISmocksContext.cs ├── Injection │ ├── DefaultServiceLocatorSetup.cs │ ├── IServiceCreator.cs │ ├── IServiceLocator.cs │ ├── IServiceLocatorContainer.cs │ ├── IServiceLocatorSetup.cs │ ├── ServiceCreator.cs │ ├── ServiceLocator.cs │ └── ServiceLocatorContainer.cs ├── Logging │ ├── LogLevel.cs │ ├── Logger.cs │ └── LoggerExtensions.cs ├── Matching │ └── It.cs ├── Properties │ └── AssemblyInfo.cs ├── Scope.cs ├── Serialization │ ├── ISerializer.cs │ └── Serializer.cs ├── Setups │ ├── ArgumentMatcher.cs │ ├── EventAccessorPair.cs │ ├── EventInterceptor.cs │ ├── EventSurrogate.cs │ ├── Fluent │ │ ├── ICallback.Generated.cs │ │ ├── ICallback.Generated.tt │ │ ├── ICallback.Generic.Generated.cs │ │ ├── ICallback.Generic.Generated.tt │ │ ├── ICallback.Generic.cs │ │ ├── ICallback.cs │ │ ├── IReturns.Generated.cs │ │ ├── IReturns.Generated.tt │ │ ├── IReturns.cs │ │ ├── IThrows.cs │ │ └── IVerifiable.cs │ ├── IArgumentMatcher.cs │ ├── IInternalSetup.Generic.cs │ ├── IInternalSetup.cs │ ├── IInternalSetupBase.cs │ ├── IInvocationTracker.cs │ ├── IItIsMatcher.cs │ ├── IRewriteTarget.cs │ ├── IRewriteTargetCollection.cs │ ├── IRewriteTargetMatcher.cs │ ├── ISetup.Generic.cs │ ├── ISetup.cs │ ├── ISetupManager.cs │ ├── ISetupMatcher.cs │ ├── ITargetMatcher.cs │ ├── Interceptor.cs │ ├── InterceptorResult.Generic.cs │ ├── InterceptorResult.cs │ ├── InvocationTracker.cs │ ├── ItIsMatcher.cs │ ├── RewriteTargetCollection.cs │ ├── RewriteTargetMatcher.cs │ ├── Setup.Generated.cs │ ├── Setup.Generated.tt │ ├── Setup.Generic.Generated.cs │ ├── Setup.Generic.Generated.tt │ ├── Setup.Generic.cs │ ├── Setup.cs │ ├── SetupBase.cs │ ├── SetupManager.cs │ ├── SetupMatcher.cs │ ├── SetupTarget.cs │ └── TargetMatcher.cs ├── Smock.cs ├── Smock.tt ├── Smock1.cs ├── Smocks.Net40.csproj ├── Smocks.Net45.csproj ├── SmocksContext.cs ├── Utility │ ├── ArgumentChecker.cs │ ├── Discoverer.cs │ ├── ExpressionCompiler.cs │ ├── ExpressionHelper.cs │ ├── IExpressionCompiler.cs │ ├── IExpressionHelper.cs │ ├── ImmutableList.cs │ ├── MethodCallInfo.cs │ └── Unit.cs └── packages.config └── SolutionInfo.cs /Build/Build.ps1: -------------------------------------------------------------------------------- 1 | # Credits to Json.NET for parts of this script. 2 | param( 3 | [string]$configuration="Debug" 4 | ) 5 | 6 | $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition 7 | $baseDir = resolve-path "$scriptPath\.." 8 | $buildDir = "$baseDir\Build" 9 | $sourceDir = "$baseDir\Source" 10 | $targets = "Clean", "Build" 11 | $targetsOption = "/t:" + [string]::Join(",", $targets) 12 | 13 | . $buildDir\Common.ps1 14 | 15 | $msbuild = (Get-Command msbuild.exe -errorAction SilentlyContinue | Select -Property Definition).Definition 16 | 17 | if (-Not $msbuild) { 18 | Write-Host "MSbuild not found!" 19 | return 20 | } 21 | 22 | # Manual cleaning, since the "clean" target does not seem to delete contents of the 'obj' folder generated 23 | # by the solutions for other framework versions. 24 | Write-Host "Cleaning solution..." 25 | Get-ChildItem -include "obj","bin" -recurse $sourceDir | ?{ $_.PSIsContainer } | foreach ($_) {Remove-Item -Recurse -Force -ErrorVariable capturedErrors -ErrorAction SilentlyContinue $_.fullname} 26 | 27 | foreach ($build in Get-Builds) 28 | { 29 | $name = $build.Name 30 | 31 | Write-Host 32 | Write-Host "Restoring" 33 | [Environment]::SetEnvironmentVariable("EnableNuGetPackageRestore", "true", "Process") 34 | & $buildDir\NuGet.exe update -self 35 | & $buildDir\NuGet.exe restore "$sourceDir\$name.sln" | Out-Default 36 | 37 | & $msbuild "$sourceDir\$name.sln" "/property:Configuration=$configuration" $targetsOption 38 | } -------------------------------------------------------------------------------- /Build/Common.ps1: -------------------------------------------------------------------------------- 1 | function Get-Builds 2 | { 3 | $builds = @( 4 | @{Name = "Smocks.Net40"; FinalDir="Net40"; NuGetDir = "net40"; }, 5 | @{Name = "Smocks.Net45"; FinalDir="Net45"; NuGetDir = "net45"; } 6 | ) 7 | 8 | return $builds 9 | } -------------------------------------------------------------------------------- /Build/Smocks.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Smocks 5 | 1.0.0.0 6 | Smocks 7 | Tom van der Kleij 8 | https://github.com/vanderkleij/Smocks/blob/master/LICENSE.md 9 | https://github.com/vanderkleij/Smocks 10 | Copyright 2015 Tom van der Kleij 11 | false 12 | Smocks is a library for mocking the normally unmockable. It can mock static and non-virtual methods and properties, amongst others. 13 | Mock Mocking Static Moq Fakes Moles Unit Testing Smock Smocks 14 | 15 | -------------------------------------------------------------------------------- /Build/nuget.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vanderkleij/Smocks/58ee0f4454c3c421288537f63fa89d24273b01b3/Build/nuget.exe -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Tom van der Kleij 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Source/Smocks.Dummy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System.Reflection; 25 | using System.Runtime.InteropServices; 26 | 27 | // General Information about an assembly is controlled through the following 28 | // set of attributes. Change these attribute values to modify the information 29 | // associated with an assembly. 30 | [assembly: AssemblyTitle("Smocks.Dummy")] 31 | [assembly: AssemblyProduct("Smocks.Dummy")] 32 | 33 | // Setting ComVisible to false makes the types in this assembly not visible 34 | // to COM components. If you need to access a type in this assembly from 35 | // COM, set the ComVisible attribute to true on that type. 36 | [assembly: ComVisible(false)] 37 | 38 | // The following GUID is for the ID of the typelib if this project is exposed to COM 39 | [assembly: Guid("4141096e-9f63-4010-9abd-1cdaf158437a")] -------------------------------------------------------------------------------- /Source/Smocks.Dummy/StaticClass.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System; 25 | 26 | namespace Smocks.Dummy 27 | { 28 | /// 29 | /// A dummy static class used for testing. 30 | /// 31 | public static class StaticClass 32 | { 33 | /// 34 | /// Tests the date time. 35 | /// 36 | /// The current time. 37 | public static DateTime TestDateTime() 38 | { 39 | return DateTime.Now; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Source/Smocks.Net40.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smocks.Net40", "Smocks\Smocks.Net40.csproj", "{B62B3313-CE1E-40BA-8E58-23FC6CE89ACF}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smocks.Tests.Net40", "Smocks.Tests\Smocks.Tests.Net40.csproj", "{7E8E2112-B2F7-49A6-A628-5BECEC0C919F}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smocks.Dummy.Net40", "Smocks.Dummy\Smocks.Dummy.Net40.csproj", "{89B26A82-4B5B-4A16-A8BB-277AD4F0DBF9}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smocks.Tests.NUnit3.Net40", "Smocks.Tests.NUnit3\Smocks.Tests.NUnit3.Net40.csproj", "{E48875E6-6FE9-453C-A6DD-E5DCB64FBBA2}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {B62B3313-CE1E-40BA-8E58-23FC6CE89ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {B62B3313-CE1E-40BA-8E58-23FC6CE89ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {B62B3313-CE1E-40BA-8E58-23FC6CE89ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {B62B3313-CE1E-40BA-8E58-23FC6CE89ACF}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {7E8E2112-B2F7-49A6-A628-5BECEC0C919F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {7E8E2112-B2F7-49A6-A628-5BECEC0C919F}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {7E8E2112-B2F7-49A6-A628-5BECEC0C919F}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {7E8E2112-B2F7-49A6-A628-5BECEC0C919F}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {89B26A82-4B5B-4A16-A8BB-277AD4F0DBF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {89B26A82-4B5B-4A16-A8BB-277AD4F0DBF9}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {89B26A82-4B5B-4A16-A8BB-277AD4F0DBF9}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {89B26A82-4B5B-4A16-A8BB-277AD4F0DBF9}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {E48875E6-6FE9-453C-A6DD-E5DCB64FBBA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {E48875E6-6FE9-453C-A6DD-E5DCB64FBBA2}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {E48875E6-6FE9-453C-A6DD-E5DCB64FBBA2}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {E48875E6-6FE9-453C-A6DD-E5DCB64FBBA2}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /Source/Smocks.Tests.NUnit3/Issues/IssueNineteenTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace Smocks.Tests.NUnit3.Issues 5 | { 6 | [TestFixture] 7 | public class IssueNineteenTests 8 | { 9 | [TestCase(Description = "The issue was that Smocks was not compatible with NUnit3. This test is an NUnit 3 test that does a basic Smocks operation. This used to fail before Smocks supported NUnit3.")] 10 | public void Issue19() 11 | { 12 | Smock.Run(context => 13 | { 14 | context.Setup(() => DateTime.Now).Returns(new DateTime(2000, 1, 1)); 15 | 16 | Assert.AreEqual(2000, DateTime.Now.Year); 17 | }); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests.NUnit3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Smocks.Tests.NUnit3")] 8 | [assembly: AssemblyProduct("Smocks.Tests.NUnit3")] 9 | 10 | // Setting ComVisible to false makes the types in this assembly not visible 11 | // to COM components. If you need to access a type in this assembly from 12 | // COM, set the ComVisible attribute to true on that type. 13 | [assembly: ComVisible(false)] 14 | 15 | // The following GUID is for the ID of the typelib if this project is exposed to COM 16 | [assembly: Guid("e48875e6-6fe9-453c-a6dd-e5dcb64fbba2")] -------------------------------------------------------------------------------- /Source/Smocks.Tests.NUnit3/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Source/Smocks.Tests/AppDomains/CecilAssemblyResolverTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Smocks.AppDomains; 4 | 5 | namespace Smocks.Tests.AppDomains 6 | { 7 | [TestFixture] 8 | public class CecilAssemblyResolverTests 9 | { 10 | [TestCase] 11 | public void Constructor_ConfigurationNull_ThrowsArgumentNullException() 12 | { 13 | var exception = Assert.Throws(() => new CecilAssemblyResolver(default(Configuration))); 14 | Assert.AreEqual("configuration", exception.ParamName); 15 | } 16 | 17 | [TestCase] 18 | public void Constructor_ConfigurationWithSearchDirectories_AddsSearchDirectories() 19 | { 20 | const string searchPath = @"c:\my\search\path"; 21 | 22 | var configuration = new Configuration 23 | { 24 | AssemblySearchDirectories = new[] { searchPath } 25 | }; 26 | 27 | var subject = new CecilAssemblyResolver(configuration); 28 | 29 | Assert.Contains(searchPath, subject.GetSearchDirectories()); 30 | } 31 | 32 | [TestCase] 33 | public void Constructor_ConfigurationWithSearchDirectoriesNull_DoesntThrow() 34 | { 35 | var configuration = new Configuration 36 | { 37 | AssemblySearchDirectories = null 38 | }; 39 | 40 | Assert.DoesNotThrow(() => new CecilAssemblyResolver(configuration)); 41 | } 42 | 43 | [TestCase] 44 | public void Constructor_WithConfiguration_AddsAppDomainBaseDirectoryAsSearchPath() 45 | { 46 | var subject = new CecilAssemblyResolver(new Configuration()); 47 | Assert.Contains(AppDomain.CurrentDomain.BaseDirectory, subject.GetSearchDirectories()); 48 | } 49 | 50 | [TestCase] 51 | public void Constructor_WithOutConfiguration_AddsAppDomainBaseDirectoryAsSearchPath() 52 | { 53 | var subject = new CecilAssemblyResolver(); 54 | Assert.Contains(AppDomain.CurrentDomain.BaseDirectory, subject.GetSearchDirectories()); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/AppDomains/LoadedAssemblyFinderTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using System.Reflection; 27 | using NUnit.Framework; 28 | using Smocks.AppDomains; 29 | 30 | namespace Smocks.Tests.AppDomains 31 | { 32 | [ExcludeFromCodeCoverage] 33 | [TestFixture] 34 | public class LoadedAssemblyFinderTests 35 | { 36 | [TestCase] 37 | public void Find_LoadedAssembly_ReturnsAssembly() 38 | { 39 | var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); 40 | var assembly = loadedAssemblies[0]; 41 | 42 | var subject = new LoadedAssemblyFinder(); 43 | var result = subject.Find(assembly.GetName()); 44 | 45 | Assert.AreEqual(assembly, result); 46 | } 47 | 48 | [TestCase] 49 | public void Find_NonLoadedAssembly_ReturnsNull() 50 | { 51 | var subject = new LoadedAssemblyFinder(); 52 | var result = subject.Find(new AssemblyName(Guid.NewGuid().ToString())); 53 | 54 | Assert.AreEqual(null, result); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/CompiledDynamicMethodTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using System.Reflection.Emit; 27 | using NUnit.Framework; 28 | using Smocks.IL; 29 | 30 | namespace Smocks.Tests.IL 31 | { 32 | [ExcludeFromCodeCoverage] 33 | [TestFixture] 34 | public class CompiledDynamicMethodTests 35 | { 36 | [TestCase] 37 | public void Constructor_MethodNull_ThrowsArgumentNullException() 38 | { 39 | var exception = Assert.Throws( 40 | () => new CompiledDynamicMethod(null)); 41 | 42 | Assert.AreEqual("method", exception.ParamName); 43 | } 44 | 45 | [TestCase("Hello, world!")] 46 | public void Invoke_StaticMethod_PassesArgumentsAndReturnsReturnValue(string message) 47 | { 48 | var method = new DynamicMethod("Test", typeof(string), new[] { typeof(string) }); 49 | var generator = method.GetILGenerator(); 50 | 51 | generator.Emit(OpCodes.Ldarg_0); 52 | generator.Emit(OpCodes.Ret); 53 | 54 | var subject = new CompiledDynamicMethod(method); 55 | var result = subject.Invoke(new object[] { message }); 56 | 57 | Assert.AreEqual(message, result); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/Dependencies/DependencyGraphNodeTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System; 24 | using System.Diagnostics.CodeAnalysis; 25 | using NUnit.Framework; 26 | using Smocks.IL; 27 | using Smocks.IL.Dependencies; 28 | using Smocks.Tests.TestUtility; 29 | 30 | namespace Smocks.Tests.IL.Dependencies 31 | { 32 | [ExcludeFromCodeCoverage] 33 | [TestFixture] 34 | public class DependencyGraphNodeTests 35 | { 36 | [TestCase] 37 | public void Constructor_ModuleComparerNull_ThrowsArgumentNullException() 38 | { 39 | var module = CecilUtility.Import(typeof(object)).Resolve().Module; 40 | var exception = Assert.Throws( 41 | () => new DependencyGraphNode(module, null)); 42 | 43 | Assert.AreEqual("moduleComparer", exception.ParamName); 44 | } 45 | 46 | [TestCase] 47 | public void Constructor_ModuleNull_ThrowsArgumentNullException() 48 | { 49 | var exception = Assert.Throws( 50 | () => new DependencyGraphNode(null, new ModuleReferenceComparer())); 51 | 52 | Assert.AreEqual("module", exception.ParamName); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/Dependencies/DependencyGraphTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System; 24 | using System.Diagnostics.CodeAnalysis; 25 | using NUnit.Framework; 26 | using Smocks.IL; 27 | using Smocks.IL.Dependencies; 28 | using Smocks.Tests.TestUtility; 29 | 30 | namespace Smocks.Tests.IL.Dependencies 31 | { 32 | [ExcludeFromCodeCoverage] 33 | [TestFixture] 34 | public class DependencyGraphTests 35 | { 36 | [TestCase] 37 | public void Constructor_ModuleComparerNull_ThrowsArgumentNullException() 38 | { 39 | var module = CecilUtility.Import(typeof(object)).Resolve().Module; 40 | var exception = Assert.Throws( 41 | () => new DependencyGraph(module, null)); 42 | 43 | Assert.AreEqual("moduleComparer", exception.ParamName); 44 | } 45 | 46 | [TestCase] 47 | public void Constructor_ModuleNull_ThrowsArgumentNullException() 48 | { 49 | var exception = Assert.Throws( 50 | () => new DependencyGraph(null, new ModuleReferenceComparer())); 51 | 52 | Assert.AreEqual("module", exception.ParamName); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/Filters/AllowAllModuleFilterTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Diagnostics.CodeAnalysis; 24 | using NUnit.Framework; 25 | using Smocks.IL.Filters; 26 | using Smocks.Tests.TestUtility; 27 | 28 | namespace Smocks.Tests.IL.Filters 29 | { 30 | [ExcludeFromCodeCoverage] 31 | [TestFixture] 32 | public class AllowAllModuleFilterTests 33 | { 34 | [TestCase] 35 | public void Accepts_ReturnsTrue() 36 | { 37 | var subject = new AllowAllModuleFilter(); 38 | 39 | Assert.IsTrue(subject.Accepts(null)); 40 | Assert.IsTrue(subject.Accepts(CecilUtility.Import(typeof(object)).Resolve().Module)); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/ParameterTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using NUnit.Framework; 27 | using Smocks.IL; 28 | using Smocks.Tests.TestUtility; 29 | 30 | namespace Smocks.Tests.IL 31 | { 32 | [ExcludeFromCodeCoverage] 33 | [TestFixture] 34 | public class ParameterTests 35 | { 36 | [TestCase] 37 | public void Constructor_ParameterTypeNull_ThrowsArgumentNullException() 38 | { 39 | var exception = Assert.Throws( 40 | () => new Parameter(null, 1)); 41 | 42 | Assert.AreEqual("parameterType", exception.ParamName); 43 | } 44 | 45 | [TestCase] 46 | public void Constructor_SetsProperties() 47 | { 48 | const int index = 42; 49 | var type = CecilUtility.Import(typeof(string)); 50 | 51 | var subject = new Parameter(type, index); 52 | 53 | Assert.AreSame(type, subject.ParameterType); 54 | Assert.AreEqual(index, subject.Index); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/IL/Resolvers/FieldResolverTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using NUnit.Framework; 27 | using Smocks.IL.Resolvers; 28 | 29 | namespace Smocks.Tests.IL.Resolvers 30 | { 31 | [ExcludeFromCodeCoverage] 32 | [TestFixture] 33 | public class FieldResolverTests 34 | { 35 | [TestCase] 36 | public void Constructor_TypeResolverNull_ThrowsArgumentNullException() 37 | { 38 | var exception = Assert.Throws(() => 39 | new FieldResolver(default(ITypeResolver))); 40 | 41 | Assert.AreEqual("typeResolver", exception.ParamName); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/Issues/IssueEightTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using Moq; 25 | using NUnit.Framework; 26 | 27 | namespace Smocks.Tests.Issues 28 | { 29 | [TestFixture] 30 | public class IssueEightTests 31 | { 32 | [TestCase] 33 | public void Issue8() 34 | { 35 | Smock.Run(context => 36 | { 37 | context.Setup(() => It.IsAny().Fetch(It.IsAny())).Returns((object)null); 38 | 39 | new EmployeeProcess().Load(new object()); 40 | }); 41 | } 42 | 43 | internal class ClassDatabase 44 | { 45 | public object Fetch(bool val2) 46 | { 47 | return null; 48 | } 49 | } 50 | 51 | internal sealed class EmployeeProcess 52 | { 53 | public void Load(object ei) 54 | { 55 | if (ei != null) 56 | { 57 | new ClassDatabase().Fetch(false); 58 | } 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Source/Smocks.Tests/Issues/IssueElevenTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using Moq; 25 | using NUnit.Framework; 26 | 27 | namespace Smocks.Tests.Issues 28 | { 29 | [TestFixture] 30 | public class IssueElevenTests 31 | { 32 | [TestCase] 33 | public void Issue11() 34 | { 35 | Smock.Run(context => 36 | { 37 | bool called = false; 38 | Message message = new Message(); 39 | 40 | context.Setup(() => It.IsAny().Send(message)).Callback((Message m) => 41 | { 42 | called = true; 43 | Assert.AreEqual(m, message); 44 | }); 45 | 46 | new MessageQueue().Send(message); 47 | 48 | Assert.IsTrue(called); 49 | }); 50 | } 51 | 52 | public class Message 53 | { 54 | } 55 | 56 | public class MessageQueue 57 | { 58 | public void Send(object message) 59 | { 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/Issues/IssueTwelveTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using NUnit.Framework; 26 | 27 | namespace Smocks.Tests.Issues 28 | { 29 | [TestFixture] 30 | public class IssueTwelveTests 31 | { 32 | [TestCase] 33 | public void Issue12() 34 | { 35 | var now = DateTime.Now.AddMonths(18); 36 | 37 | Smock.Run(ctx => 38 | { 39 | var person = new Person { FirstName = "John", LastName = "Doe", Age = 24 }; 40 | ctx.Setup(() => DateTime.Now).Returns(now); 41 | 42 | var name = person.FirstName; 43 | 44 | Assert.AreNotEqual(now, person.AccessedOn); 45 | }); 46 | } 47 | 48 | public class Person 49 | { 50 | public DateTime AccessedOn { get; set; } 51 | 52 | public string FirstName { get; set; } 53 | 54 | public string LastName { get; set; } 55 | 56 | public int Age { get; set; } 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/Issues/IssueTwentySixTests.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Aspose.Cells; 3 | using Moq; 4 | using NUnit.Framework; 5 | 6 | namespace Smocks.Tests.Issues 7 | { 8 | [TestFixture] 9 | public class IssueTwentySixTests 10 | { 11 | public interface IEntityWorksheetReaderFactory 12 | { 13 | void EntityWorksheetReader(Worksheet worksheet, CellArea cellArea); 14 | } 15 | 16 | [Test] 17 | public void Issue26() 18 | { 19 | // The issue was that this code was throwing a MethodAccessException. By resolving this issue, 20 | // it now throws a MockException. 21 | Assert.Throws(() => 22 | { 23 | Smock.Run(context => 24 | { 25 | // The Aspose.Cells assembly has the assembly attribute AllowPartiallyTrustedCallersAttribute set. 26 | // This causes issues when we rewrite the methods in the assembly to call into Smocks to handle 27 | // interception. We resolve this by simply removing said attribute, so that the calls into 28 | // Smocks are then allowed. 29 | var workbook = new Workbook(new MemoryStream()); 30 | var worksheet = workbook.Worksheets.Add("Wal-Mart Stores"); 31 | context.Setup(() => workbook.Worksheets[It.IsAny()]).Returns(worksheet); 32 | 33 | var worksheetReaderFactory = Mock.Of(); 34 | 35 | Mock.Get(worksheetReaderFactory).Verify(m => m.EntityWorksheetReader( 36 | worksheet, 37 | It.Is( 38 | ca => ca.StartRow == 1 && ca.EndRow == 5 && ca.StartColumn == 0 && ca.EndColumn == 5)), 39 | Times.Once); 40 | }); 41 | }); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | using System.Runtime.InteropServices; 26 | 27 | // General Information about an assembly is controlled through the following 28 | // set of attributes. Change these attribute values to modify the information 29 | // associated with an assembly. 30 | [assembly: AssemblyTitle("Smocks.Tests")] 31 | [assembly: AssemblyProduct("Smocks.Tests")] 32 | 33 | // Setting ComVisible to false makes the types in this assembly not visible 34 | // to COM components. If you need to access a type in this assembly from 35 | // COM, set the ComVisible attribute to true on that type. 36 | [assembly: ComVisible(false)] 37 | 38 | // The following GUID is for the ID of the typelib if this project is exposed to COM 39 | [assembly: Guid("55aff6a9-cd87-4dee-8af7-34d7e62528aa")] -------------------------------------------------------------------------------- /Source/Smocks.Tests/Settings.StyleCop: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | False 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Source/Smocks.Tests/TestUtility/AssemblyUtility.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using System.IO; 27 | using System.Reflection; 28 | using System.Reflection.Emit; 29 | 30 | namespace Smocks.Tests.TestUtility 31 | { 32 | [ExcludeFromCodeCoverage] 33 | internal static class AssemblyUtility 34 | { 35 | internal static string CreateDummyAssembly(AssemblyName assemblyName) 36 | { 37 | string directory = Path.GetTempPath(); 38 | string filename = Guid.NewGuid() + ".dll"; 39 | 40 | var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, 41 | AssemblyBuilderAccess.Save, directory); 42 | assemblyBuilder.Save(filename); 43 | 44 | return Path.Combine(directory, filename); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/TestUtility/CecilUtility.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using System.Linq.Expressions; 27 | using System.Reflection; 28 | using Mono.Cecil; 29 | 30 | namespace Smocks.Tests.TestUtility 31 | { 32 | [ExcludeFromCodeCoverage] 33 | internal static class CecilUtility 34 | { 35 | public static MethodReference Import(MethodBase method) 36 | { 37 | ModuleDefinition module = ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll); 38 | return module.ImportReference(method); 39 | } 40 | 41 | public static TypeReference Import(Type type) 42 | { 43 | ModuleDefinition module = ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll); 44 | return module.ImportReference(type); 45 | } 46 | 47 | public static FieldReference Import(FieldInfo fieldInfo) 48 | { 49 | ModuleDefinition module = ModuleDefinition.CreateModule("Dummy", ModuleKind.Dll); 50 | return module.ImportReference(fieldInfo); 51 | } 52 | 53 | public static MethodReference Import(Expression expression) 54 | { 55 | return Import(ReflectionUtility.GetMethod(expression)); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/Utility/ArgumentCheckerTests.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using NUnit.Framework; 27 | using Smocks.Utility; 28 | 29 | namespace Smocks.Tests.Utility 30 | { 31 | [ExcludeFromCodeCoverage] 32 | [TestFixture] 33 | public class ArgumentCheckerTests 34 | { 35 | [TestCase(default(string))] 36 | public void NotNull_ArgumentNull_ThrowsArgumentNullException(string testArgument) 37 | { 38 | var exception = Assert.Throws(() => 39 | ArgumentChecker.NotNull(testArgument, () => testArgument)); 40 | 41 | Assert.AreEqual("testArgument", exception.ParamName); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Source/Smocks.Tests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Source/Smocks.licenseheader: -------------------------------------------------------------------------------- 1 | extensions: designer.cs generated.cs 2 | extensions: .cs .tt 3 | #region License 4 | //// The MIT License (MIT) 5 | //// 6 | //// Copyright (c) 2015 Tom van der Kleij 7 | //// 8 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 9 | //// this software and associated documentation files (the "Software"), to deal in 10 | //// the Software without restriction, including without limitation the rights to 11 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 12 | //// the Software, and to permit persons to whom the Software is furnished to do so, 13 | //// subject to the following conditions: 14 | //// 15 | //// The above copyright notice and this permission notice shall be included in all 16 | //// copies or substantial portions of the Software. 17 | //// 18 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 20 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 21 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 22 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | #endregion 25 | 26 | -------------------------------------------------------------------------------- /Source/Smocks.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vanderkleij/Smocks/58ee0f4454c3c421288537f63fa89d24273b01b3/Source/Smocks.snk -------------------------------------------------------------------------------- /Source/Smocks/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vanderkleij/Smocks/58ee0f4454c3c421288537f63fa89d24273b01b3/Source/Smocks/.gitignore -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/CecilAssemblyResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Mono.Cecil; 3 | using Smocks.Utility; 4 | 5 | namespace Smocks.AppDomains 6 | { 7 | public class CecilAssemblyResolver : DefaultAssemblyResolver 8 | { 9 | public CecilAssemblyResolver(Configuration configuration) 10 | { 11 | ArgumentChecker.NotNull(configuration, nameof(configuration)); 12 | 13 | AddSearchDirectory(AppDomain.CurrentDomain.BaseDirectory); 14 | 15 | if (configuration.AssemblySearchDirectories != null) 16 | { 17 | foreach (string searchDirectory in configuration.AssemblySearchDirectories) 18 | { 19 | AddSearchDirectory(searchDirectory); 20 | } 21 | } 22 | } 23 | 24 | public CecilAssemblyResolver() 25 | : this(new Configuration()) 26 | { 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/EnumerableReturnValueTransformer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections; 26 | using System.Linq; 27 | 28 | namespace Smocks.AppDomains 29 | { 30 | internal class EnumerableReturnValueTransformer : IReturnValueTransformer 31 | { 32 | public bool CanTransform(Type type, object returnValue) 33 | { 34 | return type == typeof(IEnumerable); 35 | } 36 | 37 | public TReturnValue Transform(TReturnValue returnValue) 38 | { 39 | if (returnValue == null) 40 | { 41 | return default(TReturnValue); 42 | } 43 | 44 | var list = ((IEnumerable)returnValue).Cast().ToList(); 45 | return (TReturnValue)(object)list; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/GenericEnumerableReturnValueTransformer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections; 26 | using System.Collections.Generic; 27 | 28 | namespace Smocks.AppDomains 29 | { 30 | internal class GenericEnumerableReturnValueTransformer : IReturnValueTransformer 31 | { 32 | public bool CanTransform(Type type, object returnValue) 33 | { 34 | return type.IsGenericType && 35 | type.GetGenericTypeDefinition() == typeof(IEnumerable<>); 36 | } 37 | 38 | public TReturnValue Transform(TReturnValue returnValue) 39 | { 40 | if (returnValue == null) 41 | { 42 | return default(TReturnValue); 43 | } 44 | 45 | Type elementType = typeof(TReturnValue).GetGenericArguments()[0]; 46 | IList list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)); 47 | 48 | foreach (object element in (IEnumerable)returnValue) 49 | { 50 | list.Add(element); 51 | } 52 | 53 | return (TReturnValue)list; 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/IAppDomainContext.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Reflection; 27 | 28 | namespace Smocks.AppDomains 29 | { 30 | /// 31 | /// A wrapper around an that makes some common tasks 32 | /// more convenient. 33 | /// 34 | internal interface IAppDomainContext : IDisposable 35 | { 36 | /// 37 | /// Gets the wrapped . 38 | /// 39 | AppDomain AppDomain { get; } 40 | 41 | /// 42 | /// Gets the assemblies that have been loaded into the execution context of this application domain. 43 | /// 44 | /// A collection of assemblies in this application domain. 45 | IEnumerable GetAssemblies(); 46 | 47 | /// 48 | /// Registers a file for deletion 49 | /// 50 | /// The location. 51 | void RegisterForDeletion(string location); 52 | } 53 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/IAssemblyLoader.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | 27 | namespace Smocks.AppDomains 28 | { 29 | /// 30 | /// Responsible for loading assemblies. 31 | /// 32 | public interface IAssemblyLoader 33 | { 34 | /// 35 | /// Loads the assembly associated with this loader into the current . 36 | /// 37 | /// The that was loaded. 38 | Assembly Load(); 39 | } 40 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/IAssemblyLoaderFactory.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | 27 | namespace Smocks.AppDomains 28 | { 29 | /// 30 | /// Provides a mechanism for loading an assembly, given its . 31 | /// 32 | public interface IAssemblyLoaderFactory : IDisposable 33 | { 34 | /// 35 | /// Gets a loader for an assembly. 36 | /// 37 | /// The name of the assembly. 38 | /// A loader for the specified assembly, or null if none could be created. 39 | IAssemblyLoader GetLoaderForAssembly(AssemblyName assemblyName); 40 | 41 | /// 42 | /// Registers an assembly by its full name and location. If you then get 43 | /// a loader for the specified assembly name, a loader based on the provided 44 | /// location will be returned. 45 | /// 46 | /// The full name of the assembly. 47 | /// The assembly location. 48 | void RegisterAssembly(string fullName, string assemblyLocation); 49 | } 50 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/ILoadedAssemblyFinder.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | 26 | namespace Smocks.AppDomains 27 | { 28 | internal interface ILoadedAssemblyFinder 29 | { 30 | Assembly Find(AssemblyName assemblyName); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/IReturnValueTransformer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | 26 | namespace Smocks.AppDomains 27 | { 28 | internal interface IReturnValueTransformer 29 | { 30 | bool CanTransform(Type type, object returnValue); 31 | 32 | TReturnValue Transform(TReturnValue returnValue); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/ISerializableLambdaFilter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.AppDomains 25 | { 26 | internal interface ISerializableLambdaFilter 27 | { 28 | SerializableLambda Filter(IAppDomainContext context, SerializableLambda lambda); 29 | } 30 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/InvocationResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | namespace Smocks.AppDomains 28 | { 29 | [Serializable] 30 | internal class InvocationResult 31 | { 32 | public InvocationResult(Dictionary serializedTarget) 33 | { 34 | SerializedTarget = serializedTarget; 35 | } 36 | 37 | public Dictionary SerializedTarget { get; set; } 38 | } 39 | 40 | [Serializable] 41 | internal class InvocationResult : InvocationResult 42 | { 43 | public InvocationResult(Dictionary serializedTarget, T returnValue) 44 | : base(serializedTarget) 45 | { 46 | ReturnValue = returnValue; 47 | } 48 | 49 | public T ReturnValue { get; set; } 50 | } 51 | } -------------------------------------------------------------------------------- /Source/Smocks/AppDomains/LoadedAssemblyFinder.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | 27 | namespace Smocks.AppDomains 28 | { 29 | internal class LoadedAssemblyFinder : ILoadedAssemblyFinder 30 | { 31 | public Assembly Find(AssemblyName assemblyName) 32 | { 33 | var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); 34 | 35 | foreach (Assembly assembly in loadedAssemblies) 36 | { 37 | if (assembly.GetName().Name == assemblyName.Name) 38 | { 39 | return assembly; 40 | } 41 | } 42 | 43 | return null; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/CompiledDynamicMethod.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection.Emit; 25 | using Smocks.Utility; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal class CompiledDynamicMethod : ICompiledMethod 30 | { 31 | private readonly DynamicMethod _method; 32 | 33 | public CompiledDynamicMethod(DynamicMethod method) 34 | { 35 | ArgumentChecker.NotNull(method, () => method); 36 | 37 | _method = method; 38 | } 39 | 40 | public TReturnValue Invoke(object[] arguments) 41 | { 42 | return (TReturnValue)_method.Invoke(null, arguments); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Dependencies/DependencyGraph.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Collections.Generic; 24 | using Mono.Cecil; 25 | 26 | namespace Smocks.IL.Dependencies 27 | { 28 | internal class DependencyGraph : DependencyGraphNode, IDependencyGraph 29 | { 30 | public DependencyGraph( 31 | ModuleReference module, 32 | IEqualityComparer moduleComparer) 33 | : base(module, moduleComparer) 34 | { 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Dependencies/IDependencyGraph.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Collections.Generic; 24 | using Mono.Cecil; 25 | 26 | namespace Smocks.IL.Dependencies 27 | { 28 | internal interface IDependencyGraph 29 | { 30 | ModuleReference Module { get; } 31 | 32 | ISet Nodes { get; } 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Dependencies/IDependencyGraphBuilder.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Reflection; 24 | 25 | namespace Smocks.IL.Dependencies 26 | { 27 | internal interface IDependencyGraphBuilder 28 | { 29 | DependencyGraph BuildGraphForMethod(MethodBase method); 30 | } 31 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Dependencies/IDependencyNodeContainer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Collections.Generic; 24 | 25 | namespace Smocks.IL.Dependencies 26 | { 27 | internal interface IDependencyNodeContainer 28 | { 29 | ISet Nodes { get; } 30 | } 31 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/DisassembleResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | 3 | //// The MIT License (MIT) 4 | //// 5 | //// Copyright (c) 2015 Tom van der Kleij 6 | //// 7 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 8 | //// this software and associated documentation files (the "Software"), to deal in 9 | //// the Software without restriction, including without limitation the rights to 10 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 11 | //// the Software, and to permit persons to whom the Software is furnished to do so, 12 | //// subject to the following conditions: 13 | //// 14 | //// The above copyright notice and this permission notice shall be included in all 15 | //// copies or substantial portions of the Software. 16 | //// 17 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 19 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | #endregion License 25 | 26 | using System; 27 | using Mono.Cecil; 28 | using MethodBody = Mono.Cecil.Cil.MethodBody; 29 | 30 | namespace Smocks.IL 31 | { 32 | internal class DisassembleResult : IDisposable 33 | { 34 | /// 35 | /// Initializes a new instance of the class. 36 | /// 37 | /// The module. Takes ownership of the provided module. 38 | /// The body. 39 | public DisassembleResult(ModuleDefinition module, MethodBody body) 40 | { 41 | ModuleDefinition = module; 42 | Body = body; 43 | } 44 | 45 | public MethodBody Body { get; set; } 46 | 47 | public ModuleDefinition ModuleDefinition { get; set; } 48 | 49 | public void Dispose() 50 | { 51 | ModuleDefinition?.Dispose(); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Filters/AllowAllModuleFilter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using Mono.Cecil; 24 | 25 | namespace Smocks.IL.Filters 26 | { 27 | internal class AllowAllModuleFilter : IModuleFilter 28 | { 29 | public bool Accepts(ModuleDefinition module) 30 | { 31 | return true; 32 | } 33 | 34 | public void Dispose() 35 | { 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Filters/DirectReferencesModuleFilter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | using Mono.Cecil; 27 | using Smocks.IL.Dependencies; 28 | using Smocks.Utility; 29 | 30 | namespace Smocks.IL.Filters 31 | { 32 | internal class DirectReferencesModuleFilter : IModuleFilter 33 | { 34 | private readonly IEqualityComparer _moduleComparer; 35 | private IDependencyGraph _dependencyGraph; 36 | 37 | internal DirectReferencesModuleFilter( 38 | IDependencyGraph dependencyGraph, 39 | IEqualityComparer moduleComparer) 40 | { 41 | ArgumentChecker.NotNull(dependencyGraph, () => dependencyGraph); 42 | ArgumentChecker.NotNull(moduleComparer, () => moduleComparer); 43 | 44 | _dependencyGraph = dependencyGraph; 45 | _moduleComparer = moduleComparer; 46 | } 47 | 48 | public bool Accepts(ModuleDefinition module) 49 | { 50 | return _moduleComparer.Equals(module, _dependencyGraph.Module) || 51 | _dependencyGraph.Nodes.Any(node => _moduleComparer.Equals(node.Module, module)); 52 | } 53 | 54 | public void Dispose() 55 | { 56 | _dependencyGraph = null; 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Filters/IModuleFilter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System; 24 | using Mono.Cecil; 25 | 26 | namespace Smocks.IL.Filters 27 | { 28 | internal interface IModuleFilter : IDisposable 29 | { 30 | bool Accepts(ModuleDefinition module); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Filters/IModuleFilterFactory.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using Smocks.IL.Dependencies; 24 | 25 | namespace Smocks.IL.Filters 26 | { 27 | internal interface IModuleFilterFactory 28 | { 29 | IModuleFilter GetFilter(Scope scope, DependencyGraph dependencyGraph); 30 | } 31 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Filters/ModuleFilterFactory.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System; 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | using Smocks.IL.Dependencies; 27 | using Smocks.Utility; 28 | 29 | namespace Smocks.IL.Filters 30 | { 31 | internal class ModuleFilterFactory : IModuleFilterFactory 32 | { 33 | private readonly IEqualityComparer _moduleComparer; 34 | 35 | internal ModuleFilterFactory(IEqualityComparer moduleComparer) 36 | { 37 | ArgumentChecker.NotNull(moduleComparer, () => moduleComparer); 38 | 39 | _moduleComparer = moduleComparer; 40 | } 41 | 42 | public IModuleFilter GetFilter(Scope scope, DependencyGraph dependencyGraph) 43 | { 44 | switch (scope) 45 | { 46 | case Scope.All: 47 | return new AllowAllModuleFilter(); 48 | 49 | case Scope.DirectReferences: 50 | return new DirectReferencesModuleFilter(dependencyGraph, _moduleComparer); 51 | 52 | default: 53 | throw new NotSupportedException("Unknown scope"); 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/FixedPdbReaderProvider.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Mono.Cecil; 3 | using Mono.Cecil.Cil; 4 | using Mono.Cecil.Pdb; 5 | 6 | namespace Smocks.IL 7 | { 8 | /// 9 | /// This is a copy/paste of Cecil's with a small bugfix. 10 | /// Will also submit a pull request to Cecil, but until that is merged we'll use this fix. 11 | /// 12 | /// 13 | internal class FixedPdbReaderProvider : ISymbolReaderProvider 14 | { 15 | public ISymbolReader GetSymbolReader(ModuleDefinition module, string fileName) 16 | { 17 | return IsPortablePdb(GetPdbFileName(fileName)) 18 | ? new PortablePdbReaderProvider().GetSymbolReader(module, fileName) 19 | : new NativePdbReaderProvider().GetSymbolReader(module, fileName); 20 | } 21 | 22 | public ISymbolReader GetSymbolReader(ModuleDefinition module, Stream symbolStream) 23 | { 24 | return IsPortablePdb(symbolStream) 25 | ? new PortablePdbReaderProvider().GetSymbolReader(module, symbolStream) 26 | : new NativePdbReaderProvider().GetSymbolReader(module, symbolStream); 27 | } 28 | 29 | private static string GetPdbFileName(string assemblyFileName) 30 | { 31 | return Path.ChangeExtension(assemblyFileName, ".pdb"); 32 | } 33 | 34 | private static bool IsPortablePdb(string fileName) 35 | { 36 | // Here's the fix: Cecil's provider uses FileShare.None instead of FileShare.Read. 37 | // In our case, the PDB is typically already open (for reading), causing this to fail. 38 | // Since we're just reading some bytes from the PDB, I don't see why we wouldn't allow 39 | // others to simultaneously read from the stream. 40 | using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) 41 | return IsPortablePdb(file); 42 | } 43 | 44 | private static bool IsPortablePdb(Stream stream) 45 | { 46 | const uint ppdbSignature = 0x424a5342; 47 | 48 | var position = stream.Position; 49 | try 50 | { 51 | var reader = new BinaryReader(stream); 52 | return reader.ReadUInt32() == ppdbSignature; 53 | } 54 | finally 55 | { 56 | stream.Position = position; 57 | } 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IArgumentGenerator.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal interface IArgumentGenerator 30 | { 31 | IEnumerable GetArguments(Type[] parameterTypes, object target); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IAssemblyPostProcessor.cs: -------------------------------------------------------------------------------- 1 | using Mono.Cecil; 2 | 3 | namespace Smocks.IL 4 | { 5 | internal interface IAssemblyPostProcessor 6 | { 7 | void Process(AssemblyDefinition assembly); 8 | } 9 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IAssemblyRewriter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using Smocks.AppDomains; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal interface IAssemblyRewriter : IDisposable 30 | { 31 | IAssemblyLoader Rewrite(string path); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/ICompiledMethod.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.IL 25 | { 26 | internal interface ICompiledMethod 27 | { 28 | TReturnValue Invoke(object[] arguments); 29 | } 30 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IEventAccessorExtractor.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System; 25 | using Smocks.Setups; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal interface IEventAccessorExtractor 30 | { 31 | EventAccessorPair FindEventAccessor(Action addAction, Action removeAction); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IEventTargetExtractor.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using System.Reflection; 26 | using Smocks.Setups; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface IEventTargetExtractor 31 | { 32 | IEnumerable GetTargets(MethodBase method, object target); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IILGenerator.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | using System.Reflection.Emit; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface IILGenerator 31 | { 32 | LocalBuilder DeclareLocal(Type localType); 33 | 34 | LocalBuilder DeclareLocal(Type localType, bool pinned); 35 | 36 | Label DefineLabel(); 37 | 38 | void Emit(OpCode opcode); 39 | 40 | void Emit(OpCode opcode, byte arg); 41 | 42 | void Emit(OpCode opcode, ConstructorInfo con); 43 | 44 | void Emit(OpCode opcode, double arg); 45 | 46 | void Emit(OpCode opcode, FieldInfo field); 47 | 48 | void Emit(OpCode opcode, float arg); 49 | 50 | void Emit(OpCode opcode, int arg); 51 | 52 | void Emit(OpCode opcode, Label label); 53 | 54 | void Emit(OpCode opcode, Label[] labels); 55 | 56 | void Emit(OpCode opcode, LocalBuilder local); 57 | 58 | void Emit(OpCode opcode, long arg); 59 | 60 | void Emit(OpCode opcode, MethodInfo meth); 61 | 62 | void Emit(OpCode opcode, sbyte arg); 63 | 64 | void Emit(OpCode opcode, short arg); 65 | 66 | void Emit(OpCode opcode, string str); 67 | 68 | void Emit(OpCode opcode, Type cls); 69 | 70 | void MarkLabel(Label label); 71 | } 72 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IInstructionHelper.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | using Mono.Cecil.Cil; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface IInstructionHelper 31 | { 32 | IEnumerable GetUsages(IEnumerable instructions); 33 | 34 | bool ReadsParameter(MethodReference method, Instruction instruction, 35 | out Parameter parameter); 36 | 37 | bool TryGetCall(Instruction instruction, out MethodReference calledMethod); 38 | } 39 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IInstructionsCompiler.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | using Mono.Cecil.Cil; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface IInstructionsCompiler 31 | { 32 | ICompiledMethod Compile(TypeReference[] parameters, IEnumerable instructions, IEnumerable variables); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IMethodDisassembler.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | 26 | namespace Smocks.IL 27 | { 28 | internal interface IMethodDisassembler 29 | { 30 | DisassembleResult Disassemble(MethodBase method); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IMethodImporter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal interface IMethodImporter 30 | { 31 | MethodReference Import(MethodBase method); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IMethodRewriter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using Mono.Cecil; 25 | using Smocks.Setups; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal interface IMethodRewriter 30 | { 31 | bool Rewrite(Configuration configuration, MethodDefinition method, 32 | IRewriteTargetMatcher rewriteTargetMatcher); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IOpCodeMapper.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.IL 25 | { 26 | internal interface IOpCodeMapper 27 | { 28 | System.Reflection.Emit.OpCode Map(Mono.Cecil.Cil.OpCode opCode); 29 | } 30 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/IParameterDeducer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | using Mono.Cecil.Cil; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface IParameterDeducer 31 | { 32 | TypeReference[] GetParameters(MethodReference method, IEnumerable instructions); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/ISetupExtractor.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using System.Reflection; 26 | using Smocks.Setups; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal interface ISetupExtractor 31 | { 32 | IEnumerable GetSetups(MethodBase method, object target); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/ModuleDefinitionMethodImporter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | using Mono.Cecil; 26 | using Smocks.Utility; 27 | 28 | namespace Smocks.IL 29 | { 30 | internal class ModuleDefinitionMethodImporter : IMethodImporter 31 | { 32 | private readonly ModuleDefinition _moduleDefinition; 33 | 34 | internal ModuleDefinitionMethodImporter(ModuleDefinition module) 35 | { 36 | ArgumentChecker.NotNull(module, () => module); 37 | 38 | _moduleDefinition = module; 39 | } 40 | 41 | public MethodReference Import(MethodBase method) 42 | { 43 | return _moduleDefinition.ImportReference(method); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/ModuleReferenceComparer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | using System.Collections.Generic; 24 | using Mono.Cecil; 25 | 26 | namespace Smocks.IL 27 | { 28 | internal class ModuleReferenceComparer : IEqualityComparer 29 | { 30 | public bool Equals(ModuleReference left, ModuleReference right) 31 | { 32 | return string.Equals(left.Name, right.Name); 33 | } 34 | 35 | public int GetHashCode(ModuleReference reference) 36 | { 37 | return reference.Name.GetHashCode(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/OpCodeMapper.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Linq; 27 | using System.Reflection; 28 | using Mono.Cecil.Cil; 29 | using OpCode = System.Reflection.Emit.OpCode; 30 | using OpCodes = System.Reflection.Emit.OpCodes; 31 | 32 | namespace Smocks.IL 33 | { 34 | internal class OpCodeMapper : IOpCodeMapper 35 | { 36 | private static readonly Dictionary Lookup = BuildLookup(); 37 | 38 | public OpCode Map(Mono.Cecil.Cil.OpCode opCode) 39 | { 40 | return Lookup[opCode.Code]; 41 | } 42 | 43 | private static Dictionary BuildLookup() 44 | { 45 | var fields = typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public); 46 | return fields 47 | .Where(field => field.FieldType == typeof(OpCode) && GetCode(field.Name).HasValue) 48 | .ToDictionary(field => GetCode(field.Name).Value, field => (OpCode)field.GetValue(null)); 49 | } 50 | 51 | private static Code? GetCode(string name) 52 | { 53 | Code code; 54 | return Enum.TryParse(name, true, out code) ? code : default(Code?); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Parameter.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using Mono.Cecil; 25 | using Smocks.Utility; 26 | 27 | namespace Smocks.IL 28 | { 29 | internal class Parameter 30 | { 31 | internal Parameter(TypeReference parameterType, int index) 32 | { 33 | ArgumentChecker.NotNull(parameterType, () => parameterType); 34 | 35 | ParameterType = parameterType; 36 | Index = index; 37 | } 38 | 39 | public int Index { get; private set; } 40 | 41 | public TypeReference ParameterType { get; private set; } 42 | } 43 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/PostProcessors/AssemblyAttributesFilter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Security; 4 | using Mono.Cecil; 5 | 6 | namespace Smocks.IL.PostProcessors 7 | { 8 | internal class AssemblyAttributesFilter : IAssemblyPostProcessor 9 | { 10 | private static readonly Type[] AttributesToRemove = 11 | { 12 | typeof(AllowPartiallyTrustedCallersAttribute) 13 | }; 14 | 15 | public void Process(AssemblyDefinition assembly) 16 | { 17 | for (int i = assembly.CustomAttributes.Count - 1; i >= 0; --i) 18 | { 19 | if (AttributesToRemove.Any(attributeToRemove => assembly.CustomAttributes[i].AttributeType.FullName == attributeToRemove.FullName)) 20 | { 21 | assembly.CustomAttributes.RemoveAt(i); 22 | } 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/PostProcessors/ModuleMvidPostProcessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Mono.Cecil; 3 | 4 | namespace Smocks.IL.PostProcessors 5 | { 6 | internal class ModuleMvidPostProcessor : IAssemblyPostProcessor 7 | { 8 | public void Process(AssemblyDefinition assembly) 9 | { 10 | foreach (var module in assembly.Modules) 11 | { 12 | module.Mvid = Guid.NewGuid(); 13 | } 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/AssemblyTypeContainer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Reflection; 27 | using Smocks.Utility; 28 | 29 | namespace Smocks.IL.Resolvers 30 | { 31 | internal class AssemblyTypeContainer : ITypeContainer 32 | { 33 | private readonly Assembly _assembly; 34 | 35 | public AssemblyTypeContainer(Assembly assembly) 36 | { 37 | ArgumentChecker.NotNull(assembly, () => assembly); 38 | 39 | _assembly = assembly; 40 | } 41 | 42 | public Type GetType(string name) 43 | { 44 | return _assembly.GetType(name.Replace('/', '+'), true); 45 | } 46 | 47 | public IEnumerable GetTypes() 48 | { 49 | return _assembly.GetTypes(); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/FieldResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | using Mono.Cecil; 27 | using Smocks.Utility; 28 | 29 | namespace Smocks.IL.Resolvers 30 | { 31 | internal class FieldResolver : IFieldResolver 32 | { 33 | private readonly ITypeResolver _typeResolver; 34 | 35 | public FieldResolver(ITypeResolver typeResolver) 36 | { 37 | ArgumentChecker.NotNull(typeResolver, () => typeResolver); 38 | 39 | _typeResolver = typeResolver; 40 | } 41 | 42 | public FieldInfo Resolve(FieldReference fieldReference) 43 | { 44 | Type declaringType = _typeResolver.Resolve(fieldReference.DeclaringType); 45 | var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; 46 | FieldInfo result = declaringType.GetField(fieldReference.Name, flags); 47 | 48 | return result; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/IFieldResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.IL.Resolvers 28 | { 29 | internal interface IFieldResolver 30 | { 31 | FieldInfo Resolve(FieldReference fieldReference); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/IMethodResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.IL.Resolvers 28 | { 29 | internal interface IMethodResolver 30 | { 31 | MethodBase Resolve(MethodReference methodReference); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/IModuleResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using Mono.Cecil; 25 | 26 | namespace Smocks.IL.Resolvers 27 | { 28 | internal interface IModuleResolver 29 | { 30 | ITypeContainer Resolve(IMetadataScope scope); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/ITypeContainer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | namespace Smocks.IL.Resolvers 28 | { 29 | internal interface ITypeContainer 30 | { 31 | Type GetType(string name); 32 | 33 | IEnumerable GetTypes(); 34 | } 35 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/ITypeResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.IL.Resolvers 28 | { 29 | internal interface ITypeResolver 30 | { 31 | Type Resolve(TypeReference type); 32 | 33 | Type Resolve(TypeReference type, GenericBindingContext bindingContext); 34 | } 35 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/Resolvers/ModuleResolver.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Reflection; 26 | using Mono.Cecil; 27 | 28 | namespace Smocks.IL.Resolvers 29 | { 30 | internal class ModuleResolver : IModuleResolver 31 | { 32 | public ITypeContainer Resolve(IMetadataScope scope) 33 | { 34 | AssemblyNameReference assemblyName = scope as AssemblyNameReference; 35 | 36 | if (assemblyName != null) 37 | { 38 | var assembly = Assembly.Load(new AssemblyName(assemblyName.FullName)); 39 | return new AssemblyTypeContainer(assembly); 40 | } 41 | 42 | ModuleDefinition moduleDefinition = scope as ModuleDefinition; 43 | 44 | if (moduleDefinition?.Assembly != null) 45 | { 46 | var assembly = Assembly.Load(new AssemblyName(moduleDefinition.Assembly.FullName)); 47 | return new AssemblyTypeContainer(assembly); 48 | } 49 | 50 | throw new NotImplementedException(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /Source/Smocks/IL/VariableOperation.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.IL 25 | { 26 | internal enum VariableOperation 27 | { 28 | Read, 29 | Write 30 | } 31 | } -------------------------------------------------------------------------------- /Source/Smocks/Injection/IServiceCreator.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | 26 | namespace Smocks.Injection 27 | { 28 | internal interface IServiceCreator 29 | { 30 | object Create(Type type, IServiceLocatorContainer container); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/Injection/IServiceLocator.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | namespace Smocks.Injection 28 | { 29 | /// 30 | /// A service locator used for dependency injection. 31 | /// 32 | public interface IServiceLocator : IDisposable 33 | { 34 | /// 35 | /// Gets the container. 36 | /// 37 | IServiceLocatorContainer Container { get; } 38 | 39 | /// 40 | /// Resolves the specified type. 41 | /// 42 | /// The type to resolve. 43 | /// An instance of 44 | T Resolve(); 45 | 46 | /// 47 | /// Returns an instance of every registered implementation of the specified type. 48 | /// 49 | /// 50 | /// An instance of every registered implementation of the specified type. 51 | IEnumerable ResolveAll(); 52 | } 53 | } -------------------------------------------------------------------------------- /Source/Smocks/Injection/IServiceLocatorSetup.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.Injection 25 | { 26 | /// 27 | /// Setup for a dependency injection container. 28 | /// 29 | public interface IServiceLocatorSetup 30 | { 31 | /// 32 | /// Configures the specified service locator container. 33 | /// 34 | /// The service locator container. 35 | void Configure(IServiceLocatorContainer serviceLocatorContainer); 36 | } 37 | } -------------------------------------------------------------------------------- /Source/Smocks/Logging/LogLevel.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.Logging 25 | { 26 | /// 27 | /// The logging level 28 | /// 29 | public enum LogLevel 30 | { 31 | Fatal, 32 | Error, 33 | Warn, 34 | Info, 35 | Debug 36 | } 37 | } -------------------------------------------------------------------------------- /Source/Smocks/Logging/Logger.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | 26 | namespace Smocks.Logging 27 | { 28 | /// 29 | /// A logger. 30 | /// 31 | public abstract class Logger : MarshalByRefObject 32 | { 33 | /// 34 | /// Logs a message. 35 | /// 36 | /// The logging level. 37 | /// The message. 38 | public abstract void Log(LogLevel level, string message); 39 | } 40 | } -------------------------------------------------------------------------------- /Source/Smocks/Matching/It.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Diagnostics.CodeAnalysis; 26 | using System.Linq.Expressions; 27 | 28 | namespace Smocks.Matching 29 | { 30 | /// 31 | /// Provides functionality similar to Moq's It class. Both implementations 32 | /// are interchangeable for use in Smocks. Smock provides one for users 33 | /// that don't use Moq. 34 | /// 35 | [ExcludeFromCodeCoverage] 36 | public static class It 37 | { 38 | /// 39 | /// Matches any instance of . 40 | /// 41 | /// The type to match. 42 | /// The default value of . 43 | public static T IsAny() 44 | { 45 | return default(T); 46 | } 47 | 48 | /// 49 | /// Matches any instance of that matches the provided predicate. 50 | /// 51 | /// The type to match. 52 | /// The predicate. 53 | /// 54 | /// The default value of . 55 | /// 56 | public static T Is(Expression> predicate) 57 | { 58 | return default(T); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /Source/Smocks/Scope.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | namespace Smocks 24 | { 25 | /// 26 | /// The scope of Smocks: should it rewrite only direct references 27 | /// or rewrite any loaded assembly. 28 | /// 29 | public enum Scope 30 | { 31 | All, 32 | DirectReferences 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/Serialization/ISerializer.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | namespace Smocks.Serialization 28 | { 29 | internal interface ISerializer : ISerializer> 30 | { 31 | } 32 | 33 | internal interface ISerializer 34 | { 35 | object Deserialize(Type targetType, TSerialized serializedTarget); 36 | 37 | void Populate(TSerialized serializedTarget, object target); 38 | 39 | TSerialized Serialize(object target); 40 | } 41 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/ICallback.Generated.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="true" hostSpecific="true" #> 2 | <#@ output extension=".cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Assembly Name="System.Windows.Forms" #> 5 | <#@ import namespace="System" #> 6 | <#@ import namespace="System.IO" #> 7 | <#@ import namespace="System.Diagnostics" #> 8 | <#@ import namespace="System.Linq" #> 9 | <#@ import namespace="System.Collections" #> 10 | <#@ import namespace="System.Collections.Generic" #> 11 | using System; 12 | 13 | namespace Smocks.Setups.Fluent 14 | { 15 | /// 16 | /// Contains .Callback(...) setup methods 17 | /// 18 | public partial interface ICallback 19 | { 20 | <# 21 | for (int i = 0; i < 16; ++i) 22 | { 23 | var args = Enumerable.Range(1, i + 1).Select(x => "T" + x).ToList(); 24 | var genericArgs = string.Join(", ", args); 25 | var functionArgs = string.Join(", ", args.Concat(new List { "TReturnValue" })); 26 | #> 27 | /// 28 | /// Configures a callback that is invoked when the setup's target is invoked. 29 | /// 30 | <# foreach (string arg in args) { #> 31 | /// The type of the argument. 32 | <# 33 | } 34 | #> 35 | /// The callback. 36 | /// The setup. 37 | ISetup Callback<<#= genericArgs #>>(Action<<#= genericArgs #>> callback); 38 | 39 | <# 40 | } 41 | #> 42 | } 43 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/ICallback.Generic.Generated.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="true" hostSpecific="true" #> 2 | <#@ output extension=".cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Assembly Name="System.Windows.Forms" #> 5 | <#@ import namespace="System" #> 6 | <#@ import namespace="System.IO" #> 7 | <#@ import namespace="System.Diagnostics" #> 8 | <#@ import namespace="System.Linq" #> 9 | <#@ import namespace="System.Collections" #> 10 | <#@ import namespace="System.Collections.Generic" #> 11 | using System; 12 | 13 | namespace Smocks.Setups.Fluent 14 | { 15 | /// 16 | /// Contains .Callback(...) setup methods 17 | /// 18 | /// The type of the return value. 19 | public partial interface ICallback 20 | { 21 | <# 22 | for (int i = 0; i < 16; ++i) 23 | { 24 | var args = Enumerable.Range(1, i + 1).Select(x => "T" + x).ToList(); 25 | var genericArgs = string.Join(", ", args); 26 | var functionArgs = string.Join(", ", args.Concat(new List { "TReturnValue" })); 27 | #> 28 | /// 29 | /// Configures a callback that is invoked when the setup's target is invoked. 30 | /// 31 | <# foreach (string arg in args) { #> 32 | /// The type of the argument. 33 | <# 34 | } 35 | #> 36 | /// The callback. 37 | /// The setup. 38 | ISetup Callback<<#= genericArgs #>>(Action<<#= genericArgs #>> callback); 39 | 40 | <# 41 | } 42 | #> 43 | } 44 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/ICallback.Generic.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | namespace Smocks.Setups.Fluent 25 | { 26 | /// 27 | /// Contains .Callback(...) setup methods 28 | /// 29 | /// The type of the return value. 30 | public partial interface ICallback 31 | { 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/ICallback.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | namespace Smocks.Setups.Fluent 25 | { 26 | /// 27 | /// Contains .Callback(...) setup methods 28 | /// 29 | public partial interface ICallback 30 | { 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/IReturns.Generated.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="true" hostSpecific="true" #> 2 | <#@ output extension=".cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Assembly Name="System.Windows.Forms" #> 5 | <#@ import namespace="System" #> 6 | <#@ import namespace="System.IO" #> 7 | <#@ import namespace="System.Diagnostics" #> 8 | <#@ import namespace="System.Linq" #> 9 | <#@ import namespace="System.Collections" #> 10 | <#@ import namespace="System.Collections.Generic" #> 11 | using System; 12 | 13 | namespace Smocks.Setups.Fluent 14 | { 15 | /// 16 | /// Contains .Returns(...) setup methods 17 | /// 18 | /// The type of the return value. 19 | public partial interface IReturns 20 | { 21 | <# 22 | for (int i = 0; i < 16; ++i) 23 | { 24 | var args = Enumerable.Range(1, i + 1).Select(x => "T" + x).ToList(); 25 | var genericArgs = string.Join(", ", args); 26 | var functionArgs = string.Join(", ", args.Concat(new List { "TReturnValue" })); 27 | #> 28 | /// 29 | /// Configures a callback that returns the return value. 30 | /// 31 | <# foreach (string arg in args) { #> 32 | /// The type of the argument. 33 | <# 34 | } 35 | #> 36 | /// The generator. 37 | /// The setup. 38 | ISetup Returns<<#= genericArgs #>>(Func<<#= functionArgs #>> generator); 39 | 40 | <# 41 | } 42 | #> 43 | } 44 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/IReturns.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | namespace Smocks.Setups.Fluent 25 | { 26 | /// 27 | /// Contains .Returns(...) setup methods 28 | /// 29 | /// The type of the return value. 30 | public partial interface IReturns 31 | { 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/IThrows.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | 26 | namespace Smocks.Setups.Fluent 27 | { 28 | /// 29 | /// Contains .Throws(...) setup methods 30 | /// 31 | public interface IThrows 32 | { 33 | /// 34 | /// Configures an exception that will be thrown when 35 | /// the target of the setup is invoked. 36 | /// 37 | /// The type of the exception. 38 | void Throws() where TException : Exception, new(); 39 | 40 | /// 41 | /// Configures an exception that will be thrown when 42 | /// the target of the setup is invoked. 43 | /// 44 | /// The type of the exception. 45 | /// The that will be thrown. 46 | void Throws(TException exception) where TException : Exception; 47 | } 48 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Fluent/IVerifiable.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.Setups.Fluent 25 | { 26 | /// 27 | /// Contains .Verifiable(...) setup methods 28 | /// 29 | public interface IVerifiable 30 | { 31 | /// 32 | /// Marks this setup as verifiable. If the 33 | /// method is invoked after marking the setup as verifiable and the target of 34 | /// the setup has never been invoked, an exception will be thrown. 35 | /// 36 | void Verifiable(); 37 | } 38 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IArgumentMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using System.Linq.Expressions; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface IArgumentMatcher 30 | { 31 | bool IsMatch(IEnumerable setupArguments, 32 | IEnumerable actualArguments); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IInternalSetup.Generic.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | namespace Smocks.Setups 25 | { 26 | internal interface IInternalSetup : ISetup, IInternalSetup 27 | { 28 | bool HasReturnValue { get; } 29 | 30 | TReturnValue GetReturnValue(object[] arguments); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IInternalSetup.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.Setups 25 | { 26 | internal interface IInternalSetup : IInternalSetupBase 27 | { 28 | } 29 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IInternalSetupBase.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System; 25 | using Smocks.Utility; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface IInternalSetupBase 30 | { 31 | Lazy Exception { get; } 32 | 33 | MethodCallInfo MethodCall { get; } 34 | 35 | bool Verify { get; } 36 | 37 | Action CallbackAction { get; } 38 | 39 | int ArgumentsToSkipInCallbacks { get; } 40 | } 41 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IInvocationTracker.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | 26 | namespace Smocks.Setups 27 | { 28 | internal interface IInvocationTracker 29 | { 30 | void Track(MethodBase method, object[] arguments, IInternalSetup setup); 31 | 32 | void Verify(); 33 | } 34 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IItIsMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Linq.Expressions; 25 | 26 | namespace Smocks.Setups 27 | { 28 | internal interface IItIsMatcher 29 | { 30 | bool ItIsMatch(Expression setupArgument, object actualValue); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IRewriteTarget.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.ObjectModel; 25 | using System.Reflection; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface IRewriteTarget 30 | { 31 | /// 32 | /// Gets the methods that should be rewritten. 33 | /// 34 | ReadOnlyCollection Methods { get; } 35 | 36 | /// 37 | /// Gets a value indicating whether the target is an event accessor method. 38 | /// 39 | bool IsEvent { get; } 40 | } 41 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IRewriteTargetCollection.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface IRewriteTargetCollection : IEnumerable 30 | { 31 | IRewriteTargetMatcher GetMatcher(ModuleDefinition module); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/IRewriteTargetMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections.Generic; 25 | using Mono.Cecil; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface IRewriteTargetMatcher 30 | { 31 | IEnumerable GetMatchingTargets(MethodReference method); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/ISetup.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using Smocks.Setups.Fluent; 26 | 27 | namespace Smocks.Setups 28 | { 29 | /// 30 | /// A setup for an expression that can be used to configure the behaviour 31 | /// and/or expectations for the target of the setup. 32 | /// 33 | public partial interface ISetup : ICallback, IThrows, IVerifiable 34 | { 35 | /// 36 | /// Configures a callback that is invoked when the setup's target is invoked. 37 | /// 38 | /// The callback. 39 | /// The setup. 40 | ISetup Callback(Action callback); 41 | } 42 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/ISetupManager.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Collections.ObjectModel; 27 | using System.Linq.Expressions; 28 | using System.Reflection; 29 | 30 | namespace Smocks.Setups 31 | { 32 | internal interface ISetupManager : IEnumerable 33 | { 34 | ISetup Create(Expression expression); 35 | 36 | ISetup Create(Expression> expression); 37 | 38 | ReadOnlyCollection GetSetupsForMethod(MethodBase target); 39 | } 40 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/ISetupMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | 26 | namespace Smocks.Setups 27 | { 28 | internal interface ISetupMatcher 29 | { 30 | IInternalSetup GetBestMatchingSetup(MethodBase method, object[] arguments); 31 | } 32 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/ITargetMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Linq.Expressions; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal interface ITargetMatcher 30 | { 31 | bool IsMatch(Type targetType, Expression setupArgument, object actualArgument); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/InterceptorResult.Generic.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | namespace Smocks.Setups 25 | { 26 | /// 27 | /// The result of a (possible) method interception. 28 | /// 29 | /// The type of the return value. 30 | public class InterceptorResult : InterceptorResult 31 | { 32 | /// 33 | /// Initializes a new instance of the class. 34 | /// 35 | /// if set to true, the method call was intercepted. 36 | /// The replacement return value. 37 | public InterceptorResult(bool intercepted, TReturnValue returnValue) 38 | : base(intercepted) 39 | { 40 | ReturnValue = returnValue; 41 | } 42 | 43 | /// 44 | /// Gets or sets the replacement return value. 45 | /// 46 | public TReturnValue ReturnValue { get; set; } 47 | } 48 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/InterceptorResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | namespace Smocks.Setups 25 | { 26 | /// 27 | /// The result of a (possible) method interception. 28 | /// 29 | public class InterceptorResult 30 | { 31 | /// 32 | /// Initializes a new instance of the class. 33 | /// 34 | /// if the method was intercepted. 35 | public InterceptorResult(bool intercepted) 36 | { 37 | Intercepted = intercepted; 38 | } 39 | 40 | /// 41 | /// Gets or sets a value indicating whether the method call was actually intercepted. 42 | /// 43 | public bool Intercepted { get; set; } 44 | } 45 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/ItIsMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Linq.Expressions; 26 | using Smocks.Utility; 27 | 28 | namespace Smocks.Setups 29 | { 30 | internal class ItIsMatcher : IItIsMatcher 31 | { 32 | private readonly IExpressionCompiler _expressionCompiler; 33 | 34 | public ItIsMatcher(IExpressionCompiler expressionCompiler) 35 | { 36 | ArgumentChecker.NotNull(expressionCompiler, () => expressionCompiler); 37 | 38 | _expressionCompiler = expressionCompiler; 39 | } 40 | 41 | public bool ItIsMatch(Expression setupArgument, object actualValue) 42 | { 43 | MethodCallExpression expression = (MethodCallExpression)setupArgument; 44 | UnaryExpression unaryExpression = (UnaryExpression)expression.Arguments[0]; 45 | 46 | Delegate compiledExpression = _expressionCompiler.CompileExpression((LambdaExpression)unaryExpression.Operand); 47 | bool returnValue = (bool)compiledExpression.DynamicInvoke(actualValue); 48 | 49 | return returnValue; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/RewriteTargetCollection.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Collections; 25 | using System.Collections.Generic; 26 | using System.Collections.ObjectModel; 27 | using System.Linq; 28 | using Mono.Cecil; 29 | using Smocks.IL; 30 | using Smocks.Utility; 31 | 32 | namespace Smocks.Setups 33 | { 34 | internal class RewriteTargetCollection : IRewriteTargetCollection 35 | { 36 | private readonly ReadOnlyCollection _targets; 37 | 38 | public RewriteTargetCollection(IEnumerable targets) 39 | { 40 | ArgumentChecker.NotNull(targets, () => targets); 41 | 42 | _targets = targets.ToList().AsReadOnly(); 43 | } 44 | 45 | public IEnumerator GetEnumerator() 46 | { 47 | return _targets.GetEnumerator(); 48 | } 49 | 50 | public IRewriteTargetMatcher GetMatcher(ModuleDefinition module) 51 | { 52 | return new RewriteTargetMatcher(new ModuleDefinitionMethodImporter(module), _targets); 53 | } 54 | 55 | IEnumerator IEnumerable.GetEnumerator() 56 | { 57 | return GetEnumerator(); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/RewriteTargetMatcher.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Collections.ObjectModel; 27 | using System.Linq; 28 | using Mono.Cecil; 29 | using Smocks.IL; 30 | using Smocks.Utility; 31 | 32 | namespace Smocks.Setups 33 | { 34 | internal class RewriteTargetMatcher : IRewriteTargetMatcher 35 | { 36 | private readonly List> _targets; 37 | 38 | public RewriteTargetMatcher(IMethodImporter methodImporter, ReadOnlyCollection targets) 39 | { 40 | ArgumentChecker.NotNull(methodImporter, () => methodImporter); 41 | ArgumentChecker.NotNull(targets, () => targets); 42 | 43 | _targets = targets 44 | .SelectMany(target => target.Methods, (target, method) => Tuple.Create(target, methodImporter.Import(method))) 45 | .ToList(); 46 | } 47 | 48 | public IEnumerable GetMatchingTargets(MethodReference method) 49 | { 50 | return _targets 51 | .Where(pair => pair.Item2.FullName.Equals(method.FullName)) 52 | .Select(pair => pair.Item1); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Setup.Generated.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="true" hostSpecific="true" #> 2 | <#@ output extension=".cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Assembly Name="System.Windows.Forms" #> 5 | <#@ import namespace="System" #> 6 | <#@ import namespace="System.IO" #> 7 | <#@ import namespace="System.Diagnostics" #> 8 | <#@ import namespace="System.Linq" #> 9 | <#@ import namespace="System.Collections" #> 10 | <#@ import namespace="System.Collections.Generic" #> 11 | using System; 12 | using Smocks.Utility; 13 | 14 | namespace Smocks.Setups 15 | { 16 | internal partial class Setup 17 | { 18 | <# 19 | for (int i = 0; i < 16; ++i) 20 | { 21 | var args = Enumerable.Range(1, i + 1).Select(x => "T" + x).ToList(); 22 | var genericArgs = string.Join(", ", args); 23 | var functionArgs = string.Join(", ", args.Concat(new List { "TReturnValue" })); 24 | var convertCalls = string.Join(",\r\n\t\t\t\t", 25 | args.Select((value, index) => string.Format("({0})Convert.ChangeType(args[{1}], typeof({0}))", value, index))); 26 | #> 27 | /// 28 | /// Configures a callback that is invoked when the setup's target is invoked. 29 | /// 30 | <# foreach (string arg in args) { #> 31 | /// The type of the argument. 32 | <# 33 | } 34 | #> 35 | /// The callback. 36 | /// The setup. 37 | public ISetup Callback<<#= genericArgs #>>(Action<<#= genericArgs #>> callback) 38 | { 39 | if (ParameterCount != <#= args.Count #>) 40 | { 41 | throw new ArgumentException("Invalid parameter count", "callback"); 42 | } 43 | 44 | CallbackAction = args => callback( 45 | <#= convertCalls #> 46 | ); 47 | 48 | return this; 49 | } 50 | 51 | <# 52 | } 53 | #> 54 | } 55 | } -------------------------------------------------------------------------------- /Source/Smocks/Setups/Setup.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using Smocks.Utility; 26 | 27 | namespace Smocks.Setups 28 | { 29 | internal partial class Setup : SetupBase, ISetup, IInternalSetup 30 | { 31 | internal Setup(MethodCallInfo methodCall) 32 | : base(methodCall) 33 | { 34 | } 35 | 36 | public ISetup Callback(Action callback) 37 | { 38 | if (ParameterCount != 0) 39 | { 40 | throw new ArgumentException("Invalid parameter count", nameof(callback)); 41 | } 42 | 43 | CallbackAction = args => callback(); 44 | return this; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Source/Smocks/Utility/ExpressionCompiler.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System; 25 | using System.Linq.Expressions; 26 | 27 | namespace Smocks.Utility 28 | { 29 | /// 30 | /// A compiler for s. 31 | /// 32 | public class ExpressionCompiler : IExpressionCompiler 33 | { 34 | /// 35 | /// Compiles an to a . 36 | /// 37 | /// The expression. 38 | /// The compiled expression as a . 39 | public Delegate CompileExpression(LambdaExpression expression) 40 | { 41 | ArgumentChecker.NotNull(expression, () => expression); 42 | 43 | var type = expression.GetType(); 44 | bool isCompilableExpression = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Expression<>); 45 | ArgumentChecker.Assert(isCompilableExpression, "Unexpected expression type, cannot compile", "expression"); 46 | 47 | Delegate compiledDelegate = (Delegate)type.GetMethod("Compile", new Type[0]).Invoke(expression, new object[0]); 48 | return compiledDelegate; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Source/Smocks/Utility/IExpressionCompiler.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System; 25 | using System.Linq.Expressions; 26 | 27 | namespace Smocks.Utility 28 | { 29 | internal interface IExpressionCompiler 30 | { 31 | Delegate CompileExpression(LambdaExpression expression); 32 | } 33 | } -------------------------------------------------------------------------------- /Source/Smocks/Utility/IExpressionHelper.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion License 23 | 24 | using System.Linq.Expressions; 25 | using System.Reflection; 26 | 27 | namespace Smocks.Utility 28 | { 29 | internal interface IExpressionHelper 30 | { 31 | FieldInfo GetField(LambdaExpression expression); 32 | 33 | MethodCallInfo GetMethod(LambdaExpression expression); 34 | 35 | MethodCallInfo GetMethod(Expression expression); 36 | 37 | PropertyInfo GetProperty(LambdaExpression expression); 38 | 39 | PropertyInfo GetProperty(MemberExpression expression); 40 | 41 | MethodCallInfo GetPropertyGetCall(LambdaExpression expression); 42 | 43 | object GetValue(Expression expression); 44 | 45 | bool IsMethodInvocation(Expression expression, string typeName, string methodName, int parameters); 46 | } 47 | } -------------------------------------------------------------------------------- /Source/Smocks/Utility/Unit.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | using System; 23 | 24 | #endregion 25 | 26 | namespace Smocks.Utility 27 | { 28 | /// 29 | /// Used in place of Void, as System.Void cannot be used as a generic type parameter. 30 | /// 31 | [Serializable] 32 | public abstract class Unit 33 | { 34 | /// 35 | /// The value. 36 | /// 37 | public static readonly Unit Value = null; 38 | 39 | private Unit() 40 | { 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Source/Smocks/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Source/SolutionInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | //// The MIT License (MIT) 3 | //// 4 | //// Copyright (c) 2015 Tom van der Kleij 5 | //// 6 | //// Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | //// this software and associated documentation files (the "Software"), to deal in 8 | //// the Software without restriction, including without limitation the rights to 9 | //// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | //// the Software, and to permit persons to whom the Software is furnished to do so, 11 | //// subject to the following conditions: 12 | //// 13 | //// The above copyright notice and this permission notice shall be included in all 14 | //// copies or substantial portions of the Software. 15 | //// 16 | //// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | //// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | //// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | //// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | //// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | //// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | #endregion 23 | 24 | using System.Reflection; 25 | 26 | [assembly: AssemblyCompany("Tom van der Kleij")] 27 | [assembly: AssemblyVersion("0.0.0.0")] 28 | [assembly: AssemblyFileVersion("0.0.0.0")] --------------------------------------------------------------------------------