Skip to content

Commit

Permalink
Additional test cases for async methods. Fix for microsoft#70
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyTeplyakov committed Jun 26, 2015
1 parent 8411af3 commit 802c3b1
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 0 deletions.
@@ -0,0 +1,54 @@
// CodeContracts
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.Contracts;

namespace Tests.Sources
{
partial class TestMain
{
async Task<int> FooAsync(int start, int end) {
Contract.Requires(start >= 0 && start < end);
await Task.Delay(42);
return 42;
}

void Test(int start)
{
var x = FooAsync(start, start+5).Result;
Console.WriteLine(x);
}

partial void Run()
{
if (behave)
{
this.Test(0);
}
else
{
// Just failing manually, because with public preconditions only
// TestMain will never fail!
throw new ArgumentException("Done!");
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition;
public string NegativeExpectedCondition = "Done!";
}
}
48 changes: 48 additions & 0 deletions Foxtrot/Tests/PublicSurfaceOnly/CallPrivateEnsuresShouldNotFail.cs
@@ -0,0 +1,48 @@
// CodeContracts
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Contracts;

namespace Tests.Sources
{
partial class TestMain
{
void PrivateMethod(bool expected)
{
Contract.Ensures(expected);
}

partial void Run()
{
if (behave)
{
// Should not fail, because this code should be running with /publicsurface
// switch and precondition check should be removed!
this.PrivateMethod(false);
}
else
{
// Just failing manually, because with public preconditions only
// TestMain will never fail!
throw new ArgumentException("Done!");
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition;
public string NegativeExpectedCondition = "Done!";
}
}
@@ -0,0 +1,48 @@
// CodeContracts
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Contracts;

namespace Tests.Sources
{
partial class TestMain
{
void PrivateMethod(bool expected)
{
Contract.Requires(expected);
}

partial void Run()
{
if (behave)
{
// Should not fail, because this code should be running with /publicsurface
// switch and precondition check should be removed!
this.PrivateMethod(false);
}
else
{
// Just failing manually, because with public preconditions only
// TestMain will never fail!
throw new ArgumentException("Done!");
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition;
public string NegativeExpectedCondition = "Done!";
}
}
64 changes: 64 additions & 0 deletions Foxtrot/Tests/PublicSurfaceOnly/IteratorWithComplexPrecondition.cs
@@ -0,0 +1,64 @@
// CodeContracts
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Contracts;

namespace Tests.Sources
{
partial class TestMain
{
IEnumerable<int> GetInts(int start, int end) {
Contract.Requires(start >= 0 && start < end);

yield return 0;
yield return 1;
yield return 2;

while (start < end) {
yield return start++;
}

yield return 10;
yield return 11;
yield return 12;
}

void Test(int start)
{
foreach (var x in GetInts(start, start+5)) {
Console.WriteLine(x);
}
}

partial void Run()
{
if (behave)
{
this.Test(0);
}
else
{
// Just failing manually, because with public preconditions only
// TestMain will never fail!
throw new ArgumentException("Done!");
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition;
public string NegativeExpectedCondition = "Done!";
}
}
17 changes: 17 additions & 0 deletions Foxtrot/Tests/RewriterTests.cs
Expand Up @@ -243,6 +243,23 @@ public void BuildRewriteRunFromSourcesV40AgainstV35Contracts()
TestDriver.BuildRewriteRun(options);
}

[DeploymentItem("Foxtrot\\Tests\\TestInputs.xml"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestInputs.xml", "PublicSurfaceOnly", DataAccessMethod.Sequential)]
[TestMethod]
[TestCategory("Runtime"), TestCategory("CoreTest"), TestCategory("V4.5")]
public void BuildRewriteFromSources45WithPublicSurfaceOnly()
{
var options = new Options(this.TestContext);
// For testing purposes you can remove /publicsurface and see what happen. Part of the tests should fail!
//options.FoxtrotOptions = options.FoxtrotOptions + String.Format("/throwonfailure /rw:{0}.exe,TestInfrastructure.RewriterMethods", Path.GetFileNameWithoutExtension(options.TestName));
options.FoxtrotOptions = options.FoxtrotOptions + String.Format(" /publicsurface /throwonfailure /rw:{0}.exe,TestInfrastructure.RewriterMethods", Path.GetFileNameWithoutExtension(options.TestName));
options.BuildFramework = @".NETFramework\v4.5";
options.ContractFramework = @".NETFramework\v4.0";
options.UseTestHarness = true;

TestDriver.BuildRewriteRun(options);
}


private void GrabTestOptions(out string sourceFile, out string options, out string cscoptions, out List<string> refs, out List<string> libs)
{
sourceFile = (string)TestContext.DataRow["Name"];
Expand Down
42 changes: 42 additions & 0 deletions Foxtrot/Tests/TestInputs.xml
Expand Up @@ -825,4 +825,46 @@
UseExe="false"
MustSucceed="false"
/>

<PublicSurfaceOnly
Name="Foxtrot\Tests\PublicSurfaceOnly\IteratorWithComplexPrecondition.cs"
CompilerOptions=""
FoxtrotOptions=""
ContractReferenceAssemblies="false"
BinDir="false"
Compiler="CS"
MustSucceed="false"
/>

<PublicSurfaceOnly
Name="Foxtrot\Tests\PublicSurfaceOnly\AsyncMethodWithComplexPrecondition.cs"
CompilerOptions=""
FoxtrotOptions=""
References="AsyncCtpLibrary.dll"
ContractReferenceAssemblies="false"
BinDir="false"
Compiler="CS"
MustSucceed="false"
/>

<PublicSurfaceOnly
Name="Foxtrot\Tests\PublicSurfaceOnly\CallPrivateRequiresShouldNotFail.cs"
CompilerOptions=""
FoxtrotOptions=""
ContractReferenceAssemblies="false"
BinDir="false"
Compiler="CS"
MustSucceed="false"
/>

<PublicSurfaceOnly
Name="Foxtrot\Tests\PublicSurfaceOnly\CallPrivateEnsuresShouldNotFail.cs"
CompilerOptions=""
FoxtrotOptions=""
ContractReferenceAssemblies="false"
BinDir="false"
Compiler="CS"
MustSucceed="false"
/>

</TestInputs>
3 changes: 3 additions & 0 deletions Foxtrot/Tests/UnitTests/RewrittenContractTest.cs
Expand Up @@ -272,7 +272,9 @@ public class RewrittenContractTest : DisableAssertUI {
new CodeUnderTest.RewrittenContractTest().CallPrivateRequiresTrue(true);
}


[TestMethod, TestCategory("Runtime"), TestCategory("V4.0"), TestCategory("CoreTest"), TestCategory("Short")]
[Ignore] // This test should be ignored, because the whole test suite is running without /publicsurface flag!
public void PositivePrivateRequiresTest2() {
// Does not trigger due to /publicsurface
new CodeUnderTest.RewrittenContractTest().CallPrivateRequiresTrue(false);
Expand All @@ -295,6 +297,7 @@ public class RewrittenContractTest : DisableAssertUI {
}

[TestMethod, TestCategory("Runtime"), TestCategory("V4.0"), TestCategory("CoreTest"), TestCategory("Short")]
[Ignore] // This test should be ignored, because the whole test suite is running without /publicsurface flag!
public void PositivePrivateEnsuresTest2() {
// Does not trigger due to /publicsurface
new CodeUnderTest.RewrittenContractTest().CallPrivateEnsuresTrue(false);
Expand Down

0 comments on commit 802c3b1

Please sign in to comment.