C# Code Contracts and Interface Properties, http://d-fens.ch/2016/05/22/c-code-contracts-and-interface-properties
/** | |
* Copyright 2016 d-fens GmbH | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
using System.Runtime.CompilerServices; | |
using biz.dfch.CS.Utilities.General; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics.Contracts; | |
using System.IO; | |
using System.Linq; | |
using System.Management.Automation; | |
using System.Web; | |
using System.Collections; | |
namespace biz.dfch.CS.General.Tests | |
{ | |
public class ContractTest : IContractTest | |
{ | |
public int NonZeroNumber { get; set; } | |
[ContractInvariantMethod] | |
private void ContractInvariantMethod() | |
{ | |
Contract.Invariant(0 != NonZeroNumber); | |
} | |
private string _NonEmptyString; | |
public string NonEmptyString | |
{ | |
get | |
{ | |
Contract.Ensures(string.IsNullOrEmpty(Contract.Result<string>())); | |
return _NonEmptyString; | |
} | |
set | |
{ | |
Contract.Requires(string.IsNullOrEmpty(value)); | |
_NonEmptyString = value; | |
} | |
} | |
public ContractTest(int i) | |
{ | |
NonZeroNumber = 0; ///< will not violate contract | |
NonZeroNumber = i; | |
} | |
public ContractTest(string s) | |
{ | |
NonEmptyString = string.Empty; ///< will violate contract | |
NonEmptyString = s; | |
} | |
} | |
} |
/** | |
* Copyright 2016 d-fens GmbH | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
using System.IO; | |
using System.Reflection; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Management.Automation; | |
namespace biz.dfch.CS.General.Tests | |
{ | |
[TestClass] | |
public class ContractTestTest | |
{ | |
[TestMethod] | |
public void ContractInvariantSucceeds() | |
{ | |
// Arange | |
var i = 42; | |
// Act | |
var sut = new ContractTestTest(i); | |
// Assert | |
Assert.AreEqual(i, sut.NonZeroNumber); | |
} | |
[TestMethod] | |
[ExpectContractFailure] | |
public void ContractInvariantThrowsContractException() | |
{ | |
// Arange | |
var i = 0; | |
// Act | |
var sut = new ContractTestTest(i); | |
// Assert | |
// N/A | |
} | |
[TestMethod] | |
[ExpectContractFailure] | |
public void ContractBodyThrowsContractException() | |
{ | |
// Arange | |
var s = "arbitrary-non-empty-string"; | |
// Act | |
var sut = new ContractTestTest(s); | |
// Assert | |
// N/A | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment