Skip to content

Commit 9227418

Browse files
fflatennohwnd
andauthored
Address analyzer suggestions for csharp-project (#2307)
Co-authored-by: Jakub Jareš <[email protected]>
1 parent bd7bfe7 commit 9227418

23 files changed

+66
-117
lines changed

src/csharp/Pester/BoolOption.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ public class BoolOption : Option<bool>
2121
{
2222
public BoolOption(BoolOption option, bool value) : base(option, value)
2323
{
24-
2524
}
2625

2726
public BoolOption(string description, bool defaultValue) : base(description, defaultValue, defaultValue)
2827
{
29-
3028
}
3129

3230
public BoolOption(string description, bool defaultValue, bool value) : base(description, defaultValue, value)
3331
{
34-
3532
}
3633

3734
public static implicit operator BoolOption(bool value)

src/csharp/Pester/CodeCoverageConfiguration.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class CodeCoverageConfiguration : ConfigurationSection
3232
private BoolOption _singleHitBreakpoints;
3333
private DecimalOption _coveragePercentTarget;
3434

35-
3635
public static CodeCoverageConfiguration Default { get { return new CodeCoverageConfiguration(); } }
3736

3837
public static CodeCoverageConfiguration ShallowClone(CodeCoverageConfiguration configuration)
@@ -182,7 +181,6 @@ public BoolOption RecursePaths
182181
}
183182
}
184183

185-
186184
public DecimalOption CoveragePercentTarget
187185
{
188186
get { return _coveragePercentTarget; }
@@ -199,7 +197,6 @@ public DecimalOption CoveragePercentTarget
199197
}
200198
}
201199

202-
203200
public BoolOption UseBreakpoints
204201
{
205202
get { return _useBps; }

src/csharp/Pester/ConfigurationSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Pester
1919
{
2020
public abstract class ConfigurationSection
2121
{
22-
private string _description;
22+
private readonly string _description;
2323
public ConfigurationSection(string description)
2424
{
2525
_description = description;

src/csharp/Pester/ContainerInfoArrayOption.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,34 @@ public class ContainerInfoArrayOption : Option<ContainerInfo[]>
2424
{
2525
public ContainerInfoArrayOption(ContainerInfoArrayOption option, ContainerInfo[] value) : base(option, value)
2626
{
27-
2827
}
2928

3029
public ContainerInfoArrayOption(string description, ContainerInfo[] defaultValue) : base(description, defaultValue, defaultValue)
3130
{
32-
3331
}
3432

3533
public ContainerInfoArrayOption(string description, ContainerInfo[] defaultValue, ContainerInfo[] value) : base(description, defaultValue, value)
3634
{
37-
3835
}
3936

4037
public ContainerInfoArrayOption(object[] value) : base("", new ContainerInfo[0], value.Cast<ContainerInfo>().ToArray())
4138
{
42-
4339
}
4440

4541
public ContainerInfoArrayOption(ContainerInfo[] value) : base("", new ContainerInfo[0], value)
4642
{
47-
4843
}
4944

5045
public ContainerInfoArrayOption(List<object> value) : base("", new ContainerInfo[0], value.Cast<ContainerInfo>().ToArray())
5146
{
52-
5347
}
5448

5549
public ContainerInfoArrayOption(List<ContainerInfo> value) : base("", new ContainerInfo[0], value.ToArray())
5650
{
57-
5851
}
5952

6053
public ContainerInfoArrayOption(ContainerInfo value) : this(new ContainerInfo[] { value })
6154
{
62-
6355
}
6456

6557
public static implicit operator ContainerInfoArrayOption(ContainerInfo[] value)

src/csharp/Pester/Coverage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public static CodeCoverage Create()
2020
public long CommandsMissedCount { get; set; }
2121
public long FilesAnalyzedCount { get; set; }
2222

23-
public List<object> CommandsMissed = new List<object>();
24-
public List<object> CommandsExecuted = new List<object>();
25-
public List<object> FilesAnalyzed = new List<object>();
23+
public List<object> CommandsMissed = new();
24+
public List<object> CommandsExecuted = new();
25+
public List<object> FilesAnalyzed = new();
2626

2727
public override string ToString()
2828
{

src/csharp/Pester/DecimalOption.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ public class DecimalOption : Option<decimal>
2121
{
2222
public DecimalOption(DecimalOption option, decimal value) : base(option, value)
2323
{
24-
2524
}
2625

2726
public DecimalOption(string description, decimal defaultValue) : base(description, defaultValue, defaultValue)
2827
{
29-
3028
}
3129

3230
public DecimalOption(string description, decimal defaultValue, decimal value) : base(description, defaultValue, value)
3331
{
34-
3532
}
3633

3734
public static implicit operator DecimalOption(decimal value)

src/csharp/Pester/DictionaryExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public static IDictionary GetIDictionaryOrNull(this IDictionary dictionary, stri
5757
if (!dictionary.Contains(key))
5858
return null;
5959

60-
if (dictionary[key] is PSObject)
60+
if (dictionary[key] is PSObject pso)
6161
{
62-
return ((PSObject)dictionary[key]).BaseObject as IDictionary;
62+
return pso.BaseObject as IDictionary;
6363
}
6464
else
6565
{
@@ -96,9 +96,9 @@ public static T[] GetArrayOrNull<T>(this IDictionary dictionary, string key) whe
9696
var i = 0;
9797
foreach (var j in v)
9898
{
99-
if (j is T)
99+
if (j is T t)
100100
{
101-
arr[i] = (T) j;
101+
arr[i] = t;
102102
}
103103

104104
if (j is PSObject || j is PathInfo)

src/csharp/Pester/Factory.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ public static ErrorRecord CreateShouldErrorRecord(string message, string file, s
4242
public static ErrorRecord CreateErrorRecord(string errorId, string message, string file, string line, string lineText, bool terminating)
4343
{
4444
var exception = new Exception(message);
45-
var errorCategory = ErrorCategory.InvalidResult;
4645
// we use ErrorRecord.TargetObject to pass structured information about the error to a reporting system.
47-
var targetObject = new Dictionary<string, object> { };
48-
targetObject.Add("Message", message);
49-
targetObject.Add("File", file);
50-
targetObject.Add("Line", line);
51-
targetObject.Add("LineText", lineText);
52-
targetObject.Add("Terminating", terminating);
53-
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, targetObject);
54-
return errorRecord;
46+
var targetObject = new Dictionary<string, object>
47+
{
48+
["Message"] = message,
49+
["File"] = file,
50+
["Line"] = line,
51+
["LineText"] = lineText,
52+
["Terminating"] = terminating,
53+
};
54+
return new ErrorRecord(exception, errorId, ErrorCategory.InvalidResult, targetObject);
5555
}
5656

5757
public static StringBuilder CreateStringBuilder()

src/csharp/Pester/IntOption.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ public class IntOption : Option<int>
2121
{
2222
public IntOption(IntOption option, int value) : base(option, value)
2323
{
24-
2524
}
2625

2726
public IntOption(string description, int defaultValue) : base(description, defaultValue, defaultValue)
2827
{
29-
3028
}
3129

3230
public IntOption(string description, int defaultValue, int value) : base(description, defaultValue, value)
3331
{
34-
3532
}
3633

3734
public static implicit operator IntOption(int value)

src/csharp/Pester/PesterConfigurationDeserializer.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class PesterConfigurationDeserializer : PSTypeConverter
88
{
99
public override bool CanConvertFrom(object sourceValue, Type destinationType)
1010
{
11-
if (!(sourceValue is PSObject))
11+
if (sourceValue is not PSObject)
1212
return false;
1313

1414
return ((PSObject)sourceValue).TypeNames.Contains("Deserialized.PesterConfiguration");
@@ -31,15 +31,15 @@ public override object ConvertTo(object sourceValue, Type destinationType, IForm
3131

3232
private PesterConfiguration ConvertToPesterConfiguration(object sourceValue)
3333
{
34-
if (sourceValue is IDictionary)
35-
return new PesterConfiguration((IDictionary)sourceValue);
34+
if (sourceValue is IDictionary dictionary)
35+
return new PesterConfiguration(dictionary);
3636

3737
return new PesterConfiguration(ConvertToConfigurationHashtable((PSObject)sourceValue));
3838
}
3939

4040
private Hashtable ConvertToConfigurationHashtable(PSObject sourceConfiguration)
4141
{
42-
Hashtable configuration = new Hashtable();
42+
Hashtable configuration = new();
4343

4444
foreach (var property in sourceConfiguration.Properties)
4545
{
@@ -57,18 +57,18 @@ private Hashtable ConvertToConfigurationHashtable(PSObject sourceConfiguration)
5757

5858
private Hashtable ConvertToSectionHashtable(PSObject sourceSection, string sectionName)
5959
{
60-
Hashtable configurationSection = new Hashtable();
60+
Hashtable configurationSection = new();
6161

6262
foreach (var property in sourceSection.Properties)
6363
{
6464
var IsModified = ((PSObject)property.Value).Properties["IsModified"];
65-
65+
6666
// Doing this instead of IsModified -> Add to be compatible with saved PesterConfigurations from previous versions
6767
// Consider rewriting in next major release
6868
if (IsModified != null && !((bool)IsModified.Value)) {
6969
continue;
7070
}
71-
71+
7272
configurationSection.Add(
7373
property.Name,
7474
GetPropertyValue(
@@ -86,8 +86,8 @@ private object GetPropertyValue(PSObject sourceItem, string sectionName, string
8686
{
8787
var value = sourceItem.Properties["Value"].Value;
8888

89-
if (value is PSObject)
90-
value = ((PSObject)value).BaseObject;
89+
if (value is PSObject pso)
90+
value = pso.BaseObject;
9191

9292
if (value == null)
9393
return null;
@@ -96,7 +96,7 @@ private object GetPropertyValue(PSObject sourceItem, string sectionName, string
9696

9797
if (expectedType == typeof(ScriptBlock[]))
9898
{
99-
ArrayList scriptBlocks = new ArrayList();
99+
ArrayList scriptBlocks = new();
100100
foreach (string scriptBlock in (ArrayList)value)
101101
{
102102
scriptBlocks.Add(ScriptBlock.Create(scriptBlock));
@@ -106,7 +106,7 @@ private object GetPropertyValue(PSObject sourceItem, string sectionName, string
106106

107107
if (expectedType == typeof(ContainerInfo[]))
108108
{
109-
ArrayList containers = new ArrayList();
109+
ArrayList containers = new();
110110
foreach (PSObject container in (ArrayList)value)
111111
{
112112
var containerInfo = Pester.ContainerInfo.Create();
@@ -119,8 +119,8 @@ private object GetPropertyValue(PSObject sourceItem, string sectionName, string
119119
value = containers;
120120
}
121121

122-
if (value is ArrayList)
123-
value = ((ArrayList)value).ToArray();
122+
if (value is ArrayList list)
123+
value = list.ToArray();
124124

125125
return value;
126126
}

0 commit comments

Comments
 (0)