0

Maybe I'm not grokking code contracts, but ISTM that this code:

private static char GetBarcodeChecksumWithLegacyCode(string barcodeWithoutCzechSum)
{
    Contract.Requires(!string.IsNullOrWhiteSpace(barcodeWithoutCzechSum));
    Contract.Ensures(Contract.Result<char>() != null && Contract.Result<char>().ToString().Trim() != string.Empty); // <-- should fail if length of arg less than 6, which would return a "space" char (' ')

    if (barcodeWithoutCzechSum.Length > 6)
    {
        . . .
        var ch = (char)(48 + b);
        return ch;
    }
    return ' ';
}

...which in some cases returns a char of ' ', which gets converted into an empty string in the "Ensure", doesn't cause the Contract Checker to balk or squawk. Shouldn't it?

UPDATE

Okay, I was misgrokking what was supposed to be happening with Code Contracts - I thought they would tell me at compile time that my contract was in danger of failing.

In actuality, it is at runtime when they fail that they squawk. When I passed a valid arg to the method, no problem; when I passed it a too-short string, though (less than 6 chars), it told me: "*System.Diagnostics.Contracts.__ContractsRuntime.ContractException was unhandled HResult=-2146233088 Message=Postcondition failed: Contract.Result().ToString().Trim() != string.Empty*"

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

0 Answers0