Unclear precedence of AND and OR
I couldn't find anything in the documentation with regards to the precedence between AND and OR.
Consider the following test:
[Test]
public async Task And_Or_Precedence_Test()
{
char[] sut = "CD".ToCharArray();
await Assert.That(sut)
.Contains('A').And
.Contains('B').Or
.Contains('C').And
.Contains('D');
}
I would have expected the test to fail, as I would expect the condition to be interpreted as (A && B) || (C && D), to be in line with the operator precedence in C#.
However, this test succeeds, because A && B || C && D seems to be interpreted as (((A && B) || C) && D).
Did the analyzer kick in?
No, I didn't see any analyzer...
Hmm I'll need to check then.
There should be an analyzer flagging mixing of and+or..for the reason you mentioned, it can be confusing how the logic will evaluate.
So the analyzer allows chained ANDs or chained ORs, but should flag if a mix is used.
Analyzer showing for me fine:
Hmm, after restarting Visual Studio, I could see the analyzer:
It was probably an issue with the preview version.
However, as this is only an indication (that could be turned off), wouldn't it make sense to make an explicit choice of precedence in these cases? Or state, that this is not supported and throw an exception when both are mixed, so as not to lead to false positives in tests?
Probably throwing would be better, as:
await Assert.That(sut)
.Contains('A')
.And.Contains('B')
.Or
.Contains('C');
await Assert.That(sut)
.Contains('A')
.And
.Contains('B')
.Or.Contains('C');
Both are the exact same code, but due to the formatting/placement of it you might think it means something different to what it does.