SlowFox.Core
0.2.0
Prefix Reserved
See the version list below for details.
dotnet add package SlowFox.Core --version 0.2.0
NuGet\Install-Package SlowFox.Core -Version 0.2.0
<PackageReference Include="SlowFox.Core" Version="0.2.0" />
paket add SlowFox.Core --version 0.2.0
#r "nuget: SlowFox.Core, 0.2.0"
// Install SlowFox.Core as a Cake Addin #addin nuget:?package=SlowFox.Core&version=0.2.0 // Install SlowFox.Core as a Cake Tool #tool nuget:?package=SlowFox.Core&version=0.2.0
Introduction
SlowFox is a suite of .NET source generators, aiming to reduce the amount of repetitive code you need to write and maintain.
Source generators incur no run-time cost (as no reflection is involved), because the code is created at build time. Plus, using a supported IDE (like Visual Studio 2019/2022), you can see what's being generated immediately when you save your source code.
There are currently 2 generators available via SlowFox:
- Constructors, for generating constructors and private variables
- UnitTestMocks, for generating mock objects in unit tests
SlowFox.Constructors
SloxFox.Constructors is a generator that allows you to define the injectable dependencies for any given class, and the private class members, constructor and constructor assignments are all automatically created for you.
How do I get it working?
First off, install the NuGet package into your project:
Then, find a class where you want to have the dependencies injected, mark it as partial
, and add the SlowFox.InjectDependencies
attribute. Pass into this attribute the types you want to be injected:
namespace MySampleProject
{
[SlowFox.InjectDependencies(typeof(IUserReader), typeof(IFileHandler))]
public partial class MyNewClass
{
}
}
SlowFox will then generate everything needed for these dependencies, as another partial class:
namespace MySampleProject
{
public partial class MyNewClass
{
private readonly IUserReader _userReader;
private readonly IFileHandler _fileHandler;
public MyNewClass(IUserReader userReader, IFileHandler fileHandler)
{
_userReader = userReader;
_fileHandler = fileHandler;
}
}
}
You can reference these private members in your original class, and you can call this constructor during DI, or call it manually, or use it in unit tests - it's as if you've written it all yourself:
namespace MySampleProject
{
[SlowFox.InjectDependencies(typeof(IUserReader), typeof(IFileHandler))]
public partial class MyNewClass
{
public void SaveUserImage()
{
var image = _userReader.GetImage();
_fileHandler.AddFile(image);
}
}
}
This generator is compatible with:
- inheritance
- abstract classes
- generic types
- nullable types
- tuples
Configuration
Configuration is set in a .editorconfig file.
To configure the generated code to not use underscores for member names, set the skip_underscores
value to true:
[*.cs]
slowfox_generation.constructors.skip_underscores = true
To include a null check (and a throw of ArgumentNullException
if the constructor parameter is null), set include_nullcheck
to true:
[*.cs]
slowfox_generation.constructors.include_nullcheck = true
SlowFox.UnitTestMocks
SlowFox.UnitTestMocks is a generator that creates mock objects (using Moq) for the dependencies of a class that is to be tested.
There are packages that are designed for xUnit, NUnit and MSTest2.
How do I get it working?
Firstly, choose and install the NuGet package relating to your testing framework:
Framework | Package |
---|---|
xUnit | |
NUnit | |
MSTest2 |
Next, create a new test class, mark it as partial
and apply the InjectMocks
attribute, indicating the class that you're going to be tested:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
public partial class UserHandlerTests
{
}
}
SlowFox will then generate mock objects for each dependency of the selected class, and provide a Create
method that instantiates a new instance of the selected class with the mock objects used as dependencies:
namespace MySampleProject
{
public partial class UserHandlerTests
{
private Mock<IDatabase> _database;
private Mock<ILogger> _logger;
public UserHandlerTests()
{
_database = new Mock<IDatabase>(MockBehavior.Strict);
_logger = new Mock<ILogger>(MockBehavior.Strict);
}
private UserHandler Create()
{
return new UserHandler(_database.Object, _logger.Object);
}
}
}
For NUnit and MSTest, the mocks are instantiated in a
Setup
andInit
method respectively, instead of in a constructor
You can call Create()
in your tests to get the object to test, and you can reference the mock objects to set up any pre-defined responses, or to perform validation:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
public partial class UserHandlerTests
{
[Fact]
public void VerifyAddUser()
{
_database
.Setup(p => p.Save(It.IsAny<User>()));
UserHandler reader = Create();
reader.CreateNewUser();
_database
.Verify(p => p.Save(It.IsAny<User>()), Times.Once);
}
}
}
You are able to exclude specific types from being mocked, by using the ExcludeMocks
attribute. Any type specified within this attribute will be added as a parameter on the Create
method, so you can provide a value from within your test:
namespace MySampleProject
{
[SlowFox.InjectMocks(typeof(UserHandler))]
[SlowFox.ExcludeMocks(typeof(ILogger))]
public partial class UserHandlerTests
{
[Fact]
public void VerifyAddUser()
{
_database
.Setup(p => p.Save(It.IsAny<User>()));
ILogger testLogger = BuildTestLogger();
UserHandler reader = Create(testLogger);
reader.CreateNewUser();
_database
.Verify(p => p.Save(It.IsAny<User>()), Times.Once);
}
}
}
Note that types that cannot be mocked (e.g., a static or sealed type) will automatically be excluded from being mocked, and will be treated in the same way as types specified in the
ExcludeMocks
attribute
This generator is compatible with constructors that have been generated using SlowFox.Constructors.
Configuration
Configuration is set in a .editorconfig file.
To configure the generated code to not use underscores for member names, set the skip_underscores
value to true:
[*.cs]
slowfox_generation.unit_test_mocks.xunit.skip_underscores = true
To create the mocks using the Loose
behaviour (instead of the default of Strict
), set the use_loose
value to be true:
[*.cs]
slowfox_generation.unit_test_mocks.xunit.use_loose = true
These configuration keys are different for NUnit and MSTest. For NUnit the configuration prefix is
slowfox_generation.unit_test_mocks.nunit
and for MSTest the prefix isslowfox_generation.unit_test_mocks.mstest
Can I help fix any bugs with this, or add new capabilities?
Of course! Head on over to the issues page, raise it, and we'll have a chat about what needs to be done.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.0.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on SlowFox.Core:
Package | Downloads |
---|---|
SlowFox.Constructors
A source generator that automatically creates constructors and class members, allowing you to avoid writing and maintaining all that boiler-plate code yourself. |
|
SlowFox.UnitTestMocks.MSTest
A source generator that automatically creates mocks for a unit test class. This generator is for integration with MSTest projects. |
|
SlowFox.UnitTestMocks.xUnit
A source generator that automatically creates mocks for a unit test class. This generator is for integration with xUnit projects. |
|
SlowFox.UnitTestMocks.NUnit
A source generator that automatically creates mocks for a unit test class. This generator is for integration with NUnit projects. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 721 | 5/3/2024 |
1.0.1-CI-20240502-154026 | 198 | 5/2/2024 |
1.0.0 | 1,019 | 4/19/2023 |
1.0.0-CI-20230419-075719 | 774 | 4/19/2023 |
0.3.1 | 572 | 4/18/2023 |
0.3.1-CI-20230418-125027 | 594 | 4/18/2023 |
0.3.1-CI-20230418-104341 | 594 | 4/18/2023 |
0.3.0 | 937 | 2/24/2023 |
0.3.0-CI-20230224-125642 | 833 | 2/24/2023 |
0.2.0 | 1,139 | 5/23/2022 |
0.2.0-CI-20220523-151129 | 766 | 5/23/2022 |