Skip to content

Commit b732303

Browse files
committed
Add tests for JsonArray
Fixes the exception thrown when null is passed to the constructor.
1 parent 0d86986 commit b732303

3 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LightJson
5+
{
6+
using System;
7+
using global::LightJson;
8+
using Xunit;
9+
using IEnumerable = System.Collections.IEnumerable;
10+
11+
public class JsonArrayTests
12+
{
13+
[Fact]
14+
public void TestConstructor()
15+
{
16+
var obj = new JsonArray();
17+
Assert.Equal(0, obj.Count);
18+
19+
var obj1 = new JsonArray(1, "test1");
20+
Assert.Equal(2, obj1.Count);
21+
Assert.Equal(1, obj1[0].AsInteger);
22+
Assert.Equal("test1", obj1[1].AsString);
23+
24+
var obj2 = new JsonArray { 1, "test2" };
25+
Assert.Equal(2, obj2.Count);
26+
Assert.Equal(1, obj2[0].AsInteger);
27+
Assert.Equal("test2", obj2[1].AsString);
28+
29+
Assert.Throws<ArgumentNullException>("values", () => new JsonArray(default(JsonValue[])));
30+
}
31+
32+
[Fact]
33+
public void TestIndexer()
34+
{
35+
var obj = new JsonArray(1);
36+
Assert.Equal(1, obj.Count);
37+
Assert.Equal(1, obj[0].AsInteger);
38+
Assert.Equal(JsonValue.Null, obj[1]);
39+
Assert.Equal(JsonValue.Null, obj[-1]);
40+
41+
obj[0] = 2;
42+
Assert.Equal(2, obj[0].AsInteger);
43+
44+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj[-1] = 0);
45+
Assert.ThrowsAny<ArgumentException>(() => obj[1] = 0);
46+
}
47+
48+
[Fact]
49+
public void TestInsert()
50+
{
51+
var obj = new JsonArray(1);
52+
Assert.Equal(1, obj.Count);
53+
Assert.Equal(1, obj[0].AsInteger);
54+
55+
// Insert at end
56+
Assert.Same(obj, obj.Insert(obj.Count, 2));
57+
Assert.Equal(2, obj.Count);
58+
Assert.Equal(1, obj[0].AsInteger);
59+
Assert.Equal(2, obj[1].AsInteger);
60+
61+
// Insert at beginning
62+
Assert.Same(obj, obj.Insert(0, 0));
63+
Assert.Equal(3, obj.Count);
64+
Assert.Equal(0, obj[0].AsInteger);
65+
Assert.Equal(1, obj[1].AsInteger);
66+
Assert.Equal(2, obj[2].AsInteger);
67+
68+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj.Insert(-1, 0));
69+
Assert.ThrowsAny<ArgumentException>(() => obj.Insert(obj.Count + 1, 0));
70+
}
71+
72+
[Fact]
73+
public void TestRemove()
74+
{
75+
var obj = new JsonArray(0, 1, 2);
76+
Assert.Equal(3, obj.Count);
77+
78+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj.Remove(-1));
79+
Assert.ThrowsAny<ArgumentException>(() => obj.Remove(obj.Count));
80+
81+
Assert.Same(obj, obj.Remove(1));
82+
Assert.Equal(2, obj.Count);
83+
Assert.Equal(0, obj[0].AsInteger);
84+
Assert.Equal(2, obj[1].AsInteger);
85+
}
86+
87+
[Fact]
88+
public void TestClear()
89+
{
90+
var obj = new JsonArray(0, 1, 2);
91+
Assert.Equal(3, obj.Count);
92+
93+
Assert.Same(obj, obj.Clear());
94+
Assert.Equal(0, obj.Count);
95+
96+
Assert.Same(obj, obj.Clear());
97+
Assert.Equal(0, obj.Count);
98+
}
99+
100+
[Fact]
101+
public void TestContains()
102+
{
103+
var obj = new JsonArray("a", "b", "c");
104+
Assert.True(obj.Contains("b"));
105+
obj.Remove(1);
106+
Assert.False(obj.Contains("b"));
107+
108+
Assert.False(obj.Contains(JsonValue.Null));
109+
}
110+
111+
[Fact]
112+
public void TestIndexOf()
113+
{
114+
var obj = new JsonArray("a", "b", "c");
115+
Assert.Equal(1, obj.IndexOf("b"));
116+
Assert.Equal(2, obj.IndexOf("c"));
117+
obj.Remove(1);
118+
Assert.Equal(-1, obj.IndexOf("b"));
119+
Assert.Equal(1, obj.IndexOf("c"));
120+
121+
Assert.Equal(-1, obj.IndexOf(JsonValue.Null));
122+
}
123+
124+
[Fact]
125+
public void TestEnumerators()
126+
{
127+
var obj = new JsonArray("a", "b", "c");
128+
129+
using (var genericEnumerator = obj.GetEnumerator())
130+
{
131+
var legacyEnumerator = ((IEnumerable)obj).GetEnumerator();
132+
for (int i = 0; i < obj.Count; i++)
133+
{
134+
Assert.True(genericEnumerator.MoveNext());
135+
Assert.True(legacyEnumerator.MoveNext());
136+
Assert.Equal(obj[i], genericEnumerator.Current);
137+
Assert.Equal(obj[i], legacyEnumerator.Current);
138+
Assert.Equal(genericEnumerator.Current, legacyEnumerator.Current);
139+
}
140+
}
141+
}
142+
}
143+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<Compile Include="LayoutRules\SA1518UnitTests.cs" />
236236
<Compile Include="LayoutRules\SA1519UnitTests.cs" />
237237
<Compile Include="LayoutRules\SA1520UnitTests.cs" />
238+
<Compile Include="LightJson\JsonArrayTests.cs" />
238239
<Compile Include="LightJson\JsonObjectTests.cs" />
239240
<Compile Include="LightJson\JsonReaderTests.cs" />
240241
<Compile Include="LightJson\JsonValueTests.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonArray.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
namespace LightJson
55
{
6+
using System;
67
using System.Collections.Generic;
78
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
810

911
/// <summary>
1012
/// Represents an ordered collection of JsonValues.
@@ -30,6 +32,11 @@ public JsonArray()
3032
public JsonArray(params JsonValue[] values)
3133
: this()
3234
{
35+
if (values == null)
36+
{
37+
throw new ArgumentNullException(nameof(values));
38+
}
39+
3340
foreach (var value in values)
3441
{
3542
this.items.Add(value);
@@ -157,6 +164,7 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
157164
return this.GetEnumerator();
158165
}
159166

167+
[ExcludeFromCodeCoverage]
160168
private class JsonArrayDebugView
161169
{
162170
private JsonArray jsonArray;

0 commit comments

Comments
 (0)