Skip to content

Commit 06654e7

Browse files
committed
Add MPContract to prevent NotImplementedException in unit testing.
1 parent c0bfa27 commit 06654e7

60 files changed

Lines changed: 731 additions & 443 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

MsgPack.Xamarin.sln

Lines changed: 87 additions & 87 deletions
Large diffs are not rendered by default.

Sync.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<Project Name="MsgPack.Xamarin.iOS" Base="MsgPack">
3434
<Preserve Path="Properties\AssemblyInfo.cs" />
35+
<Preserve Path="MPContract.cs" />
3536
<Exclude Path="Serialization\AbstractSerializers\*" />
3637
<Exclude Path="Serialization\CodeDomSerializers\*" />
3738
<Exclude Path="Serialization\EmittingSerializers\*" />

src/MsgPack.Xamarin.Android/MsgPack.Xamarin.Android.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
<Compile Include="..\CommonAssemblyInfo.Pack.cs">
5454
<Link>Properties\CommonAssemblyInfo.Pack.cs</Link>
5555
</Compile>
56+
<Compile Include="..\MsgPack.Xamarin.iOS\MPContract.cs">
57+
<Link>MPContract.cs</Link>
58+
</Compile>
5659
<Compile Include="..\MsgPack\BigEndianBinary.cs">
5760
<Link>BigEndianBinary.cs</Link>
5861
</Compile>
@@ -616,12 +619,7 @@
616619
</Compile>
617620
<Compile Include="Properties\AssemblyInfo.cs" />
618621
</ItemGroup>
619-
<ItemGroup>
620-
<Folder Include="Serialization\" />
621-
<Folder Include="Serialization\Reflection\" />
622-
<Folder Include="Properties\" />
623-
<Folder Include="Serialization\DefaultSerializers\" />
624-
</ItemGroup>
622+
<ItemGroup />
625623
<ItemGroup>
626624
<None Include="..\MsgPack\remarks.xml">
627625
<Link>remarks.xml</Link>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#region -- License Terms --
2+
// MessagePack for CLI
3+
//
4+
// Copyright (C) 2015 FUJIWARA, Yusuke
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
#endregion
18+
19+
using System;
20+
using System.Diagnostics;
21+
22+
namespace MsgPack
23+
{
24+
#if DEBUG
25+
// Use compiler directive to ensure compiler removal of related expressions of assertion.
26+
27+
/// <summary>
28+
/// System.Contract alternative working on Xamarin.
29+
/// </summary>
30+
internal static class MPContract
31+
{
32+
public static void Assert( bool condition, string userMessage )
33+
{
34+
if ( !condition )
35+
{
36+
throw new Exception( "Assertion error: " + userMessage );
37+
}
38+
}
39+
40+
[Conditional("__NEVER")]
41+
public static void EndContractBlock()
42+
{
43+
// nop
44+
}
45+
46+
[Conditional( "__NEVER" )]
47+
public static void Requires( bool expression )
48+
{
49+
// nop
50+
}
51+
52+
[Conditional( "__NEVER" )]
53+
public static void Ensures( bool expression )
54+
{
55+
// nop
56+
}
57+
58+
public static T Result<T>()
59+
{
60+
return default( T );
61+
}
62+
}
63+
#endif // DEBUG
64+
}

src/MsgPack.Xamarin.iOS/MsgPack.Xamarin.iOS.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Folder Include="Resources\" />
44-
<Folder Include="Properties\" />
45-
<Folder Include="Serialization\" />
46-
<Folder Include="Serialization\DefaultSerializers\" />
47-
<Folder Include="Serialization\Reflection\" />
48-
<Folder Include="Serialization\ReflectionSerializers\" />
4944
<Folder Include="Serialization\ReflectionSerializers\ReflectionSerializers\" />
5045
</ItemGroup>
5146
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
@@ -617,6 +612,7 @@
617612
<Compile Include="..\MsgPack\Validation.cs">
618613
<Link>Validation.cs</Link>
619614
</Compile>
615+
<Compile Include="MPContract.cs" />
620616
<Compile Include="Properties\AssemblyInfo.cs" />
621617
</ItemGroup>
622618
<ItemGroup>

src/MsgPack/BigEndianBinary.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -24,7 +24,11 @@
2424

2525
using System;
2626
#if !UNITY
27+
#if XAMIOS || XAMDROID
28+
using Contract = MsgPack.MPContract;
29+
#else
2730
using System.Diagnostics.Contracts;
31+
#endif // XAMIOS || XAMDROID
2832
#endif // !UNITY
2933

3034
namespace MsgPack
@@ -36,9 +40,9 @@ internal static class BigEndianBinary
3640
{
3741
public static sbyte ToSByte( byte[] buffer, int offset )
3842
{
39-
#if !UNITY
40-
Contract.Assume( buffer.Length >= offset + sizeof( sbyte ) );
41-
#endif // !UNITY
43+
#if DEBUG && !UNITY
44+
Contract.Assert( buffer.Length >= offset + sizeof( sbyte ), buffer.Length + ">=" + offset + " + " + sizeof( sbyte ) );
45+
#endif // DEBUG && !UNITY
4246

4347
unchecked
4448
{
@@ -48,9 +52,9 @@ public static sbyte ToSByte( byte[] buffer, int offset )
4852

4953
public static short ToInt16( byte[] buffer, int offset )
5054
{
51-
#if !UNITY
52-
Contract.Assume( buffer.Length >= offset + sizeof( short ) );
53-
#endif // !UNITY
55+
#if DEBUG && !UNITY
56+
Contract.Assert( buffer.Length >= offset + sizeof( short ), buffer.Length + ">=" + offset + " + " + sizeof( short ) );
57+
#endif // DEBUG && !UNITY
5458

5559
unchecked
5660
{
@@ -60,9 +64,9 @@ public static short ToInt16( byte[] buffer, int offset )
6064

6165
public static int ToInt32( byte[] buffer, int offset )
6266
{
63-
#if !UNITY
64-
Contract.Assume( buffer.Length >= offset + sizeof( int ) );
65-
#endif // !UNITY
67+
#if DEBUG && !UNITY
68+
Contract.Assert( buffer.Length >= offset + sizeof( int ), buffer.Length + ">=" + offset + " + " + sizeof( int ) );
69+
#endif // DEBUG && !UNITY
6670

6771
unchecked
6872
{
@@ -72,9 +76,9 @@ public static int ToInt32( byte[] buffer, int offset )
7276

7377
public static long ToInt64( byte[] buffer, int offset )
7478
{
75-
#if !UNITY
76-
Contract.Assume( buffer.Length >= offset + sizeof( long ) );
77-
#endif // !UNITY
79+
#if DEBUG && !UNITY
80+
Contract.Assert( buffer.Length >= offset + sizeof( long ), buffer.Length + ">=" + offset + " + " + sizeof( long ) );
81+
#endif // DEBUG && !UNITY
7882

7983
unchecked
8084
{
@@ -86,18 +90,18 @@ public static long ToInt64( byte[] buffer, int offset )
8690

8791
public static byte ToByte( byte[] buffer, int offset )
8892
{
89-
#if !UNITY
90-
Contract.Assume( buffer.Length >= offset + sizeof( byte ) );
91-
#endif // !UNITY
93+
#if DEBUG && !UNITY
94+
Contract.Assert( buffer.Length >= offset + sizeof( byte ), buffer.Length + ">=" + offset + " + " + sizeof( byte ) );
95+
#endif // DEBUG && !UNITY
9296

9397
return buffer[ offset ];
9498
}
9599

96100
public static ushort ToUInt16( byte[] buffer, int offset )
97101
{
98-
#if !UNITY
99-
Contract.Assume( buffer.Length >= offset + sizeof( ushort ) );
100-
#endif // !UNITY
102+
#if DEBUG && !UNITY
103+
Contract.Assert( buffer.Length >= offset + sizeof( ushort ), buffer.Length + ">=" + offset + " + " + sizeof( ushort ) );
104+
#endif // DEBUG && !UNITY
101105

102106
unchecked
103107
{
@@ -107,9 +111,9 @@ public static ushort ToUInt16( byte[] buffer, int offset )
107111

108112
public static uint ToUInt32( byte[] buffer, int offset )
109113
{
110-
#if !UNITY
111-
Contract.Assume( buffer.Length >= offset + sizeof( uint ) );
112-
#endif // !UNITY
114+
#if DEBUG && !UNITY
115+
Contract.Assert( buffer.Length >= offset + sizeof( uint ), buffer.Length + ">=" + offset + " + " + sizeof( uint ) );
116+
#endif // DEBUG && !UNITY
113117

114118
unchecked
115119
{
@@ -119,9 +123,9 @@ public static uint ToUInt32( byte[] buffer, int offset )
119123

120124
public static ulong ToUInt64( byte[] buffer, int offset )
121125
{
122-
#if !UNITY
123-
Contract.Assume( buffer.Length >= offset + sizeof( ulong ) );
124-
#endif // !UNITY
126+
#if DEBUG && !UNITY
127+
Contract.Assert( buffer.Length >= offset + sizeof( ulong ), buffer.Length + ">=" + offset + " + " + sizeof( ulong ) );
128+
#endif // DEBUG && !UNITY
125129

126130
unchecked
127131
{

src/MsgPack/CollectionOperation.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -25,7 +25,11 @@
2525
using System;
2626
using System.Collections.Generic;
2727
#if !UNITY
28+
#if XAMIOS || XAMDROID
29+
using Contract = MsgPack.MPContract;
30+
#else
2831
using System.Diagnostics.Contracts;
32+
#endif // XAMIOS || XAMDROID
2933
#endif // !UNITY
3034
using System.Linq;
3135

@@ -49,7 +53,7 @@ public static void CopyTo<TSource, TDestination>( IEnumerable<TSource> source, i
4953
{
5054
ValidateCopyToArguments( sourceCount, index, array, arrayIndex, count );
5155
#if !UNITY
52-
Contract.Assert( converter != null );
56+
Contract.Assert( converter != null, "converter != null" );
5357
#endif // !UNITY
5458

5559
int i = 0;

src/MsgPack/Float32Bits.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -24,7 +24,11 @@
2424

2525
using System;
2626
#if !UNITY
27+
#if XAMIOS || XAMDROID
28+
using Contract = MsgPack.MPContract;
29+
#else
2730
using System.Diagnostics.Contracts;
31+
#endif // XAMIOS || XAMDROID
2832
#endif // !UNITY
2933
using System.Runtime.InteropServices;
3034

@@ -85,10 +89,10 @@ public Float32Bits( float value )
8589
/// <param name="offset">Offset to read.</param>
8690
public Float32Bits( byte[] bigEndianBytes, int offset )
8791
{
88-
#if !UNITY
89-
Contract.Assume( bigEndianBytes != null );
90-
Contract.Assume( bigEndianBytes.Length - offset >= 4, bigEndianBytes.Length + "-" + offset + ">= 4" );
91-
#endif // !UNITY
92+
#if !UNITY && DEBUG
93+
Contract.Assert( bigEndianBytes != null, "bigEndianBytes != null" );
94+
Contract.Assert( bigEndianBytes.Length - offset >= 4, bigEndianBytes.Length + "-" + offset + ">= 4" );
95+
#endif // !UNITY && DEBUG
9296

9397
this = default( Float32Bits );
9498

src/MsgPack/Float64Bits.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -24,7 +24,11 @@
2424

2525
using System;
2626
#if !UNITY
27+
#if XAMIOS || XAMDROID
28+
using Contract = MsgPack.MPContract;
29+
#else
2730
using System.Diagnostics.Contracts;
31+
#endif // XAMIOS || XAMDROID
2832
#endif // !UNITY
2933
using System.Runtime.InteropServices;
3034

@@ -105,10 +109,10 @@ internal struct Float64Bits
105109
/// <param name="offset">Offset to read.</param>
106110
public Float64Bits( byte[] bigEndianBytes, int offset )
107111
{
108-
#if !UNITY
109-
Contract.Assume( bigEndianBytes != null );
110-
Contract.Assume( bigEndianBytes.Length - offset >= 8, bigEndianBytes.Length + "-" + offset + ">= 4" );
111-
#endif // !UNITY
112+
#if !UNITY && DEBUG
113+
Contract.Assert( bigEndianBytes != null, "bigEndianBytes != null" );
114+
Contract.Assert( bigEndianBytes.Length - offset >= 8, bigEndianBytes.Length + "-" + offset + ">= 4" );
115+
#endif // !UNITY && DEBUG
112116

113117
this = default( Float64Bits );
114118

src/MsgPack/ItemsUnpacker.Skipping.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// MessagePack for CLI
55
//
6-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
6+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
77
//
88
// Licensed under the Apache License, Version 2.0 (the "License");
99
// you may not use this file except in compliance with the License.
@@ -26,7 +26,11 @@
2626
using System;
2727
using System.Collections.Generic;
2828
#if !UNITY
29+
#if XAMIOS || XAMDROID
30+
using Contract = MsgPack.MPContract;
31+
#else
2932
using System.Diagnostics.Contracts;
33+
#endif // XAMIOS || XAMDROID
3034
#endif // !UNITY
3135
using System.Globalization;
3236

@@ -44,8 +48,8 @@ partial class ItemsUnpacker
4448
var source = this._stream;
4549
var buffer = this._scalarBuffer;
4650
#if !UNITY
47-
Contract.Assert( source != null );
48-
Contract.Assert( buffer != null );
51+
Contract.Assert( source != null, "source != null" );
52+
Contract.Assert( buffer != null, "buffer != null" );
4953
#endif // !UNITY
5054

5155
long remainingItems = -1;

0 commit comments

Comments
 (0)