code合同似乎并没有工作在VS2012 [英] Code Contracts doesn't seem to work on VS2012

查看:242
本文介绍了code合同似乎并没有工作在VS2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了code合同,乍一看似乎是pretty的革命,但我似乎无法让他们的工作。我运行Windows 8和Visual Studio 2012 premium(两者的发行版本)。然后我安装了这里 code合同通过点击下载code合同链接。

然后我写了下面的code在一个全新的控制台应用程序:

 类节目
{
   静态无效的主要(字串[] args)
   {
      VAR答案=添加(0,5);
      Console.Write(答案);

      到Console.ReadLine();
   }

   静态INT添加(INT X,int y)对
   {
      Contract.Requires(X大于0&安培;&安培y与其所连接; 0);

      返回X + Y;
   }
}
 

我希望编译失败,因为的第一个参数添加 0,但该计划成功,并打印出5到控制台。

我试着用默认的code合同的设置,并且还与东西搞乱了一下,都无济于事。我目前的设置是这样的:

任何想法,我做错了什么?

更新:

下面是从生成窗口的结果。看来它做什么,而只是提出警告,而不是错误。在我看了视频,这些东西得到了标记为编译错误,程序将不能运行。

  1> ------生成开始:项目:DELETEME,配置:调试任何CPU ------
1> DELETEME  - > C:\用户\麦克风\文档\的Visual Studio 2012 \项目\ DELETEME \ DELETEME \斌\调试\ DeleteMe.exe
1> codeContracts:任务管理器不可用。
1> codeContracts:DELETEME:运行静态合同分析。
1> codeContracts:建议需要:Contract.Requires(假);
1> codeContracts:DELETEME:验证:0.0%
1> codeContracts:DELETEME:合同密度:0.87
1> codeContracts:DELETEME:总法分析4
1> codeContracts:DELETEME:方法与0警告3
1> codeContracts:DELETEME:总时间4.974sec。 1243ms /法
1> codeContracts:DELETEME:方法与必要preconditions:1
1> codeContracts:DELETEME:1发现新的候选preconditions在00:00:00.1718843
1> codeContracts:DELETEME:保留1 preconditions滤波后
1> codeContracts:DELETEME:0推断对象不变
1> codeContracts:DELETEME:保留0对象不变量滤波后
1> codeContracts:DELETEME:检测0 code修复
1> codeContracts:DELETEME:证明责任与code修正:0
1> C:\用户\麦克\文档\的Visual Studio 2012 \项目\ DELETEME \ DELETEME \的Program.cs(14,10,14,33):警告:codeContracts:需要的是假的:X> 0安培;&安培; Ÿ> 0
1> C:\用户\麦克\文档\的Visual Studio 2012 \项目\ DELETEME \ DELETEME \的Program.cs(22,10,22,44):警告:codeContracts:位置与previous警告
1> codeContracts:经过1断言:1假
1> codeContracts:DELETEME:
1> codeContracts:DELETEME:静态的合同做分析。
==========生成:1成功,0失败,0上升到最新,0跳过==========
 

解决方案

所以,这个问题似乎是一些限制和code合同相结合的陷阱的。希望这个答案可以帮助像我这样刚刚起步。

首先,code合同的没有的支持的Visual Studio 2012(其他版本比前preSS),因为构建1.4.50327.0,但你必须运行 devenv.exe的/设置如果你的版本是早于1.4.50910.0。请参阅发行说明了解更多信息。

我遇到的第一个问题是,缓存结果复选框是code合同属性选项卡中的静态检查一节中检查。此选项默认情况下,还需要SQL Server CE存储其缓存的数据,这是不被Windows 8,VS2012或code合同安装。不幸的是,你的程序将继续汇编蛮好的,你必须手动地挖通生成输出看到错误:

  

codeContracts:XXX:未处理的异常:   System.IO.FileNotFoundException:找不到无法加载文件或程序集   System.Data.SqlServerCe,版本= 3.5.1.0,文化=中立,   公钥= 89845dcd8080cc91或它的某一个依赖。该   系统找不到指定的文件。

取消勾选​​缓存结果复选框,将解决这个问题,因为将安装SQL Server CE。

第二个问题是违规视为<一个是code合同href="http://connect.microsoft.com/VisualStudio/feedback/details/646880/$c$c-contracts-dont-listen-to-treat-warnings-as-errors-setting">warnings,而不是编译错误。即使你将警告视为错误启用后,你的程序将继续编译并运行成功。如果你有万吨忽略警告较大的项目,它可能很难注意到这些新的code合同警告。在演示视频我看了,这些警告也反映在Visual Studio IDE(调用code具有蓝色的下划线),但我似乎并没有得到在Visual Studio 2012这一行为。

这样的设计决定我感到不安。如果我定义在我的code合同,一个函数必须接受一个大于0的整数,我大摇大摆地传递了一个0,这是一个错误。没有警告。我打破了该合同,简单明了。

总的来说,我会说,code合同是非常强大的,并有可能改变我们测试软件的方式。 MS研究肯定做了伟大的工作。但是,我不认为这是真的准备好为主流呢。这需要一些调整,以获得工作,它不的无缝的集成到Visual Studio生成过程,它也是pretty的慢。在较小的项目,它的工作如预期,但是当我在插入到一个更大的项目,花了足足有十分钟,分析所有的code。

I'm reading up on Code Contracts, which at first glance seem to be pretty revolutionary, but I can't seem to get them working. I'm running Windows 8 and Visual Studio 2012 Premium (Release versions of both). I then installed Code Contracts from here by clicking on the Download Code Contracts link.

I then wrote the following code in a brand new console app:

class Program
{
   static void Main(string[] args)
   {
      var answer = Add(0, 5);
      Console.Write(answer);

      Console.ReadLine();
   }

   static int Add(int x, int y)
   {
      Contract.Requires(x > 0 && y > 0);

      return x + y;
   }
}

I expect the compilation to fail, since the first parameter of Add is 0, but the program succeeds and prints 5 out to the console.

I've tried with the default Code Contract settings, and also mucked with stuff a bit to no avail. My current settings look like this:

Any ideas what I'm doing wrong?

UPDATE:

Here's the results from the Build window. It appears it's doing something, but just raising warnings instead of errors. In the video I watched, these things got flagged as compile errors and the program wouldn't even run.

1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------
1>  DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe
1>  CodeContracts: Task manager is unavailable.
1>  CodeContracts: DeleteMe: Run static contract analysis.
1>  CodeContracts: Suggested requires: Contract.Requires(false);
1>  CodeContracts: DeleteMe: Validated:  0.0 %
1>  CodeContracts: DeleteMe: Contract density: 0.87
1>  CodeContracts: DeleteMe: Total methods analyzed 4
1>  CodeContracts: DeleteMe: Methods with 0 warnings 3
1>  CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method
1>  CodeContracts: DeleteMe: Methods with necessary preconditions: 1
1>  CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843
1>  CodeContracts: DeleteMe: Retained 1 preconditions after filtering
1>  CodeContracts: DeleteMe: Inferred 0 object invariants
1>  CodeContracts: DeleteMe: Retained 0 object invariants after filtering
1>  CodeContracts: DeleteMe: Detected 0 code fixes
1>  CodeContracts: DeleteMe: Proof obligations with a code fix: 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning
1>  CodeContracts: Checked 1 assertion: 1 false
1>  CodeContracts: DeleteMe: 
1>  CodeContracts: DeleteMe: Static contract analysis done.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

解决方案

So, the issue seemed to be a combination of several limitations and gotchas with Code Contracts. Hopefully this answer will help people like me just starting out.

First off, Code Contracts does support Visual Studio 2012 (any version other than Express) since build 1.4.50327.0, though you have to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more information.

The first issue I was having was that the "Cache Results" check box was checked in the "Static Checking" section of the Code Contracts properties tab. This option is on by default, and also requires SQL Server CE to store its cached data, which is not installed by Windows 8, VS2012 or Code Contracts. Unfortunately, your program will continue to compile just fine, and you'd have to manually go dig through the Build output to see the error:

CodeContracts: xxx: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

Unchecking the "Cache Results" checkbox will fix this issue, as would installing SQL Server CE.

The second issue is that Code Contract violations are treated as warnings, and not compile errors. Even if you have "Treat Warnings as Errors" enabled, your program will continue to compile and run successfully. If you have a larger project with tons of Warnings you ignore, it could potentially be difficult to notice these new Code Contract warnings. In the demo video I saw, these warnings were also reflected in the Visual Studio IDE (the calling code had a blue underline), however I don't seem to get this behavior in Visual Studio 2012.

This design decision disturbs me. If I define a contract within my code, that a function must take an integer greater than 0, and I blatantly pass in a 0, this is an error. Not a warning. I broke that contract, plain and simple.

Overall, I'd say Code Contracts is extremely powerful, and could potentially change the way we test software. MS Research definitely did great work. However, I don't think it's really ready for mainstream yet. It takes some tweaking to get working, it doesn't seamlessly integrate into the Visual Studio build process, and it's also pretty slow. On smaller projects, it worked as expected, but when I plugged it in to a larger project, it took a good ten minutes to analyze all the code.

这篇关于code合同似乎并没有工作在VS2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆