• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JaCraig / Mecha / 12871043181

20 Jan 2025 03:17PM UTC coverage: 81.554% (-0.3%) from 81.902%
12871043181

push

github

JaCraig
feat(core): enhance exception handling and refactor code

This commit introduces several enhancements and refactorings across multiple files to improve exception handling, code readability, and consistency.

- Added `ArgumentThrowingClass` to `ExceptionHandlerTests.cs` to test `ArgumentNullException.ThrowIfNull`.
- Added two new test methods `IgnoreException` and `IgnoreExceptionCalledFromDotThrowsX` in `ExceptionHandlerTests.cs`.
- Updated `coverlet.msbuild` and `coverlet.collector` package versions in `Mecha.Core.Tests.csproj` from `6.0.3` to `6.0.4`.
- Replaced array initialization using `Array.Empty<T>()` with `[]` in multiple files for brevity.
- Modified `ExceptionHandler` class to handle nullable exceptions and methods, and added a new method `IsFromThrowsMethodOnException`. This check looks for exceptions that are thrown by calling the .ThrowIfX methods on exception types.
- Updated `Services` class to use nullable `IServiceProvider` and renamed `ServiceProviderLock` to `_ServiceProviderLock`.
- Refactored constructors in `FileStreamGenerator`, `HttpClientGenerator`, `SpecialGenerator`, and `StreamGenerator` to use new C# 9.0 record-like syntax.
- Updated `GeneratorManager` constructor to use new C# 9.0 record-like syntax and replaced array initialization with `[.. generators.OrderBy(x => x.Order)]`.
- Changed `ParameterValues` class to handle nullable `ParameterValue` and refactored the `Same` method for better readability.
- Updated `Mech` class to handle nullable `ServiceProvider`.
- Refactored `AttemptToSubstitute` method in `Mech` class to use new C# 9.0 collection initialization syntax.
- Updated `MutatorManager` constructor to use new C# 9.0 collection initialization syntax.
- Changed `RunnerBaseClass` constructor to public and updated `DefaultRunner` to use it.
- Updated `MethodInvoker<TTarget>` to use a constructor with `MethodInfo` parameter.
- Changed loop variable names for consistency.
- Updated several methods to use shorthand ar... (continued)

745 of 1052 branches covered (70.82%)

Branch coverage included in aggregate %.

94 of 109 new or added lines in 22 files covered. (86.24%)

2 existing lines in 2 files now uncovered.

1333 of 1496 relevant lines covered (89.1%)

6421052.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.89
/Mecha.Core/Generator/DefaultGenerators/SpecialGenerator.cs
1
using Fast.Activator;
2
using Mecha.Core.ExtensionMethods;
3
using Mecha.Core.Generator.Interfaces;
4
using System;
5
using System.Linq;
6
using System.Reflection;
7

8
namespace Mecha.Core.Generator.DefaultGenerators
9
{
10
    /// <summary>
11
    /// Special generator class.
12
    /// </summary>
13
    /// <seealso cref="IGenerator"/>
14
    /// <remarks>Initializes a new instance of the <see cref="SpecialGenerator"/> class.</remarks>
15
    /// <param name="random">The random generator.</param>
16
    public class SpecialGenerator(Mirage.Random? random) : IGenerator
14✔
17
    {
18
        /// <summary>
19
        /// Gets the order.
20
        /// </summary>
21
        /// <value>The order.</value>
22
        public int Order => int.MinValue;
3✔
23

24
        /// <summary>
25
        /// Gets the random generator.
26
        /// </summary>
27
        private Mirage.Random? Random { get; } = random;
180✔
28

29
        /// <summary>
30
        /// Determines whether this instance can generate the specified parameter.
31
        /// </summary>
32
        /// <param name="parameter">The parameter.</param>
33
        /// <returns>
34
        /// <c>true</c> if this instance can generate the specified parameter; otherwise, <c>false</c>.
35
        /// </returns>
36
        public bool CanGenerate(ParameterInfo? parameter) => parameter?.ParameterType.IsSpecialType(out Type? _) == true;
21,329,283✔
37

38
        /// <summary>
39
        /// Generates the next object of the specified parameter type.
40
        /// </summary>
41
        /// <param name="parameter">The parameter.</param>
42
        /// <param name="min">The minimum.</param>
43
        /// <param name="max">The maximum.</param>
44
        /// <returns>The next object.</returns>
45
        public ParameterValue Next(ParameterInfo? parameter, object? min, object? max)
46
        {
47
            if (parameter is null || !CanGenerate(parameter))
167✔
48
                return new ParameterValue("Special Case Generator", null);
1✔
49
            var Amount = Random?.Next(0, 100) ?? 0;
166!
50
            Type? ArrayType = parameter.ParameterType.GetUnderlyingArrayType();
166✔
51
            Type? ElementType = ArrayType?.GetElementType();
166!
52
            var ArrayInstance = (Array)FastActivator.CreateInstance(ArrayType, [Amount]);
166✔
53
            if (ElementType?.GetConstructors().Any(IsDefaultConstructor) != true)
166!
54
                return new ParameterValue("Special Case Generator", ArrayInstance);
166✔
55
            var Index = 0;
×
NEW
56
            foreach (var Item in Random?.Next(ElementType, Amount) ?? [])
×
57
            {
58
                ArrayInstance.SetValue(Item, Index);
×
59
                ++Index;
×
60
            }
61
            return new ParameterValue("Special Case Generator", ArrayInstance);
×
62
        }
63

64
        /// <summary>
65
        /// Determines whether [is default constructor] [the specified constructor].
66
        /// </summary>
67
        /// <param name="constructor">The constructor.</param>
68
        /// <returns>
69
        /// <c>true</c> if [is default constructor] [the specified constructor]; otherwise, <c>false</c>.
70
        /// </returns>
71
        private static bool IsDefaultConstructor(ConstructorInfo constructor) => constructor.GetParameters().Length == 0;
×
72
    }
73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc