Skip to content

Commit

Permalink
[#675] Added test cases to handle passing null or whitespace to Value…
Browse files Browse the repository at this point in the history
…s() constructors

Presently failing, next commit addresses bug.
  • Loading branch information
Adam Smith-Platts committed Dec 13, 2023
1 parent 2d4f207 commit 3a7c334
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Tests/Schema.NET.Test/Values2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ public void Constructor_Items_HasAllItems()
Assert.Equal(new List<object>() { 1, "Foo" }, values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string>((List<object>)null!));
Expand Down
19 changes: 19 additions & 0 deletions Tests/Schema.NET.Test/Values3Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ public void Constructor_Items_HasAllItems()
Assert.Equal(new List<object>() { 1, "Foo", DayOfWeek.Friday }, values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string, DayOfWeek>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string, DayOfWeek>((List<object>)null!));
Expand Down
21 changes: 21 additions & 0 deletions Tests/Schema.NET.Test/Values4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ public void Constructor_Items_HasAllItems()
values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string, DayOfWeek, Person>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Assert.Empty(values.Value4);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string, DayOfWeek, Person>((List<object>)null!));
Expand Down
23 changes: 23 additions & 0 deletions Tests/Schema.NET.Test/Values5Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ public void Constructor_Items_HasAllItems()
values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string, DayOfWeek, Person, DateTime>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Assert.Empty(values.Value4);
Assert.False(values.HasValue5);
Assert.Empty(values.Value5);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string, DayOfWeek, Person, DateTime>((List<object>)null!));
Expand Down
25 changes: 25 additions & 0 deletions Tests/Schema.NET.Test/Values6Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ public void Constructor_Items_HasAllItems()
values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string, DayOfWeek, Person, DateTime, bool>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Assert.Empty(values.Value4);
Assert.False(values.HasValue5);
Assert.Empty(values.Value5);
Assert.False(values.HasValue6);
Assert.Empty(values.Value6);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string, DayOfWeek, Person, DateTime, bool>((List<object>)null!));
Expand Down
27 changes: 27 additions & 0 deletions Tests/Schema.NET.Test/Values7Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ public void Constructor_Items_HasAllItems()
values.Cast<object>().ToList());
}

[Fact]
public void Constructor_StringItems_NullOrWhitespaceDoesntHaveValue()
{
object[] nullOrWhitespaceValues = new[]
{
string.Empty,
null!,
"\u2028 \u2029 \u0009 \u000A \u000B \u000C \u000D \u0085",
};
var values = new Values<int, string, DayOfWeek, Person, DateTime, bool, TimeSpan>(nullOrWhitespaceValues);

Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2, $"{nameof(values.HasValue2)}: Expected: False, Actual: True");
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Assert.Empty(values.Value4);
Assert.False(values.HasValue5);
Assert.Empty(values.Value5);
Assert.False(values.HasValue6);
Assert.Empty(values.Value6);
Assert.False(values.HasValue7);
Assert.Empty(values.Value7);
}

[Fact]
public void Constructor_NullList_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new Values<int, string, DayOfWeek, Person, DateTime, bool, TimeSpan>((List<object>)null!));
Expand Down

0 comments on commit 3a7c334

Please sign in to comment.