File: RegexConverterTests.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (161 lines) | stat: -rw-r--r-- 5,111 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
´╗┐using System;
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
using System.Data.Linq;
#endif
#if !SILVERLIGHT && !NETFX_CORE
using System.Data.SqlTypes;
#endif
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Utilities;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json.Tests.TestObjects;

namespace Newtonsoft.Json.Tests.Converters
{
  [TestFixture]
  public class RegexConverterTests : TestFixtureBase
  {
    public class RegexTestClass
    {
      public Regex Regex { get; set; }
    }

    [Test]
    public void SerializeToText()
    {
      Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

      string json = JsonConvert.SerializeObject(regex, Formatting.Indented, new RegexConverter());

      Assert.AreEqual(@"{
  ""Pattern"": ""abc"",
  ""Options"": 513
}", json);
    }

    [Test]
    public void SerializeToBson()
    {
      Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      string expected = "13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00";
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());

      Assert.AreEqual(expected, bson);
    }

    [Test]
    public void DeserializeFromText()
    {
      string json = @"{
  ""Pattern"": ""abc"",
  ""Options"": 513
}";

      Regex newRegex = JsonConvert.DeserializeObject<Regex>(json, new RegexConverter());
      Assert.AreEqual("abc", newRegex.ToString());
      Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, newRegex.Options);
    }

    [Test]
    public void DeserializeFromBson()
    {
      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"));
      BsonReader reader = new BsonReader(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

      Assert.AreEqual("abc", c.Regex.ToString());
      Assert.AreEqual(RegexOptions.IgnoreCase, c.Regex.Options);
    }

    [Test]
    public void ConvertEmptyRegexBson()
    {
      Regex regex = new Regex(string.Empty);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);
      serializer.Converters.Add(new RegexConverter());

      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

      Assert.AreEqual("", c.Regex.ToString());
      Assert.AreEqual(RegexOptions.None, c.Regex.Options);
    }

    [Test]
    public void ConvertRegexWithAllOptionsBson()
    {
      Regex regex = new Regex(
        "/",
        RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00";
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());

      Assert.AreEqual(expected, bson);

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);
      serializer.Converters.Add(new RegexConverter());

      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

      Assert.AreEqual("/", c.Regex.ToString());
      Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture, c.Regex.Options);
    }

    [Test]
    public void ConvertEmptyRegexJson()
    {
      Regex regex = new Regex("");

      string json = JsonConvert.SerializeObject(new RegexTestClass { Regex = regex }, Formatting.Indented, new RegexConverter());

      Assert.AreEqual(@"{
  ""Regex"": {
    ""Pattern"": """",
    ""Options"": 0
  }
}", json);

      RegexTestClass newRegex = JsonConvert.DeserializeObject<RegexTestClass>(json, new RegexConverter());
      Assert.AreEqual("", newRegex.Regex.ToString());
      Assert.AreEqual(RegexOptions.None, newRegex.Regex.Options);
    }
  }
}