| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace DirectSight.Parser.Analysis; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a metric, which is a key/value pair. |
| | | 7 | | /// </summary> |
| | | 8 | | public class Metric |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// The cyclomatic complexity URI. |
| | | 12 | | /// </summary> |
| | 1 | 13 | | private static readonly Uri CyclomaticComplexityUri = new Uri("https://en.wikipedia.org/wiki/Cyclomatic_complexity") |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The code coverage URI. |
| | | 17 | | /// </summary> |
| | 1 | 18 | | private static readonly Uri CodeCoverageUri = new Uri("https://en.wikipedia.org/wiki/Code_coverage"); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The n path complexity URI. |
| | | 22 | | /// </summary> |
| | 1 | 23 | | private static readonly Uri NPathComplexityUri = new Uri("https://modess.io/npath-complexity-cyclomatic-complexity-e |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The crap score URI. |
| | | 27 | | /// </summary> |
| | 1 | 28 | | private static readonly Uri CrapScoreUri = new Uri("https://googletesting.blogspot.de/2011/02/this-code-is-crap.html |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The MC/DC Uri. |
| | | 32 | | /// </summary> |
| | 1 | 33 | | private static readonly Uri McdcCoverageUri = new Uri("https://en.wikipedia.org/wiki/Modified_condition/decision_cov |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="Metric"/> class. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="name">The name.</param> |
| | | 39 | | /// <param name="explanationUrl">The explanation url.</param> |
| | | 40 | | /// <param name="metricType">The type of the metric.</param> |
| | | 41 | | /// <param name="value">The value.</param> |
| | | 42 | | public Metric(string name, Uri explanationUrl, MetricType metricType, decimal? value) |
| | 10 | 43 | | : this(name, name, explanationUrl, metricType, value) |
| | 10 | 44 | | { |
| | 10 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="Metric"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="name">The name.</param> |
| | | 51 | | /// <param name="abbreviation">The abbreviation.</param> |
| | | 52 | | /// <param name="explanationUrl">The explanation url.</param> |
| | | 53 | | /// <param name="metricType">The type of the metric.</param> |
| | | 54 | | /// <param name="value">The value.</param> |
| | 14743 | 55 | | public Metric(string name, string abbreviation, Uri explanationUrl, MetricType metricType, decimal? value) |
| | 14743 | 56 | | { |
| | 14743 | 57 | | this.Name = name ?? throw new ArgumentNullException(nameof(name)); |
| | 14743 | 58 | | this.Abbreviation = abbreviation.ToLowerInvariant().Replace(" ", string.Empty); |
| | 14743 | 59 | | this.ExplanationUrl = explanationUrl; |
| | 14743 | 60 | | this.MetricType = metricType; |
| | 14743 | 61 | | this.Value = value; |
| | 14743 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Initializes a new instance of the <see cref="Metric"/> class. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="name">The name.</param> |
| | | 68 | | /// <param name="explanationUrl">The explanation url.</param> |
| | | 69 | | /// <param name="metricType">The type of the metric.</param> |
| | | 70 | | /// <param name="value">The value.</param> |
| | | 71 | | /// <param name="mergeOrder">The merge order.</param> |
| | | 72 | | public Metric(string name, Uri explanationUrl, MetricType metricType, decimal? value, MetricMergeOrder mergeOrder) |
| | 2 | 73 | | : this(name, name, explanationUrl, metricType, value) |
| | 2 | 74 | | { |
| | 2 | 75 | | this.MergeOrder = mergeOrder; |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Initializes a new instance of the <see cref="Metric"/> class. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="name">The name.</param> |
| | | 82 | | /// <param name="abbreviation">The abbreviation.</param> |
| | | 83 | | /// <param name="explanationUrl">The explanation url.</param> |
| | | 84 | | /// <param name="metricType">The type of the metric.</param> |
| | | 85 | | /// <param name="value">The value.</param> |
| | | 86 | | /// <param name="mergeOrder">The merge order.</param> |
| | | 87 | | public Metric(string name, string abbreviation, Uri explanationUrl, MetricType metricType, decimal? value, MetricMer |
| | 8757 | 88 | | : this(name, abbreviation, explanationUrl, metricType, value) |
| | 8757 | 89 | | { |
| | 8757 | 90 | | this.MergeOrder = mergeOrder; |
| | 8757 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the merge order. |
| | | 95 | | /// </summary> |
| | 3655 | 96 | | public MetricMergeOrder MergeOrder { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the name. |
| | | 100 | | /// </summary> |
| | 25651 | 101 | | public string Name { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the abbreviation. |
| | | 105 | | /// </summary> |
| | 10 | 106 | | public string Abbreviation { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the explanation url. |
| | | 110 | | /// </summary> |
| | 290 | 111 | | public Uri ExplanationUrl { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the metric type. |
| | | 115 | | /// </summary> |
| | 792 | 116 | | public MetricType MetricType { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the value. |
| | | 120 | | /// </summary> |
| | 34341 | 121 | | public decimal? Value { get; internal set; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Initializes a new instance of the <see cref="Metric"/> class which represents branch coverage. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="value">The value.</param> |
| | | 127 | | /// <returns>The metric.</returns> |
| | | 128 | | public static Metric BranchCoverage(decimal? value) |
| | 2987 | 129 | | { |
| | 2987 | 130 | | return new Metric( |
| | 2987 | 131 | | "Branch coverage", |
| | 2987 | 132 | | "bcov", |
| | 2987 | 133 | | CodeCoverageUri, |
| | 2987 | 134 | | MetricType.CoveragePercentual, |
| | 2987 | 135 | | value); |
| | 2987 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Initializes a new instance of the <see cref="Metric"/> class which represents sequence coverage. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="value">The value.</param> |
| | | 142 | | /// <returns>The metric.</returns> |
| | | 143 | | public static Metric SequenceCoverage(decimal? value) |
| | 2987 | 144 | | { |
| | 2987 | 145 | | return new Metric( |
| | 2987 | 146 | | "Sequence coverage", |
| | 2987 | 147 | | "seq", |
| | 2987 | 148 | | CodeCoverageUri, |
| | 2987 | 149 | | MetricType.CoveragePercentual, |
| | 2987 | 150 | | value); |
| | 2987 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Initializes a new instance of the <see cref="Metric"/> class which represents cyclomatic complexity. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="value">The value.</param> |
| | | 157 | | /// <returns>The metric.</returns> |
| | | 158 | | public static Metric CyclomaticComplexity(decimal? value) |
| | 2987 | 159 | | { |
| | 2987 | 160 | | return new Metric( |
| | 2987 | 161 | | "Cyclomatic complexity", |
| | 2987 | 162 | | "cc", |
| | 2987 | 163 | | CyclomaticComplexityUri, |
| | 2987 | 164 | | MetricType.CodeQuality, |
| | 2987 | 165 | | value, |
| | 2987 | 166 | | MetricMergeOrder.LowerIsBetter); |
| | 2987 | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Initializes a new instance of the <see cref="Metric"/> class which represents NPath complexity. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <param name="value">The value.</param> |
| | | 173 | | /// <returns>The metric.</returns> |
| | | 174 | | public static Metric NPathComplexity(decimal? value) |
| | 2885 | 175 | | { |
| | 2885 | 176 | | return new Metric( |
| | 2885 | 177 | | "NPath complexity", |
| | 2885 | 178 | | "npth", |
| | 2885 | 179 | | NPathComplexityUri, |
| | 2885 | 180 | | MetricType.CodeQuality, |
| | 2885 | 181 | | value, |
| | 2885 | 182 | | MetricMergeOrder.LowerIsBetter); |
| | 2885 | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Initializes a new instance of the <see cref="Metric"/> class which represents crap score. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <param name="value">The value.</param> |
| | | 189 | | /// <returns>The metric.</returns> |
| | | 190 | | public static Metric CrapScore(decimal? value) |
| | 2885 | 191 | | { |
| | 2885 | 192 | | return new Metric( |
| | 2885 | 193 | | "Crap Score", |
| | 2885 | 194 | | "crp", |
| | 2885 | 195 | | CrapScoreUri, |
| | 2885 | 196 | | MetricType.CodeQuality, |
| | 2885 | 197 | | value, |
| | 2885 | 198 | | MetricMergeOrder.LowerIsBetter); |
| | 2885 | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Returns a <see cref="string" /> that represents this instance. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <returns> |
| | | 205 | | /// A <see cref="string" /> that represents this instance. |
| | | 206 | | /// </returns> |
| | | 207 | | public override string ToString() |
| | 0 | 208 | | { |
| | 0 | 209 | | return $"{this.Name}: {this.Value}"; |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Determines whether the specified <see cref="object"/> is equal to this instance. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="obj">The <see cref="object"/> to compare with this instance.</param> |
| | | 216 | | /// <returns> |
| | | 217 | | /// <c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>. |
| | | 218 | | /// </returns> |
| | | 219 | | public override bool Equals(object obj) |
| | 1300 | 220 | | { |
| | 1300 | 221 | | if (obj == null || !obj.GetType().Equals(typeof(Metric))) |
| | 0 | 222 | | { |
| | 0 | 223 | | return false; |
| | | 224 | | } |
| | | 225 | | else |
| | 1300 | 226 | | { |
| | 1300 | 227 | | var metric = (Metric)obj; |
| | 1300 | 228 | | return metric.Name.Equals(this.Name); |
| | | 229 | | } |
| | 1300 | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Returns a hash code for this instance. |
| | | 234 | | /// </summary> |
| | | 235 | | /// <returns> |
| | | 236 | | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| | | 237 | | /// </returns> |
| | 360 | 238 | | public override int GetHashCode() => this.Name.GetHashCode(); |
| | | 239 | | } |