Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Utilities\DynamicReflectionDelegateFactoryTests.cs

Symbol Coverage: 86.67% (26 of 30)

Branch Coverage: 33.33% (2 of 6)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 30


L V Source
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 !PocketPC && !SILVERLIGHT
27
using System;
28
using System.Collections.Generic;
29
using System.Linq;
30
using System.Reflection;
31
using System.Text;
32
using NUnit.Framework;
33
using Newtonsoft.Json.Utilities;
34
using Newtonsoft.Json.Tests.TestObjects;
35
using Newtonsoft.Json.Tests.Serialization;
36

  
37
namespace Newtonsoft.Json.Tests.Utilities
38
{
39
  public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
40
  {
41
    [Test]
42
    [ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.")]
43
    public void CreateGetWithBadObjectTarget()
44
    {
45
 1
      Person p = new Person();
46
 1
      p.Name = "Hi";
47

  
48
 1
      Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
49

  
50
 1
      setter(p);
51
 0
    }
52

  
53
    [Test]
54
    [ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.")]
55
    public void CreateSetWithBadObjectTarget()
56
    {
57
 1
      Person p = new Person();
58
 1
      Movie m = new Movie();
59

  
60
 1
      Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
61

  
62
 1
      setter(m, "Hi");
63

  
64
 1
      Assert.AreEqual(m.Name, "Hi");
65

  
66
 1
      setter(p, "Hi");
67
 0
    }
68

  
69
    [Test]
70
    [ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Specified cast is not valid.")]
71
    public void CreateSetWithBadTarget()
72
    {
73
 1
      object structTest = new StructTest();
74

  
75
 1
      Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
76

  
77
 1
      setter(structTest, "Hi");
78

  
79
 1
      Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
80

  
81
 1
      setter(new TimeSpan(), "Hi");
82
 0
    }
83

  
84
    [Test]
85
    [ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Unable to cast object of type 'System.Version' to type 'System.String'.")]
86
    public void CreateSetWithBadObjectValue()
87
    {
88
 1
      Movie m = new Movie();
89

  
90
 1
      Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
91

  
92
 1
      setter(m, new Version());
93
 0
    }
94

  
95
    [Test]
96
    public void CreateStaticMethodCall()
97
    {
98
 1
      MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
99

  
100
 1
      Assert.IsNotNull(castMethodInfo);
101

  
102
 1
      MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
103

  
104
 1
      object result = call(null, "First!");
105
 1
      Assert.IsNotNull(result);
106

  
107
 1
      JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
108
 1
      Assert.AreEqual("First!", key.Value);
109
 1
    }
110
  }
111
}
112
#endif