~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/VersionConverterTests.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 Newtonsoft.Json.Converters;
 
28
#if !NETFX_CORE
 
29
using NUnit.Framework;
 
30
#else
 
31
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
32
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
33
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
34
#endif
 
35
 
 
36
namespace Newtonsoft.Json.Tests.Converters
 
37
{
 
38
  public class VersionClass
 
39
  {
 
40
    public VersionClass(string version1, string version2)
 
41
    {
 
42
      this.StringProperty1 = "StringProperty1";
 
43
      this.Version1 = new Version(version1);
 
44
      this.Version2 = new Version(version2);
 
45
      this.StringProperty2 = "StringProperty2";
 
46
    }
 
47
 
 
48
    public VersionClass()
 
49
    {
 
50
    }
 
51
 
 
52
    public string StringProperty1 { get; set; }
 
53
    public Version Version1 { get; set; }
 
54
    public Version Version2 { get; set; }
 
55
    public string StringProperty2 { get; set; }
 
56
  }
 
57
 
 
58
  [TestFixture]
 
59
  public class VersionConverterTests : TestFixtureBase
 
60
  {
 
61
    private void SerializeVersionClass(string version1, string version2)
 
62
    {
 
63
      VersionClass versionClass = new VersionClass(version1, version2);
 
64
 
 
65
      string json = JsonConvert.SerializeObject(versionClass, Formatting.Indented, new VersionConverter());
 
66
 
 
67
      string expectedJson = string.Format(@"{{
 
68
  ""StringProperty1"": ""StringProperty1"",
 
69
  ""Version1"": ""{0}"",
 
70
  ""Version2"": ""{1}"",
 
71
  ""StringProperty2"": ""StringProperty2""
 
72
}}", version1, version2);
 
73
 
 
74
      Assert.AreEqual(expectedJson, json);
 
75
    }
 
76
 
 
77
    [Test]
 
78
    public void SerializeVersionClass()
 
79
    {
 
80
      SerializeVersionClass("1.0.0.0", "2.0.0.0");
 
81
      SerializeVersionClass("1.2.0.0", "2.3.0.0");
 
82
      SerializeVersionClass("1.2.3.0", "2.3.4.0");
 
83
      SerializeVersionClass("1.2.3.4", "2.3.4.5");
 
84
 
 
85
      SerializeVersionClass("1.2", "2.3");
 
86
      SerializeVersionClass("1.2.3", "2.3.4");
 
87
      SerializeVersionClass("1.2.3.4", "2.3.4.5");
 
88
    }
 
89
 
 
90
    private void DeserializeVersionClass(string version1, string version2)
 
91
    {
 
92
      string json = string.Format(@"{{""StringProperty1"": ""StringProperty1"", ""Version1"": ""{0}"", ""Version2"": ""{1}"", ""StringProperty2"": ""StringProperty2""}}", version1, version2);
 
93
      Version expectedVersion1 = new Version(version1);
 
94
      Version expectedVersion2 = new Version(version2);
 
95
 
 
96
      VersionClass versionClass = JsonConvert.DeserializeObject<VersionClass>(json, new VersionConverter());
 
97
 
 
98
      Assert.AreEqual("StringProperty1", versionClass.StringProperty1);
 
99
      Assert.AreEqual(expectedVersion1, versionClass.Version1);
 
100
      Assert.AreEqual(expectedVersion2, versionClass.Version2);
 
101
      Assert.AreEqual("StringProperty2", versionClass.StringProperty2);
 
102
    }
 
103
 
 
104
    [Test]
 
105
    public void DeserializeVersionClass()
 
106
    {
 
107
      DeserializeVersionClass("1.0.0.0", "2.0.0.0");
 
108
      DeserializeVersionClass("1.2.0.0", "2.3.0.0");
 
109
      DeserializeVersionClass("1.2.3.0", "2.3.4.0");
 
110
      DeserializeVersionClass("1.2.3.4", "2.3.4.5");
 
111
 
 
112
      DeserializeVersionClass("1.2", "2.3");
 
113
      DeserializeVersionClass("1.2.3", "2.3.4");
 
114
      DeserializeVersionClass("1.2.3.4", "2.3.4.5");
 
115
    }
 
116
 
 
117
    [Test]
 
118
    public void RoundtripImplicitConverter()
 
119
    {
 
120
      var version = new Version(1, 0, 0, 0);
 
121
      string reportJSON = JsonConvert.SerializeObject(version);
 
122
 
 
123
      //Test
 
124
      Version report2 = JsonConvert.DeserializeObject<Version>(reportJSON);
 
125
      string reportJSON2 = JsonConvert.SerializeObject(report2);
 
126
 
 
127
      Assert.AreEqual(reportJSON, reportJSON2);
 
128
    }
 
129
  }
 
130
}
 
 
b'\\ No newline at end of file'