• 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

82.05
/Mecha.Core/Generator/DefaultGenerators/DefaultValueGenerator.cs
1
/*
2
Copyright 2021 James Craig
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
using Fast.Activator;
18
using Mecha.Core.ExtensionMethods;
19
using Mecha.Core.Generator.DefaultGenerators.Utils;
20
using Mecha.Core.Generator.Interfaces;
21
using System.Diagnostics.CodeAnalysis;
22
using System.Reflection;
23

24
namespace Mecha.Core.Generator.DefaultGenerators
25
{
26
    /// <summary>
27
    /// Default value generator
28
    /// </summary>
29
    /// <seealso cref="IGenerator"/>
30
    public class DefaultValueGenerator : IGenerator
31
    {
32
        /// <summary>
33
        /// Gets the order.
34
        /// </summary>
35
        /// <value>The order.</value>
36
        public int Order => int.MinValue;
3✔
37

38
        /// <summary>
39
        /// Determines whether this instance can generate the specified parameter.
40
        /// </summary>
41
        /// <param name="parameter">The parameter.</param>
42
        /// <returns>
43
        /// <c>true</c> if this instance can generate the specified parameter; otherwise, <c>false</c>.
44
        /// </returns>
45
        public bool CanGenerate(ParameterInfo? parameter) => (!parameter?.HasDefaultValue ?? false)
31,044,397!
46
            && (!parameter?.ParameterType.IsSpecialType(out System.Type? _) ?? false);
31,044,397✔
47

48
        /// <summary>
49
        /// Generates the next object of the specified parameter type.
50
        /// </summary>
51
        /// <param name="parameter">The parameter.</param>
52
        /// <param name="min">The minimum.</param>
53
        /// <param name="max">The maximum.</param>
54
        /// <returns>The next object.</returns>
55
        public ParameterValue? Next(ParameterInfo? parameter, object? min, object? max)
56
        {
57
            if (parameter is null || !CanGenerate(parameter))
9,715,144✔
58
                return new ParameterValue("DefaultValue Generator", null);
2✔
59
            DisallowNullAttribute? NotNullable = parameter.GetCustomAttribute<DisallowNullAttribute>();
9,715,142✔
60
            System.Type? ResultType = parameter.ParameterType;
9,715,142✔
61
            if (!ResultType.IsValueType && NotNullable is null)
9,715,142✔
62
                return new("DefaultValue Generator", null);
9,655,485✔
63
            if (ResultType.IsSpecialType(out System.Type? _))
59,657!
64
                ResultType = ResultType.GetUnderlyingArrayType();
×
65
            if (ResultType is null)
59,657!
NEW
66
                return new("DefaultValue Generator", null);
×
67
            try
68
            {
69
                return new ParameterValue("DefaultValue Generator", DefaultValueLookup.Values.TryGetValue(ResultType.GetHashCode(), out var ReturnValue)
59,657✔
70
                    ? ReturnValue
59,657✔
71
                    : FastActivator.CreateInstance(ResultType));
59,657✔
72
            }
73
            catch
×
74
            {
75
                return new ParameterValue("DefaultValue Generator", null);
×
76
            }
77
        }
59,657✔
78
    }
79
}
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