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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/TestFixtureBase.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
using System.Globalization;
 
29
using System.IO;
 
30
#if NET20
 
31
using Newtonsoft.Json.Serialization;
 
32
using Newtonsoft.Json.Utilities.LinqBridge;
 
33
#endif
 
34
using System.Text;
 
35
using System.Threading;
 
36
#if !NETFX_CORE
 
37
using NUnit.Framework;
 
38
#else
 
39
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
40
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
41
using TestMethod = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
42
#endif
 
43
using Newtonsoft.Json.Utilities;
 
44
using System.Collections;
 
45
#if NET20
 
46
using Newtonsoft.Json.Utilities.LinqBridge;
 
47
#else
 
48
using System.Linq;
 
49
#endif
 
50
 
 
51
namespace Newtonsoft.Json.Tests
 
52
{
 
53
  [TestFixture]
 
54
  public abstract class TestFixtureBase
 
55
  {
 
56
    protected string GetOffset(DateTime d, DateFormatHandling dateFormatHandling)
 
57
    {
 
58
      StringWriter sw = new StringWriter();
 
59
      JsonConvert.WriteDateTimeOffset(sw, DateTime.SpecifyKind(d, DateTimeKind.Local).GetUtcOffset(), dateFormatHandling);
 
60
      sw.Flush();
 
61
 
 
62
      return sw.ToString();
 
63
    }
 
64
 
 
65
#if !NETFX_CORE
 
66
    [SetUp]
 
67
    protected void TestSetup()
 
68
    {
 
69
      //CultureInfo turkey = CultureInfo.CreateSpecificCulture("tr");
 
70
      //Thread.CurrentThread.CurrentCulture = turkey;
 
71
      //Thread.CurrentThread.CurrentUICulture = turkey;
 
72
    }
 
73
 
 
74
    protected void WriteEscapedJson(string json)
 
75
    {
 
76
      Console.WriteLine(EscapeJson(json));
 
77
    }
 
78
 
 
79
    protected string EscapeJson(string json)
 
80
    {
 
81
      return @"@""" + json.Replace(@"""", @"""""") + @"""";
 
82
    }
 
83
#endif
 
84
  }
 
85
 
 
86
#if NETFX_CORE
 
87
  public static class Console
 
88
  {
 
89
    public static void WriteLine(params object[] args)
 
90
    {
 
91
    }
 
92
  }
 
93
#endif
 
94
 
 
95
  public static class CustomAssert
 
96
  {
 
97
    public static void IsInstanceOfType(Type t, object instance)
 
98
    {
 
99
#if !NETFX_CORE
 
100
      Assert.IsInstanceOfType(t, instance);
 
101
#else
 
102
      if (!instance.GetType().IsAssignableFrom(t))
 
103
        throw new Exception("Blah");
 
104
#endif
 
105
    }
 
106
 
 
107
    public static void Contains(IList collection, object value)
 
108
    {
 
109
#if !NETFX_CORE
 
110
      Assert.Contains(value, collection);
 
111
#else
 
112
      if (!collection.Cast<object>().Any(i => i.Equals(value)))
 
113
        throw new Exception("Value not found in collection.");
 
114
#endif
 
115
    }
 
116
  }
 
117
 
 
118
  public static class ExceptionAssert
 
119
  {
 
120
    public static void Throws<TException>(string message, Action action)
 
121
        where TException : Exception
 
122
    {
 
123
      try
 
124
      {
 
125
        action();
 
126
 
 
127
        Assert.Fail("Exception of type {0} expected; got none exception", typeof(TException).Name);
 
128
      }
 
129
      catch (TException ex)
 
130
      {
 
131
        if (message != null)
 
132
          Assert.AreEqual(message, ex.Message, "Unexpected exception message." + Environment.NewLine + "Expected: " + message + Environment.NewLine + "Got: " + ex.Message);
 
133
      }
 
134
      catch (Exception ex)
 
135
      {
 
136
        throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex);
 
137
      }
 
138
    }
 
139
  }
 
140
}