Skip to content

Commit e5abf91

Browse files
Update UnitTester and ObjectExtensions for improvements
- Changed `OptimizationStatus` in `ProcessCollectionToDispose` to `Completed`. - Updated last modified date in `UnitTester.cs`. - Added `System.Diagnostics.CodeAnalysis` using directive. - Enhanced `UnitTester` with `[ExcludeFromCodeCoverage]` and `[DebuggerStepThrough]` attributes. - Added `[NotNull]` attributes to parameters in `PrintToDebug` and `SaveToFile` methods. - Refactored `SaveToFile` and `SaveToFileAsync` to use `this.OutputDirectory`. - Marked `SaveToFileAsync` with `[AsyncStateMachine(typeof(Task))]`. - Introduced `OutputDirectory` property for better access to the output directory.
1 parent 607eb61 commit e5abf91

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

source/dotNetTips.Spargine.8.Extensions/ObjectExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static class ObjectExtensions
6060
/// <param name="items">The collection of items to dispose.</param>
6161
/// <exception cref="ArgumentNullException">Thrown if <paramref name="items"/> is null.</exception>
6262
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
[Information(nameof(ProcessCollectionToDispose), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Optimize, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
63+
[Information(nameof(ProcessCollectionToDispose), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
6464
private static void ProcessCollectionToDispose<T>(IEnumerable<T> items) where T : IDisposable
6565
{
6666
if (items.HasItems())

source/dotNetTips.Spargine.8.Tester/UnitTester.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created : 10-22-2023
55
//
66
// Last Modified By : David McCarter
7-
// Last Modified On : 04-24-2025
7+
// Last Modified On : 04-28-2025
88
// ***********************************************************************
99
// <copyright file="UnitTester.cs" company="David McCarter - dotNetTips.com">
1010
// McCarter Consulting (David McCarter)
@@ -16,6 +16,7 @@
1616
// </summary>
1717
// ***********************************************************************
1818
using System.Diagnostics;
19+
using System.Diagnostics.CodeAnalysis;
1920
using System.Reflection;
2021
using System.Runtime.CompilerServices;
2122
using DotNetTips.Spargine.Core;
@@ -32,8 +33,9 @@ namespace DotNetTips.Spargine.Tester;
3233
/// Initializes a new instance of the <see cref="UnitTester"/> class.
3334
/// </remarks>
3435
/// <param name="outputDirectory">The directory where output files will be saved. Defaults to the current directory if not specified.</param>
36+
[ExcludeFromCodeCoverage]
37+
[DebuggerStepThrough]
3538
[Information(Status = Status.NeedsDocumentation, Documentation = "ADD URL")]
36-
[method: Information(nameof(UnitTester), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.NotRequired, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
3739
public abstract class UnitTester(string outputDirectory = null)
3840
{
3941
/// <summary>
@@ -55,10 +57,7 @@ public abstract class UnitTester(string outputDirectory = null)
5557
[Information(nameof(PropertiesToString), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
5658
protected virtual string PropertiesToString<T>(T input, Func<PropertyInfo, bool> propertySelector)
5759
{
58-
input = input.ArgumentNotNull();
59-
propertySelector = propertySelector.ArgumentNotNull();
60-
61-
return string.Join(", ", input.GetType()
60+
return string.Join(ControlChars.CommaSpace, input.GetType()
6261
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
6362
.Where(propertySelector)
6463
.Select(prop =>
@@ -78,8 +77,9 @@ protected virtual string PropertiesToString<T>(T input, Func<PropertyInfo, bool>
7877
/// The name of the calling method. This is automatically populated by the compiler unless explicitly provided.
7978
/// </param>
8079
/// <exception cref="ArgumentNullException">Thrown when <paramref name="collection"/> or <paramref name="propertySelector"/> is null.</exception>
80+
[DebuggerStepThrough]
8181
[Information(nameof(PrintToDebug), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
82-
public void PrintToDebug<T>(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
82+
public void PrintToDebug<T>([NotNull] IEnumerable<T> collection, [NotNull] Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
8383
{
8484
collection = collection.ArgumentNotNull();
8585
propertySelector = propertySelector.ArgumentNotNull();
@@ -108,8 +108,9 @@ public void PrintToDebug<T>(IEnumerable<T> collection, Func<PropertyInfo, bool>
108108
/// <exception cref="ArgumentNullException">
109109
/// Thrown when <paramref name="input"/> or <paramref name="propertySelector"/> is null.
110110
/// </exception>
111+
[DebuggerStepThrough]
111112
[Information(nameof(PrintToDebug), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
112-
public void PrintToDebug<T>(T input, Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
113+
public void PrintToDebug<T>([NotNull] T input, [NotNull] Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
113114
{
114115
input = input.ArgumentNotNull();
115116
propertySelector = propertySelector.ArgumentNotNull();
@@ -128,14 +129,15 @@ public void PrintToDebug<T>(T input, Func<PropertyInfo, bool> propertySelector,
128129
/// The name of the calling method. This is automatically populated by the compiler unless explicitly provided.
129130
/// </param>
130131
/// <exception cref="ArgumentNullException">Thrown when <paramref name="collection"/> or <paramref name="propertySelector"/> is null.</exception>
132+
[DebuggerStepThrough]
131133
[Information(nameof(SaveToFile), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
132-
public void SaveToFile<T>(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
134+
public void SaveToFile<T>([NotNull] IEnumerable<T> collection, [NotNull] Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
133135
{
134136
collection = collection.ArgumentNotNull();
135137
propertySelector = propertySelector.ArgumentNotNull();
136138
methodName = methodName.ArgumentNotNullOrEmpty();
137139

138-
var filePath = Path.Combine(this._outputDirectory, $"{methodName}.txt");
140+
var filePath = Path.Combine(this.OutputDirectory, $"{methodName}.txt");
139141

140142
var content = collection
141143
.Select(item => this.PropertiesToString(item, propertySelector))
@@ -156,14 +158,15 @@ public void SaveToFile<T>(IEnumerable<T> collection, Func<PropertyInfo, bool> pr
156158
/// <exception cref="ArgumentNullException">
157159
/// Thrown when <paramref name="input"/> or <paramref name="propertySelector"/> is null.
158160
/// </exception>
161+
[DebuggerStepThrough]
159162
[Information(nameof(SaveToFile), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Completed, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
160-
public void SaveToFile<T>(T input, Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
163+
public void SaveToFile<T>([NotNull] T input, [NotNull] Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
161164
{
162165
input = input.ArgumentNotNull();
163166
propertySelector = propertySelector.ArgumentNotNull();
164167
methodName = methodName.ArgumentNotNullOrEmpty();
165168

166-
var filePath = Path.Combine(this._outputDirectory, $"{methodName}.txt");
169+
var filePath = Path.Combine(this.OutputDirectory, $"{methodName}.txt");
167170

168171
var content = this.PropertiesToString(input, propertySelector);
169172
File.WriteAllText(filePath, content);
@@ -185,19 +188,29 @@ public void SaveToFile<T>(T input, Func<PropertyInfo, bool> propertySelector, [C
185188
/// <exception cref="ArgumentException">
186189
/// Thrown when <paramref name="methodName"/> is null or empty.
187190
/// </exception>
191+
[AsyncStateMachine(typeof(Task))]
192+
[DebuggerStepThrough]
188193
[Information(nameof(SaveToFileAsync), UnitTestStatus = UnitTestStatus.None, OptimizationStatus = OptimizationStatus.Optimize, BenchmarkStatus = BenchmarkStatus.NotRequired, Status = Status.New)]
189-
public async Task SaveToFileAsync<T>(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
194+
public async Task SaveToFileAsync<T>([NotNull] IEnumerable<T> collection, [NotNull] Func<PropertyInfo, bool> propertySelector, [CallerMemberName] string methodName = ControlChars.EmptyString)
190195
{
191196
collection = collection.ArgumentNotNull();
192197
propertySelector = propertySelector.ArgumentNotNull();
193198
methodName = methodName.ArgumentNotNullOrEmpty();
194199

195-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"{methodName}.txt");
200+
var filePath = Path.Combine(this.OutputDirectory, $"{methodName}.txt");
196201

197202
var content = collection
198203
.Select(item => this.PropertiesToString(item, propertySelector))
199204
.ToArray();
200205

201206
await File.WriteAllLinesAsync(filePath, content, CancellationToken.None).ConfigureAwait(false);
202207
}
208+
209+
/// <summary>
210+
/// Gets the output directory where files will be saved.
211+
/// </summary>
212+
/// <value>
213+
/// A string representing the directory path where output files will be saved.
214+
/// </value>
215+
public string OutputDirectory => this._outputDirectory;
203216
}

0 commit comments

Comments
 (0)