-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Have bool and string implement ISpanParsable<T> #82836
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/libraries/System.Runtime/tests/System/BooleanTests.GenericMath.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Xunit; | ||
|
||
namespace System.Tests | ||
{ | ||
public class BooleanTests_GenericMath | ||
{ | ||
// | ||
// IParsable and ISpanParsable | ||
// | ||
|
||
[Theory] | ||
[MemberData(nameof(BooleanTests.Parse_Valid_TestData), MemberType = typeof(BooleanTests))] | ||
public static void ParseValidStringTest(string value, bool expected) | ||
{ | ||
bool result; | ||
|
||
// Default | ||
Assert.True(ParsableHelper<bool>.TryParse(value, provider: null, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, ParsableHelper<bool>.Parse(value, provider: null)); | ||
|
||
// Current Culture | ||
Assert.True(ParsableHelper<bool>.TryParse(value, provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, ParsableHelper<bool>.Parse(value, provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(BooleanTests.Parse_Invalid_TestData), MemberType = typeof(BooleanTests))] | ||
public static void ParseInvalidStringTest(string value, Type exceptionType) | ||
{ | ||
bool result; | ||
|
||
// Default | ||
Assert.False(ParsableHelper<bool>.TryParse(value, provider: null, out result)); | ||
Assert.Equal(default(bool), result); | ||
Assert.Throws(exceptionType, () => ParsableHelper<bool>.Parse(value, provider: null)); | ||
|
||
// Current Culture | ||
Assert.False(ParsableHelper<bool>.TryParse(value, provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(default(bool), result); | ||
Assert.Throws(exceptionType, () => ParsableHelper<bool>.Parse(value, provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(BooleanTests.Parse_ValidWithOffsetCount_TestData), MemberType = typeof(BooleanTests))] | ||
public static void ParseValidSpanTest(string value, int offset, int count, bool expected) | ||
{ | ||
bool result; | ||
|
||
// Default | ||
Assert.True(SpanParsableHelper<bool>.TryParse(value.AsSpan(offset, count), provider: null, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, SpanParsableHelper<bool>.Parse(value.AsSpan(offset, count), provider: null)); | ||
|
||
// Current Culture | ||
Assert.True(SpanParsableHelper<bool>.TryParse(value.AsSpan(offset, count), provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, SpanParsableHelper<bool>.Parse(value.AsSpan(offset, count), provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(BooleanTests.Parse_Invalid_TestData), MemberType = typeof(BooleanTests))] | ||
public static void ParseInvalidSpanTest(string value, Type exceptionType) | ||
{ | ||
if (value is null) | ||
{ | ||
// null and empty span are treated the same | ||
return; | ||
} | ||
|
||
bool result; | ||
|
||
// Default | ||
Assert.False(SpanParsableHelper<bool>.TryParse(value.AsSpan(), provider: null, out result)); | ||
Assert.Equal(default(bool), result); | ||
Assert.Throws(exceptionType, () => SpanParsableHelper<bool>.Parse(value.AsSpan(), provider: null)); | ||
|
||
// Current Culture | ||
Assert.False(SpanParsableHelper<bool>.TryParse(value.AsSpan(), provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(default(bool), result); | ||
Assert.Throws(exceptionType, () => SpanParsableHelper<bool>.Parse(value.AsSpan(), provider: CultureInfo.CurrentCulture)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/libraries/System.Runtime/tests/System/StringTests.GenericMath.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Xunit; | ||
|
||
namespace System.Tests | ||
{ | ||
public class StringTests_GenericMath | ||
{ | ||
public static IEnumerable<object[]> Parse_Valid_TestData() | ||
{ | ||
yield return new object[] { "Hello" }; | ||
yield return new object[] { "hello" }; | ||
yield return new object[] { "HELLO" }; | ||
yield return new object[] { "hElLo" }; | ||
yield return new object[] { " Hello " }; | ||
yield return new object[] { "Hello\0" }; | ||
yield return new object[] { " \0 \0 Hello \0 " }; | ||
yield return new object[] { "World" }; | ||
yield return new object[] { "world" }; | ||
yield return new object[] { "WORLD" }; | ||
yield return new object[] { "wOrLd" }; | ||
yield return new object[] { "World " }; | ||
yield return new object[] { "World\0" }; | ||
yield return new object[] { " World \0\0\0 " }; | ||
} | ||
|
||
public static IEnumerable<object[]> Parse_Invalid_TestData() | ||
{ | ||
yield return new object[] { null, typeof(ArgumentNullException) }; | ||
// We cannot easily test inputs that exceed `string.MaxLength` without risk of OOM | ||
} | ||
|
||
public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData() | ||
{ | ||
foreach (object[] inputs in Parse_Valid_TestData()) | ||
{ | ||
yield return new object[] { inputs[0], 0, ((string)inputs[0]).Length, inputs[0] }; | ||
} | ||
|
||
yield return new object[] { " \0 \0 Hello, World! \0 ", 6, 5, "Hello" }; | ||
yield return new object[] { " \0 \0 Hello, World! \0 ", 13, 5, "World" }; | ||
yield return new object[] { " \0 \0 Hello, World! \0 ", 6, 13, "Hello, World!" }; | ||
} | ||
|
||
// | ||
// IParsable and ISpanParsable | ||
// | ||
|
||
[Theory] | ||
[MemberData(nameof(StringTests_GenericMath.Parse_Valid_TestData), MemberType = typeof(StringTests_GenericMath))] | ||
public static void ParseValidStringTest(string value) | ||
{ | ||
string result; | ||
string expected = value; | ||
|
||
// Default | ||
Assert.True(ParsableHelper<string>.TryParse(value, provider: null, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, ParsableHelper<string>.Parse(value, provider: null)); | ||
|
||
// Current Culture | ||
Assert.True(ParsableHelper<string>.TryParse(value, provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, ParsableHelper<string>.Parse(value, provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(StringTests_GenericMath.Parse_Invalid_TestData), MemberType = typeof(StringTests_GenericMath))] | ||
public static void ParseInvalidStringTest(string value, Type exceptionType) | ||
{ | ||
string result; | ||
|
||
// Default | ||
Assert.False(ParsableHelper<string>.TryParse(value, provider: null, out result)); | ||
Assert.Equal(default(string), result); | ||
Assert.Throws(exceptionType, () => ParsableHelper<string>.Parse(value, provider: null)); | ||
|
||
// Current Culture | ||
Assert.False(ParsableHelper<string>.TryParse(value, provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(default(string), result); | ||
Assert.Throws(exceptionType, () => ParsableHelper<string>.Parse(value, provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(StringTests_GenericMath.Parse_ValidWithOffsetCount_TestData), MemberType = typeof(StringTests_GenericMath))] | ||
public static void ParseValidSpanTest(string value, int offset, int count, string expected) | ||
{ | ||
string result; | ||
|
||
// Default | ||
Assert.True(SpanParsableHelper<string>.TryParse(value.AsSpan(offset, count), provider: null, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, SpanParsableHelper<string>.Parse(value.AsSpan(offset, count), provider: null)); | ||
|
||
// Current Culture | ||
Assert.True(SpanParsableHelper<string>.TryParse(value.AsSpan(offset, count), provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(expected, result); | ||
Assert.Equal(expected, SpanParsableHelper<string>.Parse(value.AsSpan(offset, count), provider: CultureInfo.CurrentCulture)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(StringTests_GenericMath.Parse_Invalid_TestData), MemberType = typeof(StringTests_GenericMath))] | ||
public static void ParseInvalidSpanTest(string value, Type exceptionType) | ||
{ | ||
if (value is null) | ||
{ | ||
// null and empty span are treated the same | ||
return; | ||
} | ||
|
||
string result; | ||
|
||
// Default | ||
Assert.False(SpanParsableHelper<string>.TryParse(value.AsSpan(), provider: null, out result)); | ||
Assert.Equal(default(string), result); | ||
Assert.Throws(exceptionType, () => SpanParsableHelper<string>.Parse(value.AsSpan(), provider: null)); | ||
|
||
// Current Culture | ||
Assert.False(SpanParsableHelper<string>.TryParse(value.AsSpan(), provider: CultureInfo.CurrentCulture, out result)); | ||
Assert.Equal(default(string), result); | ||
Assert.Throws(exceptionType, () => SpanParsableHelper<string>.Parse(value.AsSpan(), provider: CultureInfo.CurrentCulture)); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really matter other than for consistency with other use, but whereas for a generic
T
we'll use[MaybeNullWhen(false)] out T
, for a non-generic we'll use[NotNullWhen(true)] out string?
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with this since its what the interface declaration was.
Is there a preference on updating to
NotNullWhen(true)
instead or is there any conflict in doing that given the usage via generics?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a functionality perspective, it doesn't matter:
[MaybeNullWhen(false)] out string
and[NotNullWhen(true)] out string?
are identical. We prefer the latter form in general because it more closely aligns with syntax of the language: theout
is in fact nullable, hence the?
, and it's just that in certain circumstances we can use attributes to provide more information to the flow analysis, that we know even though it's nullable it won't be when the method returns true.We can't, however, use
[NotNullWhen(true)] out T
, because the consumer might instantiate with a nullableT
such thatnull
is a valid value in the domain, such thatNotNullWhen(true)
would potentially be a lie. Hence, even though we prefer[NotNullWhen(true)]
, we use[MaybeNullWhen(false)]
with unconstrained generics, since it ends up functionally being the same and is correct.So, we do have a preference for
[NotNullWhen(true)]
when it's possible to use it. In this particular case, it's even less impactful because we're explicitly implementing the interface, so effectively no consumer will see the annotations here (outside of via reflection, which is really remote).