├── .github ├── FUNDING.yml ├── workflows │ ├── changelog.config │ ├── sponsor.yml │ ├── publish.yml │ ├── includes.yml │ ├── changelog.yml │ ├── test │ │ └── action.yml │ └── build.yml ├── release.yml └── dependabot.yml ├── src ├── Moq.snk ├── Moq │ ├── icon.png │ ├── readme.md │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Language │ │ ├── Flow │ │ │ ├── ICallBaseResult.cs │ │ │ ├── IThrowsResult.cs │ │ │ ├── IReturnsResult.cs │ │ │ ├── ICallbackResult.cs │ │ │ ├── IReturnsThrows.cs │ │ │ ├── ISetup.cs │ │ │ └── SetterSetupPhrase.cs │ │ ├── ICallBase.cs │ │ ├── GenericTypeParameters.tt │ │ ├── ICallbackSetter.cs │ │ ├── ICallbackGetter.cs │ │ ├── IRaise.tt │ │ ├── ISetupSequentialResult.cs │ │ ├── IReturns.tt │ │ ├── ISetupConditionResult.cs │ │ ├── IThrows.tt │ │ ├── IRaise.cs │ │ ├── IReturnsGetter.cs │ │ └── ISetupSequentialAction.cs │ ├── MockExceptionReasons.cs │ ├── ISetupList.cs │ ├── IInvocationList.cs │ ├── Range.cs │ ├── Interception │ │ ├── IProxy.cs │ │ ├── Mock.cs │ │ ├── IInterceptor.cs │ │ └── ProxyFactory.cs │ ├── Obsolete │ │ ├── MockExtensions.cs │ │ ├── SequenceExtensions.cs │ │ └── IOccurrence.cs │ ├── Expressions │ │ └── Visitors │ │ │ ├── Apply.cs │ │ │ └── EvaluateCaptures.cs │ ├── Behavior.cs │ ├── IMatcher.cs │ ├── MockBehavior.cs │ ├── MockExtensions.cs │ ├── IMocked.cs │ ├── Switches.cs │ ├── ITypeMatcher.cs │ ├── Protected │ │ └── ProtectedExtension.cs │ ├── InvocationFunc.cs │ ├── InvocationAction.cs │ ├── Async │ │ ├── IAwaitableFactory.cs │ │ ├── Awaitable.cs │ │ ├── TaskFactory.cs │ │ ├── ValueTaskFactory.cs │ │ ├── TaskFactory`1.cs │ │ ├── ValueTaskFactory`1.cs │ │ └── AwaitableFactory`1.cs │ ├── IMock.cs │ ├── MethodSetup.cs │ ├── IInvocation.cs │ ├── Behaviors │ │ ├── NoOp.cs │ │ ├── ReturnBase.cs │ │ ├── ReturnValue.cs │ │ ├── ThrowException.cs │ │ ├── Callback.cs │ │ ├── ReturnComputedValue.cs │ │ └── ThrowComputedException.cs │ ├── MatchExpression.cs │ ├── DefaultValue.cs │ ├── Matchers │ │ ├── AnyMatcher.cs │ │ ├── ExpressionMatcher.cs │ │ ├── LazyEvalMatcher.cs │ │ └── RefMatcher.cs │ ├── Pair.cs │ ├── Condition.cs │ ├── Expectation.cs │ ├── DefaultExpressionCompiler.cs │ ├── ParameterTypes.cs │ ├── MockSequence.cs │ ├── Linq │ │ └── Mock.cs │ ├── ExpressionReconstructor.cs │ ├── TypeMatcherAttribute.cs │ └── InnerMockSetup.cs ├── Moq.Tests.ComTypes │ ├── Moq.Tests.ComTypes.dll │ ├── BuildDLLFromIDL.cmd │ ├── ComTypes.idl │ └── README.md ├── Moq.Tests.FSharpTypes │ ├── IHasProperty.fs │ ├── IHasIndexer.fs │ ├── IHasActionEvent.fs │ ├── HasAbstractIndexer.fs │ ├── HasAbstractProperty.fs │ ├── IHasEventHandlerEvent.fs │ ├── HasAbstractActionEvent.fs │ ├── HasActionEvent.fs │ ├── HasAbstractEventHandlerEvent.fs │ ├── HasProperty.fs │ ├── HasIndexer.fs │ └── Moq.Tests.FSharpTypes.fsproj ├── Moq.Tests │ ├── StringExtensions.cs │ ├── ReflectionExtensions.cs │ ├── Async │ │ └── AwaitableFixture.cs │ ├── RecordsFixture.cs │ ├── HidePropertyFixture.cs │ ├── Regressions │ │ ├── StreamFixture.cs │ │ └── FluentMockIssues.cs │ ├── CaptureMatchFixture.cs │ ├── CallBaseFixture.cs │ ├── IsValueTypeFixture.cs │ ├── IsSubtypeFixture.cs │ ├── UnmatchableMatchersFixture.cs │ ├── SetupsFixture.cs │ ├── Matchers │ │ ├── AnyMatcherFixture.cs │ │ └── ParamArrayMatcherFixture.cs │ ├── StringBuilderExtensionsFixture.cs │ ├── EventHandlerTypesMustMatchFixture.cs │ ├── SequentialActionExtensionsFixture.cs │ ├── Moq.Tests.csproj │ ├── ProxyFactories │ │ └── MostSpecificOverrideFixture.cs │ ├── OccurrenceFixture.cs │ └── MockedFixture.cs ├── Moq.Tests.VisualBasic │ └── Moq.Tests.VisualBasic.vbproj └── Directory.props ├── docs ├── Gemfile ├── _sass │ ├── utility │ │ ├── max-width.scss │ │ ├── text-alignment.scss │ │ ├── padding.scss │ │ ├── margin.scss │ │ ├── width.scss │ │ ├── position.scss │ │ ├── height.scss │ │ ├── flex.scss │ │ └── space-between.scss │ └── reset.scss ├── assets │ ├── images │ │ ├── github-32.png │ │ └── moq-icon.png │ └── main.scss ├── user-guide │ └── index.md ├── index.md ├── _config.yml ├── _layouts │ ├── default.html │ └── homepage.html ├── _includes │ ├── head.html │ └── header.html └── Gemfile.lock ├── assets ├── img │ ├── moq.png │ ├── moq-icon.png │ ├── moq-small.png │ └── moq-bigger.png └── css │ └── style.scss ├── _config.yml ├── Directory.Build.rsp ├── .gitignore ├── .gitattributes └── License.txt /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: devlooped 2 | -------------------------------------------------------------------------------- /src/Moq.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/src/Moq.snk -------------------------------------------------------------------------------- /docs/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'jekyll-relative-links' 4 | -------------------------------------------------------------------------------- /docs/_sass/utility/max-width.scss: -------------------------------------------------------------------------------- 1 | .max-w-650 { 2 | max-width: 650px; 3 | } 4 | -------------------------------------------------------------------------------- /src/Moq/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/src/Moq/icon.png -------------------------------------------------------------------------------- /assets/img/moq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/assets/img/moq.png -------------------------------------------------------------------------------- /docs/_sass/utility/text-alignment.scss: -------------------------------------------------------------------------------- 1 | .text-center { 2 | text-align: center; 3 | } 4 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate 2 | 3 | exclude: [ 'src/', '*.sln', 'Gemfile*', '*.rsp' ] -------------------------------------------------------------------------------- /assets/img/moq-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/assets/img/moq-icon.png -------------------------------------------------------------------------------- /assets/img/moq-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/assets/img/moq-small.png -------------------------------------------------------------------------------- /assets/img/moq-bigger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/assets/img/moq-bigger.png -------------------------------------------------------------------------------- /docs/assets/images/github-32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/docs/assets/images/github-32.png -------------------------------------------------------------------------------- /docs/assets/images/moq-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/docs/assets/images/moq-icon.png -------------------------------------------------------------------------------- /docs/user-guide/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | # User Guide 5 | 6 | This is work in progress and currently under construction. 7 | -------------------------------------------------------------------------------- /docs/_sass/utility/padding.scss: -------------------------------------------------------------------------------- 1 | .p { 2 | 3 | &x-8 { 4 | padding-left: 4px; 5 | padding-right: 4px; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/Moq.Tests.ComTypes/Moq.Tests.ComTypes.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImoutoChan/openmoq/HEAD/src/Moq.Tests.ComTypes/Moq.Tests.ComTypes.dll -------------------------------------------------------------------------------- /Directory.Build.rsp: -------------------------------------------------------------------------------- 1 | # See https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-response-files 2 | -nr:false 3 | -m:1 4 | -v:m 5 | -clp:Summary;ForceNoAlign -------------------------------------------------------------------------------- /src/Moq/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /docs/_sass/utility/margin.scss: -------------------------------------------------------------------------------- 1 | .mx-auto { 2 | margin-left: auto; 3 | margin-right: auto; 4 | } 5 | 6 | .my-36 { 7 | margin-top: 36px; 8 | margin-bottom: 36px; 9 | } 10 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | ![](assets/images/moq-icon.png) 5 | 6 | Welcome to the documentation for Moq! 7 | 8 | [Quickstart](https://github.com/moq/moq/wiki/Quickstart) \| [User Guide](user-guide/index.md) 9 | -------------------------------------------------------------------------------- /docs/_sass/utility/width.scss: -------------------------------------------------------------------------------- 1 | .w { 2 | 3 | &-full { 4 | width: 100%; 5 | } 6 | 7 | &-24 { 8 | width: 24px; 9 | } 10 | 11 | &-42 { 12 | width: 42px; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /docs/_sass/utility/position.scss: -------------------------------------------------------------------------------- 1 | .absolute { 2 | position: absolute; 3 | } 4 | 5 | .relative { 6 | position: relative; 7 | } 8 | 9 | .sticky { 10 | position: sticky; 11 | } 12 | 13 | .top-0 { 14 | top: 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Resources; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: ComVisible(false)] 6 | [assembly: CLSCompliant(true)] 7 | [assembly: NeutralResourcesLanguage("en-US")] 8 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | baseurl: /moq 2 | 3 | defaults: 4 | - scope: 5 | path: "" 6 | values: 7 | layout: "default" 8 | - scope: 9 | path: "index.md" 10 | values: 11 | layout: "homepage" 12 | 13 | plugins: 14 | - jekyll-relative-links 15 | -------------------------------------------------------------------------------- /docs/_sass/utility/height.scss: -------------------------------------------------------------------------------- 1 | .h { 2 | 3 | &-full { 4 | height: 100%; 5 | } 6 | 7 | &-24 { 8 | height: 24px; 9 | } 10 | 11 | &-42 { 12 | height: 42px; 13 | } 14 | 15 | &-64 { 16 | height: 64px; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Moq.Tests.ComTypes/BuildDLLFromIDL.cmd: -------------------------------------------------------------------------------- 1 | midl /mktyplib203 /env win32 /tlb ComTypes.tlb ComTypes.idl 2 | tlbimp ComTypes.tlb /out:Moq.Tests.ComTypes.dll /namespace:Moq.Tests.ComTypes /keyfile:..\..\Moq.snk /asmversion:4.0.0.0 3 | peverify Moq.Tests.ComTypes.dll 4 | del ComTypes.tlb 5 | -------------------------------------------------------------------------------- /docs/_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {% include head.html %} 4 | 5 | {% include header.html %} 6 |
7 | {{ content }} 8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /docs/_layouts/homepage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {% include head.html %} 4 | 5 | {% include header.html %} 6 |
7 | {{ content }} 8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /docs/_sass/utility/flex.scss: -------------------------------------------------------------------------------- 1 | .flex { 2 | display: flex; 3 | 4 | &-grow { 5 | flex-grow: 1; 6 | } 7 | } 8 | 9 | .items { 10 | 11 | &-center { 12 | align-items: center; 13 | } 14 | } 15 | 16 | .justify { 17 | 18 | &-stretch { 19 | justify-content: stretch; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /docs/_sass/utility/space-between.scss: -------------------------------------------------------------------------------- 1 | .space-x { 2 | 3 | &-4 > * + * { 4 | margin-left: 4px; 5 | } 6 | 7 | &-8 > * + * { 8 | margin-left: 8px; 9 | } 10 | 11 | &-12 > * + * { 12 | margin-left: 12px; 13 | } 14 | 15 | &-16 > * + * { 16 | margin-left: 16px; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/IHasProperty.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type IHasProperty = 9 | abstract member Property: obj with get, set 10 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/IHasIndexer.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type IHasIndexer = 9 | abstract member Item: int -> obj with get, set 10 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/IHasActionEvent.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type IHasActionEvent = 9 | [] 10 | abstract member Event: IDelegateEvent 11 | -------------------------------------------------------------------------------- /.github/workflows/changelog.config: -------------------------------------------------------------------------------- 1 | usernames-as-github-logins=true 2 | issues_wo_labels=true 3 | pr_wo_labels=true 4 | exclude-labels=bydesign,dependencies,duplicate,question,invalid,wontfix,need info,docs 5 | enhancement-label=:sparkles: Implemented enhancements: 6 | bugs-label=:bug: Fixed bugs: 7 | issues-label=:hammer: Other: 8 | pr-label=:twisted_rightwards_arrows: Merged: 9 | unreleased=false 10 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasAbstractIndexer.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | [] 9 | type HasAbstractIndexer() = 10 | abstract Item: int -> obj with get, set 11 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasAbstractProperty.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | [] 9 | type HasAbstractProperty() = 10 | abstract Property: obj with get, set 11 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/IHasEventHandlerEvent.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type IHasEventHandlerEvent = 9 | [] 10 | abstract member Event: IEvent 11 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasAbstractActionEvent.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | [] 9 | type HasAbstractActionEvent = 10 | [] 11 | abstract member Event: IDelegateEvent 12 | -------------------------------------------------------------------------------- /docs/_sass/reset.scss: -------------------------------------------------------------------------------- 1 | *, 2 | *::before, 3 | *::after { 4 | box-sizing: border-box; 5 | } 6 | 7 | footer, 8 | header, 9 | main { 10 | display: block; 11 | } 12 | 13 | blockquote, 14 | body, 15 | figure, 16 | h1, 17 | h2, 18 | h3, 19 | h4, 20 | img, 21 | p, 22 | pre { 23 | margin: 0; 24 | padding: 0; 25 | } 26 | 27 | :root, 28 | body { 29 | min-height: 100%; 30 | } 31 | 32 | img { 33 | display: inline-block; 34 | } 35 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasActionEvent.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type HasActionEvent() = 9 | let event = new DelegateEvent(); 10 | 11 | [] 12 | member this.Event = event.Publish 13 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasAbstractEventHandlerEvent.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | [] 9 | type HasAbstractEventHandlerEvent = 10 | [] 11 | abstract member Event: IEvent 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | app 3 | obj 4 | artifacts 5 | pack 6 | TestResults 7 | .vs 8 | .vscode 9 | .idea 10 | 11 | *.suo 12 | *.sdf 13 | *.userprefs 14 | *.user 15 | *.nupkg 16 | *.metaproj 17 | *.tmp 18 | *.log 19 | *.cache 20 | *.binlog 21 | *.zip 22 | __azurite*.* 23 | __*__ 24 | 25 | .nuget 26 | *.lock.json 27 | *.nuget.props 28 | *.nuget.targets 29 | 30 | node_modules 31 | _site 32 | .jekyll-metadata 33 | .jekyll-cache 34 | Gemfile.lock 35 | package-lock.json 36 | -------------------------------------------------------------------------------- /assets/css/style.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "jekyll-theme-slate"; 5 | 6 | .inner { 7 | max-width: 960px; 8 | } 9 | 10 | pre, code { 11 | background-color: unset; 12 | font-size: unset; 13 | } 14 | 15 | code { 16 | font-size: 0.80em; 17 | } 18 | 19 | h1 > img { 20 | border: unset; 21 | box-shadow: unset; 22 | vertical-align: middle; 23 | -moz-box-shadow: unset; 24 | -o-box-shadow: unset; 25 | -ms-box-shadow: unset; 26 | } 27 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasProperty.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type HasProperty() = 9 | let mutable property = new obj() 10 | abstract Property: obj with get, set 11 | default this.Property with get() = property and set(value: obj) = property <- value 12 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/HasIndexer.fs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.FSharpTypes 5 | 6 | open System; 7 | 8 | type HasIndexer() = 9 | let mutable item = new obj() 10 | abstract Item: int -> obj with get, set 11 | default this.Item with get(index: int) = item and set (index: int) (value: obj) = item <- value 12 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/ICallBaseResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface ICallBaseResult : IThrows, IThrowsResult, IFluentInterface 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/IThrowsResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface IThrowsResult : IOccurrence, IVerifies, IFluentInterface 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq/MockExceptionReasons.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | [Flags] 9 | enum MockExceptionReasons 10 | { 11 | IncorrectNumberOfCalls = 1, 12 | NoMatchingCalls = 4, 13 | NoSetup = 8, 14 | ReturnValueRequired = 16, 15 | UnmatchedSetup = 32, 16 | UnverifiedInvocations = 64, 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Moq/ISetupList.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.Collections.Generic; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// A list of setups that have been configured on a mock, 10 | /// in chronological order (that is, oldest setup first, most recent setup last). 11 | /// 12 | public interface ISetupList : IReadOnlyList 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/IReturnsResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface IReturnsResult : ICallback, IOccurrence, IRaise, IVerifies, IFluentInterface 13 | { 14 | } 15 | } -------------------------------------------------------------------------------- /src/Moq/Language/Flow/ICallbackResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface ICallbackResult : ICallBase, ICallBaseResult, IThrows, IThrowsResult, IFluentInterface 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq.Tests/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests 5 | { 6 | public static class StringExtensions 7 | { 8 | public static bool ContainsConsecutiveLines(this string str, params string[] lines) 9 | { 10 | var haystack = str.Replace("\r\n", "\n"); 11 | var needle = string.Join("\n", lines); 12 | return haystack.Contains(needle); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Moq/IInvocationList.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.Collections.Generic; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// A list of invocations which have been performed on a mock. 10 | /// 11 | public interface IInvocationList : IReadOnlyList 12 | { 13 | /// 14 | /// Resets all invocations recorded for this mock. 15 | /// 16 | void Clear(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Moq.Tests/ReflectionExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.Collections.Generic; 5 | using System.Reflection; 6 | 7 | namespace Moq.Tests 8 | { 9 | public static class ReflectionExtensions 10 | { 11 | public static IEnumerable GetAccessors(this EventInfo @event, bool nonPublic = false) 12 | { 13 | yield return @event.GetAddMethod(nonPublic); 14 | yield return @event.GetRemoveMethod(nonPublic); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Moq/Language/ICallBase.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | using Moq.Language.Flow; 7 | 8 | namespace Moq.Language 9 | { 10 | /// 11 | /// Defines the CallBase verb. 12 | /// 13 | [EditorBrowsable(EditorBrowsableState.Never)] 14 | public interface ICallBase : IFluentInterface 15 | { 16 | /// 17 | /// Calls the real method of the object. 18 | /// 19 | ICallBaseResult CallBase(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /docs/_includes/head.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {{ page.title }} 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # sln, csproj files (and friends) are always CRLF, even on linux 2 | *.sln text eol=crlf 3 | *.proj text eol=crlf 4 | *.csproj text eol=crlf 5 | 6 | # These are windows specific files which we may as well ensure are 7 | # always crlf on checkout 8 | *.bat text eol=crlf 9 | *.cmd text eol=crlf 10 | 11 | # Opt in known filetypes to always normalize line endings on checkin 12 | # and always use native endings on checkout 13 | *.c text 14 | *.config text 15 | *.h text 16 | *.cs text 17 | *.md text 18 | *.tt text 19 | *.txt text 20 | 21 | # Some must always be checked out as lf so enforce that for those files 22 | # If these are not lf then bash/cygwin on windows will not be able to 23 | # excute the files 24 | *.sh text eol=lf -------------------------------------------------------------------------------- /docs/_includes/header.html: -------------------------------------------------------------------------------- 1 |
2 | 13 |
14 | -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 1 | changelog: 2 | exclude: 3 | labels: 4 | - bydesign 5 | - dependencies 6 | - duplicate 7 | - question 8 | - invalid 9 | - wontfix 10 | - need info 11 | - docs 12 | - techdebt 13 | authors: 14 | - devlooped-bot 15 | - dependabot 16 | - github-actions 17 | categories: 18 | - title: ✨ Implemented enhancements 19 | labels: 20 | - enhancement 21 | - title: 🐛 Fixed bugs 22 | labels: 23 | - bug 24 | - title: 📝 Documentation updates 25 | labels: 26 | - docs 27 | - title: 🔨 Other 28 | labels: 29 | - '*' 30 | exclude: 31 | labels: 32 | - dependencies 33 | -------------------------------------------------------------------------------- /.github/workflows/sponsor.yml: -------------------------------------------------------------------------------- 1 | name: sponsor 💜 2 | on: 3 | issues: 4 | types: [opened, edited, reopened] 5 | pull_request: 6 | types: [opened, edited, synchronize, reopened] 7 | 8 | jobs: 9 | sponsor: 10 | runs-on: ubuntu-latest 11 | continue-on-error: true 12 | env: 13 | token: ${{ secrets.GH_TOKEN }} 14 | if: ${{ !endsWith(github.event.sender.login, '[bot]') && !endsWith(github.event.sender.login, 'bot') }} 15 | steps: 16 | - name: 🤘 checkout 17 | if: env.token != '' 18 | uses: actions/checkout@v2 19 | 20 | - name: 💜 sponsor 21 | if: env.token != '' 22 | uses: devlooped/actions-sponsor@main 23 | with: 24 | token: ${{ env.token }} 25 | -------------------------------------------------------------------------------- /src/Moq/Range.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | /// 7 | /// Kind of range to use in a filter specified through 8 | /// . 9 | /// 10 | public enum Range 11 | { 12 | /// 13 | /// The range includes the to and 14 | /// from values. 15 | /// 16 | Inclusive, 17 | 18 | /// 19 | /// The range does not include the to and 20 | /// from values. 21 | /// 22 | Exclusive 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Moq.Tests/Async/AwaitableFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Tests.Async 5 | { 6 | using System.Threading.Tasks; 7 | 8 | using Moq.Async; 9 | 10 | using Xunit; 11 | 12 | public class AwaitableFixture 13 | { 14 | [Fact] 15 | public void TryGetResultRecursive_is_recursive() 16 | { 17 | const int expectedResult = 42; 18 | var obj = Task.FromResult(Task.FromResult(expectedResult)); 19 | var result = Awaitable.TryGetResultRecursive(obj); 20 | Assert.Equal(expectedResult, result); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Moq/Interception/IProxy.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Internals 7 | { 8 | /// Do not use. (Moq requires this interface so that methods can be set up on interface mocks.) 9 | // NOTE: This type is actually specific to our DynamicProxy implementation of `ProxyFactory`. 10 | // It is directly related to `InterfaceProxy`, see the comment there. 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface IProxy 13 | { 14 | /// 15 | object Interceptor { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Moq/Obsolete/MockExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | namespace Moq 8 | { 9 | static partial class MockExtensions 10 | { 11 | /// 12 | /// Resets all invocations recorded for this mock. 13 | /// 14 | /// The mock whose recorded invocations should be reset. 15 | [Obsolete("Use `mock.Invocations.Clear()` instead.")] 16 | [EditorBrowsable(EditorBrowsableState.Never)] 17 | public static void ResetCalls(this Mock mock) => mock.Invocations.Clear(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Moq.Tests.VisualBasic/Moq.Tests.VisualBasic.vbproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net472;net6.0 5 | True 6 | portable 7 | False 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Moq.Tests/RecordsFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using Xunit; 5 | 6 | namespace Moq.Tests 7 | { 8 | public class RecordsFixture 9 | { 10 | [Fact] 11 | public void Can_mock_EmptyRecord() 12 | { 13 | _ = new Mock().Object; 14 | } 15 | 16 | [Fact] 17 | public void Can_mock_DerivedEmptyRecord() 18 | { 19 | _ = new Mock().Object; 20 | } 21 | 22 | public record EmptyRecord 23 | { 24 | } 25 | 26 | public record DerivedEmptyRecord : EmptyRecord 27 | { 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Please see the documentation for all configuration options: 2 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 3 | 4 | version: 2 5 | updates: 6 | - package-ecosystem: nuget 7 | directory: / 8 | schedule: 9 | interval: daily 10 | groups: 11 | Azure: 12 | patterns: 13 | - "Azure*" 14 | - "Microsoft.Azure*" 15 | System: 16 | patterns: 17 | - "System*" 18 | Extensions: 19 | patterns: 20 | - "Microsoft.Extensions*" 21 | Web: 22 | patterns: 23 | - "Microsoft.AspNetCore*" 24 | Tests: 25 | patterns: 26 | - "Microsoft.NET.Tests*" 27 | - "xunit*" 28 | - "coverlet*" 29 | ThisAssembly: 30 | patterns: 31 | - "ThisAssembly*" 32 | ProtoBuf: 33 | patterns: 34 | - "protobuf-*" 35 | -------------------------------------------------------------------------------- /src/Moq.Tests/HidePropertyFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using Xunit; 5 | 6 | namespace Moq.Tests 7 | { 8 | public class HidePropertyFixture 9 | { 10 | public class A 11 | { 12 | public string Prop { get; } 13 | } 14 | 15 | public class B : A 16 | { 17 | public new virtual int Prop { get; } 18 | } 19 | 20 | public class C : B 21 | { 22 | } 23 | 24 | [Fact] 25 | public void SetupsDerivedProperty() 26 | { 27 | var mock = new Mock(); 28 | var value = 5; 29 | 30 | mock.Setup(m => m.Prop).Returns(value); 31 | 32 | Assert.Equal(value, mock.Object.Prop); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/IReturnsThrows.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface IReturnsThrows : IReturns, IThrows, IFluentInterface 13 | where TMock : class 14 | { 15 | } 16 | 17 | /// 18 | /// Implements the fluent API. 19 | /// 20 | [EditorBrowsable(EditorBrowsableState.Never)] 21 | public interface IReturnsThrowsGetter : IReturnsGetter, IThrows, IFluentInterface 22 | where TMock : class 23 | { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Directory.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | annotations 5 | Moq 6 | Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors 7 | $(MSBuildThisFileDirectory)Moq.snk 8 | true 9 | 00240000048000009400000006020000002400005253413100040000010001009f7a95086500f8f66d892174803850fed9c22225c2ccfff21f39c8af8abfa5415b1664efd0d8e0a6f7f2513b1c11659bd84723dc7900c3d481b833a73a2bcf1ed94c16c4be64d54352c86956c89930444e9ac15124d3693e3f029818e8410f167399d6b995324b635e95353ba97bfab856abbaeb9b40c9b160070c6325e22ddc 10 | 69f491c39445e920 11 | 12 | $(MSBuildThisFileDirectory)..\ 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Moq/Expressions/Visitors/Apply.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.Linq.Expressions; 5 | 6 | namespace Moq 7 | { 8 | static partial class ExpressionExtensions 9 | { 10 | /// 11 | /// Applies the specified to this expression tree. 12 | /// 13 | /// The to which should be applied. 14 | /// The that should be applied to . 15 | public static Expression Apply(this Expression expression, ExpressionVisitor visitor) 16 | { 17 | return visitor.Visit(expression); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Moq/Behavior.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal abstract class Behavior 10 | After: 11 | abstract class Behavior 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal abstract class Behavior 17 | After: 18 | abstract class Behavior 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal abstract class Behavior 24 | After: 25 | abstract class Behavior 26 | */ 27 | abstract class Behavior 28 | { 29 | protected Behavior() 30 | { 31 | } 32 | 33 | public abstract void Execute(Invocation invocation); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Moq/IMatcher.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | 9 | /* Unmerged change from project 'Moq(netstandard2.0)' 10 | Before: 11 | internal interface IMatcher 12 | After: 13 | interface IMatcher 14 | */ 15 | 16 | /* Unmerged change from project 'Moq(netstandard2.1)' 17 | Before: 18 | internal interface IMatcher 19 | After: 20 | interface IMatcher 21 | */ 22 | 23 | /* Unmerged change from project 'Moq(net6.0)' 24 | Before: 25 | internal interface IMatcher 26 | After: 27 | interface IMatcher 28 | */ 29 | interface IMatcher 30 | { 31 | bool Matches(object argument, Type parameterType); 32 | 33 | void SetupEvaluatedSuccessfully(object argument, Type parameterType); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Moq/Interception/Mock.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | partial class Mock : IInterceptor 7 | { 8 | void IInterceptor.Intercept(Invocation invocation) 9 | { 10 | if (HandleWellKnownMethods.Handle(invocation, this)) 11 | { 12 | return; 13 | } 14 | 15 | RecordInvocation.Handle(invocation, this); 16 | 17 | if (FindAndExecuteMatchingSetup.Handle(invocation, this)) 18 | { 19 | return; 20 | } 21 | 22 | if (HandleEventSubscription.Handle(invocation, this)) 23 | { 24 | return; 25 | } 26 | 27 | FailForStrictMock.Handle(invocation, this); 28 | 29 | Return.Handle(invocation, this); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Moq.Tests/Regressions/StreamFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.IO; 5 | 6 | using Xunit; 7 | 8 | namespace Moq.Tests 9 | { 10 | public class StreamFixture 11 | { 12 | [Fact] 13 | public void ShouldMockStream() 14 | { 15 | var mockStream = new Mock(); 16 | 17 | mockStream.Setup(stream => stream.Seek(0, SeekOrigin.Begin)).Returns(0L); 18 | 19 | var position = mockStream.Object.Seek(0, SeekOrigin.Begin); 20 | 21 | Assert.Equal(0, position); 22 | 23 | mockStream.Setup(stream => stream.Flush()); 24 | mockStream.Setup(stream => stream.SetLength(100)); 25 | 26 | mockStream.Object.Flush(); 27 | mockStream.Object.SetLength(100); 28 | 29 | mockStream.VerifyAll(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # Builds a final release version and pushes to nuget.org 2 | # whenever a release is published. 3 | # Requires: secrets.NUGET_API_KEY 4 | 5 | name: publish 6 | on: 7 | release: 8 | types: [released] 9 | 10 | env: 11 | DOTNET_NOLOGO: true 12 | Configuration: Release 13 | 14 | defaults: 15 | run: 16 | shell: bash 17 | 18 | jobs: 19 | publish: 20 | runs-on: windows-latest 21 | steps: 22 | - name: 🤘 checkout 23 | uses: actions/checkout@v2 24 | with: 25 | submodules: recursive 26 | fetch-depth: 0 27 | 28 | - name: 🙏 build 29 | run: dotnet build -m:1 -p:version=${GITHUB_REF#refs/*/v} 30 | 31 | - name: 🧪 test 32 | uses: ./.github/workflows/test 33 | 34 | - name: 📦 pack 35 | run: dotnet pack -m:1 -p:version=${GITHUB_REF#refs/*/v} 36 | 37 | - name: 🚀 nuget 38 | run: dotnet nuget push bin/*.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}} --skip-duplicate 39 | -------------------------------------------------------------------------------- /src/Moq/MockBehavior.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | /// 7 | /// Options to customize the behavior of the mock. 8 | /// 9 | public enum MockBehavior 10 | { 11 | /// 12 | /// Causes the mock to always throw 13 | /// an exception for invocations that don't have a 14 | /// corresponding setup. 15 | /// 16 | Strict, 17 | /// 18 | /// Will never throw exceptions, returning default 19 | /// values when necessary (null for reference types, 20 | /// zero for value types or empty enumerables and arrays). 21 | /// 22 | Loose, 23 | /// 24 | /// Default mock behavior, which equals . 25 | /// 26 | Default = Loose, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Moq/MockExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// Provides additional methods on mocks. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public static partial class MockExtensions 13 | { 14 | /// 15 | /// Resets this mock's state. This includes its setups, configured default return values, registered event handlers, and all recorded invocations. 16 | /// 17 | /// The mock whose state should be reset. 18 | public static void Reset(this Mock mock) 19 | { 20 | mock.ConfiguredDefaultValues.Clear(); 21 | mock.MutableSetups.Clear(); 22 | mock.EventHandlers.Clear(); 23 | mock.Invocations.Clear(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Moq/IMocked.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// Implemented by all generated mock object instances. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface IMocked : IMocked 13 | where T : class 14 | { 15 | /// 16 | /// Reference the Mock that contains this as the mock.Object value. 17 | /// 18 | new Mock Mock { get; } 19 | } 20 | 21 | /// 22 | /// Implemented by all generated mock object instances. 23 | /// 24 | [EditorBrowsable(EditorBrowsableState.Never)] 25 | public interface IMocked 26 | { 27 | /// 28 | /// Reference the Mock that contains this as the mock.Object value. 29 | /// 30 | Mock Mock { get; } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Moq/Switches.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// Represents a switch, or a combination of switches, that can be either enabled or disabled. 10 | /// When set via or , they determine how a mock will operate. 11 | /// 12 | [Flags] 13 | public enum Switches 14 | { 15 | /// 16 | /// The default set of switches. The switches covered by this enumeration value may change between different versions of Moq. 17 | /// 18 | Default = 0, 19 | 20 | /// 21 | /// When enabled, specifies that source file information should be collected for each setup. 22 | /// This results in more helpful error messages, but may affect performance. 23 | /// 24 | CollectDiagnosticFileInfoForSetups = 1 << 0, 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Moq/Language/GenericTypeParameters.tt: -------------------------------------------------------------------------------- 1 | <#+ 2 | private const int GenericTypeMax = 16; 3 | private const int GenericTypeMin = 5; 4 | 5 | private const string GenericTypeFormat = "T{0}"; 6 | private const string GenericArgumentFormat = "T{0} arg{0}"; 7 | 8 | private static readonly string[] ordinalNumbers = new[] { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth" }; 9 | private static readonly string[] wordNumbers = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen" }; 10 | 11 | public static string ConvertToWord(int number) 12 | { 13 | return wordNumbers[number - 1]; 14 | } 15 | 16 | public static string ConvertToOrdinal(int number) 17 | { 18 | return ordinalNumbers[number - 1]; 19 | } 20 | 21 | private static string GetGenericList(int typeCount, string format, string separator = ", ") 22 | { 23 | return string.Join(separator, Enumerable.Range(1, typeCount).Select(n => string.Format(format, n)).ToArray()); 24 | } 25 | #> -------------------------------------------------------------------------------- /src/Moq/Interception/IInterceptor.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal interface IInterceptor 10 | After: 11 | interface IInterceptor 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal interface IInterceptor 17 | After: 18 | interface IInterceptor 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal interface IInterceptor 24 | After: 25 | interface IInterceptor 26 | */ 27 | /// 28 | /// This role interface represents a 's ability to intercept method invocations for its . 29 | /// It is meant for use by . 30 | /// 31 | interface IInterceptor 32 | { 33 | void Intercept(Invocation invocation); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Moq/ITypeMatcher.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// Types that implement this interface represent a criterion against which generic type arguments are matched. 10 | /// 11 | /// To be used in combination with . 12 | /// 13 | /// 14 | public interface ITypeMatcher 15 | { 16 | /// 17 | /// Matches the provided type argument against the criterion represented by this type matcher. 18 | /// 19 | /// 20 | /// The generic type argument that should be matched. 21 | /// 22 | /// 23 | /// if the provided type argument matched the criterion represented by this instance; 24 | /// otherwise, . 25 | /// 26 | bool Matches(Type typeArgument); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.github/workflows/includes.yml: -------------------------------------------------------------------------------- 1 | name: +Mᐁ includes 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - 'main' 7 | paths: 8 | - '**.md' 9 | - '!changelog.md' 10 | 11 | jobs: 12 | includes: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: 🤖 defaults 16 | uses: devlooped/actions-bot@v1 17 | with: 18 | name: ${{ secrets.BOT_NAME }} 19 | email: ${{ secrets.BOT_EMAIL }} 20 | gh_token: ${{ secrets.GH_TOKEN }} 21 | github_token: ${{ secrets.GITHUB_TOKEN }} 22 | 23 | - name: 🤘 checkout 24 | uses: actions/checkout@v2 25 | with: 26 | token: ${{ env.GH_TOKEN }} 27 | 28 | - name: +Mᐁ includes 29 | uses: devlooped/actions-includes@v1 30 | 31 | - name: ✍ pull request 32 | uses: peter-evans/create-pull-request@v4 33 | with: 34 | base: main 35 | branch: markdown-includes 36 | delete-branch: true 37 | labels: docs 38 | author: ${{ env.BOT_AUTHOR }} 39 | committer: ${{ env.BOT_AUTHOR }} 40 | commit-message: +Mᐁ includes 41 | title: +Mᐁ includes 42 | body: +Mᐁ includes 43 | token: ${{ env.GH_TOKEN }} 44 | -------------------------------------------------------------------------------- /.github/workflows/changelog.yml: -------------------------------------------------------------------------------- 1 | name: changelog 2 | on: 3 | workflow_dispatch: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | changelog: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: 🤖 defaults 12 | uses: devlooped/actions-bot@v1 13 | with: 14 | name: ${{ secrets.BOT_NAME }} 15 | email: ${{ secrets.BOT_EMAIL }} 16 | gh_token: ${{ secrets.GH_TOKEN }} 17 | github_token: ${{ secrets.GITHUB_TOKEN }} 18 | 19 | - name: 🤘 checkout 20 | uses: actions/checkout@v2 21 | with: 22 | fetch-depth: 0 23 | ref: main 24 | token: ${{ env.GH_TOKEN }} 25 | 26 | - name: ⚙ ruby 27 | uses: ruby/setup-ruby@v1 28 | with: 29 | ruby-version: 3.0.3 30 | 31 | - name: ⚙ changelog 32 | run: | 33 | gem install github_changelog_generator 34 | github_changelog_generator --user ${GITHUB_REPOSITORY%/*} --project ${GITHUB_REPOSITORY##*/} --token $GH_TOKEN --o changelog.md --config-file .github/workflows/changelog.config 35 | 36 | - name: 🚀 changelog 37 | run: | 38 | git add changelog.md 39 | (git commit -m "🖉 Update changelog with ${GITHUB_REF#refs/*/}" && git push) || echo "Done" -------------------------------------------------------------------------------- /src/Moq/Protected/ProtectedExtension.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Protected 7 | { 8 | /// 9 | /// Enables the Protected() method on , 10 | /// allowing setups to be set for protected members by using their 11 | /// name as a string, rather than strong-typing them which is not possible 12 | /// due to their visibility. 13 | /// 14 | [EditorBrowsable(EditorBrowsableState.Never)] 15 | public static class ProtectedExtension 16 | { 17 | /// 18 | /// Enable protected setups for the mock. 19 | /// 20 | /// Mocked object type. Typically omitted as it can be inferred from the mock instance. 21 | /// The mock to set the protected setups on. 22 | public static IProtectedMock Protected(this Mock mock) 23 | where T : class 24 | { 25 | Guard.NotNull(mock, nameof(mock)); 26 | 27 | return new ProtectedMock(mock); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Moq/Language/ICallbackSetter.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | using Moq.Language.Flow; 8 | 9 | namespace Moq.Language 10 | { 11 | /// 12 | /// Defines the Callback verb for property setter setups. 13 | /// 14 | /// Type of the property. 15 | [EditorBrowsable(EditorBrowsableState.Never)] 16 | public interface ICallbackSetter : IFluentInterface 17 | { 18 | /// 19 | /// Specifies a callback to invoke when the property is set that receives the 20 | /// property value being set. 21 | /// 22 | /// Callback method to invoke. 23 | /// 24 | /// Invokes the given callback with the property value being set. 25 | /// 26 | /// mock.SetupSet(x => x.Suspended) 27 | /// .Callback((bool state) => Console.WriteLine(state)); 28 | /// 29 | /// 30 | ICallbackResult Callback(Action action); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Moq/InvocationFunc.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// A delegate-like type for use with `setup.Returns` which instructs the `Returns` verb 10 | /// to provide the callback with the current , instead of 11 | /// with a list of arguments. 12 | /// 13 | /// This type is useful in scenarios involving generic type argument matchers (such as 14 | /// ) as allows the discovery of both 15 | /// arguments and type arguments. 16 | /// 17 | /// 18 | public readonly struct InvocationFunc 19 | { 20 | internal readonly Func Func; 21 | 22 | /// 23 | /// Initializes a new instance of the type. 24 | /// 25 | /// The delegate that should be wrapped by this instance. 26 | public InvocationFunc(Func func) 27 | { 28 | this.Func = func; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Moq/InvocationAction.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// A delegate-like type for use with `setup.Callback` which instructs the `Callback` verb 10 | /// to provide the callback with the current , instead of 11 | /// with a list of arguments. 12 | /// 13 | /// This type is useful in scenarios involving generic type argument matchers (such as 14 | /// ) as allows the discovery of both 15 | /// arguments and type arguments. 16 | /// 17 | /// 18 | public readonly struct InvocationAction 19 | { 20 | internal readonly Action Action; 21 | 22 | /// 23 | /// Initializes a new instance of the type. 24 | /// 25 | /// The delegate that should be wrapped by this instance. 26 | public InvocationAction(Action action) 27 | { 28 | this.Action = action; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Moq/Async/IAwaitableFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq.Expressions; 7 | 8 | namespace Moq.Async 9 | { 10 | 11 | /* Unmerged change from project 'Moq(netstandard2.0)' 12 | Before: 13 | internal interface IAwaitableFactory 14 | After: 15 | interface IAwaitableFactory 16 | */ 17 | 18 | /* Unmerged change from project 'Moq(netstandard2.1)' 19 | Before: 20 | internal interface IAwaitableFactory 21 | After: 22 | interface IAwaitableFactory 23 | */ 24 | 25 | /* Unmerged change from project 'Moq(net6.0)' 26 | Before: 27 | internal interface IAwaitableFactory 28 | After: 29 | interface IAwaitableFactory 30 | */ 31 | interface IAwaitableFactory 32 | { 33 | Type ResultType { get; } 34 | 35 | object CreateCompleted(object result = null); 36 | 37 | object CreateFaulted(Exception exception); 38 | 39 | object CreateFaulted(IEnumerable exceptions); 40 | 41 | Expression CreateResultExpression(Expression awaitableExpression); 42 | 43 | bool TryGetResult(object awaitable, out object result); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Moq/IMock.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq 5 | { 6 | /// 7 | /// Covariant interface for such that casts between IMock<Employee> to IMock<Person> 8 | /// are possible. Only covers the covariant members of . 9 | /// 10 | public interface IMock where T : class 11 | { 12 | /// 13 | /// Exposes the mocked object instance. 14 | /// 15 | T Object { get; } 16 | 17 | /// 18 | /// Behavior of the mock, according to the value set in the constructor. 19 | /// 20 | MockBehavior Behavior { get; } 21 | 22 | /// 23 | /// Whether the base member virtual implementation will be called for mocked classes if no setup is matched. 24 | /// Defaults to . 25 | /// 26 | bool CallBase { get; set; } 27 | 28 | /// 29 | /// Specifies the behavior to use when returning default values for unexpected invocations on loose mocks. 30 | /// 31 | DefaultValue DefaultValue { get; set; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Moq.Tests.FSharpTypes/Moq.Tests.FSharpTypes.fsproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net472;net6.0 5 | True 6 | portable 7 | False 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/Moq/MethodSetup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.Linq.Expressions; 5 | using System.Reflection; 6 | 7 | namespace Moq 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal abstract class MethodSetup : Setup 13 | After: 14 | abstract class MethodSetup : Setup 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal abstract class MethodSetup : Setup 20 | After: 21 | abstract class MethodSetup : Setup 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal abstract class MethodSetup : Setup 27 | After: 28 | abstract class MethodSetup : Setup 29 | */ 30 | /// 31 | /// Abstract base class for setups that target a single, specific method. 32 | /// 33 | abstract class MethodSetup : Setup 34 | { 35 | protected MethodSetup(Expression originalExpression, Mock mock, MethodExpectation expectation) 36 | : base(originalExpression, mock, expectation) 37 | { 38 | } 39 | 40 | public MethodInfo Method => ((MethodExpectation)this.Expectation).Method; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Moq/Language/ICallbackGetter.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | using Moq.Language.Flow; 8 | 9 | namespace Moq.Language 10 | { 11 | /// 12 | /// Defines the Callback verb for property getter setups. 13 | /// 14 | /// 15 | /// Mocked type. 16 | /// Type of the property. 17 | [EditorBrowsable(EditorBrowsableState.Never)] 18 | public interface ICallbackGetter : IFluentInterface 19 | where TMock : class 20 | { 21 | /// 22 | /// Specifies a callback to invoke when the property is retrieved. 23 | /// 24 | /// Callback method to invoke. 25 | /// 26 | /// Invokes the given callback with the property value being set. 27 | /// 28 | /// mock.SetupGet(x => x.Suspended) 29 | /// .Callback(() => called = true) 30 | /// .Returns(true); 31 | /// 32 | /// 33 | IReturnsThrowsGetter Callback(Action action); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Moq/Interception/ProxyFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Reflection; 6 | 7 | namespace Moq 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal abstract class ProxyFactory 13 | After: 14 | abstract class ProxyFactory 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal abstract class ProxyFactory 20 | After: 21 | abstract class ProxyFactory 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal abstract class ProxyFactory 27 | After: 28 | abstract class ProxyFactory 29 | */ 30 | abstract class ProxyFactory 31 | { 32 | /// 33 | /// Gets the global instance used by Moq. 34 | /// 35 | public static ProxyFactory Instance { get; } = new CastleProxyFactory(); 36 | 37 | public abstract object CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, object[] arguments); 38 | 39 | public abstract bool IsMethodVisible(MethodInfo method, out string messageIfNotVisible); 40 | 41 | public abstract bool IsTypeVisible(Type type); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Moq.Tests/CaptureMatchFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using Xunit; 5 | 6 | namespace Moq.Tests 7 | { 8 | public class CaptureMatchFixture 9 | { 10 | [Fact] 11 | public void CanRunCaptureCallback() 12 | { 13 | var capturedValue = string.Empty; 14 | var captureMatch = new CaptureMatch(s => capturedValue = s); 15 | 16 | var mock = new Mock(); 17 | mock.Setup(x => x.DoSomething(Capture.With(captureMatch))); 18 | 19 | mock.Object.DoSomething("Hello!"); 20 | 21 | Assert.Equal("Hello!", capturedValue); 22 | } 23 | 24 | [Fact] 25 | public void CanRunCaptureCallbackWithPredicate() 26 | { 27 | var capturedValue = string.Empty; 28 | var captureMatch = new CaptureMatch(s => capturedValue += s, s => s.StartsWith("W")); 29 | 30 | var mock = new Mock(); 31 | mock.Setup(x => x.DoSomething(Capture.With(captureMatch))); 32 | 33 | mock.Object.DoSomething("Hello!"); 34 | mock.Object.DoSomething("World!"); 35 | 36 | Assert.Equal("World!", capturedValue); 37 | } 38 | 39 | public interface IFoo 40 | { 41 | void DoSomething(string item); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Moq/Obsolete/SequenceExtensions.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | using System.Linq.Expressions; 7 | 8 | using Moq.Language; 9 | 10 | namespace Moq 11 | { 12 | partial class SequenceExtensions 13 | { 14 | /// 15 | /// Return a sequence of values, once per call. 16 | /// 17 | [EditorBrowsable(EditorBrowsableState.Never)] 18 | [Obsolete("Please use instance method Mock.SetupSequence instead.")] 19 | public static ISetupSequentialResult SetupSequence( 20 | this Mock mock, 21 | Expression> expression) 22 | where TMock : class 23 | { 24 | return mock.SetupSequence(expression); 25 | } 26 | 27 | /// 28 | /// Performs a sequence of actions, one per call. 29 | /// 30 | [EditorBrowsable(EditorBrowsableState.Never)] 31 | [Obsolete("Please use instance method Mock.SetupSequence instead.")] 32 | public static ISetupSequentialAction SetupSequence(this Mock mock, Expression> expression) 33 | where TMock : class 34 | { 35 | return mock.SetupSequence(expression); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Moq.Tests/CallBaseFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using Xunit; 5 | 6 | namespace Moq.Tests 7 | { 8 | public class CallBaseFixture 9 | { 10 | [Fact] 11 | public void Void_method_in_base_class_not_called_when_CallBase_not_specified() 12 | { 13 | var mock = new Mock(); 14 | 15 | mock.Object.Method(); 16 | 17 | Assert.False(mock.Object.MethodInvoked); 18 | } 19 | 20 | [Fact] 21 | public void Void_method_in_base_class_called_when_CallBase_specified_on_mock() 22 | { 23 | var mock = new Mock() { CallBase = true }; 24 | 25 | mock.Object.Method(); 26 | 27 | Assert.True(mock.Object.MethodInvoked); 28 | } 29 | 30 | [Fact] 31 | public void Void_method_in_base_class_called_when_CallBase_specified_on_setup() 32 | { 33 | var mock = new Mock(); 34 | mock.Setup(m => m.Method()).CallBase(); 35 | 36 | mock.Object.Method(); 37 | 38 | Assert.True(mock.Object.MethodInvoked); 39 | } 40 | 41 | public class Base 42 | { 43 | public bool MethodInvoked { get; private set; } 44 | 45 | public virtual void Method() 46 | { 47 | this.MethodInvoked = true; 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Moq/Language/IRaise.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="false" hostspecific="false" language="C#" #> 2 | <#@ output extension=".Generated.cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Import Namespace="System.Linq" #> 5 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 6 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 7 | 8 | using System; 9 | using System.ComponentModel; 10 | 11 | namespace Moq.Language 12 | { 13 | partial interface IRaise 14 | { <# 15 | for (var typeCount = 1; typeCount <= GenericTypeMax; typeCount++) 16 | { 17 | var typeList = GetGenericList(typeCount, GenericTypeFormat); 18 | #> 19 | 20 | /// 21 | /// Specifies the event that will be raised when the setup is matched. 22 | /// 23 | /// The expression that represents an event attach or detach action. 24 | /// The function that will build the 25 | /// to pass when raising the event. 26 | <# 27 | for (var typeIndex = 1; typeIndex <= typeCount; typeIndex++) 28 | { 29 | #> 30 | /// The type of the <#= ConvertToOrdinal(typeIndex) #> argument received by the expected invocation. 31 | <# 32 | } 33 | #> 34 | /// 35 | IVerifies Raises<<#= typeList #>>(Action eventExpression, Func<<#= typeList #>, EventArgs> func); 36 | <# 37 | } 38 | #> 39 | } 40 | } 41 | <#@ Include File="GenericTypeParameters.tt" #> 42 | -------------------------------------------------------------------------------- /.github/workflows/test/action.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | description: runs dotnet tests with retry 3 | runs: 4 | using: "composite" 5 | steps: 6 | - name: 🧪 test 7 | shell: bash --noprofile --norc {0} 8 | env: 9 | LC_ALL: en_US.utf8 10 | run: | 11 | [ -f .bash_profile ] && source .bash_profile 12 | counter=0 13 | exitcode=0 14 | reset="\e[0m" 15 | warn="\e[0;33m" 16 | while [ $counter -lt 6 ] 17 | do 18 | # run test and forward output also to a file in addition to stdout (tee command) 19 | if [ $filter ] 20 | then 21 | echo -e "${warn}Retry $counter for $filter ${reset}" 22 | dotnet test --no-build -m:1 --blame-hang --blame-hang-timeout 5m --filter=$filter | tee ./output.log 23 | else 24 | dotnet test --no-build -m:1 --blame-hang --blame-hang-timeout 5m | tee ./output.log 25 | fi 26 | # capture dotnet test exit status, different from tee 27 | exitcode=${PIPESTATUS[0]} 28 | if [ $exitcode == 0 ] 29 | then 30 | exit 0 31 | fi 32 | # cat output, get failed test names, remove trailing whitespace, sort+dedupe, join as FQN~TEST with |, remove trailing |. 33 | filter=$(cat ./output.log | grep -o -P '(?<=\sFailed\s)[\w\._]*' | sed 's/ *$//g' | sort -u | awk 'BEGIN { ORS="|" } { print("FullyQualifiedName~" $0) }' | grep -o -P '.*(?=\|$)') 34 | ((counter++)) 35 | done 36 | exit $exitcode 37 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/ISetup.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | /// 9 | /// Implements the fluent API. 10 | /// 11 | [EditorBrowsable(EditorBrowsableState.Never)] 12 | public interface ISetup : ICallback, ICallbackResult, IRaise, IVerifies, IFluentInterface 13 | where TMock : class 14 | { 15 | } 16 | 17 | /// 18 | /// Implements the fluent API. 19 | /// 20 | [EditorBrowsable(EditorBrowsableState.Never)] 21 | public interface ISetup : ICallback, IReturnsThrows, IVerifies, IFluentInterface 22 | where TMock : class 23 | { 24 | } 25 | 26 | /// 27 | /// Implements the fluent API. 28 | /// 29 | [EditorBrowsable(EditorBrowsableState.Never)] 30 | public interface ISetupGetter : ICallbackGetter, IReturnsThrowsGetter, IVerifies, IFluentInterface 31 | where TMock : class 32 | { 33 | } 34 | 35 | /// 36 | /// Implements the fluent API. 37 | /// 38 | [EditorBrowsable(EditorBrowsableState.Never)] 39 | public interface ISetupSetter : ICallbackSetter, ICallbackResult, IRaise, IVerifies, IFluentInterface 40 | where TMock : class 41 | { 42 | } 43 | } -------------------------------------------------------------------------------- /src/Moq/IInvocation.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Reflection; 7 | 8 | namespace Moq 9 | { 10 | /// 11 | /// Provides information about an invocation of a mock object. 12 | /// 13 | public interface IInvocation 14 | { 15 | /// 16 | /// Gets the method of the invocation. 17 | /// 18 | MethodInfo Method { get; } 19 | 20 | /// 21 | /// Gets the arguments of the invocation. 22 | /// 23 | IReadOnlyList Arguments { get; } 24 | 25 | /// 26 | /// Gets the setup that matched this invocation (or if there was no matching setup). 27 | /// 28 | ISetup MatchingSetup { get; } 29 | 30 | /// 31 | /// Gets whether this invocation was successfully verified by any of the various `Verify` methods. 32 | /// 33 | bool IsVerified { get; } 34 | 35 | /// 36 | /// The value being returned for a non-void method if no exception was thrown. 37 | /// 38 | object ReturnValue { get; } 39 | 40 | /// 41 | /// Optional exception if the method invocation results in an exception being thrown. 42 | /// 43 | Exception Exception { get; } 44 | } 45 | } -------------------------------------------------------------------------------- /src/Moq/Behaviors/NoOp.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Behaviors 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal sealed class NoOp : Behavior 10 | After: 11 | sealed class NoOp : Behavior 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal sealed class NoOp : Behavior 17 | After: 18 | sealed class NoOp : Behavior 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal sealed class NoOp : Behavior 24 | After: 25 | sealed class NoOp : Behavior 26 | */ 27 | sealed class NoOp : Behavior 28 | { 29 | public static readonly NoOp Instance = new NoOp(); 30 | 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private NoOp() 35 | After: 36 | NoOp() 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private NoOp() 42 | After: 43 | NoOp() 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private NoOp() 49 | After: 50 | NoOp() 51 | */ 52 | NoOp() 53 | { 54 | } 55 | 56 | public override void Execute(Invocation invocation) 57 | { 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, 4 | and Contributors. All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | * Neither the names of the copyright holders nor the names of its 17 | contributors may be used to endorse or promote products derived from this 18 | software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /src/Moq/Language/Flow/SetterSetupPhrase.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq.Language.Flow 7 | { 8 | 9 | /* Unmerged change from project 'Moq(netstandard2.0)' 10 | Before: 11 | internal class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 12 | After: 13 | class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 14 | */ 15 | 16 | /* Unmerged change from project 'Moq(netstandard2.1)' 17 | Before: 18 | internal class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 19 | After: 20 | class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 21 | */ 22 | 23 | /* Unmerged change from project 'Moq(net6.0)' 24 | Before: 25 | internal class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 26 | After: 27 | class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 28 | */ 29 | class SetterSetupPhrase : VoidSetupPhrase, ISetupSetter where T : class 30 | { 31 | public SetterSetupPhrase(MethodCall setup) : base(setup) 32 | { 33 | } 34 | 35 | public ICallbackResult Callback(Action callback) 36 | { 37 | this.Setup.SetCallbackBehavior(callback); 38 | return this; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Moq.Tests/IsValueTypeFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | using Xunit; 7 | 8 | namespace Moq.Tests 9 | { 10 | public class IsValueTypeFixture 11 | { 12 | [Fact] 13 | public void It_IsValueType_works_for_unconstrained_type_parameter() 14 | { 15 | var mock = new Mock(); 16 | 17 | mock.Object.Unconstrained(1); // should match 18 | mock.Object.Unconstrained(true); // should match 19 | mock.Object.Unconstrained(""); 20 | mock.Object.Unconstrained(new Exception()); 21 | mock.Object.Unconstrained(3.141f); // should match 22 | 23 | mock.Verify(m => m.Unconstrained(It.IsAny()), Times.Exactly(3)); 24 | } 25 | 26 | [Fact] 27 | public void It_IsValueType_works_for_constrained_type_parameter() 28 | { 29 | var mock = new Mock(); 30 | 31 | mock.Object.Constrained(1); // should match 32 | mock.Object.Constrained(true); // should match 33 | //mock.Object.Constrained(""); 34 | //mock.Object.Constrained(new Exception()); 35 | mock.Object.Constrained(3.141f); // should match 36 | 37 | mock.Verify(m => m.Constrained(It.IsAny()), Times.Exactly(3)); 38 | } 39 | 40 | public interface IX 41 | { 42 | void Unconstrained(T arg); 43 | void Constrained(T arg) where T : struct; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Moq.Tests/IsSubtypeFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.IO; 7 | 8 | using Xunit; 9 | 10 | namespace Moq.Tests 11 | { 12 | public class IsSubtypeFixture 13 | { 14 | [Fact] 15 | public void It_IsSubtype_works_for_base_type_relationships() 16 | { 17 | var mock = new Mock(); 18 | 19 | mock.Object.Method(); // should match 20 | mock.Object.Method(); 21 | mock.Object.Method(); // should match 22 | mock.Object.Method(); 23 | mock.Object.Method(); // should match 24 | mock.Object.Method(); 25 | mock.Object.Method(); 26 | 27 | mock.Verify(m => m.Method>(), Times.Exactly(3)); 28 | } 29 | 30 | [Fact] 31 | public void It_IsSubtype_works_for_interface_implementation_relationships() 32 | { 33 | var mock = new Mock(); 34 | 35 | mock.Object.Method(); // should match 36 | mock.Object.Method(); 37 | mock.Object.Method>(); // should match 38 | mock.Object.Method(); 39 | mock.Object.Method(); 40 | 41 | mock.Verify(m => m.Method>(), Times.Exactly(2)); 42 | } 43 | 44 | public interface IX 45 | { 46 | void Method(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Moq/Async/Awaitable.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Async 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal static class Awaitable 10 | After: 11 | static class Awaitable 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal static class Awaitable 17 | After: 18 | static class Awaitable 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal static class Awaitable 24 | After: 25 | static class Awaitable 26 | */ 27 | static class Awaitable 28 | { 29 | /// 30 | /// Recursively gets the result of (i.e. "unwraps") completed awaitables 31 | /// until a value is found that isn't a successfully completed awaitable. 32 | /// 33 | /// 34 | /// As an example, given := Task.FromResult(Task.FromResult(42)), 35 | /// this method will return 42. 36 | /// 37 | /// The (possibly awaitable) object to be "unwrapped". 38 | public static object TryGetResultRecursive(object obj) 39 | { 40 | if (obj != null 41 | && AwaitableFactory.TryGet(obj.GetType()) is { } awaitableFactory 42 | && awaitableFactory.TryGetResult(obj, out var result)) 43 | { 44 | return Awaitable.TryGetResultRecursive(result); 45 | } 46 | 47 | return obj; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Moq/Obsolete/IOccurrence.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | namespace Moq.Language 8 | { 9 | /// 10 | /// Defines occurrence members to constraint setups. 11 | /// 12 | [EditorBrowsable(EditorBrowsableState.Never)] 13 | public interface IOccurrence : IFluentInterface 14 | { 15 | /// 16 | /// The expected invocation can happen at most once. 17 | /// 18 | /// 19 | /// 20 | /// var mock = new Mock<ICommand>(); 21 | /// mock.Setup(foo => foo.Execute("ping")) 22 | /// .AtMostOnce(); 23 | /// 24 | /// 25 | [Obsolete("Use 'mock.Verify(call, Times.AtMostOnce)' or 'setup.Verifiable(Times.AtMostOnce)' instead.")] 26 | [EditorBrowsable(EditorBrowsableState.Never)] 27 | IVerifies AtMostOnce(); 28 | /// 29 | /// The expected invocation can happen at most specified number of times. 30 | /// 31 | /// The number of times to accept calls. 32 | /// 33 | /// 34 | /// var mock = new Mock<ICommand>(); 35 | /// mock.Setup(foo => foo.Execute("ping")) 36 | /// .AtMost( 5 ); 37 | /// 38 | /// 39 | [Obsolete("Use 'mock.Verify(call, Times.AtMost(callCount))' or 'setup.Verifiable(Times.AtMost(callCount))' instead.")] 40 | [EditorBrowsable(EditorBrowsableState.Never)] 41 | IVerifies AtMost(int callCount); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Moq/MatchExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq.Expressions; 6 | 7 | namespace Moq 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class MatchExpression : Expression 13 | After: 14 | sealed class MatchExpression : Expression 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class MatchExpression : Expression 20 | After: 21 | sealed class MatchExpression : Expression 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class MatchExpression : Expression 27 | After: 28 | sealed class MatchExpression : Expression 29 | */ 30 | sealed class MatchExpression : Expression 31 | { 32 | public readonly Match Match; 33 | 34 | public MatchExpression(Match match) 35 | { 36 | this.Match = match; 37 | } 38 | 39 | public override ExpressionType NodeType => ExpressionType.Extension; 40 | 41 | public override Type Type => this.Match.RenderExpression.Type; 42 | 43 | // This node type is irreducible in order to prevent compilation. 44 | // The best possible reduction would involve `RenderExpression`, 45 | // which isn't intended to be used for that purpose. 46 | public override bool CanReduce => false; 47 | 48 | protected override Expression VisitChildren(ExpressionVisitor visitor) => this; 49 | 50 | public override string ToString() => this.Match.RenderExpression.ToString(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/ReturnBase.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Behaviors 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal sealed class ReturnBase : Behavior 10 | After: 11 | sealed class ReturnBase : Behavior 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal sealed class ReturnBase : Behavior 17 | After: 18 | sealed class ReturnBase : Behavior 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal sealed class ReturnBase : Behavior 24 | After: 25 | sealed class ReturnBase : Behavior 26 | */ 27 | sealed class ReturnBase : Behavior 28 | { 29 | public static readonly ReturnBase Instance = new ReturnBase(); 30 | 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private ReturnBase() 35 | After: 36 | ReturnBase() 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private ReturnBase() 42 | After: 43 | ReturnBase() 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private ReturnBase() 49 | After: 50 | ReturnBase() 51 | */ 52 | ReturnBase() 53 | { 54 | } 55 | 56 | public override void Execute(Invocation invocation) 57 | { 58 | invocation.ReturnValue = invocation.CallBase(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Moq/Language/ISetupSequentialResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | namespace Moq.Language 8 | { 9 | /// 10 | /// Language for ReturnSequence 11 | /// 12 | [EditorBrowsable(EditorBrowsableState.Never)] 13 | public interface ISetupSequentialResult 14 | { 15 | // would be nice to Mixin somehow the IReturn and IThrows with 16 | // another ReturnType 17 | 18 | /// 19 | /// Returns value 20 | /// 21 | ISetupSequentialResult Returns(TResult value); 22 | 23 | /// 24 | /// Uses delegate to get return value 25 | /// 26 | /// The function that will calculate the return value. 27 | ISetupSequentialResult Returns(Func valueFunction); 28 | 29 | /// 30 | /// Throws an exception 31 | /// 32 | ISetupSequentialResult Throws(Exception exception); 33 | 34 | /// 35 | /// Throws an exception 36 | /// 37 | ISetupSequentialResult Throws() where TException : Exception, new(); 38 | 39 | /// 40 | /// Uses delegate to throws an exception 41 | /// 42 | ISetupSequentialResult Throws(Func exceptionFunction) where TException : Exception; 43 | 44 | /// 45 | /// Calls original method 46 | /// 47 | ISetupSequentialResult CallBase(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/ReturnValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | namespace Moq.Behaviors 5 | { 6 | 7 | /* Unmerged change from project 'Moq(netstandard2.0)' 8 | Before: 9 | internal sealed class ReturnValue : Behavior 10 | After: 11 | sealed class ReturnValue : Behavior 12 | */ 13 | 14 | /* Unmerged change from project 'Moq(netstandard2.1)' 15 | Before: 16 | internal sealed class ReturnValue : Behavior 17 | After: 18 | sealed class ReturnValue : Behavior 19 | */ 20 | 21 | /* Unmerged change from project 'Moq(net6.0)' 22 | Before: 23 | internal sealed class ReturnValue : Behavior 24 | After: 25 | sealed class ReturnValue : Behavior 26 | */ 27 | sealed class ReturnValue : Behavior 28 | 29 | /* Unmerged change from project 'Moq(netstandard2.0)' 30 | Before: 31 | private readonly object value; 32 | After: 33 | readonly object value; 34 | */ 35 | 36 | /* Unmerged change from project 'Moq(netstandard2.1)' 37 | Before: 38 | private readonly object value; 39 | After: 40 | readonly object value; 41 | */ 42 | 43 | /* Unmerged change from project 'Moq(net6.0)' 44 | Before: 45 | private readonly object value; 46 | After: 47 | readonly object value; 48 | */ 49 | { 50 | readonly object value; 51 | 52 | public ReturnValue(object value) 53 | { 54 | this.value = value; 55 | } 56 | 57 | public object Value => this.value; 58 | 59 | public override void Execute(Invocation invocation) 60 | { 61 | invocation.ReturnValue = this.value; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Moq/DefaultValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System.ComponentModel; 5 | 6 | namespace Moq 7 | { 8 | /// 9 | /// Determines the way default values are generated 10 | /// calculated for loose mocks. 11 | /// 12 | public enum DefaultValue 13 | { 14 | /// 15 | /// Default behavior, which generates empty values for 16 | /// value types (i.e. default(int)), empty array and 17 | /// enumerables, and nulls for all other reference types. 18 | /// 19 | Empty, 20 | 21 | /// 22 | /// Whenever the default value generated by 23 | /// is null, replaces this value with a mock (if the type 24 | /// can be mocked). 25 | /// 26 | /// 27 | /// For sealed classes, a null value will be generated. 28 | /// 29 | Mock, 30 | 31 | /// 32 | /// 33 | /// All default value generation strategies other than or 34 | /// are represented by this enumeration value. 35 | /// 36 | /// 37 | /// Do not set (nor ) to this value. 38 | /// If you want to set up a custom default value generation strategy, set 39 | /// or instead. 40 | /// 41 | /// 42 | /// 43 | /// 44 | [EditorBrowsable(EditorBrowsableState.Advanced)] 45 | Custom 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Moq.Tests.ComTypes/ComTypes.idl: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | // See the `README.md` in the same directory as this file for instructions on 5 | // how to build `Moq.Tests.ComTypes` from these IDL definitions. 6 | 7 | // The below IDL definitions are based on information on Microsoft Docs, found 8 | // under .NET Framework 4 -> .NET Framework Advanced Development -> Interoperability 9 | // -> Advanced COM Interoperability -> Type Library to Assembly Conversion Summary 10 | // -> Imported Member Conversion: 11 | // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/k639e386(v%3dvs.100) 12 | 13 | [uuid("2d5e9d51-b50d-4bb3-a2fd-193681186ab7")] 14 | library ComTypes 15 | { 16 | importlib("stdole32.tlb"); 17 | 18 | [uuid("26049007-4f94-41ef-94f0-efc7b15d41c5")] 19 | interface IButton : IUnknown 20 | { 21 | } 22 | 23 | [uuid("c223276a-752b-4f16-bdfc-094bc453559f")] 24 | interface IButtonEvents : IUnknown 25 | { 26 | HRESULT Click(); 27 | } 28 | 29 | [uuid("aa7236e7-e655-497d-a700-79a17efcd734")] 30 | coclass Button 31 | { 32 | [default] interface IButton; 33 | [default, source] interface IButtonEvents; 34 | } 35 | 36 | [uuid("5ee16909-c4c7-49dc-8808-219078e5d3ba")] 37 | interface IHasIndexer : IUnknown 38 | { 39 | [propget, bindable, defaultbind] HRESULT Item([in] long index, [out, retval] long *value); 40 | [propput, bindable, defaultbind] HRESULT Item([in] long index, [in] long value); 41 | } 42 | 43 | [uuid("62a88a92-d8e5-45f7-bb7f-668a74c58749")] 44 | interface IHasProperty : IUnknown 45 | { 46 | [propget] HRESULT Property([out, retval] long *value); 47 | [propput] HRESULT Property([in] long value); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /docs/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | addressable (2.8.0) 5 | public_suffix (>= 2.0.2, < 5.0) 6 | colorator (1.1.0) 7 | concurrent-ruby (1.1.9) 8 | em-websocket (0.5.2) 9 | eventmachine (>= 0.12.9) 10 | http_parser.rb (~> 0.6.0) 11 | eventmachine (1.2.7) 12 | ffi (1.15.3) 13 | forwardable-extended (2.6.0) 14 | http_parser.rb (0.6.0) 15 | i18n (1.8.10) 16 | concurrent-ruby (~> 1.0) 17 | jekyll (4.2.0) 18 | addressable (~> 2.4) 19 | colorator (~> 1.0) 20 | em-websocket (~> 0.5) 21 | i18n (~> 1.0) 22 | jekyll-sass-converter (~> 2.0) 23 | jekyll-watch (~> 2.0) 24 | kramdown (~> 2.3) 25 | kramdown-parser-gfm (~> 1.0) 26 | liquid (~> 4.0) 27 | mercenary (~> 0.4.0) 28 | pathutil (~> 0.9) 29 | rouge (~> 3.0) 30 | safe_yaml (~> 1.0) 31 | terminal-table (~> 2.0) 32 | jekyll-relative-links (0.6.1) 33 | jekyll (>= 3.3, < 5.0) 34 | jekyll-sass-converter (2.1.0) 35 | sassc (> 2.0.1, < 3.0) 36 | jekyll-watch (2.2.1) 37 | listen (~> 3.0) 38 | kramdown (2.3.1) 39 | rexml 40 | kramdown-parser-gfm (1.1.0) 41 | kramdown (~> 2.0) 42 | liquid (4.0.3) 43 | listen (3.7.0) 44 | rb-fsevent (~> 0.10, >= 0.10.3) 45 | rb-inotify (~> 0.9, >= 0.9.10) 46 | mercenary (0.4.0) 47 | pathutil (0.16.2) 48 | forwardable-extended (~> 2.6) 49 | public_suffix (4.0.6) 50 | rb-fsevent (0.11.0) 51 | rb-inotify (0.10.1) 52 | ffi (~> 1.0) 53 | rexml (3.2.5) 54 | rouge (3.26.0) 55 | safe_yaml (1.0.5) 56 | sassc (2.4.0) 57 | ffi (~> 1.9) 58 | terminal-table (2.0.0) 59 | unicode-display_width (~> 1.1, >= 1.1.1) 60 | unicode-display_width (1.7.0) 61 | 62 | PLATFORMS 63 | x86_64-linux-musl 64 | 65 | DEPENDENCIES 66 | jekyll-relative-links 67 | 68 | BUNDLED WITH 69 | 2.2.2 70 | -------------------------------------------------------------------------------- /src/Moq/Matchers/AnyMatcher.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | 7 | namespace Moq.Matchers 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class AnyMatcher : IMatcher 13 | After: 14 | sealed class AnyMatcher : IMatcher 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class AnyMatcher : IMatcher 20 | After: 21 | sealed class AnyMatcher : IMatcher 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class AnyMatcher : IMatcher 27 | After: 28 | sealed class AnyMatcher : IMatcher 29 | */ 30 | sealed class AnyMatcher : IMatcher 31 | { 32 | public static AnyMatcher Instance { get; } = new AnyMatcher(); 33 | 34 | 35 | /* Unmerged change from project 'Moq(netstandard2.0)' 36 | Before: 37 | private AnyMatcher() 38 | After: 39 | AnyMatcher() 40 | */ 41 | 42 | /* Unmerged change from project 'Moq(netstandard2.1)' 43 | Before: 44 | private AnyMatcher() 45 | After: 46 | AnyMatcher() 47 | */ 48 | 49 | /* Unmerged change from project 'Moq(net6.0)' 50 | Before: 51 | private AnyMatcher() 52 | After: 53 | AnyMatcher() 54 | */ 55 | AnyMatcher() 56 | { 57 | } 58 | 59 | public bool Matches(object argument, Type parameterType) => true; 60 | 61 | public void SetupEvaluatedSuccessfully(object argument, Type parameterType) 62 | { 63 | Debug.Assert(this.Matches(argument, parameterType)); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/ThrowException.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | 7 | namespace Moq.Behaviors 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class ThrowException : Behavior 13 | After: 14 | sealed class ThrowException : Behavior 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class ThrowException : Behavior 20 | After: 21 | sealed class ThrowException : Behavior 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class ThrowException : Behavior 27 | After: 28 | sealed class ThrowException : Behavior 29 | */ 30 | sealed class ThrowException : Behavior 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private readonly Exception exception; 35 | After: 36 | readonly Exception exception; 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private readonly Exception exception; 42 | After: 43 | readonly Exception exception; 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private readonly Exception exception; 49 | After: 50 | readonly Exception exception; 51 | */ 52 | { 53 | readonly Exception exception; 54 | 55 | public ThrowException(Exception exception) 56 | { 57 | Debug.Assert(exception != null); 58 | 59 | this.exception = exception; 60 | } 61 | 62 | public override void Execute(Invocation invocation) 63 | { 64 | throw this.exception; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Moq/Pair.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | 9 | /* Unmerged change from project 'Moq(netstandard2.0)' 10 | Before: 11 | internal readonly struct Pair : IEquatable> 12 | After: 13 | readonly struct Pair : IEquatable> 14 | */ 15 | 16 | /* Unmerged change from project 'Moq(netstandard2.1)' 17 | Before: 18 | internal readonly struct Pair : IEquatable> 19 | After: 20 | readonly struct Pair : IEquatable> 21 | */ 22 | 23 | /* Unmerged change from project 'Moq(net6.0)' 24 | Before: 25 | internal readonly struct Pair : IEquatable> 26 | After: 27 | readonly struct Pair : IEquatable> 28 | */ 29 | readonly struct Pair : IEquatable> 30 | { 31 | public readonly T1 Item1; 32 | public readonly T2 Item2; 33 | 34 | public Pair(T1 item1, T2 item2) 35 | { 36 | this.Item1 = item1; 37 | this.Item2 = item2; 38 | } 39 | 40 | public void Deconstruct(out T1 item1, out T2 item2) 41 | { 42 | item1 = this.Item1; 43 | item2 = this.Item2; 44 | } 45 | 46 | public bool Equals(Pair other) 47 | { 48 | return object.Equals(this.Item1, other.Item1) 49 | && object.Equals(this.Item2, other.Item2); 50 | } 51 | 52 | public override bool Equals(object obj) 53 | { 54 | return obj is Pair other && this.Equals(other); 55 | } 56 | 57 | public override int GetHashCode() 58 | { 59 | return unchecked(1001 * this.Item1?.GetHashCode() ?? 101 + this.Item2?.GetHashCode() ?? 11); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/Callback.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | 7 | namespace Moq.Behaviors 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class Callback : Behavior 13 | After: 14 | sealed class Callback : Behavior 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class Callback : Behavior 20 | After: 21 | sealed class Callback : Behavior 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class Callback : Behavior 27 | After: 28 | sealed class Callback : Behavior 29 | */ 30 | sealed class Callback : Behavior 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private readonly Action callback; 35 | After: 36 | readonly Action callback; 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private readonly Action callback; 42 | After: 43 | readonly Action callback; 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private readonly Action callback; 49 | After: 50 | readonly Action callback; 51 | */ 52 | { 53 | readonly Action callback; 54 | 55 | public Callback(Action callback) 56 | { 57 | Debug.Assert(callback != null); 58 | 59 | this.callback = callback; 60 | } 61 | 62 | public override void Execute(Invocation invocation) 63 | { 64 | this.callback.Invoke(invocation); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # Builds and runs tests in all three supported OSes 2 | # Pushes CI feed if secrets.SLEET_CONNECTION is provided 3 | 4 | name: build 5 | on: 6 | workflow_dispatch: 7 | push: 8 | branches: [ main, dev, 'dev/*', 'feature/*', 'rel/*' ] 9 | paths-ignore: 10 | - changelog.md 11 | - code-of-conduct.md 12 | - security.md 13 | - support.md 14 | - readme.md 15 | pull_request: 16 | types: [opened, synchronize, reopened] 17 | 18 | env: 19 | DOTNET_NOLOGO: true 20 | VersionPrefix: 42.42.${{ github.run_number }} 21 | VersionLabel: ${{ github.ref }} 22 | 23 | defaults: 24 | run: 25 | shell: bash 26 | 27 | jobs: 28 | build: 29 | runs-on: windows-latest 30 | steps: 31 | - name: 🤘 checkout 32 | uses: actions/checkout@v2 33 | with: 34 | submodules: recursive 35 | fetch-depth: 0 36 | 37 | - name: 🙏 build 38 | run: dotnet build -m:1 39 | 40 | - name: 🧪 test 41 | uses: ./.github/workflows/test 42 | 43 | - name: 📦 pack 44 | run: dotnet pack -m:1 45 | 46 | # Only push CI package to sleet feed if building on ubuntu (fastest) 47 | - name: 🚀 sleet 48 | env: 49 | SLEET_CONNECTION: ${{ secrets.SLEET_CONNECTION }} 50 | if: env.SLEET_CONNECTION != '' 51 | run: | 52 | dotnet tool install -g --version 4.0.18 sleet 53 | sleet push bin --config none -f --verbose -p "SLEET_FEED_CONTAINER=nuget" -p "SLEET_FEED_CONNECTIONSTRING=${{ secrets.SLEET_CONNECTION }}" -p "SLEET_FEED_TYPE=azure" || echo "No packages found" 54 | 55 | dotnet-format: 56 | runs-on: windows-latest 57 | steps: 58 | - name: 🤘 checkout 59 | uses: actions/checkout@v2 60 | with: 61 | submodules: recursive 62 | fetch-depth: 0 63 | 64 | - name: ✓ ensure format 65 | run: | 66 | dotnet format whitespace --verify-no-changes -v:diag --exclude ~/.nuget 67 | dotnet format style --verify-no-changes -v:diag --exclude ~/.nuget 68 | -------------------------------------------------------------------------------- /src/Moq/Condition.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | namespace Moq 7 | { 8 | 9 | /* Unmerged change from project 'Moq(netstandard2.0)' 10 | Before: 11 | internal sealed class Condition 12 | After: 13 | sealed class Condition 14 | */ 15 | 16 | /* Unmerged change from project 'Moq(netstandard2.1)' 17 | Before: 18 | internal sealed class Condition 19 | After: 20 | sealed class Condition 21 | */ 22 | 23 | /* Unmerged change from project 'Moq(net6.0)' 24 | Before: 25 | internal sealed class Condition 26 | After: 27 | sealed class Condition 28 | */ 29 | sealed class Condition 30 | 31 | /* Unmerged change from project 'Moq(netstandard2.0)' 32 | Before: 33 | private Func condition; 34 | private Action success; 35 | After: 36 | Func condition; 37 | Action success; 38 | */ 39 | 40 | /* Unmerged change from project 'Moq(netstandard2.1)' 41 | Before: 42 | private Func condition; 43 | private Action success; 44 | After: 45 | Func condition; 46 | Action success; 47 | */ 48 | 49 | /* Unmerged change from project 'Moq(net6.0)' 50 | Before: 51 | private Func condition; 52 | private Action success; 53 | After: 54 | Func condition; 55 | Action success; 56 | */ 57 | { 58 | Func condition; 59 | Action success; 60 | 61 | public Condition(Func condition, Action success = null) 62 | { 63 | this.condition = condition; 64 | this.success = success; 65 | } 66 | 67 | public bool IsTrue => this.condition?.Invoke() == true; 68 | 69 | public void SetupEvaluatedSuccessfully() => this.success?.Invoke(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Moq.Tests/UnmatchableMatchersFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | using Xunit; 7 | 8 | namespace Moq.Tests 9 | { 10 | public class UnmatchableMatchersFixture 11 | { 12 | [Fact] 13 | public void Matchers_that_are_unmatchable_due_to_implicit_conversion_operator_cause_setup_failure() 14 | { 15 | var mock = new Mock(); 16 | var ex = Assert.Throws(() => mock.Setup(x => x.UseB(It.IsAny()))); 17 | Assert.Contains("'It.IsAny()' is unmatchable", ex.Message); 18 | } 19 | 20 | [Fact] 21 | public void Matchers_with_explicit_primitive_type_cast_are_not_considered_unmatchable() 22 | { 23 | var mock = new Mock(); 24 | mock.Setup(x => x.UseLong((int)It.IsAny())); 25 | } 26 | 27 | [Fact] 28 | public void Matchers_with_implicit_primitive_type_coercions_are_not_considered_unmatchable_1() 29 | { 30 | var mock = new Mock(); 31 | mock.Setup(x => x.UseLong(It.IsAny())); 32 | } 33 | 34 | [Fact] 35 | public void Matchers_with_implicit_primitive_nullable_type_coercions_are_not_considered_unmatchable_2() 36 | { 37 | var mock = new Mock(); 38 | mock.Setup(x => x.UseNullableLong(It.IsAny())); 39 | } 40 | 41 | public interface IX 42 | { 43 | void UseB(B arg); 44 | void UseInt(int arg); 45 | void UseLong(long arg); 46 | void UseNullableLong(long? arg); 47 | } 48 | 49 | public readonly struct A 50 | { 51 | } 52 | 53 | public readonly struct B 54 | { 55 | public static implicit operator B(A a) 56 | { 57 | return new B(); 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Moq/Expectation.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq.Expressions; 6 | 7 | using Moq.Async; 8 | 9 | namespace Moq 10 | { 11 | 12 | /* Unmerged change from project 'Moq(netstandard2.0)' 13 | Before: 14 | internal abstract class Expectation : IEquatable 15 | After: 16 | abstract class Expectation : IEquatable 17 | */ 18 | 19 | /* Unmerged change from project 'Moq(netstandard2.1)' 20 | Before: 21 | internal abstract class Expectation : IEquatable 22 | After: 23 | abstract class Expectation : IEquatable 24 | */ 25 | 26 | /* Unmerged change from project 'Moq(net6.0)' 27 | Before: 28 | internal abstract class Expectation : IEquatable 29 | After: 30 | abstract class Expectation : IEquatable 31 | */ 32 | /// 33 | /// Represents a set (or the "shape") of invocations 34 | /// against which concrete s can be matched. 35 | /// 36 | abstract class Expectation : IEquatable 37 | { 38 | public abstract LambdaExpression Expression { get; } 39 | 40 | public virtual bool HasResultExpression(out IAwaitableFactory awaitableFactory) 41 | { 42 | awaitableFactory = null; 43 | return false; 44 | } 45 | 46 | public override bool Equals(object obj) 47 | { 48 | return obj is Expectation other && this.Equals(other); 49 | } 50 | 51 | public abstract bool Equals(Expectation other); 52 | 53 | public abstract override int GetHashCode(); 54 | 55 | public abstract bool IsMatch(Invocation invocation); 56 | 57 | public virtual void SetupEvaluatedSuccessfully(Invocation invocation) 58 | { 59 | } 60 | 61 | public override string ToString() 62 | { 63 | return this.Expression.ToStringFixed(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Moq.Tests/SetupsFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq; 6 | using System.Linq.Expressions; 7 | 8 | using Xunit; 9 | 10 | namespace Moq.Tests 11 | { 12 | public class SetupsFixture 13 | { 14 | [Fact] 15 | public void Mock_made_with_new_operator_initially_has_no_setups() 16 | { 17 | var mock = new Mock(); 18 | Assert.Empty(mock.Setups); 19 | } 20 | 21 | [Fact] 22 | public void Setup_adds_one_setup_with_same_expression_to_Setups() 23 | { 24 | Expression> setupExpression = m => m.ToString(); 25 | 26 | var mock = new Mock(); 27 | mock.Setup(setupExpression); 28 | 29 | var setup = Assert.Single(mock.Setups); 30 | Assert.Equal(setupExpression, setup.Expression, ExpressionComparer.Default); 31 | } 32 | 33 | [Fact] 34 | public void Mock_Reset_results_in_empty_Setups() 35 | { 36 | var mock = new Mock(); 37 | mock.Setup(m => m.ToString()); 38 | Assert.NotEmpty(mock.Setups); 39 | mock.Reset(); 40 | Assert.Empty(mock.Setups); 41 | } 42 | 43 | [Fact] 44 | public void Setups_includes_conditional_setups() 45 | { 46 | var mock = new Mock(); 47 | mock.When(() => true).Setup(m => m.ToString()); 48 | 49 | var setup = Assert.Single(mock.Setups); 50 | Assert.True(setup.IsConditional); 51 | } 52 | 53 | [Fact] 54 | public void Setups_includes_overridden_setups() 55 | { 56 | var mock = new Mock(); 57 | mock.Setup(m => m.ToString()); 58 | mock.Setup(m => m.ToString()); 59 | 60 | var setups = mock.Setups.ToArray(); 61 | Assert.Equal(2, setups.Length); 62 | Assert.True(setups[0].IsOverridden); 63 | Assert.False(setups[1].IsOverridden); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Moq/Matchers/ExpressionMatcher.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | using System.Linq.Expressions; 7 | 8 | namespace Moq.Matchers 9 | { 10 | 11 | /* Unmerged change from project 'Moq(netstandard2.0)' 12 | Before: 13 | internal class ExpressionMatcher : IMatcher 14 | After: 15 | class ExpressionMatcher : IMatcher 16 | */ 17 | 18 | /* Unmerged change from project 'Moq(netstandard2.1)' 19 | Before: 20 | internal class ExpressionMatcher : IMatcher 21 | After: 22 | class ExpressionMatcher : IMatcher 23 | */ 24 | 25 | /* Unmerged change from project 'Moq(net6.0)' 26 | Before: 27 | internal class ExpressionMatcher : IMatcher 28 | After: 29 | class ExpressionMatcher : IMatcher 30 | */ 31 | class ExpressionMatcher : IMatcher 32 | 33 | /* Unmerged change from project 'Moq(netstandard2.0)' 34 | Before: 35 | private Expression expression; 36 | After: 37 | Expression expression; 38 | */ 39 | 40 | /* Unmerged change from project 'Moq(netstandard2.1)' 41 | Before: 42 | private Expression expression; 43 | After: 44 | Expression expression; 45 | */ 46 | 47 | /* Unmerged change from project 'Moq(net6.0)' 48 | Before: 49 | private Expression expression; 50 | After: 51 | Expression expression; 52 | */ 53 | { 54 | Expression expression; 55 | 56 | public ExpressionMatcher(Expression expression) 57 | { 58 | this.expression = expression; 59 | } 60 | 61 | public bool Matches(object argument, Type parameterType) 62 | { 63 | return argument is Expression valueExpression 64 | && ExpressionComparer.Default.Equals(this.expression, valueExpression); 65 | } 66 | 67 | public void SetupEvaluatedSuccessfully(object argument, Type parameterType) 68 | { 69 | Debug.Assert(this.Matches(argument, parameterType)); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Moq/Matchers/LazyEvalMatcher.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | using System.Linq.Expressions; 7 | 8 | namespace Moq.Matchers 9 | { 10 | 11 | /* Unmerged change from project 'Moq(netstandard2.0)' 12 | Before: 13 | internal class LazyEvalMatcher : IMatcher 14 | After: 15 | class LazyEvalMatcher : IMatcher 16 | */ 17 | 18 | /* Unmerged change from project 'Moq(netstandard2.1)' 19 | Before: 20 | internal class LazyEvalMatcher : IMatcher 21 | After: 22 | class LazyEvalMatcher : IMatcher 23 | */ 24 | 25 | /* Unmerged change from project 'Moq(net6.0)' 26 | Before: 27 | internal class LazyEvalMatcher : IMatcher 28 | After: 29 | class LazyEvalMatcher : IMatcher 30 | */ 31 | class LazyEvalMatcher : IMatcher 32 | 33 | /* Unmerged change from project 'Moq(netstandard2.0)' 34 | Before: 35 | private Expression expression; 36 | After: 37 | Expression expression; 38 | */ 39 | 40 | /* Unmerged change from project 'Moq(netstandard2.1)' 41 | Before: 42 | private Expression expression; 43 | After: 44 | Expression expression; 45 | */ 46 | 47 | /* Unmerged change from project 'Moq(net6.0)' 48 | Before: 49 | private Expression expression; 50 | After: 51 | Expression expression; 52 | */ 53 | { 54 | Expression expression; 55 | 56 | public LazyEvalMatcher(Expression expression) 57 | { 58 | this.expression = expression; 59 | } 60 | 61 | public bool Matches(object argument, Type parameterType) 62 | { 63 | var eval = Evaluator.PartialEval(this.expression); 64 | return eval is ConstantExpression ce && new ConstantMatcher(ce.Value).Matches(argument, parameterType); 65 | } 66 | 67 | public void SetupEvaluatedSuccessfully(object argument, Type parameterType) 68 | { 69 | Debug.Assert(this.Matches(argument, parameterType)); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/ReturnComputedValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | 7 | namespace Moq.Behaviors 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class ReturnComputedValue : Behavior 13 | After: 14 | sealed class ReturnComputedValue : Behavior 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class ReturnComputedValue : Behavior 20 | After: 21 | sealed class ReturnComputedValue : Behavior 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class ReturnComputedValue : Behavior 27 | After: 28 | sealed class ReturnComputedValue : Behavior 29 | */ 30 | sealed class ReturnComputedValue : Behavior 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private readonly Func valueFactory; 35 | After: 36 | readonly Func valueFactory; 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private readonly Func valueFactory; 42 | After: 43 | readonly Func valueFactory; 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private readonly Func valueFactory; 49 | After: 50 | readonly Func valueFactory; 51 | */ 52 | { 53 | readonly Func valueFactory; 54 | 55 | public ReturnComputedValue(Func valueFactory) 56 | { 57 | Debug.Assert(valueFactory != null); 58 | 59 | this.valueFactory = valueFactory; 60 | } 61 | 62 | public override void Execute(Invocation invocation) 63 | { 64 | invocation.ReturnValue = this.valueFactory.Invoke(invocation); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Moq/Language/IReturns.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="false" hostspecific="false" language="C#" #> 2 | <#@ output extension=".Generated.cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Import Namespace="System.Linq" #> 5 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 6 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 7 | 8 | using System; 9 | using System.ComponentModel; 10 | using Moq.Language.Flow; 11 | 12 | namespace Moq.Language 13 | { 14 | partial interface IReturns 15 | { <# 16 | for (var typeCount = 2; typeCount <= GenericTypeMax; typeCount++) 17 | { 18 | var typeList = GetGenericList(typeCount, GenericTypeFormat); 19 | #> 20 | 21 | /// 22 | /// Specifies a function that will calculate the value to return from the method, 23 | /// retrieving the arguments for the invocation. 24 | /// 25 | <# 26 | for (var typeIndex = 1; typeIndex <= typeCount; typeIndex++) 27 | { 28 | #> 29 | /// The type of the <#= ConvertToOrdinal(typeIndex) #> argument of the invoked method. 30 | <# 31 | } 32 | #> 33 | /// The function that will calculate the return value. 34 | /// Returns a calculated value which is evaluated lazily at the time of the invocation. 35 | /// 36 | /// 37 | /// The return value is calculated from the value of the actual method invocation arguments. 38 | /// Notice how the arguments are retrieved by simply declaring them as part of the lambda 39 | /// expression: 40 | /// 41 | /// 42 | /// mock.Setup(x => x.Execute( 43 | <# 44 | for (var typeIndex = 1; typeIndex < typeCount; typeIndex++) 45 | { 46 | #> 47 | /// It.IsAny<int>(), 48 | <# 49 | } 50 | #> 51 | /// It.IsAny<int>())) 52 | /// .Returns((<#= GetGenericList(typeCount, "int arg{0}") #>) => <#= GetGenericList(typeCount, "arg{0}", " + ") #>); 53 | /// 54 | /// 55 | IReturnsResult Returns<<#= typeList #>>(Func<<#= typeList #>, TResult> valueFunction); 56 | <# 57 | } 58 | #> 59 | } 60 | } 61 | <#@ Include File="GenericTypeParameters.tt" #> 62 | -------------------------------------------------------------------------------- /src/Moq/Language/ISetupConditionResult.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | using System.Linq.Expressions; 7 | 8 | using Moq.Language.Flow; 9 | 10 | namespace Moq.Language 11 | { 12 | /// 13 | /// Implements the fluent API. 14 | /// 15 | [EditorBrowsable(EditorBrowsableState.Never)] 16 | public interface ISetupConditionResult where T : class 17 | { 18 | /// 19 | /// The expectation will be considered only in the former condition. 20 | /// 21 | /// 22 | /// 23 | ISetup Setup(Expression> expression); 24 | 25 | /// 26 | /// The expectation will be considered only in the former condition. 27 | /// 28 | /// 29 | /// 30 | /// 31 | ISetup Setup(Expression> expression); 32 | 33 | /// 34 | /// Setups the get. 35 | /// 36 | /// The type of the property. 37 | /// The expression. 38 | /// 39 | ISetupGetter SetupGet(Expression> expression); 40 | 41 | /// 42 | /// Setups the set. 43 | /// 44 | /// The type of the property. 45 | /// The setter expression. 46 | /// 47 | ISetupSetter SetupSet(Action setterExpression); 48 | 49 | /// 50 | /// Setups the set. 51 | /// 52 | /// The setter expression. 53 | /// 54 | ISetup SetupSet(Action setterExpression); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Moq/Behaviors/ThrowComputedException.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Diagnostics; 6 | 7 | namespace Moq.Behaviors 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class ThrowComputedException : Behavior 13 | After: 14 | sealed class ThrowComputedException : Behavior 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class ThrowComputedException : Behavior 20 | After: 21 | sealed class ThrowComputedException : Behavior 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class ThrowComputedException : Behavior 27 | After: 28 | sealed class ThrowComputedException : Behavior 29 | */ 30 | sealed class ThrowComputedException : Behavior 31 | 32 | /* Unmerged change from project 'Moq(netstandard2.0)' 33 | Before: 34 | private readonly Func exceptionFactory; 35 | After: 36 | readonly Func exceptionFactory; 37 | */ 38 | 39 | /* Unmerged change from project 'Moq(netstandard2.1)' 40 | Before: 41 | private readonly Func exceptionFactory; 42 | After: 43 | readonly Func exceptionFactory; 44 | */ 45 | 46 | /* Unmerged change from project 'Moq(net6.0)' 47 | Before: 48 | private readonly Func exceptionFactory; 49 | After: 50 | readonly Func exceptionFactory; 51 | */ 52 | { 53 | readonly Func exceptionFactory; 54 | 55 | public ThrowComputedException(Func exceptionFactory) 56 | { 57 | Debug.Assert(exceptionFactory != null); 58 | 59 | this.exceptionFactory = exceptionFactory; 60 | } 61 | 62 | public override void Execute(Invocation invocation) 63 | { 64 | throw this.exceptionFactory.Invoke(invocation); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Moq/DefaultExpressionCompiler.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq.Expressions; 6 | 7 | namespace Moq 8 | { 9 | 10 | /* Unmerged change from project 'Moq(netstandard2.0)' 11 | Before: 12 | internal sealed class DefaultExpressionCompiler : ExpressionCompiler 13 | After: 14 | sealed class DefaultExpressionCompiler : ExpressionCompiler 15 | */ 16 | 17 | /* Unmerged change from project 'Moq(netstandard2.1)' 18 | Before: 19 | internal sealed class DefaultExpressionCompiler : ExpressionCompiler 20 | After: 21 | sealed class DefaultExpressionCompiler : ExpressionCompiler 22 | */ 23 | 24 | /* Unmerged change from project 'Moq(net6.0)' 25 | Before: 26 | internal sealed class DefaultExpressionCompiler : ExpressionCompiler 27 | After: 28 | sealed class DefaultExpressionCompiler : ExpressionCompiler 29 | */ 30 | sealed class DefaultExpressionCompiler : ExpressionCompiler 31 | { 32 | new public static readonly DefaultExpressionCompiler Instance = new DefaultExpressionCompiler(); 33 | 34 | 35 | /* Unmerged change from project 'Moq(netstandard2.0)' 36 | Before: 37 | private DefaultExpressionCompiler() 38 | After: 39 | DefaultExpressionCompiler() 40 | */ 41 | 42 | /* Unmerged change from project 'Moq(netstandard2.1)' 43 | Before: 44 | private DefaultExpressionCompiler() 45 | After: 46 | DefaultExpressionCompiler() 47 | */ 48 | 49 | /* Unmerged change from project 'Moq(net6.0)' 50 | Before: 51 | private DefaultExpressionCompiler() 52 | After: 53 | DefaultExpressionCompiler() 54 | */ 55 | DefaultExpressionCompiler() 56 | { 57 | } 58 | 59 | public override Delegate Compile(LambdaExpression expression) 60 | { 61 | return expression.Compile(); 62 | } 63 | 64 | public override TDelegate Compile(Expression expression) 65 | { 66 | return expression.Compile(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Moq/Language/IThrows.tt: -------------------------------------------------------------------------------- 1 | <#@ template debug="false" hostspecific="false" language="C#" #> 2 | <#@ output extension=".Generated.cs" #> 3 | <#@ Assembly Name="System.Core" #> 4 | <#@ Import Namespace="System.Linq" #> 5 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 6 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 7 | 8 | using System; 9 | using System.ComponentModel; 10 | using Moq.Language.Flow; 11 | 12 | namespace Moq.Language 13 | { 14 | partial interface IThrows 15 | { <# 16 | for (var typeCount = 2; typeCount <= GenericTypeMax; typeCount++) 17 | { 18 | var typeList = GetGenericList(typeCount, GenericTypeFormat); 19 | #> 20 | 21 | /// 22 | /// Specifies a function that will calculate the exception to throw when the method is invoked, 23 | /// retrieving the arguments for the invocation. 24 | /// 25 | <# 26 | for (var typeIndex = 1; typeIndex <= typeCount; typeIndex++) 27 | { 28 | #> 29 | /// The type of the <#= ConvertToOrdinal(typeIndex) #> argument of the invoked method. 30 | <# 31 | } 32 | #> 33 | /// Type of exception that will be calculated and thrown when the setup is matched. 34 | /// The function that will calculate the exception to be thrown. 35 | /// 36 | /// 37 | /// The thrown exception is calculated from the value of the actual method invocation arguments. 38 | /// Notice how the arguments are retrieved by simply declaring them as part of the lambda 39 | /// expression: 40 | /// 41 | /// 42 | /// mock.Setup(x => x.Execute( 43 | <# 44 | for (var typeIndex = 1; typeIndex < typeCount; typeIndex++) 45 | { 46 | #> 47 | /// It.IsAny<string>(), 48 | <# 49 | } 50 | #> 51 | /// It.IsAny<string>())) 52 | /// .Throws((<#= GetGenericList(typeCount, "string arg{0}") #>) => new Exception(<#= GetGenericList(typeCount, "arg{0}", " + ") #>)); 53 | /// 54 | /// 55 | IThrowsResult Throws<<#= typeList #>, TException>(Func<<#= typeList #>, TException> exceptionFunction) where TException : Exception; 56 | <# 57 | } 58 | #> 59 | } 60 | } 61 | <#@ Include File="GenericTypeParameters.tt" #> 62 | -------------------------------------------------------------------------------- /src/Moq/Language/IRaise.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | namespace Moq.Language 8 | { 9 | /// 10 | /// Defines the Raises verb. 11 | /// 12 | [EditorBrowsable(EditorBrowsableState.Never)] 13 | public partial interface IRaise : IFluentInterface 14 | { 15 | /// 16 | /// Specifies the event that will be raised 17 | /// when the setup is met. 18 | /// 19 | /// An expression that represents an event attach or detach action. 20 | /// The event arguments to pass for the raised event. 21 | /// 22 | /// The following example shows how to raise an event when 23 | /// the setup is met: 24 | /// 25 | /// var mock = new Mock<IContainer>(); 26 | /// 27 | /// mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>())) 28 | /// .Raises(add => add.Added += null, EventArgs.Empty); 29 | /// 30 | /// 31 | IVerifies Raises(Action eventExpression, EventArgs args); 32 | 33 | /// 34 | /// Specifies the event that will be raised 35 | /// when the setup is matched. 36 | /// 37 | /// An expression that represents an event attach or detach action. 38 | /// A function that will build the 39 | /// to pass when raising the event. 40 | /// 41 | IVerifies Raises(Action eventExpression, Func func); 42 | 43 | /// 44 | /// Specifies the custom event that will be raised 45 | /// when the setup is matched. 46 | /// 47 | /// An expression that represents an event attach or detach action. 48 | /// The arguments to pass to the custom delegate (non EventHandler-compatible). 49 | IVerifies Raises(Action eventExpression, params object[] args); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Moq/Language/IReturnsGetter.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.ComponentModel; 6 | 7 | using Moq.Language.Flow; 8 | 9 | namespace Moq.Language 10 | { 11 | /// 12 | /// Defines the Returns verb for property get setups. 13 | /// 14 | /// Mocked type. 15 | /// Type of the property. 16 | [EditorBrowsable(EditorBrowsableState.Never)] 17 | public interface IReturnsGetter : IFluentInterface 18 | where TMock : class 19 | { 20 | /// 21 | /// Specifies the value to return. 22 | /// 23 | /// The value to return, or . 24 | /// 25 | /// Return a true value from the property getter call: 26 | /// 27 | /// mock.SetupGet(x => x.Suspended) 28 | /// .Returns(true); 29 | /// 30 | /// 31 | IReturnsResult Returns(TProperty value); 32 | 33 | /// 34 | /// Specifies a function that will calculate the value to return for the property. 35 | /// 36 | /// The function that will calculate the return value. 37 | /// 38 | /// Return a calculated value when the property is retrieved: 39 | /// 40 | /// mock.SetupGet(x => x.Suspended) 41 | /// .Returns(() => returnValues[0]); 42 | /// 43 | /// The lambda expression to retrieve the return value is lazy-executed, 44 | /// meaning that its value may change depending on the moment the property 45 | /// is retrieved and the value the returnValues array has at 46 | /// that moment. 47 | /// 48 | IReturnsResult Returns(Func valueFunction); 49 | 50 | /// 51 | /// Calls the real property of the object and returns its return value. 52 | /// 53 | /// The value calculated by the real property of the object. 54 | IReturnsResult CallBase(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Moq.Tests/Matchers/AnyMatcherFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq.Expressions; 6 | 7 | using Xunit; 8 | 9 | namespace Moq.Tests.Matchers 10 | { 11 | public class AnyMatcherFixture 12 | { 13 | [Fact] 14 | public void MatchesNull() 15 | { 16 | var expr = ToExpression(() => It.IsAny()).Body; 17 | 18 | var (matcher, _) = MatcherFactory.CreateMatcher(expr); 19 | 20 | Assert.True(matcher.Matches(null, typeof(object))); 21 | } 22 | 23 | [Fact] 24 | public void MatchesIfAssignableType() 25 | { 26 | var expr = ToExpression(() => It.IsAny()).Body; 27 | 28 | var (matcher, _) = MatcherFactory.CreateMatcher(expr); 29 | 30 | Assert.True(matcher.Matches("foo", typeof(object))); 31 | } 32 | 33 | [Fact] 34 | public void MatchesIfAssignableInterface() 35 | { 36 | var expr = ToExpression(() => It.IsAny()).Body; 37 | 38 | var (matcher, _) = MatcherFactory.CreateMatcher(expr); 39 | 40 | Assert.True(matcher.Matches(new Disposable(), typeof(IDisposable))); 41 | } 42 | 43 | [Fact] 44 | public void DoesntMatchIfNotAssignableType() 45 | { 46 | var expr = ToExpression(() => It.IsAny()).Body; 47 | 48 | var (matcher, _) = MatcherFactory.CreateMatcher(expr); 49 | 50 | Assert.False(matcher.Matches("foo", typeof(IFormatProvider))); 51 | 52 | /* Unmerged change from project 'Moq.Tests(net6.0)' 53 | Before: 54 | private LambdaExpression ToExpression(Expression> expr) 55 | After: 56 | LambdaExpression ToExpression(Expression> expr) 57 | */ 58 | } 59 | 60 | LambdaExpression ToExpression(Expression> expr) 61 | { 62 | return expr; 63 | } 64 | 65 | class Disposable : IDisposable 66 | { 67 | public void Dispose() 68 | { 69 | throw new NotImplementedException(); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Moq.Tests.ComTypes/README.md: -------------------------------------------------------------------------------- 1 | # Moq.Types.ComTypes.dll 2 | 3 | This directory contains an assembly `Moq.Types.ComTypes.dll` containing 4 | Component Object Model (COM) interop types, used to test various aspects 5 | of Moq compatibility with COM. 6 | 7 | This assembly can be built from the `ComTypes.idl` definition file also 8 | contained in this directory. 9 | 10 | 11 | ## Building 12 | 13 | 14 | ### Requirements 15 | 16 | In order to build the DLL, you will need the following tools: 17 | 18 | * **`midl`** (Microsoft's MIDL Compiler) for producing a Type Library 19 | from an `.idl` source file. This tool is part of the Windows SDK. 20 | See ["Using the MIDL Compiler" on Microsoft Docs](https://docs.microsoft.com/en-us/windows/desktop/Midl/using-the-midl-compiler-2) 21 | for more information. 22 | 23 | * **`tlbimp`** (Microsoft's .NET Framework Type Library to Assembly 24 | Converter) for producing a COM interop assembly from a Type Library. 25 | This tool is part of the .NET Framework SDK, and it is automatically 26 | installed with Visual Studio. See ["Tlbimp.exe (Type Library Importer)" on Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/framework/tools/tlbimp-exe-type-library-importer) 27 | for more information. 28 | 29 | * Optionally, **`peverify`** (Microsoft's .NET Framework PE Verifier) 30 | which is also part of the .NET Framework SDK and installed together 31 | with Visual Studio. See ["Peverify.exe (PEVerify Tool)" on Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/framework/tools/peverify-exe-peverify-tool) 32 | 33 | 34 | ### Steps 35 | 36 | Open a command prompt and ensure that the tools mentioned above can be 37 | found in the `PATH`. For instance, start Visual Studio's developer 38 | command prompt. 39 | 40 | Next, you can either run the `BuildDLLFromIDL.cmd` batch file found in 41 | this directory, or run the following equivalent steps manually: 42 | 43 | 1. Create a COM Type Library (a `.tlb` file) from the `.idl` definition: 44 | 45 | ``` 46 | midl.exe /mktyplib203 ^ 47 | /env win32 ^ 48 | /tlb ComTypes.tlb ^ 49 | ComTypes.idl 50 | ``` 51 | 52 | 2. Convert the generated type library to a .NET interop types assembly: 53 | 54 | ``` 55 | tlbimp ComTypes.tlb ^ 56 | /out:Moq.Tests.ComTypes.dll ^ 57 | /namespace:Moq.Tests.ComTypes ^ 58 | /keyfile:..\..\Moq.snk ^ 59 | /asmversion:4.0.0.0 60 | ``` 61 | 62 | 3. Optionally verify the generated assembly: 63 | 64 | ``` 65 | peverify Moq.Tests.ComTypes.dll 66 | ``` 67 | -------------------------------------------------------------------------------- /src/Moq.Tests/Matchers/ParamArrayMatcherFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Linq.Expressions; 8 | 9 | using Xunit; 10 | 11 | namespace Moq.Tests.Matchers 12 | { 13 | public class ParamArrayMatcherFixture 14 | { 15 | [Theory] 16 | [InlineData(42, "", true)] 17 | [InlineData(42, null, true)] 18 | [InlineData(3.141f, "", false)] 19 | [InlineData(null, "", false)] 20 | public void Matches_several_matchers_from_params_array(object first, object second, bool shouldMatch) 21 | { 22 | var seconds = new List(); 23 | var methodCallExpr = (MethodCallExpression)ToExpression(x => x.Method(It.IsAny(), Capture.In(seconds))).Body; 24 | var expr = methodCallExpr.Arguments.Single(); 25 | var parameter = typeof(IX).GetMethod("Method").GetParameters().Single(); 26 | 27 | var (matcher, _) = MatcherFactory.CreateMatcher(expr, parameter); 28 | 29 | Assert.Equal(shouldMatch, matcher.Matches(new object[] { first, second }, typeof(object[]))); 30 | } 31 | 32 | [Fact] 33 | public void SetupEvaluatedSuccessfully_succeeds_for_matching_values() 34 | { 35 | var seconds = new List(); 36 | var methodCallExpr = (MethodCallExpression)ToExpression(x => x.Method(It.IsAny(), Capture.In(seconds))).Body; 37 | var expr = methodCallExpr.Arguments.Single(); 38 | var parameter = typeof(IX).GetMethod("Method").GetParameters().Single(); 39 | 40 | var (matcher, _) = MatcherFactory.CreateMatcher(expr, parameter); 41 | 42 | matcher.SetupEvaluatedSuccessfully(new object[] { 42, "" }, typeof(object[])); 43 | 44 | /* Unmerged change from project 'Moq.Tests(net6.0)' 45 | Before: 46 | private LambdaExpression ToExpression(Expression> expr) 47 | After: 48 | LambdaExpression ToExpression(Expression> expr) 49 | */ 50 | } 51 | 52 | LambdaExpression ToExpression(Expression> expr) 53 | { 54 | return expr; 55 | } 56 | 57 | public interface IX 58 | { 59 | void Method(params object[] args); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Moq/Async/TaskFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq.Expressions; 7 | using System.Reflection; 8 | using System.Threading.Tasks; 9 | 10 | namespace Moq.Async 11 | { 12 | 13 | /* Unmerged change from project 'Moq(netstandard2.0)' 14 | Before: 15 | internal sealed class TaskFactory : AwaitableFactory 16 | After: 17 | sealed class TaskFactory : AwaitableFactory 18 | */ 19 | 20 | /* Unmerged change from project 'Moq(netstandard2.1)' 21 | Before: 22 | internal sealed class TaskFactory : AwaitableFactory 23 | After: 24 | sealed class TaskFactory : AwaitableFactory 25 | */ 26 | 27 | /* Unmerged change from project 'Moq(net6.0)' 28 | Before: 29 | internal sealed class TaskFactory : AwaitableFactory 30 | After: 31 | sealed class TaskFactory : AwaitableFactory 32 | */ 33 | sealed class TaskFactory : AwaitableFactory 34 | { 35 | public static readonly TaskFactory Instance = new TaskFactory(); 36 | 37 | 38 | /* Unmerged change from project 'Moq(netstandard2.0)' 39 | Before: 40 | private TaskFactory() 41 | After: 42 | TaskFactory() 43 | */ 44 | 45 | /* Unmerged change from project 'Moq(netstandard2.1)' 46 | Before: 47 | private TaskFactory() 48 | After: 49 | TaskFactory() 50 | */ 51 | 52 | /* Unmerged change from project 'Moq(net6.0)' 53 | Before: 54 | private TaskFactory() 55 | After: 56 | TaskFactory() 57 | */ 58 | TaskFactory() 59 | { 60 | } 61 | 62 | public override Task CreateCompleted() 63 | { 64 | return Task.FromResult(default); 65 | } 66 | 67 | public override Task CreateFaulted(Exception exception) 68 | { 69 | var tcs = new TaskCompletionSource(); 70 | tcs.SetException(exception); 71 | return tcs.Task; 72 | } 73 | 74 | public override Task CreateFaulted(IEnumerable exceptions) 75 | { 76 | var tcs = new TaskCompletionSource(); 77 | tcs.SetException(exceptions); 78 | return tcs.Task; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Moq/ParameterTypes.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections; 6 | using System.Collections.Generic; 7 | using System.Reflection; 8 | 9 | namespace Moq 10 | { 11 | 12 | /* Unmerged change from project 'Moq(netstandard2.0)' 13 | Before: 14 | internal readonly struct ParameterTypes : IReadOnlyList 15 | After: 16 | readonly struct ParameterTypes : IReadOnlyList 17 | */ 18 | 19 | /* Unmerged change from project 'Moq(netstandard2.1)' 20 | Before: 21 | internal readonly struct ParameterTypes : IReadOnlyList 22 | After: 23 | readonly struct ParameterTypes : IReadOnlyList 24 | */ 25 | 26 | /* Unmerged change from project 'Moq(net6.0)' 27 | Before: 28 | internal readonly struct ParameterTypes : IReadOnlyList 29 | After: 30 | readonly struct ParameterTypes : IReadOnlyList 31 | */ 32 | /// 33 | /// Allocation-free adapter type for treating a `ParameterInfo[]` array like a `Type[]` array. 34 | /// 35 | readonly struct ParameterTypes : IReadOnlyList 36 | 37 | /* Unmerged change from project 'Moq(netstandard2.0)' 38 | Before: 39 | private readonly ParameterInfo[] parameters; 40 | After: 41 | readonly ParameterInfo[] parameters; 42 | */ 43 | 44 | /* Unmerged change from project 'Moq(netstandard2.1)' 45 | Before: 46 | private readonly ParameterInfo[] parameters; 47 | After: 48 | readonly ParameterInfo[] parameters; 49 | */ 50 | 51 | /* Unmerged change from project 'Moq(net6.0)' 52 | Before: 53 | private readonly ParameterInfo[] parameters; 54 | After: 55 | readonly ParameterInfo[] parameters; 56 | */ 57 | { 58 | readonly ParameterInfo[] parameters; 59 | 60 | public ParameterTypes(ParameterInfo[] parameters) 61 | { 62 | this.parameters = parameters; 63 | } 64 | 65 | public Type this[int index] => this.parameters[index].ParameterType; 66 | 67 | public int Count => this.parameters.Length; 68 | 69 | public IEnumerator GetEnumerator() 70 | { 71 | for (int i = 0, n = this.Count; i < n; ++i) 72 | { 73 | yield return this[i]; 74 | } 75 | } 76 | 77 | IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Moq/Async/ValueTaskFactory.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Threading.Tasks; 7 | 8 | namespace Moq.Async 9 | { 10 | 11 | /* Unmerged change from project 'Moq(netstandard2.0)' 12 | Before: 13 | internal sealed class ValueTaskFactory : AwaitableFactory 14 | After: 15 | sealed class ValueTaskFactory : AwaitableFactory 16 | */ 17 | 18 | /* Unmerged change from project 'Moq(netstandard2.1)' 19 | Before: 20 | internal sealed class ValueTaskFactory : AwaitableFactory 21 | After: 22 | sealed class ValueTaskFactory : AwaitableFactory 23 | */ 24 | 25 | /* Unmerged change from project 'Moq(net6.0)' 26 | Before: 27 | internal sealed class ValueTaskFactory : AwaitableFactory 28 | After: 29 | sealed class ValueTaskFactory : AwaitableFactory 30 | */ 31 | sealed class ValueTaskFactory : AwaitableFactory 32 | { 33 | public static readonly ValueTaskFactory Instance = new ValueTaskFactory(); 34 | 35 | 36 | /* Unmerged change from project 'Moq(netstandard2.0)' 37 | Before: 38 | private ValueTaskFactory() 39 | After: 40 | ValueTaskFactory() 41 | */ 42 | 43 | /* Unmerged change from project 'Moq(netstandard2.1)' 44 | Before: 45 | private ValueTaskFactory() 46 | After: 47 | ValueTaskFactory() 48 | */ 49 | 50 | /* Unmerged change from project 'Moq(net6.0)' 51 | Before: 52 | private ValueTaskFactory() 53 | After: 54 | ValueTaskFactory() 55 | */ 56 | ValueTaskFactory() 57 | { 58 | } 59 | 60 | public override ValueTask CreateCompleted() 61 | { 62 | return default; 63 | } 64 | 65 | public override ValueTask CreateFaulted(Exception exception) 66 | { 67 | var tcs = new TaskCompletionSource(); 68 | tcs.SetException(exception); 69 | return new ValueTask(tcs.Task); 70 | } 71 | 72 | public override ValueTask CreateFaulted(IEnumerable exceptions) 73 | { 74 | var tcs = new TaskCompletionSource(); 75 | tcs.SetException(exceptions); 76 | return new ValueTask(tcs.Task); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Moq.Tests/StringBuilderExtensionsFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | using System.Linq.Expressions; 6 | using System.Text; 7 | 8 | using Xunit; 9 | 10 | namespace Moq.Tests 11 | { 12 | public class StringBuilderExtensionsFixture 13 | { 14 | [Fact] 15 | public void AppendExpression_formats_call_to_indexer_setter_method_using_indexer_syntax() 16 | { 17 | // foo => foo.set_Item("index", "value") 18 | var foo = Expression.Parameter(typeof(IFoo), "foo"); 19 | var expression = 20 | Expression.Lambda>( 21 | Expression.Call( 22 | foo, 23 | typeof(IFoo).GetProperty("Item").SetMethod, 24 | Expression.Constant("index"), 25 | Expression.Constant("value")), 26 | foo); 27 | 28 | Assert.Equal(@"foo => foo[""index""] = ""value""", GetAppendExpressionResult(expression)); 29 | } 30 | 31 | [Fact] 32 | public void AppendExpression_formats_ternary_conditional_expression_correctly() 33 | { 34 | // 1 == 2 ? 3 : 4 35 | var expression = Expression.Condition( 36 | Expression.Equal(Expression.Constant(1), Expression.Constant(2)), 37 | Expression.Constant(3), 38 | Expression.Constant(4)); 39 | 40 | Assert.Equal(@"1 == 2 ? 3 : 4", GetAppendExpressionResult(expression)); 41 | } 42 | 43 | [Fact] 44 | public void AppendExpression_formats_is_operator_correctly() 45 | { 46 | // 1 is string 47 | var expression = Expression.TypeIs(Expression.Constant(1), typeof(string)); 48 | 49 | Assert.Equal(@"1 is string", GetAppendExpressionResult(expression)); 50 | 51 | /* Unmerged change from project 'Moq.Tests(net6.0)' 52 | Before: 53 | private string GetAppendExpressionResult(Expression expression) 54 | After: 55 | string GetAppendExpressionResult(Expression expression) 56 | */ 57 | } 58 | 59 | string GetAppendExpressionResult(Expression expression) 60 | { 61 | return new StringBuilder().AppendExpression(expression).ToString(); 62 | } 63 | 64 | public interface IFoo 65 | { 66 | object this[object index] { get; set; } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Moq.Tests/EventHandlerTypesMustMatchFixture.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors. 2 | // All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. 3 | 4 | using System; 5 | 6 | using Xunit; 7 | 8 | namespace Moq.Tests 9 | { 10 | public class EventHandlerTypesMustMatchFixture 11 | { 12 | [Fact] 13 | public void CLI_requires_event_handlers_to_have_the_exact_same_type() 14 | { 15 | var mouse = new Mouse(); 16 | var result = 2; 17 | 18 | mouse.LeftButtonClicked += new Action(_ => result += 3); 19 | mouse.LeftButtonClicked += new Action(_ => result *= 4); 20 | mouse.RaiseLeftButtonClicked(new LeftButton()); 21 | 22 | Assert.Equal(20, result); 23 | } 24 | 25 | [Fact] 26 | public void CLI_throws_if_event_handlers_do_not_have_the_exact_same_type() 27 | { 28 | var mouse = new Mouse(); 29 | mouse.LeftButtonClicked += new Action