string.ContainAllOf and string.ContainAnyOf#633
string.ContainAllOf and string.ContainAnyOf#633dennisdoomen merged 4 commits intofluentassertions:developfrom beeleebow:string-containanyall
Conversation
|
Nice additions! I would rename To make it complete you could add the |
|
If staying with the name On a side note: |
|
Ah, now I understand what you're trying to accomplish here. Did you know you can also do |
|
I think |
|
Okay, great. Thanks for the feedback guys. With regards to the But we could have an extra overload that takes only a With that said, here is what I am thinking: Containment
We could also add Equivalency
There are similar arguments to be made about Matching
(There is also the So that all said, it seems there is a balancing act to be struck between consistency and size/noise of the API. You guys are probably much better judges than me of where that line is so I'm happy to take your direction here. As @jnyrup suggests the approach of chaining assertions via |
|
You'll end up with lots of extension methods. I see that as well, but on the other hand, they do add value. |
… Fix typos (change -> chance, successfull -> successful)
|
logMsg.Should().NotContainAll(new[] {"A", "B"}, "Logging message should contain A or B but not both.").But that is only if it works as "should contain any but not all". Strictly speaking I think the first case do make sense in a testing framework, while I share the concern that second can be confusing whether or not This is similar to the case for var list = new List<int>();
list.Should().OnlyContain(e => e == 0)which fails on empty collections. |
dennisdoomen
left a comment
There was a problem hiding this comment.
Great work. I do have some (minor) remarks. Hopefully you are willing to address them.
| var missing = values.Where(v => !Contains(Subject, v, StringComparison.Ordinal)).ToArray(); | ||
| Execute.Assertion | ||
| .ForCondition(values.All(v => Contains(Subject, v, StringComparison.Ordinal))) | ||
| .BecauseOf(because, becauseArgs) |
There was a problem hiding this comment.
Use the phrase Expected {context:string} {0} to contain..... This allows wrapping this call in an AssertionScope (and an upcoming feature that will try to get the [CallerMemberName])
| //----------------------------------------------------------------------------------------------------------- | ||
| act | ||
| .ShouldThrow<ArgumentException>() | ||
| .WithMessage("Cannot assert string containment of values in null collection"); |
There was a problem hiding this comment.
I prefer to use wildcards in WithMessage so that only the relevant parts are verified. Also see point 10 from http://www.continuousimprover.com/2015/11/12-tips-to-write-unit-tests-that-dont.html
| //----------------------------------------------------------------------------------------------------------- | ||
| // Assert | ||
| //----------------------------------------------------------------------------------------------------------- | ||
| act |
There was a problem hiding this comment.
Please verify only a single condition per test case.
| #region ContainAll | ||
|
|
||
| [Fact] | ||
| public void When_null_collection_containall_should_throw_argument_exception() |
There was a problem hiding this comment.
Please don't repeat the name of a method or member in the name of the test case. See point 11 of http://www.continuousimprover.com/2015/11/12-tips-to-write-unit-tests-that-dont.html
| //----------------------------------------------------------------------------------------------------------- | ||
| // Assert | ||
| //----------------------------------------------------------------------------------------------------------- | ||
| const string expectedErrorMsg = "Expected string \"red green yellow\" to contain strings: " + |
There was a problem hiding this comment.
Why don't you use interpolation strings?
There was a problem hiding this comment.
No problems. Happy to make the changes. Do you prefer the changes be added in new commits or rolled into the original commits (i.e. rewrite git history)? Also, what C# language level is acceptable? 7? (I played it cautious with string.Format etc. because I wasn't sure what your build process etc. was)
There was a problem hiding this comment.
I prefer an amend. And yes, C# 7 is perfectly fine.
|
@dennisdoomen have performed the requested changes. Let me know if I have missed anything. Note that this PR contains only |
One thing I always find myself doing is asserting the contents of a string multiple times. Usually I am wanting to assert that something like a log message contains some important information in no particular order (so as to not have an overly rigid test). For example:
I thought it might be nice if I could write this instead:
I also thought a similar
ContainAnyOfcould be useful, so I added it while I was there.I updated the CONTRIBUTING.MD document as well, adding the disclaimer about the v 5 rework and suggesting PRs be targeted at develop (it said master before).