Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Linq\JConstructorTests.cs

Symbol Coverage: 96.77% (30 of 31)

Branch Coverage: 88.89% (8 of 9)

Cyclomatic Complexity Avg: 1.33 Max:3

Code Lines: 29


L V Source
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using Newtonsoft.Json.Linq;
6
using NUnit.Framework;
7
using System.IO;
8

  
9
namespace Newtonsoft.Json.Tests.Linq
10
{
11
  public class JConstructorTests : TestFixtureBase
12
  {
13
    [Test]
14
    public void Load()
15
    {
16
 1
      JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
17
 1
      reader.Read();
18

  
19
 1
      JConstructor constructor = JConstructor.Load(reader);
20
 1
      Assert.AreEqual("Date", constructor.Name);
21
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
22
 1
    }
23

  
24
    [Test]
25
    public void CreateWithMultiValue()
26
    {
27
 1
      JConstructor constructor = new JConstructor("Test", new List<int> { 1, 2, 3 });
28
 1
      Assert.AreEqual("Test", constructor.Name);
29
 1
      Assert.AreEqual(3, constructor.Children().Count());
30
 1
      Assert.AreEqual(1, (int)constructor.Children().ElementAt(0));
31
 1
      Assert.AreEqual(2, (int)constructor.Children().ElementAt(1));
32
 1
      Assert.AreEqual(3, (int)constructor.Children().ElementAt(2));
33
 1
    }
34

  
35
    [Test]
36
    public void Iterate()
37
    {
38
 1
      JConstructor c = new JConstructor("MrConstructor", 1, 2, 3, 4, 5);
39

  
40
 1
      int i = 1;
41
 1
      foreach (JToken token in c)
42
      {
43
 5
        Assert.AreEqual(i, (int)token);
44
 5
        i++;
45
      }
46
 1
    }
47

  
48
    [Test]
49
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Set JConstructor values with invalid key value: ""badvalue"". Argument position index expected.")]
50
    public void SetValueWithInvalidIndex()
51
    {
52
 1
      JConstructor c = new JConstructor();
53
 1
      c["badvalue"] = new JValue(3);
54
 0
    }
55

  
56
    [Test]
57
    public void SetValue()
58
    {
59
 1
      object key = 0;
60

  
61
 1
      JConstructor c = new JConstructor();
62
 1
      c.Name = "con";
63
 1
      c.Add(null);
64
 1
      c[key] = new JValue(3);
65

  
66
 1
      Assert.AreEqual(3, (int)c[key]);
67
 1
    }
68
  }
69
}