以下是 Pex 文档 pexandcontracts.pdf http://research.microsoft.com/en-us/projects/pex/pexandcontracts.pdf中的代码示例。我意识到这不是特定于 pex 的问题,而是与代码合同有关,但它是 pex 教程中的代码。
namespace CodeDigging
{
public class StringExtension
{
public static string TrimAfter(string value, string suffix)
{
// <pex>
Contract.Requires(!string.IsNullOrEmpty(suffix));
// </pex>
Contract.Requires(value != null);
Contract.Ensures(!Contract.Result<string>().EndsWith(suffix));
int index = value.IndexOf(suffix);
if (index < 0)
return value; // (1) First possible contract violation
return value.Substring(0, index); // (2) second possible contract violation
}
}
}
静态验证器提供以下警告:
CodeContracts:确保未经证实:!Contract.Result().EndsWith(suffix) 在点 (1) 和点 (2)。
我的问题是,如何解决这个问题?根据 pex 的探索,合同没有被违反的可能性。(... 在那儿?)