Test fails when covering a method that has specific set of code contracts

Hi

I've searched the forum and haven't seen this reported yet. I am experiencing some of our tests passing fine when run via R# and or command line, but when being covered they fail for a methods that have a specific combination of code contracts. The error message for the failed test is unusual too : System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Below is sample code. I have also attached a solution to repro it. I am using dotCover 1.0.87.3. Is it a bug or am I doing something wrong?

Snippet

namespace DotCoverCrashTest
{
     using System;
     using System.Diagnostics.Contracts;
     using Xunit;

     public static class ClassWithContracts
     {
          public static bool TrySomething1(string s, out string result)
          {
               Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(s));
               result = "test";
               return true;
          }

          public static bool TrySomething2(string s, out string result)
          {
               Contract.Ensures(
                    Contract.Result<bool>() ?
                    Contract.ValueAtReturn(out result) != null :
                    Contract.ValueAtReturn(out result) == null);
               result = "test";
               return true;
          }

          public static bool TrySomething3(string s, out string result)
          {
               // this method has the two contracts defined above combined and fails when covered
               Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(s));
               Contract.Ensures(
                    Contract.Result<bool>() ?
                    Contract.ValueAtReturn(out result) != null :
                    Contract.ValueAtReturn(out result) == null);
               result = "test";
               return true;
          }
     }

     public class ClassWithContractsTests
     {
          [Fact]
          public void Test1()
          {
               string result;
               Assert.True(ClassWithContracts.TrySomething1("Test", out result));
          }

          [Fact]
          public void Test2()
          {
               string result;
               Assert.True(ClassWithContracts.TrySomething2("Test", out result));
          }

          [Fact]
          public void Test3()
          {
               // this test fails when being covered
               string result;
               Assert.True(ClassWithContracts.TrySomething3("Test", out result));
          }
     }
}


Attachment(s):
DotCoverCrashTest.7z.zip
2 comments

Just to add, this occurs both running it via resharper xunit runner plugin and the dotcover command line tool. So I don't think it's the xunit runner plugin.

0

Hi Damian,

  Thank you very much for reporting this. We'll look into this issue.

WBR, Oleg

0

Please sign in to leave a comment.