Skip to content

Commit

Permalink
Sequential setup (#20)
Browse files Browse the repository at this point in the history
* implemented sequential setup, updated nugets

* adopted tests for sequential mocks

* updated documentation

* updated documentation

* updated documentation
  • Loading branch information
SvetlovA authored Aug 29, 2023
1 parent 1b52ba1 commit 8900d3f
Show file tree
Hide file tree
Showing 148 changed files with 9,808 additions and 810 deletions.
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# SMock
SMock is opensource lib for mocking static and instance methods and properties.
SMock is opensource lib for mocking static and instance methods and properties. [API Documntation](https://svetlova.github.io/static-mock/api/index.html)
# Installation
Download and install the package from [NuGet](https://www.nuget.org/packages/SMock/) or [GitHub](https://github.com/SvetlovA/static-mock/pkgs/nuget/SMock)
# Getting Started
## Hook Manager Types
SMock is based on [MonoMod](https://github.com/MonoMod/MonoMod) library that produce hook functionality
## Code Examples
### Returns
Setup is possible in two ways **Hierarchical** and **Sequential**
### Returns (Hierarchical)
```cs
Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
{
Expand Down Expand Up @@ -36,16 +37,33 @@ Mock.Setup(context => StaticClass.MethodToMockAsync(context.It.IsAny<int>()), as
Assert.AreEqual(expectedResult, actualResult);
}).Returns<int>(async argument => await Task.FromResult(argument));
```
[Other returns setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/ReturnsTests)
### Throws
[Other returns hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/ReturnsTests)
### Returns (Sequential)
```cs
using var _ = Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()))
.Returns(expectedResult);

var actualResult = StaticClass.MethodToMock(1);
Assert.AreNotEqual(originalResult, actualResult);
Assert.AreEqual(expectedResult, actualResult);
```
[Other returns sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/ReturnsTests)
### Throws (Hierarchical)
```cs
Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
Assert.Throws<Exception>(() => StaticClass.MethodToMock());
}).Throws<Exception>();
```
[Other throws setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/ThrowsTests)
### Callback
[Other throws hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/ThrowsTests)
### Throws (Sequential)
```cs
using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Throws<Exception>();

Assert.Throws<Exception>(() => StaticClass.MethodToMock());
```
[Other throws sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/ThrowsTests)
### Callback (Hierarchical)
```cs
Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
Expand All @@ -67,14 +85,20 @@ Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
DoSomething(argument);
});
```
[Other callback setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/CallbackTests)
### SetupDefault
[Other callback hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/CallbackTests)
### Callback (Sequential)
```cs
Mock.SetupDefault(() => StaticClass.VoidMethodToMock(), () =>
using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Callback(() =>
{
StaticClass.VoidMethodToMock(); // This method do nothing
DoSomething();
});

var actualResult = StaticClass.MethodToMock();
Assert.AreNotEqual(originalResult, actualResult);
Assert.AreEqual(expectedResult, actualResult);
```
[Other callback sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/CallbackTests)

[Other examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests)
# Library license
The library is available under the [MIT license](https://github.com/SvetlovA/static-mock/blob/master/LICENSE).
42 changes: 33 additions & 9 deletions docfx_project/articles/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Download and install the package from [NuGet](https://www.nuget.org/packages/SMo
## Hook Manager Types
SMock is based on [MonoMod](https://github.com/MonoMod/MonoMod) library that produce hook functionality
## Code Examples
### Returns
Setup is possible in two ways **Hierarchical** and **Sequential**
### Returns (Hierarchical)
```cs
Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
{
Expand Down Expand Up @@ -36,16 +37,33 @@ Mock.Setup(context => StaticClass.MethodToMockAsync(context.It.IsAny<int>()), as
Assert.AreEqual(expectedResult, actualResult);
}).Returns<int>(async argument => await Task.FromResult(argument));
```
[Other returns setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/ReturnsTests)
### Throws
[Other returns hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/ReturnsTests)
### Returns (Sequential)
```cs
using var _ = Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()))
.Returns(expectedResult);

var actualResult = StaticClass.MethodToMock(1);
Assert.AreNotEqual(originalResult, actualResult);
Assert.AreEqual(expectedResult, actualResult);
```
[Other returns sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/ReturnsTests)
### Throws (Hierarchical)
```cs
Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
Assert.Throws<Exception>(() => StaticClass.MethodToMock());
}).Throws<Exception>();
```
[Other throws setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/ThrowsTests)
### Callback
[Other throws hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/ThrowsTests)
### Throws (Sequential)
```cs
using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Throws<Exception>();

Assert.Throws<Exception>(() => StaticClass.MethodToMock());
```
[Other throws sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/ThrowsTests)
### Callback (Hierarchical)
```cs
Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
Expand All @@ -67,14 +85,20 @@ Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
DoSomething(argument);
});
```
[Other callback setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/CallbackTests)
### SetupDefault
[Other callback hierarchical setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Hierarchical/CallbackTests)
### Callback (Sequential)
```cs
Mock.SetupDefault(() => StaticClass.VoidMethodToMock(), () =>
using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Callback(() =>
{
StaticClass.VoidMethodToMock(); // This method do nothing
DoSomething();
});

var actualResult = StaticClass.MethodToMock();
Assert.AreNotEqual(originalResult, actualResult);
Assert.AreEqual(expectedResult, actualResult);
```
[Other callback sequential setup examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests/Sequential/CallbackTests)

[Other examples](https://github.com/SvetlovA/static-mock/tree/master/src/StaticMock.Tests/Tests)
# Library license
The library is available under the [MIT license](https://github.com/SvetlovA/static-mock/blob/master/LICENSE).
2 changes: 2 additions & 0 deletions docfx_project/genertae.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotnet tool update -g docfx
docfx docfx.json
1 change: 1 addition & 0 deletions docfx_project/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# SMock
SMock is opensource lib for mocking static and instance methods and properties.
12 changes: 6 additions & 6 deletions docs/api/StaticMock.Entities.Context.It.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@



<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/net7_update/apiSpec/new?filename=StaticMock_Entities_Context_It.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.It%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/SequentialSetup/apiSpec/new?filename=StaticMock_Entities_Context_It.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.It%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
</head>

<script type="module">
Expand Down Expand Up @@ -79,7 +79,7 @@ <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>

<h1 id="StaticMock_Entities_Context_It" data-uid="StaticMock.Entities.Context.It" class="text-break">
Class It
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/It.cs/#L6"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/It.cs/#L6"><i class="bi bi-code-slash"></i></a>
</h1>

<div class="facts text-secondary">
Expand Down Expand Up @@ -146,7 +146,7 @@ <h2 class="section" id="constructors">Constructors

<h3 id="StaticMock_Entities_Context_It__ctor_StaticMock_Entities_Context_SetupContextState_" data-uid="StaticMock.Entities.Context.It.#ctor(StaticMock.Entities.Context.SetupContextState)">
It(SetupContextState)
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/It.cs/#L10"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/It.cs/#L10"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -181,7 +181,7 @@ <h2 class="section" id="methods">Methods

<h3 id="StaticMock_Entities_Context_It_IsAny__1" data-uid="StaticMock.Entities.Context.It.IsAny``1">
IsAny&lt;TValue&gt;()
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/It.cs/#L15"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/It.cs/#L15"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -217,7 +217,7 @@ <h4 class="section">Type Parameters</h4>

<h3 id="StaticMock_Entities_Context_It_Is__1_System_Linq_Expressions_Expression_System_Func___0_System_Boolean___" data-uid="StaticMock.Entities.Context.It.Is``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
Is&lt;TValue&gt;(Expression&lt;Func&lt;TValue, bool&gt;&gt;)
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/It.cs/#L17"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/It.cs/#L17"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -258,7 +258,7 @@ <h4 class="section">Type Parameters</h4>
</article>

<div class="contribution d-print-none">
<a href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/It.cs/#L6" class="edit-link">Edit this page</a>
<a href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/It.cs/#L6" class="edit-link">Edit this page</a>
</div>


Expand Down
10 changes: 5 additions & 5 deletions docs/api/StaticMock.Entities.Context.ItParameterExpression.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@



<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/net7_update/apiSpec/new?filename=StaticMock_Entities_Context_ItParameterExpression.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.ItParameterExpression%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/SequentialSetup/apiSpec/new?filename=StaticMock_Entities_Context_ItParameterExpression.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.ItParameterExpression%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
</head>

<script type="module">
Expand Down Expand Up @@ -79,7 +79,7 @@ <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>

<h1 id="StaticMock_Entities_Context_ItParameterExpression" data-uid="StaticMock.Entities.Context.ItParameterExpression" class="text-break">
Class ItParameterExpression
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L6"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L6"><i class="bi bi-code-slash"></i></a>
</h1>

<div class="facts text-secondary">
Expand Down Expand Up @@ -146,7 +146,7 @@ <h2 class="section" id="properties">Properties

<h3 id="StaticMock_Entities_Context_ItParameterExpression_ParameterExpression" data-uid="StaticMock.Entities.Context.ItParameterExpression.ParameterExpression">
ParameterExpression
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L9"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L9"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -177,7 +177,7 @@ <h4 class="section">Property Value</h4>

<h3 id="StaticMock_Entities_Context_ItParameterExpression_ParameterType" data-uid="StaticMock.Entities.Context.ItParameterExpression.ParameterType">
ParameterType
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L8"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L8"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -208,7 +208,7 @@ <h4 class="section">Property Value</h4>
</article>

<div class="contribution d-print-none">
<a href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L6" class="edit-link">Edit this page</a>
<a href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/ItParameterExpression.cs/#L6" class="edit-link">Edit this page</a>
</div>


Expand Down
8 changes: 4 additions & 4 deletions docs/api/StaticMock.Entities.Context.SetupContext.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@



<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/net7_update/apiSpec/new?filename=StaticMock_Entities_Context_SetupContext.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.SetupContext%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
<meta name="docfx:docurl" content="https://github.com/SvetlovA/static-mock/new/SequentialSetup/apiSpec/new?filename=StaticMock_Entities_Context_SetupContext.md&amp;value=---%0Auid%3A%20StaticMock.Entities.Context.SetupContext%0Asummary%3A%20&#39;*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax&#39;%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">
</head>

<script type="module">
Expand Down Expand Up @@ -79,7 +79,7 @@ <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>

<h1 id="StaticMock_Entities_Context_SetupContext" data-uid="StaticMock.Entities.Context.SetupContext" class="text-break">
Class SetupContext
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/SetupContext.cs/#L3"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/SetupContext.cs/#L3"><i class="bi bi-code-slash"></i></a>
</h1>

<div class="facts text-secondary">
Expand Down Expand Up @@ -146,7 +146,7 @@ <h2 class="section" id="properties">Properties

<h3 id="StaticMock_Entities_Context_SetupContext_It" data-uid="StaticMock.Entities.Context.SetupContext.It">
It
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/SetupContext.cs/#L7"><i class="bi bi-code-slash"></i></a>
<a class="header-action link-secondary" title="View source" href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/SetupContext.cs/#L7"><i class="bi bi-code-slash"></i></a>
</h3>

<div class="markdown level1 summary"></div>
Expand Down Expand Up @@ -177,7 +177,7 @@ <h4 class="section">Property Value</h4>
</article>

<div class="contribution d-print-none">
<a href="https://github.com/SvetlovA/static-mock/blob/net7_update/src/StaticMock/Entities/Context/SetupContext.cs/#L3" class="edit-link">Edit this page</a>
<a href="https://github.com/SvetlovA/static-mock/blob/SequentialSetup/src/StaticMock/Entities/Context/SetupContext.cs/#L3" class="edit-link">Edit this page</a>
</div>


Expand Down
Loading

0 comments on commit 8900d3f

Please sign in to comment.