Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HasValueX incorrectly returning true #676

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated Schema.NET.Test to properly handle testing that a OneOrMany<T…
…> collection is empty, where T is a string

- Added `AssertEx` class that exposes a `Empty()` method that takes a `OneOrMany<T>`.
- Updated all `Assert.Empty(OneOrMany<string>!)` instances to use `AssertEx.Empty(OneOrMany<string>)`.

Tests now pass!
  • Loading branch information
Adam Smith-Platts committed Dec 13, 2023
commit 2d4f207ee15d9ad0eb3c139373603cf38db01e0f
18 changes: 18 additions & 0 deletions Tests/Schema.NET.Test/AssertEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Schema.NET.Test;

using System.Collections;
using Xunit;

/// <summary>
/// Provides extended methods and helpers to the Xunit.Assert class.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "Name needs to be short to ensure readability of consuming code.")]
public static class AssertEx
{
/// <summary>
/// Verifies that a <see cref="OneOrMany{T}"/> collection is empty.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <inheritdoc cref="Assert.Empty(IEnumerable)"/>
public static void Empty<T>(OneOrMany<T> collection) => Assert.Empty(collection);
}
2 changes: 1 addition & 1 deletion Tests/Schema.NET.Test/OneOrManyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Constructor_NullList_ThrowsArgumentNullException() =>
public void Count_DefaultStructConstructor_ReturnsZero() => Assert.Empty(default(OneOrMany<int>));

[Fact]
public void Count_DefaultClassConstructor_ReturnsZero() => Assert.Empty(default(OneOrMany<string>)!);
public void Count_DefaultClassConstructor_ReturnsZero() => AssertEx.Empty(default(OneOrMany<string>));

[Fact]
public void Count_NullItem_ReturnsZero() => Assert.Empty(new OneOrMany<int?>((int?)null));
Expand Down
6 changes: 3 additions & 3 deletions Tests/Schema.NET.Test/Values2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Constructor_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public void ImplicitConversionOperator_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand All @@ -112,7 +112,7 @@ public void ImplicitConversionOperator_Value1ListPassed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Equal(2, values.Value1.Count);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1, 2 }, values.Cast<object>().ToList());
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/Schema.NET.Test/Values3Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Constructor_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand All @@ -39,7 +39,7 @@ public void Constructor_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.Equal(new List<object>() { DayOfWeek.Friday }, values.Cast<object>().ToList());
Expand Down Expand Up @@ -119,7 +119,7 @@ public void ImplicitConversionOperator_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand All @@ -143,7 +143,7 @@ public void ImplicitConversionOperator_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.Equal(new List<object>() { DayOfWeek.Friday }, values.Cast<object>().ToList());
Expand All @@ -157,7 +157,7 @@ public void ImplicitConversionOperator_Value1CollectionPassed_OnlyValue1HasValue
Assert.True(values.HasValue1);
Assert.Equal(2, values.Value1.Count);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1, 2 }, values.Cast<object>().ToList());
}

Expand All @@ -181,7 +181,7 @@ public void ImplicitConversionOperator_Value3CollectionPassed_OnlyValue3HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Equal(2, values.Value3.Count);
Assert.Equal(new List<object>() { DayOfWeek.Friday, DayOfWeek.Monday }, values.Cast<object>().ToList());
Expand Down
18 changes: 9 additions & 9 deletions Tests/Schema.NET.Test/Values4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Constructor_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand All @@ -39,7 +39,7 @@ public void Constructor_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.Equal(new List<object>() { DayOfWeek.Friday }, values.Cast<object>().ToList());
Expand All @@ -53,7 +53,7 @@ public void Constructor_Value4Passed_OnlyValue4HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void ImplicitConversionOperator_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1 }, values.Cast<object>().ToList());
}

Expand All @@ -173,7 +173,7 @@ public void ImplicitConversionOperator_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.Equal(new List<object>() { DayOfWeek.Friday }, values.Cast<object>().ToList());
Expand All @@ -187,7 +187,7 @@ public void ImplicitConversionOperator_Value4Passed_OnlyValue4HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand All @@ -204,7 +204,7 @@ public void ImplicitConversionOperator_Value1CollectionPassed_OnlyValue1HasValue
Assert.True(values.HasValue1);
Assert.Equal(2, values.Value1.Count);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.Equal(new List<object>() { 1, 2 }, values.Cast<object>().ToList());
}

Expand All @@ -228,7 +228,7 @@ public void ImplicitConversionOperator_Value3CollectionPassed_OnlyValue3HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Equal(2, values.Value3.Count);
Assert.Equal(
Expand All @@ -244,7 +244,7 @@ public void ImplicitConversionOperator_Value4CollectionPassed_OnlyValue4HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand Down
24 changes: 12 additions & 12 deletions Tests/Schema.NET.Test/Values5Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Constructor_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand Down Expand Up @@ -51,7 +51,7 @@ public void Constructor_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.False(values.HasValue4);
Expand All @@ -69,7 +69,7 @@ public void Constructor_Value4Passed_OnlyValue4HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand All @@ -88,7 +88,7 @@ public void Constructor_Value5Passed_OnlyValue5HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand Down Expand Up @@ -196,7 +196,7 @@ public void ImplicitConversionOperator_Value1Passed_OnlyValue1HasValue()
Assert.True(values.HasValue1);
Assert.Single(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand Down Expand Up @@ -232,7 +232,7 @@ public void ImplicitConversionOperator_Value3Passed_OnlyValue3HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Single(values.Value3);
Assert.False(values.HasValue4);
Expand All @@ -250,7 +250,7 @@ public void ImplicitConversionOperator_Value4Passed_OnlyValue4HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand All @@ -269,7 +269,7 @@ public void ImplicitConversionOperator_Value5Passed_OnlyValue5HasValue()
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand All @@ -288,7 +288,7 @@ public void ImplicitConversionOperator_Value1CollectionPassed_OnlyValue1HasValue
Assert.True(values.HasValue1);
Assert.Equal(2, values.Value1.Count);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand Down Expand Up @@ -324,7 +324,7 @@ public void ImplicitConversionOperator_Value3CollectionPassed_OnlyValue3HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.True(values.HasValue3);
Assert.Equal(2, values.Value3.Count);
Assert.False(values.HasValue4);
Expand All @@ -344,7 +344,7 @@ public void ImplicitConversionOperator_Value4CollectionPassed_OnlyValue4HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.True(values.HasValue4);
Expand All @@ -362,7 +362,7 @@ public void ImplicitConversionOperator_Value5CollectionPassed_OnlyValue5HasValue
Assert.False(values.HasValue1);
Assert.Empty(values.Value1);
Assert.False(values.HasValue2);
Assert.Empty(values.Value2!);
AssertEx.Empty(values.Value2);
Assert.False(values.HasValue3);
Assert.Empty(values.Value3);
Assert.False(values.HasValue4);
Expand Down
Loading