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

JaCraig / Aspectus / 15117626527

19 May 2025 03:55PM UTC coverage: 81.842% (+0.4%) from 81.41%
15117626527

push

github

JaCraig
fix(tests): update logging to prevent null reference errors

Refactor logging statements in CompilerTests.cs to use the null-conditional operator. This change enhances the robustness of the tests by preventing potential `NullReferenceException` when the `Logger` is null.

- Updated logging in `CreateMultipleTypes`, `CreateType`, and `Creation` methods.
- Used null-conditional operator (`?.`) for safer logging calls.

230 of 328 branches covered (70.12%)

Branch coverage included in aggregate %.

392 of 432 relevant lines covered (90.74%)

2283368.23 hits per line

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

69.48
/Aspectus/HelperFunctions/ExtensionMethods.cs
1
/*
2
Copyright 2016 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 Microsoft.Extensions.ObjectPool;
18
using System;
19
using System.Collections.Generic;
20
using System.Globalization;
21
using System.Linq;
22
using System.Text;
23

24
namespace Aspectus.HelperFunctions
25
{
26
    /// <summary>
27
    /// Extension methods
28
    /// </summary>
29
    internal static class ExtensionMethods
30
    {
31
        /// <summary>
32
        /// Adds items to the collection if it passes the predicate test
33
        /// </summary>
34
        /// <typeparam name="T">Collection type</typeparam>
35
        /// <param name="collection">Collection to add to</param>
36
        /// <param name="predicate">Predicate that an item needs to satisfy in order to be added</param>
37
        /// <param name="items">Items to add to the collection</param>
38
        /// <returns>True if any are added, false otherwise</returns>
39
        public static bool AddIf<T>(this ICollection<T> collection, Predicate<T> predicate, params T[] items)
40
        {
41
            if (collection is null || predicate is null)
30,827,462!
42
                return false;
×
43
            if (items is null || items.Length == 0)
30,827,462✔
44
                return true;
9✔
45
            var ReturnValue = false;
30,827,453✔
46
            for (int I = 0, ItemsLength = items.Length; I < ItemsLength; I++)
154,138,467✔
47
            {
48
                T? Item = items[I];
30,828,054✔
49
                if (predicate(Item))
30,828,054✔
50
                {
51
                    collection.Add(Item);
606✔
52
                    ReturnValue = true;
606✔
53
                }
54
            }
55

56
            return ReturnValue;
30,827,453✔
57
        }
58

59
        /// <summary>
60
        /// Adds an item to the collection if it isn't already in the collection
61
        /// </summary>
62
        /// <typeparam name="T">Collection type</typeparam>
63
        /// <param name="collection">Collection to add to</param>
64
        /// <param name="predicate">Predicate that an item needs to satisfy in order to be added</param>
65
        /// <param name="items">Items to add to the collection</param>
66
        /// <returns>True if it is added, false otherwise</returns>
67
        public static bool AddIf<T>(this ICollection<T> collection, Predicate<T> predicate, IEnumerable<T> items) => collection is not null && predicate is not null && (items?.Any() != true || collection.AddIf(predicate, [.. items]));
23!
68

69
        /// <summary>
70
        /// Adds an item to the collection if it isn't already in the collection
71
        /// </summary>
72
        /// <typeparam name="T">Collection type</typeparam>
73
        /// <param name="collection">Collection to add to</param>
74
        /// <param name="predicate">
75
        /// Predicate used to determine if two values are equal. Return true if they are the same,
76
        /// false otherwise
77
        /// </param>
78
        /// <param name="items">Items to add to the collection</param>
79
        /// <returns>True if it is added, false otherwise</returns>
80
        public static bool AddIfUnique<T>(this ICollection<T> collection, Func<T, T, bool> predicate, params T[] items) => collection is not null && predicate is not null && (items is null || collection.AddIf(x => !collection.Any(y => predicate(x, y)), items));
92,487,166!
81

82
        /// <summary>
83
        /// Adds an item to the collection if it isn't already in the collection
84
        /// </summary>
85
        /// <typeparam name="T">Collection type</typeparam>
86
        /// <param name="collection">Collection to add to</param>
87
        /// <param name="items">Items to add to the collection</param>
88
        /// <returns>True if it is added, false otherwise</returns>
89
        public static bool AddIfUnique<T>(this ICollection<T> collection, IEnumerable<T> items) => collection is not null && (items is null || collection.AddIf(x => !collection.Contains(x), items));
76!
90

91
        /// <summary>
92
        /// Adds an item to the collection if it isn't already in the collection
93
        /// </summary>
94
        /// <typeparam name="T">Collection type</typeparam>
95
        /// <param name="collection">Collection to add to</param>
96
        /// <param name="items">Items to add to the collection</param>
97
        /// <returns>True if it is added, false otherwise</returns>
98
        public static bool AddIfUnique<T>(this ICollection<T> collection, params T[] items) => collection is not null && (items is null || collection.AddIf(x => !collection.Contains(x), items));
806!
99

100
        /// <summary>
101
        /// Does an AppendFormat and then an AppendLine on the StringBuilder
102
        /// </summary>
103
        /// <param name="builder">Builder object</param>
104
        /// <param name="provider">Format provider (CultureInfo) to use</param>
105
        /// <param name="format">Format string</param>
106
        /// <param name="objects">Objects to format</param>
107
        /// <returns>The StringBuilder passed in</returns>
108
        public static StringBuilder? AppendLineFormat(this StringBuilder builder, IFormatProvider provider, string format, params object[] objects)
109
        {
110
            if (builder is null || string.IsNullOrEmpty(format))
369!
111
                return builder;
×
112
            objects ??= [];
369!
113
            provider ??= CultureInfo.InvariantCulture;
369!
114
            return builder.AppendFormat(provider, format, objects).AppendLine();
369✔
115
        }
116

117
        /// <summary>
118
        /// Does an AppendFormat and then an AppendLine on the StringBuilder
119
        /// </summary>
120
        /// <param name="builder">Builder object</param>
121
        /// <param name="format">Format string</param>
122
        /// <param name="objects">Objects to format</param>
123
        /// <returns>The StringBuilder passed in</returns>
124
        public static StringBuilder? AppendLineFormat(this StringBuilder builder, string format, params object[] objects) => builder.AppendLineFormat(CultureInfo.InvariantCulture, format, objects);
369✔
125

126
        /// <summary>
127
        /// Does an action for each item in the IEnumerable
128
        /// </summary>
129
        /// <typeparam name="T">Object type</typeparam>
130
        /// <param name="list">IEnumerable to iterate over</param>
131
        /// <param name="action">Action to do</param>
132
        /// <returns>The original list</returns>
133
        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> list, Action<T> action)
134
        {
135
            if (list is null)
30,842,054!
136
                return [];
×
137
            if (action is null)
30,842,054!
138
                return list;
×
139
            foreach (T? Item in list)
123,288,382✔
140
                action(Item);
30,802,137✔
141
            return list;
30,842,054✔
142
        }
143

144
        /// <summary>
145
        /// Does a function for each item in the IEnumerable, returning a list of the results
146
        /// </summary>
147
        /// <typeparam name="T">Object type</typeparam>
148
        /// <typeparam name="TResult">Return type</typeparam>
149
        /// <param name="list">IEnumerable to iterate over</param>
150
        /// <param name="function">Function to do</param>
151
        /// <returns>The resulting list</returns>
152
        public static IEnumerable<TResult> ForEach<T, TResult>(this IEnumerable<T> list, Func<T, TResult> function)
153
        {
154
            if (list is null || function is null)
9!
155
                return [];
×
156
            var ReturnList = new List<TResult>(list.Count());
9✔
157
            foreach (T? Item in list)
506✔
158
                ReturnList.Add(function(Item));
244✔
159
            return ReturnList;
9✔
160
        }
161

162
        /// <summary>
163
        /// Returns the type's name (Actual C# name, not the funky version from the Name property)
164
        /// </summary>
165
        /// <param name="objectType">Type to get the name of</param>
166
        /// <param name="objectPool">The object pool.</param>
167
        /// <returns>string name of the type</returns>
168
        public static string GetName(this Type objectType, ObjectPool<StringBuilder>? objectPool)
169
        {
170
            if (objectType is null)
318!
171
                return string.Empty;
×
172
            StringBuilder Output = objectPool?.Get() ?? new StringBuilder();
318!
173
            if (objectType.Name == "Void")
318!
174
            {
175
                objectPool?.Return(Output);
×
176
                return "void";
×
177
            }
178
            else
179
            {
180
                _ = Output.Append(objectType.DeclaringType is null ? objectType.Namespace : objectType.DeclaringType.GetName(objectPool))
318!
181
                    .Append('.');
318✔
182
                if (objectType.Name.Contains('`', StringComparison.Ordinal))
318✔
183
                {
184
                    Type[] GenericTypes = objectType.GetGenericArguments();
30✔
185
                    _ = Output.Append(objectType.Name, 0, objectType.Name.IndexOf('`'))
30✔
186
                        .Append('<');
30✔
187
                    var Seperator = string.Empty;
30✔
188
                    foreach (Type GenericType in GenericTypes)
120✔
189
                    {
190
                        _ = Output.Append(Seperator).Append(GenericType.GetName(objectPool));
30✔
191
                        Seperator = ",";
30✔
192
                    }
193
                    _ = Output.Append('>');
30✔
194
                }
195
                else
196
                {
197
                    _ = Output.Append(objectType.Name);
288✔
198
                }
199
            }
200
            var ReturnValue = Output.ToString().Replace("&", string.Empty, StringComparison.Ordinal);
318✔
201
            objectPool?.Return(Output);
318✔
202
            return ReturnValue;
318✔
203
        }
204

205
        /// <summary>
206
        /// Determines if the type has a default constructor
207
        /// </summary>
208
        /// <param name="type">Type to check</param>
209
        /// <returns>True if it does, false otherwise</returns>
210
        public static bool HasDefaultConstructor(this Type type)
211
        {
212
            return type?
50!
213
                .GetConstructors()
50✔
214
                .Any(x => x.GetParameters().Length == 0) == true;
100✔
215
        }
216

217
        /// <summary>
218
        /// Converts the list to a string where each item is seperated by the Seperator
219
        /// </summary>
220
        /// <typeparam name="T">Item type</typeparam>
221
        /// <param name="list">List to convert</param>
222
        /// <param name="itemOutput">
223
        /// Used to convert the item to a string (defaults to calling ToString)
224
        /// </param>
225
        /// <param name="seperator">Seperator to use between items (defaults to ,)</param>
226
        /// <returns>The string version of the list</returns>
227
        public static string ToString<T>(this IEnumerable<T> list, Func<T, string>? itemOutput = null, string seperator = ",")
228
        {
229
            if (list?.Any() != true)
736!
230
            {
231
                return string.Empty;
209✔
232
            }
233

234
            seperator ??= string.Empty;
527!
235
            itemOutput ??= DefaultToStringConverter;
527!
236
            return string.Join(seperator, list.Select(itemOutput));
527✔
237
        }
238

239
        /// <summary>
240
        /// Default to string converter.
241
        /// </summary>
242
        /// <typeparam name="TValue">The type of the value.</typeparam>
243
        /// <param name="value">The value.</param>
244
        /// <returns>The value as a string</returns>
245
        private static string DefaultToStringConverter<TValue>(TValue value) => value?.ToString() ?? string.Empty;
×
246
    }
247
}
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