From 1463f95af54a8da3eb3a7d724cffa408f6f5a30a Mon Sep 17 00:00:00 2001 From: Alexander Peel Date: Mon, 17 Feb 2014 17:35:46 +0200 Subject: [PATCH 1/2] Added basic code contracts to public api 1. Added code contract to project compilation 2. Added not null postcondition to mock factory methods 3. Added not null postcondition to "Object" properties on mock --- Source/Linq/Mock.cs | 4 ++++ Source/Mock.Generic.cs | 7 ++++++- Source/Mock.cs | 7 ++++++- Source/Moq.csproj | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Source/Linq/Mock.cs b/Source/Linq/Mock.cs index 1d3586b14..0f7588722 100644 --- a/Source/Linq/Mock.cs +++ b/Source/Linq/Mock.cs @@ -40,6 +40,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Linq.Expressions; @@ -57,6 +58,7 @@ public partial class Mock /// The mocked object created. public static T Of() where T : class { + Contract.Ensures(Contract.Result() != null); return Mocks.CreateMockQuery().First(); } @@ -69,6 +71,8 @@ public static T Of() where T : class [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By Design")] public static T Of(Expression> predicate) where T : class { + Contract.Ensures(Contract.Result() != null); + return Mocks.CreateMockQuery().First(predicate); } } diff --git a/Source/Mock.Generic.cs b/Source/Mock.Generic.cs index 953dccfc0..148f5f2d7 100644 --- a/Source/Mock.Generic.cs +++ b/Source/Mock.Generic.cs @@ -40,6 +40,7 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using System.Linq.Expressions; using Moq.Language.Flow; using Moq.Proxy; @@ -162,7 +163,11 @@ private void CheckParameters() [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "The public Object property is the only one visible to Moq consumers. The protected member is for internal use only.")] public virtual new T Object { - get { return (T)base.Object; } + get + { + Contract.Ensures(Contract.Result() != null); + return (T)base.Object; + } } /// diff --git a/Source/Mock.cs b/Source/Mock.cs index 469dda953..ee98553cf 100644 --- a/Source/Mock.cs +++ b/Source/Mock.cs @@ -38,6 +38,7 @@ //[This is the BSD license, see // http://www.opensource.org/licenses/bsd-license.php] +using System.Diagnostics.Contracts; using Moq.Properties; using Moq.Proxy; using System; @@ -152,7 +153,11 @@ private void SetDefaultValue(DefaultValue value) [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "The public Object property is the only one visible to Moq consumers. The protected member is for internal use only.")] public object Object { - get { return this.GetObject(); } + get + { + Contract.Ensures(Contract.Result() != null); + return this.GetObject(); + } } private object GetObject() diff --git a/Source/Moq.csproj b/Source/Moq.csproj index bb5fdc6f3..472c3f6b7 100644 --- a/Source/Moq.csproj +++ b/Source/Moq.csproj @@ -33,6 +33,40 @@ false + False + False + True + False + False + True + True + True + True + True + True + True + True + True + True + False + True + False + False + True + False + False + + + + + + + + True + False + Full + Build + 3 full From be6d418808361de3753b227e90a0bc803b8328c4 Mon Sep 17 00:00:00 2001 From: Alexander Peel Date: Mon, 17 Feb 2014 18:02:49 +0200 Subject: [PATCH 2/2] Optimized code contracts in project file 1. Added code contracts to release build 2. Disabled contract analysis for Moq itself --- Source/Moq.csproj | 54 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/Source/Moq.csproj b/Source/Moq.csproj index 472c3f6b7..f07de08e7 100644 --- a/Source/Moq.csproj +++ b/Source/Moq.csproj @@ -38,23 +38,23 @@ True False False - True - True - True - True - True + False + False + False + False + False True - True - True - True + False + False + False True False - True + False False False True False - False + True @@ -82,6 +82,40 @@ true true AllRules.ruleset + False + False + True + False + False + False + True + True + False + False + False + True + True + False + False + False + True + False + True + True + False + True + + + + + + + + True + False + Full + Build + 0