fluentassertions / fluentassertions/fluentassertions

[API Proposal]: Parsability of strings

Open
#2,632 18 comments 0 reactions 0 assignees View on GitHub
api-suggestion feature
Dominant language
C#
Stars
3.8k
Forks
720
Avg merge
1d 1h
Merged PRs (30d)
29

Description

### Background and motivation

In .NET 7 static abstracts Members for interfaces were introduced.
One of them is the `IParsable` interface.
Currently there is no direct support to test for parsability.
To see if something can be parsed, one would currently have to call `Invoking().Should().NotThrow()`, which seems a bit clunky, adds more overhead then necessary and (depending on the targeted type) does not always provide a helpful and clear failure reasoning.
The use of Invoking also results in loosing the reference to the original string inside of the call-chain.

### API Proposal

```C#
public class StringAssertions : ReferenceTypeAssertions
where TAssertions : StringAssertions
{
public AndWhichConstraint BeParsableInto(IFormatProvider? formatProvider = null, string because = "", params object[] becauseArgs) where TTarget : IParsable;
public AndConstraint NotBeParsableInto(IFormatProvider? formatProvider = null, string because = "", params object[] becauseArgs) where TTarget : IParsable;
}
```

### API Usage

```C#
var number = "1"
number.Should()
.BeParsableInto()
.And
.NotBeParsableInto();
```

### Alternative Designs

```csharp
public AndWhichConstraint BeParsableInto(string because = "", params object[] becauseArgs) where TTarget : IParsable;
public AndWhichConstraint BeParsableInto(IFormatProvider? formatProvider, string because = "", params object[] becauseArgs) where TTarget : IParsable;

public AndConstraint NotBeParsableInto(string because = "", params object[] becauseArgs) where TTarget : IParsable;
public AndConstraint NotBeParsableInto(IFormatProvider? formatProvider, string because = "", params object[] becauseArgs) where TTarget : IParsable;
```

other names that came to me were:
(Not)ParseInto
(Not)BeParsableAs

### Risks

None that I know of, unless adding .NET 7+ support itself should be considered as a risk-factor.

### Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

Yes, please assign this issue to me.

**Edit**:
- corrected copy&paste mistake in overload.
- corrected some spelling mistakes

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.