-
Notifications
You must be signed in to change notification settings - Fork 387
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 nullability indicators for the Parse/TryParse overloads #1446
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
using System.Globalization; | ||
using UnitsNet.Tests.CustomQuantities; | ||
using UnitsNet.Units; | ||
using Xunit; | ||
|
||
namespace UnitsNet.Tests | ||
|
@@ -87,5 +89,24 @@ public void TryParse_MappedCustomUnit() | |
Assert.Equal(1, q.Value); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_NullString_Returns_False() | ||
{ | ||
QuantityParser quantityParser = UnitsNetSetup.Default.QuantityParser; | ||
|
||
var success = quantityParser.TryParse<Mass, MassUnit>(null, null, Mass.From, out Mass _); | ||
|
||
Assert.False(success); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_WithInvalidValue_Returns_False() | ||
{ | ||
QuantityParser quantityParser = UnitsNetSetup.Default.QuantityParser; | ||
|
||
var success = quantityParser.TryParse<Mass, MassUnit>("XX kg", CultureInfo.InvariantCulture, Mass.From, out Mass _); | ||
|
||
Assert.False(success); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,5 +137,20 @@ public void Parse_MappedCustomUnit() | |
|
||
Assert.Equal(HowMuchUnit.Some, parsedUnit); | ||
} | ||
|
||
[Fact] | ||
public void TryParse_WithNullAbbreviation_ReturnsFalse() | ||
{ | ||
UnitParser unitParser = UnitsNetSetup.Default.UnitParser; | ||
Assert.Multiple(() => | ||
{ | ||
var success = unitParser.TryParse(null, out LengthUnit unit); | ||
Assert.False(success); | ||
}, () => | ||
{ | ||
var success = unitParser.TryParse(null, typeof(LengthUnit), out Enum? _); | ||
Assert.False(success); | ||
}); | ||
} | ||
Comment on lines
+141
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you Ok with this type of testing? A lot of my v6 tests use the same convention (merging only when the method name is the same). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely, I have a very low bar for testing schemes 😄 as long as they do the job and the name of the test makes sense and kinda follows the other tests in the same area. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to mention, we're using FluentAssertions at work and I do love that. The syntax is a breeze, assertion messages way more readable and in this case you would do I wouldn't mind refactoring all tests over to it if I found surplus energy and time. wink wink, nudge nudge 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know if you've got an AI agent- but given a little bit of preparing the right prompt- it should be able to reformat a whole class without much trouble... |
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
This is of course not necessary (and would probably increase our file size a little) but this is how the
IParsable
interface has it...Haven't tried it, but I image we would get no warning in the block below:
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.
We should definitely do this, and seeing how widely used this is I don't imagine it increases any binary size in practice - meaning, the nuget is probably already loaded if not already bundled in the target framework.
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.
Okay, maybe a slight extra overhead in metadata of our methods, but still - worth it.