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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.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
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
 
27
using System;
 
28
using System.Reflection;
 
29
#if !NETFX_CORE
 
30
using NUnit.Framework;
 
31
#else
 
32
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
33
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
34
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
35
#endif
 
36
using Newtonsoft.Json.Serialization;
 
37
using Newtonsoft.Json.Utilities;
 
38
using Newtonsoft.Json.Tests.TestObjects;
 
39
using Newtonsoft.Json.Tests.Serialization;
 
40
 
 
41
namespace Newtonsoft.Json.Tests.Utilities
 
42
{
 
43
  [TestFixture]
 
44
  public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
 
45
  {
 
46
    [Test]
 
47
    public void CreateGetWithBadObjectTarget()
 
48
    {
 
49
      ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
 
50
      () =>
 
51
      {
 
52
        Person p = new Person();
 
53
        p.Name = "Hi";
 
54
 
 
55
        Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
 
56
 
 
57
        setter(p);
 
58
      });
 
59
    }
 
60
 
 
61
    [Test]
 
62
    public void CreateSetWithBadObjectTarget()
 
63
    {
 
64
      ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
 
65
      () =>
 
66
      {
 
67
        Person p = new Person();
 
68
        Movie m = new Movie();
 
69
 
 
70
        Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
 
71
 
 
72
        setter(m, "Hi");
 
73
 
 
74
        Assert.AreEqual(m.Name, "Hi");
 
75
 
 
76
        setter(p, "Hi");
 
77
      });
 
78
    }
 
79
 
 
80
    [Test]
 
81
    public void CreateSetWithBadTarget()
 
82
    {
 
83
      ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.",
 
84
      () =>
 
85
      {
 
86
        object structTest = new StructTest();
 
87
 
 
88
        Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
 
89
 
 
90
        setter(structTest, "Hi");
 
91
 
 
92
        Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
 
93
 
 
94
        setter(new TimeSpan(), "Hi");
 
95
      });
 
96
    }
 
97
 
 
98
    [Test]
 
99
    public void CreateSetWithBadObjectValue()
 
100
    {
 
101
      ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
 
102
      () =>
 
103
      {
 
104
        Movie m = new Movie();
 
105
 
 
106
        Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
 
107
 
 
108
        setter(m, new Version("1.1.1.1"));
 
109
      });
 
110
    }
 
111
 
 
112
    [Test]
 
113
    public void CreateStaticMethodCall()
 
114
    {
 
115
      MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
 
116
 
 
117
      Assert.IsNotNull(castMethodInfo);
 
118
 
 
119
      MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
 
120
 
 
121
      object result = call(null, "First!");
 
122
      Assert.IsNotNull(result);
 
123
 
 
124
      JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
 
125
      Assert.AreEqual("First!", key.Value);
 
126
    }
 
127
 
 
128
    //[Test]
 
129
    //public void sdfsdf()
 
130
    //{
 
131
    //  string name = "MyAssembly";
 
132
    //  string filename = name + ".dll";
 
133
 
 
134
    //  AssemblyName s = new AssemblyName(name);
 
135
 
 
136
    //  AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
 
137
    //  ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
 
138
    //  TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
 
139
    //  MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
 
140
 
 
141
    //  DynamicReflectionDelegateFactory.GenerateCreateSetFieldIL(typeof(ClassWithGuid).GetField("GuidField"), methodBuilder.GetILGenerator());
 
142
    //  typeBuilder.CreateType();
 
143
    //  assemblyBuilder.Save(filename);
 
144
    //}
 
145
 
 
146
    //[Test]
 
147
    //public void sdfsdf1()
 
148
    //{
 
149
    //  string name = "MyAssembly1";
 
150
    //  string filename = name + ".dll";
 
151
 
 
152
    //  AssemblyName s = new AssemblyName(name);
 
153
 
 
154
    //  AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
 
155
    //  ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
 
156
    //  TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
 
157
    //  MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
 
158
 
 
159
    //  DynamicReflectionDelegateFactory.GenerateCreateSetPropertyIL(typeof(Car).GetProperty("Model"), methodBuilder.GetILGenerator());
 
160
    //  typeBuilder.CreateType();
 
161
    //  assemblyBuilder.Save(filename);
 
162
    //}
 
163
  }
 
164
}
 
165
#endif
 
 
b'\\ No newline at end of file'