Skip to content

Commit 3af84b9

Browse files
committed
Modified enumerated types to be parsed with case-insensitivity.
Added tests for code-coverage.
1 parent 61531d0 commit 3af84b9

File tree

8 files changed

+157
-7
lines changed

8 files changed

+157
-7
lines changed

src/DemaConsulting.SpdxModel/SpdxAnnotationType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static class SpdxAnnotationTypeExtensions
5454
/// <exception cref="InvalidOperationException">on error</exception>
5555
public static SpdxAnnotationType FromText(string annotationType)
5656
{
57-
return annotationType switch
57+
return annotationType.ToUpperInvariant() switch
5858
{
5959
"" => SpdxAnnotationType.Missing,
6060
"REVIEW" => SpdxAnnotationType.Review,

src/DemaConsulting.SpdxModel/SpdxChecksumAlgorithm.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static class SpdxChecksumAlgorithmExtensions
129129
/// <exception cref="InvalidOperationException">on error</exception>
130130
public static SpdxChecksumAlgorithm FromText(string checksumAlgorithm)
131131
{
132-
return checksumAlgorithm switch
132+
return checksumAlgorithm.ToUpperInvariant() switch
133133
{
134134
"" => SpdxChecksumAlgorithm.Missing,
135135
"SHA1" => SpdxChecksumAlgorithm.Sha1,
@@ -144,9 +144,9 @@ public static SpdxChecksumAlgorithm FromText(string checksumAlgorithm)
144144
"SHA3-256" => SpdxChecksumAlgorithm.Sha3256,
145145
"SHA3-384" => SpdxChecksumAlgorithm.Sha3384,
146146
"SHA3-512" => SpdxChecksumAlgorithm.Sha3512,
147-
"BLAKE2b-256" => SpdxChecksumAlgorithm.Blake2B256,
148-
"BLAKE2b-384" => SpdxChecksumAlgorithm.Blake2B384,
149-
"BLAKE2b-512" => SpdxChecksumAlgorithm.Blake2B512,
147+
"BLAKE2B-256" => SpdxChecksumAlgorithm.Blake2B256,
148+
"BLAKE2B-384" => SpdxChecksumAlgorithm.Blake2B384,
149+
"BLAKE2B-512" => SpdxChecksumAlgorithm.Blake2B512,
150150
"BLAKE3" => SpdxChecksumAlgorithm.Blake3,
151151
"ADLER32" => SpdxChecksumAlgorithm.Adler32,
152152
_ => throw new InvalidOperationException($"Unsupported SPDX Checksum Algorithm '{checksumAlgorithm}'")

src/DemaConsulting.SpdxModel/SpdxFileType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static class SpdxFileTypeExtensions
9494
/// <exception cref="InvalidOperationException">on error</exception>
9595
public static SpdxFileType FromText(string fileType)
9696
{
97-
return fileType switch
97+
return fileType.ToUpperInvariant() switch
9898
{
9999
"SOURCE" => SpdxFileType.Source,
100100
"BINARY" => SpdxFileType.Binary,

src/DemaConsulting.SpdxModel/SpdxRelationshipType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public static class SpdxRelationshipTypeExtensions
269269
/// <exception cref="InvalidOperationException">on error</exception>
270270
public static SpdxRelationshipType FromText(string relationshipType)
271271
{
272-
return relationshipType switch
272+
return relationshipType.ToUpperInvariant() switch
273273
{
274274
"" => SpdxRelationshipType.Missing,
275275
"DESCRIBES" => SpdxRelationshipType.Describes,

test/DemaConsulting.SpdxModel.Tests/SpdxAnnotationTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,42 @@ public void Enhance()
142142
Assert.AreEqual("2023-11-20T12:34:23Z", annotations[1].Date);
143143
Assert.AreEqual(SpdxAnnotationType.Other, annotations[1].Type);
144144
}
145+
146+
/// <summary>
147+
/// Tests the <see cref="SpdxAnnotationTypeExtensions.FromText(string)"/> method for the "REVIEW" annotation type.
148+
/// </summary>
149+
[TestMethod]
150+
public void SpdxAnnotationTypeExtensions_FromText_Review()
151+
{
152+
Assert.AreEqual(SpdxAnnotationType.Review, SpdxAnnotationTypeExtensions.FromText("REVIEW"));
153+
Assert.AreEqual(SpdxAnnotationType.Review, SpdxAnnotationTypeExtensions.FromText("review"));
154+
Assert.AreEqual(SpdxAnnotationType.Review, SpdxAnnotationTypeExtensions.FromText("Review"));
155+
}
156+
157+
/// <summary>
158+
/// Tests the <see cref="SpdxAnnotationTypeExtensions.FromText(string)"/> method for an invalid annotation type.
159+
/// </summary>
160+
[TestMethod]
161+
public void SpdxAnnotationTypeExtensions_FromText_Invalid()
162+
{
163+
Assert.ThrowsException<InvalidOperationException>(() => SpdxAnnotationTypeExtensions.FromText("invalid"));
164+
}
165+
166+
/// <summary>
167+
/// Tests the <see cref="SpdxAnnotationTypeExtensions.ToText(SpdxAnnotationType)"/> method for the "REVIEW" annotation type.
168+
/// </summary>
169+
[TestMethod]
170+
public void SpdxAnnotationTypeExtensions_ToText_Review()
171+
{
172+
Assert.AreEqual("REVIEW", SpdxAnnotationType.Review.ToText());
173+
}
174+
175+
/// <summary>
176+
/// Tests the <see cref="SpdxAnnotationTypeExtensions.ToText(SpdxAnnotationType)"/> method for an invalid annotation type.
177+
/// </summary>
178+
[TestMethod]
179+
public void SpdxAnnotationTypeExtensions_ToText_Invalid()
180+
{
181+
Assert.ThrowsException<InvalidOperationException>(() => ((SpdxAnnotationType)1000).ToText());
182+
}
145183
}

test/DemaConsulting.SpdxModel.Tests/SpdxChecksumTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,40 @@ public void Enhance()
123123
Assert.AreEqual(SpdxChecksumAlgorithm.Md5, checksums[1].Algorithm);
124124
Assert.AreEqual("624c1abb3664f4b35547e7c73864ad24", checksums[1].Value);
125125
}
126+
127+
/// <summary>
128+
/// Tests the <see cref="SpdxChecksumAlgorithmExtensions.FromText(string)"/> method.
129+
/// </summary>
130+
[TestMethod]
131+
public void SpdxChecksumAlgorithmExtensions_FromText_Sha256()
132+
{
133+
Assert.AreEqual(SpdxChecksumAlgorithm.Sha256, SpdxChecksumAlgorithmExtensions.FromText("SHA256"));
134+
}
135+
136+
/// <summary>
137+
/// Tests the <see cref="SpdxChecksumAlgorithmExtensions.FromText(string)"/> method.
138+
/// </summary>
139+
[TestMethod]
140+
public void SpdxChecksumAlgorithmExtensions_FromText_InvalidAlgorithm()
141+
{
142+
Assert.ThrowsException<InvalidOperationException>(() => SpdxChecksumAlgorithmExtensions.FromText("unknown"));
143+
}
144+
145+
/// <summary>
146+
/// Tests the <see cref="SpdxChecksumAlgorithmExtensions.ToText(SpdxChecksumAlgorithm)"/> method.
147+
/// </summary>
148+
[TestMethod]
149+
public void SpdxChecksumAlgorithmExtensions_ToText_Sha256()
150+
{
151+
Assert.AreEqual("SHA256", SpdxChecksumAlgorithm.Sha256.ToText());
152+
}
153+
154+
/// <summary>
155+
/// Tests the <see cref="SpdxChecksumAlgorithmExtensions.ToText(SpdxChecksumAlgorithm)"/> method.
156+
/// </summary>
157+
[TestMethod]
158+
public void SpdxChecksumAlgorithmExtensions_ToText_InvalidAlgorithm()
159+
{
160+
Assert.ThrowsException<InvalidOperationException>(() => ((SpdxChecksumAlgorithm)1000).ToText());
161+
}
126162
}

test/DemaConsulting.SpdxModel.Tests/SpdxFileTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,42 @@ public void Enhance()
201201
Assert.AreEqual(SpdxChecksumAlgorithm.Sha1, files[1].Checksums[0].Algorithm);
202202
Assert.AreEqual("c2b4e1c67a2d28fced849ee1bb76e7391b93f125", files[1].Checksums[0].Value);
203203
}
204+
205+
/// <summary>
206+
/// Tests the <see cref="SpdxFileTypeExtensions.FromText(string)"/> method.
207+
/// </summary>
208+
[TestMethod]
209+
public void SpdxFileTypeExtensions_FromText_Source()
210+
{
211+
Assert.AreEqual(SpdxFileType.Source, SpdxFileTypeExtensions.FromText("SOURCE"));
212+
Assert.AreEqual(SpdxFileType.Source, SpdxFileTypeExtensions.FromText("source"));
213+
Assert.AreEqual(SpdxFileType.Source, SpdxFileTypeExtensions.FromText("Source"));
214+
}
215+
216+
/// <summary>
217+
/// Tests the <see cref="SpdxFileTypeExtensions.FromText(string)"/> method.
218+
/// </summary>
219+
[TestMethod]
220+
public void SpdxFileTypeExtensions_FromText_Invalid()
221+
{
222+
Assert.ThrowsException<InvalidOperationException>(() => SpdxFileTypeExtensions.FromText("Invalid"));
223+
}
224+
225+
/// <summary>
226+
/// Tests the <see cref="SpdxFileTypeExtensions.ToText"/> method.
227+
/// </summary>
228+
[TestMethod]
229+
public void SpdxFileTypeExtensions_ToText_Source()
230+
{
231+
Assert.AreEqual("SOURCE", SpdxFileType.Source.ToText());
232+
}
233+
234+
/// <summary>
235+
/// Tests the <see cref="SpdxFileTypeExtensions.ToText"/> method.
236+
/// </summary>
237+
[TestMethod]
238+
public void SpdxFileTypeExtensions_ToText_Invalid()
239+
{
240+
Assert.ThrowsException<InvalidOperationException>(() => ((SpdxFileType)1000).ToText());
241+
}
204242
}

test/DemaConsulting.SpdxModel.Tests/SpdxRelationshipTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,42 @@ public void Enhance()
181181
Assert.AreEqual(SpdxRelationshipType.DevToolOf, relationships[1].RelationshipType);
182182
Assert.AreEqual("SPDXRef-Package4", relationships[1].RelatedSpdxElement);
183183
}
184+
185+
/// <summary>
186+
/// Tests the <see cref="SpdxRelationshipTypeExtensions.FromText(string)"/> method for the "CONTAINS" relationship type.
187+
/// </summary>
188+
[TestMethod]
189+
public void SpdxRelationshipTypeExtensions_FromText_Contains()
190+
{
191+
Assert.AreEqual(SpdxRelationshipType.Contains, SpdxRelationshipTypeExtensions.FromText("CONTAINS"));
192+
Assert.AreEqual(SpdxRelationshipType.Contains, SpdxRelationshipTypeExtensions.FromText("contains"));
193+
Assert.AreEqual(SpdxRelationshipType.Contains, SpdxRelationshipTypeExtensions.FromText("Contains"));
194+
}
195+
196+
/// <summary>
197+
/// Tests the <see cref="SpdxRelationshipTypeExtensions.FromText(string)"/> method for an invalid relationship type.
198+
/// </summary>
199+
[TestMethod]
200+
public void SpdxRelationshipTypeExtensions_FromText_Invalid()
201+
{
202+
Assert.ThrowsException<InvalidOperationException>(() => SpdxRelationshipTypeExtensions.FromText("Invalid"));
203+
}
204+
205+
/// <summary>
206+
/// Tests the <see cref="SpdxRelationshipTypeExtensions.ToText(SpdxRelationshipType)"/> method for the "CONTAINS" relationship type.
207+
/// </summary>
208+
[TestMethod]
209+
public void SpdxRelationshipTypeExtensions_ToText_Contains()
210+
{
211+
Assert.AreEqual("CONTAINS", SpdxRelationshipType.Contains.ToText());
212+
}
213+
214+
/// <summary>
215+
/// Tests the <see cref="SpdxRelationshipTypeExtensions.ToText(SpdxRelationshipType)"/> method for an invalid relationship type.
216+
/// </summary>
217+
[TestMethod]
218+
public void SpdxRelationshipTypeExtensions_ToText_Invalid()
219+
{
220+
Assert.ThrowsException<InvalidOperationException>(() => ((SpdxRelationshipType)1000).ToText());
221+
}
184222
}

0 commit comments

Comments
 (0)