| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace DirectSight.Parser.Analysis; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents an element (e.g. a method or property) in a code file. |
| | | 7 | | /// </summary> |
| | | 8 | | public class CodeElement |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="CodeElement" /> class. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="name">The name.</param> |
| | | 14 | | /// <param name="type">The <see cref="Analysis.CodeElementType"/>.</param> |
| | | 15 | | /// <param name="firstLine">The number of the first line.</param> |
| | | 16 | | /// <param name="lastLine">The number of the last line.</param> |
| | | 17 | | /// <param name="coverageQuota">The coverage quota.</param> |
| | | 18 | | internal CodeElement(string name, CodeElementType type, int firstLine, int lastLine, decimal? coverageQuota) |
| | 3 | 19 | | : this(name, name, type, firstLine, lastLine, coverageQuota) |
| | 3 | 20 | | { |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="CodeElement" /> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="fullName">The full name.</param> |
| | | 27 | | /// <param name="name">The name.</param> |
| | | 28 | | /// <param name="type">The <see cref="Analysis.CodeElementType"/>.</param> |
| | | 29 | | /// <param name="firstLine">The number of the first line.</param> |
| | | 30 | | /// <param name="lastLine">The number of the last line.</param> |
| | | 31 | | /// <param name="coverageQuota">The coverage quota.</param> |
| | 4426 | 32 | | internal CodeElement(string fullName, string name, CodeElementType type, int firstLine, int lastLine, decimal? cover |
| | 4426 | 33 | | { |
| | 4426 | 34 | | this.FullName = fullName ?? throw new ArgumentNullException(nameof(name)); |
| | 4426 | 35 | | this.Name = name ?? throw new ArgumentNullException(nameof(name)); |
| | 4426 | 36 | | this.CodeElementType = type; |
| | 4426 | 37 | | this.FirstLine = firstLine; |
| | 4426 | 38 | | this.LastLine = lastLine; |
| | 4426 | 39 | | if (coverageQuota.HasValue) |
| | 4424 | 40 | | { |
| | 4424 | 41 | | this.CoverageQuota = Math.Min(100, Math.Max(0, coverageQuota.Value)); |
| | 4424 | 42 | | } |
| | 4426 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the full name. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <value> |
| | | 49 | | /// The full name. |
| | | 50 | | /// </value> |
| | 7717 | 51 | | public string FullName { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the name. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <value> |
| | | 57 | | /// The name. |
| | | 58 | | /// </value> |
| | 111 | 59 | | public string Name { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the <see cref="Analysis.CodeElementType"/>. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <value> |
| | | 65 | | /// The <see cref="Analysis.CodeElementType"/>. |
| | | 66 | | /// </value> |
| | 110 | 67 | | public CodeElementType CodeElementType { get; } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the number of the first line. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <value> |
| | | 73 | | /// The number of the first line. |
| | | 74 | | /// </value> |
| | 11014 | 75 | | public int FirstLine { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the number of the last line. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <value> |
| | | 81 | | /// The number of the last line. |
| | | 82 | | /// </value> |
| | 2087 | 83 | | public int LastLine { get; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets the coverage quota of the code element. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <value>The coverage quota.</value> |
| | 9258 | 89 | | public decimal? CoverageQuota { get; private set; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Applies the given coverage quota if greater than existing quota. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="quota">The quota.</param> |
| | | 95 | | public void ApplyMaximumCoverageQuota(decimal? quota) |
| | 1097 | 96 | | { |
| | 1097 | 97 | | if (quota.HasValue) |
| | 1097 | 98 | | { |
| | 1097 | 99 | | if (this.CoverageQuota.HasValue) |
| | 1097 | 100 | | { |
| | 1097 | 101 | | this.CoverageQuota = Math.Max(quota.Value, this.CoverageQuota.Value); |
| | 1097 | 102 | | } |
| | | 103 | | else |
| | 0 | 104 | | { |
| | 0 | 105 | | this.CoverageQuota = quota; |
| | 0 | 106 | | } |
| | 1097 | 107 | | } |
| | 1097 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Returns a <see cref="string" /> that represents this instance. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <returns> |
| | | 114 | | /// A <see cref="string" /> that represents this instance. |
| | | 115 | | /// </returns> |
| | | 116 | | public override string ToString() |
| | 0 | 117 | | { |
| | 0 | 118 | | return this.Name; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Determines whether the specified <see cref="object"/> is equal to this instance. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="obj">The <see cref="object"/> to compare with this instance.</param> |
| | | 125 | | /// <returns> |
| | | 126 | | /// <c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>. |
| | | 127 | | /// </returns> |
| | | 128 | | public override bool Equals(object obj) |
| | 1097 | 129 | | { |
| | 1097 | 130 | | if (obj == null || !obj.GetType().Equals(typeof(CodeElement))) |
| | 0 | 131 | | { |
| | 0 | 132 | | return false; |
| | | 133 | | } |
| | | 134 | | else |
| | 1097 | 135 | | { |
| | 1097 | 136 | | var codeElement = (CodeElement)obj; |
| | 1097 | 137 | | return this.FullName.Equals(codeElement.FullName) && this.FirstLine == codeElement.FirstLine; |
| | | 138 | | } |
| | 1097 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Returns a hash code for this instance. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <returns> |
| | | 145 | | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| | | 146 | | /// </returns> |
| | 5523 | 147 | | public override int GetHashCode() => this.FullName.GetHashCode() + this.FirstLine.GetHashCode(); |
| | | 148 | | } |