~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
 
29
using System.Data.Linq;
 
30
#endif
 
31
#if !SILVERLIGHT && !NETFX_CORE
 
32
using System.Data.SqlTypes;
 
33
#endif
 
34
using System.IO;
 
35
using System.Text.RegularExpressions;
 
36
using Newtonsoft.Json.Bson;
 
37
using Newtonsoft.Json.Converters;
 
38
using Newtonsoft.Json.Utilities;
 
39
#if !NETFX_CORE
 
40
using NUnit.Framework;
 
41
#else
 
42
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
43
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
44
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
45
#endif
 
46
using Newtonsoft.Json.Tests.TestObjects;
 
47
 
 
48
namespace Newtonsoft.Json.Tests.Converters
 
49
{
 
50
  [TestFixture]
 
51
  public class RegexConverterTests : TestFixtureBase
 
52
  {
 
53
    public class RegexTestClass
 
54
    {
 
55
      public Regex Regex { get; set; }
 
56
    }
 
57
 
 
58
    [Test]
 
59
    public void SerializeToText()
 
60
    {
 
61
      Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
 
62
 
 
63
      string json = JsonConvert.SerializeObject(regex, Formatting.Indented, new RegexConverter());
 
64
 
 
65
      Assert.AreEqual(@"{
 
66
  ""Pattern"": ""abc"",
 
67
  ""Options"": 513
 
68
}", json);
 
69
    }
 
70
 
 
71
    [Test]
 
72
    public void SerializeToBson()
 
73
    {
 
74
      Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
 
75
 
 
76
      MemoryStream ms = new MemoryStream();
 
77
      BsonWriter writer = new BsonWriter(ms);
 
78
      JsonSerializer serializer = new JsonSerializer();
 
79
      serializer.Converters.Add(new RegexConverter());
 
80
 
 
81
      serializer.Serialize(writer, new RegexTestClass { Regex = regex });
 
82
 
 
83
      string expected = "13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00";
 
84
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
 
85
 
 
86
      Assert.AreEqual(expected, bson);
 
87
    }
 
88
 
 
89
    [Test]
 
90
    public void DeserializeFromText()
 
91
    {
 
92
      string json = @"{
 
93
  ""Pattern"": ""abc"",
 
94
  ""Options"": 513
 
95
}";
 
96
 
 
97
      Regex newRegex = JsonConvert.DeserializeObject<Regex>(json, new RegexConverter());
 
98
      Assert.AreEqual("abc", newRegex.ToString());
 
99
      Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, newRegex.Options);
 
100
    }
 
101
 
 
102
    [Test]
 
103
    public void DeserializeFromBson()
 
104
    {
 
105
      MemoryStream ms = new MemoryStream(MiscellaneousUtils.HexToBytes("13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00"));
 
106
      BsonReader reader = new BsonReader(ms);
 
107
      JsonSerializer serializer = new JsonSerializer();
 
108
      serializer.Converters.Add(new RegexConverter());
 
109
 
 
110
      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
 
111
 
 
112
      Assert.AreEqual("abc", c.Regex.ToString());
 
113
      Assert.AreEqual(RegexOptions.IgnoreCase, c.Regex.Options);
 
114
    }
 
115
 
 
116
    [Test]
 
117
    public void ConvertEmptyRegexBson()
 
118
    {
 
119
      Regex regex = new Regex(string.Empty);
 
120
 
 
121
      MemoryStream ms = new MemoryStream();
 
122
      BsonWriter writer = new BsonWriter(ms);
 
123
      JsonSerializer serializer = new JsonSerializer();
 
124
      serializer.Converters.Add(new RegexConverter());
 
125
 
 
126
      serializer.Serialize(writer, new RegexTestClass { Regex = regex });
 
127
 
 
128
      ms.Seek(0, SeekOrigin.Begin);
 
129
      BsonReader reader = new BsonReader(ms);
 
130
      serializer.Converters.Add(new RegexConverter());
 
131
 
 
132
      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
 
133
 
 
134
      Assert.AreEqual("", c.Regex.ToString());
 
135
      Assert.AreEqual(RegexOptions.None, c.Regex.Options);
 
136
    }
 
137
 
 
138
    [Test]
 
139
    public void ConvertRegexWithAllOptionsBson()
 
140
    {
 
141
      Regex regex = new Regex(
 
142
        "/",
 
143
        RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
 
144
 
 
145
      MemoryStream ms = new MemoryStream();
 
146
      BsonWriter writer = new BsonWriter(ms);
 
147
      JsonSerializer serializer = new JsonSerializer();
 
148
      serializer.Converters.Add(new RegexConverter());
 
149
 
 
150
      serializer.Serialize(writer, new RegexTestClass { Regex = regex });
 
151
 
 
152
      string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00";
 
153
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
 
154
 
 
155
      Assert.AreEqual(expected, bson);
 
156
 
 
157
      ms.Seek(0, SeekOrigin.Begin);
 
158
      BsonReader reader = new BsonReader(ms);
 
159
      serializer.Converters.Add(new RegexConverter());
 
160
 
 
161
      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
 
162
 
 
163
      Assert.AreEqual("/", c.Regex.ToString());
 
164
      Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture, c.Regex.Options);
 
165
    }
 
166
 
 
167
    [Test]
 
168
    public void ConvertEmptyRegexJson()
 
169
    {
 
170
      Regex regex = new Regex("");
 
171
 
 
172
      string json = JsonConvert.SerializeObject(new RegexTestClass { Regex = regex }, Formatting.Indented, new RegexConverter());
 
173
 
 
174
      Assert.AreEqual(@"{
 
175
  ""Regex"": {
 
176
    ""Pattern"": """",
 
177
    ""Options"": 0
 
178
  }
 
179
}", json);
 
180
 
 
181
      RegexTestClass newRegex = JsonConvert.DeserializeObject<RegexTestClass>(json, new RegexConverter());
 
182
      Assert.AreEqual("", newRegex.Regex.ToString());
 
183
      Assert.AreEqual(RegexOptions.None, newRegex.Regex.Options);
 
184
    }
 
185
  }
 
186
}
 
 
b'\\ No newline at end of file'