Skip to content

Commit 1c39858

Browse files
nickevansukRehanSaeed
authored andcommitted
Removed Values<> tests and improved based on feedback
1 parent b54e183 commit 1c39858

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

Source/Schema.NET/OneOrMany{T}.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public int Count
107107
/// <param name="item">The single item value.</param>
108108
/// <returns>The result of the conversion.</returns>
109109
#pragma warning disable CA2225 // Operator overloads have named alternates
110-
public static implicit operator OneOrMany<T>(T item) => item != null && item.GetType() == typeof(string) && string.IsNullOrWhiteSpace(item as string) ? default : new OneOrMany<T>(item);
110+
public static implicit operator OneOrMany<T>(T item) => item != null && IsStringNullOrWhiteSpace(item) ? default : new OneOrMany<T>(item);
111111
#pragma warning restore CA2225 // Operator overloads have named alternates
112112

113113
/// <summary>
@@ -116,7 +116,7 @@ public int Count
116116
/// <param name="array">The array of values.</param>
117117
/// <returns>The result of the conversion.</returns>
118118
#pragma warning disable CA2225 // Operator overloads have named alternates
119-
public static implicit operator OneOrMany<T>(T[] array) => new OneOrMany<T>(array);
119+
public static implicit operator OneOrMany<T>(T[] array) => new OneOrMany<T>(array?.Where(x => x != null && !IsStringNullOrWhiteSpace(x)));
120120
#pragma warning restore CA2225 // Operator overloads have named alternates
121121

122122
/// <summary>
@@ -125,7 +125,7 @@ public int Count
125125
/// <param name="list">The list of values.</param>
126126
/// <returns>The result of the conversion.</returns>
127127
#pragma warning disable CA2225 // Operator overloads have named alternates
128-
public static implicit operator OneOrMany<T>(List<T> list) => new OneOrMany<T>(list.Where(x => x != null && !(x.GetType() == typeof(string) && string.IsNullOrWhiteSpace(x as string))));
128+
public static implicit operator OneOrMany<T>(List<T> list) => new OneOrMany<T>(list?.Where(x => x != null && !IsStringNullOrWhiteSpace(x)));
129129
#pragma warning restore CA2225 // Operator overloads have named alternates
130130

131131
/// <summary>
@@ -272,5 +272,7 @@ public override int GetHashCode()
272272

273273
return 0;
274274
}
275+
276+
private static bool IsStringNullOrWhiteSpace(T item) => item.GetType() == typeof(string) && string.IsNullOrWhiteSpace(item as string);
275277
}
276278
}

Tests/Schema.NET.Test/OneOrManyTest.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public void GetHashCode_TwoItems_HashCodeEqualToTwoItems() =>
243243
[InlineData("")]
244244
[InlineData(" ")]
245245
[InlineData(" ")]
246-
public void ToString_EmptyOrWhiteSpace_BookOmitsNameProperty(string name)
246+
public void ToString_NullEmptyOrWhiteSpace_BookOmitsNameProperty(string name)
247247
{
248248
var book = new Book() { Name = name };
249249
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Book\"}", book.ToString());
@@ -254,7 +254,7 @@ public void ToString_EmptyOrWhiteSpace_BookOmitsNameProperty(string name)
254254
[InlineData("")]
255255
[InlineData(" ")]
256256
[InlineData(" ")]
257-
public void ToString_EmptyOrWhiteSpace_BookOmitsNamePropertyFromList(string name)
257+
public void ToString_NullEmptyOrWhiteSpace_BookOmitsNamePropertyFromList(string name)
258258
{
259259
var book = new Book() { Name = new List<string> { "Hamlet", name } };
260260
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Book\",\"name\":\"Hamlet\"}", book.ToString());
@@ -265,22 +265,32 @@ public void ToString_EmptyOrWhiteSpace_BookOmitsNamePropertyFromList(string name
265265
[InlineData("")]
266266
[InlineData(" ")]
267267
[InlineData(" ")]
268-
public void ToString_EmptyOrWhiteSpace_OrganizationOmitsAddressProperty(string address)
268+
public void ToString_NullEmptyOrWhiteSpace_BookOmitsNamePropertyFromArray(string name)
269269
{
270-
var organization = new Organization() { Address = address };
271-
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Organization\"}", organization.ToString());
270+
var book = new Book() { Name = new string[] { "Hamlet", name } };
271+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Book\",\"name\":\"Hamlet\"}", book.ToString());
272272
}
273273

274274
[Theory]
275275
[InlineData(null)]
276276
[InlineData("")]
277277
[InlineData(" ")]
278278
[InlineData(" ")]
279-
public void ToString_EmptyOrWhiteSpace_OrganizationOmitsNamePropertyFromList(string address)
279+
public void ToString_NullEmptyOrWhiteSpace_OrganizationOmitsNamePropertyFromList(string address)
280280
{
281281
var organization = new Organization() { Name = new List<string> { "Cardiff, UK", address } };
282282
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Organization\",\"name\":\"Cardiff, UK\"}", organization.ToString());
283283
}
284284

285+
[Theory]
286+
[InlineData(null)]
287+
[InlineData("")]
288+
[InlineData(" ")]
289+
[InlineData(" ")]
290+
public void ToString_NullEmptyOrWhiteSpace_OrganizationOmitsNamePropertyFromArray(string address)
291+
{
292+
var organization = new Organization() { Name = new string[] { "Cardiff, UK", address } };
293+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Organization\",\"name\":\"Cardiff, UK\"}", organization.ToString());
294+
}
285295
}
286296
}

Tests/Schema.NET.Test/core/EventTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class EventTest
4040
PriceCurrency = "USD", // Recommended
4141
Availability = ItemAvailability.InStock, // Recommended
4242
ValidFrom = new DateTimeOffset(2017, 1, 20, 16, 20, 0, TimeSpan.FromHours(-8)), // Recommended
43-
Category = NullString,
4443
},
4544
new Offer
4645
{

0 commit comments

Comments
 (0)