Pex抱怨“缺陷”因违反方法声明的代码合同而被暴露

编程入门 行业动态 更新时间:2024-10-19 16:30:07
本文介绍了Pex抱怨“缺陷”因违反方法声明的代码合同而被暴露的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当采用Code Contracts检查空参数值时,Pex不会忽略方法,因为没有检查参数的空值。 示例:

Pex keeps flunking out methods as not checking for null values for parameters when Code Contracts are employed to check for null parameter values. Example:

/// <summary> /// Bit shifts the enum bit pattern leftward. /// </summary> /// <param name="bitPattern"></param> /// <param name="timesToShift">The number of times to shift the bit pattern.</param> /// <returns></returns> /// <seealso cref="http&#58;//msdn.microsoft/en-us/library/a1sway8w.aspx"/> public static Enum BitShiftLeft(this Enum bitPattern, int timesToShift) { Contract.Requires(bitPattern != default(Enum)); Contract.Requires(EnumFlagsUtilities.DoesEnumHaveFlagsAttribute(bitPattern), ErrorMessageUtils.EnumNotAFlagsEnumMessage); int enumAsAnInt = Convert.ToInt32(bitPattern); enumAsAnInt <<= timesToShift; Enum result = (Enum)Convert.ChangeType(enumAsAnInt, bitPattern.GetType()); return result; }

推荐答案

Pex生成这些输入以实现完整的代码覆盖。

那些违反顶级Requires子句的测试用例应该被标记为"成功"和"rdquo;"。他们不是吗?

有一些属性,例如 PexAllowedContractRequiresFailureAttribute , PexAllowedContractRequiresFailureAtTypeUnderTestSurfaceAttribute ,用于控制合同失败的分类方式(" ;失败" vs."成功")。

>问题是,我声明如果用户试图通过声明一个需要bitpattern的合同来做到这一点,那将会发生什么!= default(Enum)!

好吧,从某种意义上说,你宣布将要发生的一切:毕竟, 你写了代码。如果有什么东西,bug会进入发生在 实际行为偏离预期的行为的地方。从这个意义上说,Pex生成这些测试用例是件好事。有时它们似乎是有点多余,但他们只是展示实际会发生什么。如果这是你所期望的那么多,那么我会认为这是一件好事。(否则,这是一个错误。)

Well, in some sense you declare everything that will happen: After all, you wrote the code. Bugs creep in if something happens where the actual behavior deviates from your intended behavior. In that sense, it's a good thing that Pex generates these test cases. They might sometimes appear to be a bit redundant, but they just show what would actually happen. If that's what you expect, then I would consider that to be a good thing. (Otherwise, it's a bug.)

错误可以在实际代码中,也可以在合同中.Pex分析两者。

更多推荐

Pex抱怨“缺陷”因违反方法声明的代码合同而被暴露

本文发布于:2023-11-12 12:15:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缺陷   声明   合同   代码   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!