forked from microsoft/CodeContracts
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional test cases for async methods. Fix for microsoft#70
- Loading branch information
1 parent
8411af3
commit 802c3b1
Showing
7 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Foxtrot/Tests/PublicSurfaceOnly/AsyncMethodWithComplexPrecondition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
48
Foxtrot/Tests/PublicSurfaceOnly/CallPrivateEnsuresShouldNotFail.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Foxtrot/Tests/PublicSurfaceOnly/CallPrivateRequiresShouldNotFail.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
64
Foxtrot/Tests/PublicSurfaceOnly/IteratorWithComplexPrecondition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters