Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Linq\JTokenWriterTest.cs
Symbol Coverage: 100.00% (75 of 75)
Branch Coverage: 100.00% (8 of 8)
Cyclomatic Complexity Avg: 1.33 Max:2
Code Lines: 82
Symbol Coverage Trend
View:
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 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.Text; |
|
29 |
using NUnit.Framework; |
|
30 |
using Newtonsoft.Json; |
|
31 |
using System.IO; |
|
32 |
using Newtonsoft.Json.Linq; |
|
33 |
using System.Linq; |
|
34 |
||
35 |
namespace Newtonsoft.Json.Tests.Linq |
|
36 |
{ |
|
37 |
public class JTokenWriterTest : TestFixtureBase |
|
38 |
{ |
|
39 |
[Test] |
|
40 |
public void ValueFormatting() |
|
41 |
{ |
|
42 |
1 |
byte[] data = Encoding.UTF8.GetBytes("Hello world.");
|
43 |
||
44 |
JToken root; |
|
45 |
1 |
using (JTokenWriter jsonWriter = new JTokenWriter())
|
46 |
{ |
|
47 |
1 |
jsonWriter.WriteStartArray();
|
48 |
1 |
jsonWriter.WriteValue('@');
|
49 |
1 |
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
|
50 |
1 |
jsonWriter.WriteValue(true);
|
51 |
1 |
jsonWriter.WriteValue(10);
|
52 |
1 |
jsonWriter.WriteValue(10.99);
|
53 |
1 |
jsonWriter.WriteValue(0.99);
|
54 |
1 |
jsonWriter.WriteValue(0.000000000000000001d);
|
55 |
1 |
jsonWriter.WriteValue(0.000000000000000001m);
|
56 |
1 |
jsonWriter.WriteValue((string)null);
|
57 |
1 |
jsonWriter.WriteValue("This is a string.");
|
58 |
1 |
jsonWriter.WriteNull();
|
59 |
1 |
jsonWriter.WriteUndefined();
|
60 |
1 |
jsonWriter.WriteValue(data);
|
61 |
1 |
jsonWriter.WriteEndArray();
|
62 |
||
63 |
1 |
root = jsonWriter.Token;
|
64 |
} |
|
65 |
||
66 |
1 |
Assert.IsInstanceOfType(typeof(JArray), root);
|
67 |
1 |
Assert.AreEqual(13, root.Children().Count());
|
68 |
1 |
Assert.AreEqual("@", (string)root[0]);
|
69 |
1 |
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", (string)root[1]);
|
70 |
1 |
Assert.AreEqual(true, (bool)root[2]);
|
71 |
1 |
Assert.AreEqual(10, (int)root[3]);
|
72 |
1 |
Assert.AreEqual(10.99, (double)root[4]);
|
73 |
1 |
Assert.AreEqual(0.99, (double)root[5]);
|
74 |
1 |
Assert.AreEqual(0.000000000000000001d, (double)root[6]);
|
75 |
1 |
Assert.AreEqual(0.000000000000000001m, (decimal)root[7]);
|
76 |
1 |
Assert.AreEqual(string.Empty, (string)root[8]);
|
77 |
1 |
Assert.AreEqual("This is a string.", (string)root[9]);
|
78 |
1 |
Assert.AreEqual(null, ((JValue)root[10]).Value);
|
79 |
1 |
Assert.AreEqual(null, ((JValue)root[11]).Value);
|
80 |
1 |
Assert.AreEqual(data, (byte[])root[12]);
|
81 |
1 |
}
|
82 |
||
83 |
[Test] |
|
84 |
public void State() |
|
85 |
{ |
|
86 |
1 |
using (JsonWriter jsonWriter = new JTokenWriter())
|
87 |
{ |
|
88 |
1 |
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
89 |
||
90 |
1 |
jsonWriter.WriteStartObject();
|
91 |
1 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
92 |
||
93 |
1 |
jsonWriter.WritePropertyName("CPU");
|
94 |
1 |
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
95 |
||
96 |
1 |
jsonWriter.WriteValue("Intel");
|
97 |
1 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
98 |
||
99 |
1 |
jsonWriter.WritePropertyName("Drives");
|
100 |
1 |
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
101 |
||
102 |
1 |
jsonWriter.WriteStartArray();
|
103 |
1 |
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
104 |
||
105 |
1 |
jsonWriter.WriteValue("DVD read/writer");
|
106 |
1 |
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
107 |
||
108 |
1 |
jsonWriter.WriteValue(new byte[0]);
|
109 |
1 |
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
110 |
||
111 |
1 |
jsonWriter.WriteEnd();
|
112 |
1 |
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
113 |
||
114 |
1 |
jsonWriter.WriteEndObject();
|
115 |
1 |
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
116 |
} |
|
117 |
1 |
}
|
118 |
||
119 |
[Test] |
|
120 |
public void WriteComment() |
|
121 |
{ |
|
122 |
1 |
JTokenWriter writer = new JTokenWriter();
|
123 |
||
124 |
1 |
writer.WriteStartArray();
|
125 |
1 |
writer.WriteComment("fail");
|
126 |
1 |
writer.WriteEndArray();
|
127 |
||
128 |
1 |
Assert.AreEqual(@"[
|
129 |
1 |
/*fail*/]", writer.Token.ToString());
|
130 |
1 |
}
|
131 |
||
132 |
[Test] |
|
133 |
public void WriteRaw() |
|
134 |
{ |
|
135 |
1 |
JTokenWriter writer = new JTokenWriter();
|
136 |
||
137 |
1 |
writer.WriteStartArray();
|
138 |
1 |
writer.WriteRaw("fail");
|
139 |
1 |
writer.WriteRaw("fail");
|
140 |
1 |
writer.WriteEndArray();
|
141 |
||
142 |
// this is a bug. write raw shouldn't be autocompleting like this |
|
143 |
// hard to fix without introducing Raw and RawValue token types |
|
144 |
// meh |
|
145 |
1 |
Assert.AreEqual(@"[
|
146 |
1 |
fail,
|
147 |
1 |
fail
|
148 |
1 |
]", writer.Token.ToString());
|
149 |
1 |
}
|
150 |
||
151 |
[Test] |
|
152 |
public void WriteRawValue() |
|
153 |
{ |
|
154 |
1 |
JTokenWriter writer = new JTokenWriter();
|
155 |
||
156 |
1 |
writer.WriteStartArray();
|
157 |
1 |
writer.WriteRawValue("fail");
|
158 |
1 |
writer.WriteRawValue("fail");
|
159 |
1 |
writer.WriteEndArray();
|
160 |
||
161 |
1 |
Assert.AreEqual(@"[
|
162 |
1 |
fail,
|
163 |
1 |
fail
|
164 |
1 |
]", writer.Token.ToString());
|
165 |
1 |
}
|
166 |
} |
|
167 |
} |