Skip to content

Commit b54e183

Browse files
nickevansukRehanSaeed
authored andcommitted
Add support for IsNullOrWhiteSpace and improve tests
1 parent 7811315 commit b54e183

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

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

Lines changed: 2 additions & 2 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) => new OneOrMany<T>(item);
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);
111111
#pragma warning restore CA2225 // Operator overloads have named alternates
112112

113113
/// <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);
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))));
129129
#pragma warning restore CA2225 // Operator overloads have named alternates
130130

131131
/// <summary>

Tests/Schema.NET.Test/OneOrManyTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,50 @@ public void GetHashCode_OneItem_HashCodeEqualToSingleItem() =>
237237
[Fact]
238238
public void GetHashCode_TwoItems_HashCodeEqualToTwoItems() =>
239239
Assert.Equal(NET.HashCode.OfEach(new List<int>() { 1, 2 }), new OneOrMany<int>(1, 2).GetHashCode());
240+
241+
[Theory]
242+
[InlineData(null)]
243+
[InlineData("")]
244+
[InlineData(" ")]
245+
[InlineData(" ")]
246+
public void ToString_EmptyOrWhiteSpace_BookOmitsNameProperty(string name)
247+
{
248+
var book = new Book() { Name = name };
249+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Book\"}", book.ToString());
250+
}
251+
252+
[Theory]
253+
[InlineData(null)]
254+
[InlineData("")]
255+
[InlineData(" ")]
256+
[InlineData(" ")]
257+
public void ToString_EmptyOrWhiteSpace_BookOmitsNamePropertyFromList(string name)
258+
{
259+
var book = new Book() { Name = new List<string> { "Hamlet", name } };
260+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Book\",\"name\":\"Hamlet\"}", book.ToString());
261+
}
262+
263+
[Theory]
264+
[InlineData(null)]
265+
[InlineData("")]
266+
[InlineData(" ")]
267+
[InlineData(" ")]
268+
public void ToString_EmptyOrWhiteSpace_OrganizationOmitsAddressProperty(string address)
269+
{
270+
var organization = new Organization() { Address = address };
271+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Organization\"}", organization.ToString());
272+
}
273+
274+
[Theory]
275+
[InlineData(null)]
276+
[InlineData("")]
277+
[InlineData(" ")]
278+
[InlineData(" ")]
279+
public void ToString_EmptyOrWhiteSpace_OrganizationOmitsNamePropertyFromList(string address)
280+
{
281+
var organization = new Organization() { Name = new List<string> { "Cardiff, UK", address } };
282+
Assert.Equal("{\"@context\":\"http://schema.org\",\"@type\":\"Organization\",\"name\":\"Cardiff, UK\"}", organization.ToString());
283+
}
284+
240285
}
241286
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Schema.NET.Test
88
// https://developers.google.com/search/docs/data-types/events
99
public class EventTest
1010
{
11-
private static readonly string NullTelephone = null;
11+
private static readonly string NullString = null;
1212
private static readonly ItemAvailability? NullItemAvailability = null;
1313

1414
private readonly Event @event = new Event()
@@ -39,7 +39,8 @@ public class EventTest
3939
Price = 30M, // Recommended
4040
PriceCurrency = "USD", // Recommended
4141
Availability = ItemAvailability.InStock, // Recommended
42-
ValidFrom = new DateTimeOffset(2017, 1, 20, 16, 20, 0, TimeSpan.FromHours(-8)) // Recommended
42+
ValidFrom = new DateTimeOffset(2017, 1, 20, 16, 20, 0, TimeSpan.FromHours(-8)), // Recommended
43+
Category = NullString,
4344
},
4445
new Offer
4546
{
@@ -53,7 +54,7 @@ public class EventTest
5354
Performer = new Person() // Recommended
5455
{
5556
Name = "Andy Lagunoff", // Recommended
56-
Telephone = NullTelephone // Should be ignored
57+
Telephone = NullString // Should be ignored
5758
}
5859
};
5960

0 commit comments

Comments
 (0)