-
-
Notifications
You must be signed in to change notification settings - Fork 544
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
Scoped value formatters #2478
Comments
Maybe |
It is also an option, this way collection of formatters could be internal. |
I'm fine with that. What about you @jnyrup ? |
I like the proposal of having scoped formatters 👍 As the signature is When retrieving all formatters in Perhaps we should also move the current To set a scoped formatter you'll only have to do using var scope = new AssertionScope();
scope.FormattingOptions.AddFormatter(new MyCustomFormatter()); |
That sounds like a great idea. Let's go for that. |
@FLAMESpl are you still interested in this one? |
@dennisdoomen: I would certainly be interested 😃 |
I meant in implementing this. |
Ah whoops, I misunderstood. |
I am willing to take that, if @FLAMESpl doesn't want to. |
Should a nested |
Everything that is associated with an |
Ok, the tricky part is how to determine which scoped formatters to remove at scope dispose 🤔 |
Yeah, it means that the scope needs some state on which scope has added a particular formatter. |
Since @FLAMESpl does not respond to this issue, I started the implementation. For example: Should this be possible? [Fact]
public void Test2()
{
using var outerScope = new AssertionScope();
var outerFormatter = new OuterFormatter();
var innerFormatter = new InnerFormatter();
outerScope.FormattingOptions.AddFormatter(outerFormatter);
using var innerScope = new AssertionScope();
innerScope.FormattingOptions.AddFormatter(innerFormatter);
innerScope.FormattingOptions.RemoveFormatter(outerFormatter);
} My main concern is, that this would become pretty "slow" when traversing back a lot of parent [Fact]
public void Test2()
{
using var outerScope = new AssertionScope();
var outerFormatter = new OuterFormatter();
var innerFormatter = new InnerFormatter();
outerScope.FormattingOptions.AddFormatter(outerFormatter);
using var innerScope = new AssertionScope();
innerScope.FormattingOptions.AddFormatter(innerFormatter);
using var innerScope1 = new AssertionScope();
innerScope1.FormattingOptions.AddFormatter(innerFormatter);
// ...
using var innerScope100 = new AssertionScope();
innerScope100.FormattingOptions.AddFormatter(innerFormatter);
innerScope100.FormattingOptions.RemoveFormatter(outerFormatter);
} That said: my current implementation does not allow such a thing. |
No, but inner scope options should be able to override the formatters used by the outer scope. So if the inner options remove a formatter that was added by the outer scope, that should only affect the inner scope. |
I struggle to understand, why the scoped formatters are not tied to the The fact that the formatters are mainly static and we forward Second, with the current implementation I am able to do the following: So: long story short I wonder what you are thinking about this: using var outerScope = new AssertionScope();
var outerFormatter = new OuterFormatter();
outerScope.AddFormatter(outerFormatter); which adds a scoped formatter, and // as suggested forwarding the first to the second
Formatter.AddFormatter();
AssertionOptions.FormattingOptions.AddFormatter(); to add a custom formatter. Edit: After a few times back-and-forth I finally managed this.. Ever day a new challenge 🎉🎉 |
I've never seen this before, but it does seem to be related to formatters. |
Well, that's what you are trying to change, right?
I don't understand the question. When you use
This should add a global formatter that is available to all assertion scopes.
I thought we said to use |
I have to admit, I had blinders on.. After I rethought the API it suddenly made sense.. Sorry for the spam..
What I meant was: why are the scoped formatters tied to |
Don't worry about that. I had to study the entire thread and look at the code to build a good understanding as well. |
When will this be released? |
With v7 - we don't have an ETA |
Background and motivation
This feature is about allowing adding custom
IValueFormatter
s toAssertionScope
. In my development I discovered that I need to selectively customize formatting of an object based on the test's context. There is an option to remove once added formatter but it will not play nicely if tests would be executed simultanously. Having scoped formatters have a side bonus of reusable parametrizing formatter instances.API Proposal
First make
IFormatterCollection
interface.Then add it to
AssertionScope
:Formatter
API remains unchanged. Internally it will utilizeIFormatterCollection
for sake of coherence.API Usage
Alternative Designs
Alternative would be to add a custom key-value store in
AssertionScope
that could be read inside aIValueFormatter
.Risks
None I can think of.
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.
The text was updated successfully, but these errors were encountered: