Skip to content

Commit 77e88cc

Browse files
authored
Merge pull request #45 from RusticiSoftware/UnableToCast
Support for single member properties of ContextActivities
2 parents 48ffd42 + e73e9bf commit 77e88cc

3 files changed

Lines changed: 122 additions & 8 deletions

File tree

TinCan/ContextActivities.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,60 @@ public ContextActivities(JObject jobj)
3535
if (jobj["parent"] != null)
3636
{
3737
parent = new List<Activity>();
38-
foreach (JObject jactivity in jobj["parent"]) {
39-
parent.Add((Activity)jactivity);
38+
if (jobj["parent"].Type == JTokenType.Array)
39+
{
40+
foreach (JObject jactivity in jobj["parent"]) {
41+
parent.Add((Activity)jactivity);
42+
}
43+
}
44+
else
45+
{
46+
parent.Add((Activity)jobj["parent"]);
4047
}
4148
}
4249
if (jobj["grouping"] != null)
4350
{
4451
grouping = new List<Activity>();
45-
foreach (JObject jactivity in jobj["grouping"]) {
46-
grouping.Add((Activity)jactivity);
52+
if (jobj["grouping"].Type == JTokenType.Array)
53+
{
54+
foreach (JObject jactivity in jobj["grouping"])
55+
{
56+
grouping.Add((Activity)jactivity);
57+
}
58+
}
59+
else
60+
{
61+
grouping.Add((Activity)jobj["grouping"]);
4762
}
4863
}
4964
if (jobj["category"] != null)
5065
{
5166
category = new List<Activity>();
52-
foreach (JObject jactivity in jobj["category"]) {
53-
category.Add((Activity)jactivity);
67+
if (jobj["category"].Type == JTokenType.Array)
68+
{
69+
foreach (JObject jactivity in jobj["category"])
70+
{
71+
category.Add((Activity)jactivity);
72+
}
73+
}
74+
else
75+
{
76+
category.Add((Activity)jobj["category"]);
5477
}
5578
}
5679
if (jobj["other"] != null)
5780
{
5881
other = new List<Activity>();
59-
foreach (JObject jactivity in jobj["other"]) {
60-
other.Add((Activity)jactivity);
82+
if (jobj["other"].Type == JTokenType.Array)
83+
{
84+
foreach (JObject jactivity in jobj["other"])
85+
{
86+
other.Add((Activity)jactivity);
87+
}
88+
}
89+
else
90+
{
91+
other.Add((Activity)jobj["other"]);
6192
}
6293
}
6394
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2018 Rustici Software
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
namespace TinCanTests
17+
{
18+
using System.Collections.Generic;
19+
using NUnit.Framework;
20+
using TinCan;
21+
using TinCan.Json;
22+
23+
[TestFixture]
24+
class ContextActivitiesTest
25+
{
26+
private Activity sampleActivity1 = new Activity
27+
{
28+
id = "http://0.bar/"
29+
};
30+
31+
private Activity sampleActivity2 = new Activity
32+
{
33+
id = "http://1.bar/"
34+
};
35+
36+
[Test]
37+
public void ConstructorWithObject()
38+
{
39+
var json = "{" +
40+
"\"parent\": " + sampleActivity1.ToJSON() + "," +
41+
"\"grouping\": " + sampleActivity1.ToJSON() + "," +
42+
"\"category\": " + sampleActivity1.ToJSON() + "," +
43+
"\"other\": " + sampleActivity1.ToJSON() +
44+
"}";
45+
46+
ContextActivities contextActivities = new ContextActivities(new StringOfJSON(json));
47+
48+
ValidateActivityList(contextActivities.parent, 1);
49+
ValidateActivityList(contextActivities.grouping, 1);
50+
ValidateActivityList(contextActivities.category, 1);
51+
ValidateActivityList(contextActivities.other, 1);
52+
}
53+
54+
[Test]
55+
public void ConstructorWithArray()
56+
{
57+
var json = "{" +
58+
"\"parent\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," +
59+
"\"grouping\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," +
60+
"\"category\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," +
61+
"\"other\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]" +
62+
"}";
63+
64+
ContextActivities contextActivities = new ContextActivities(new StringOfJSON(json));
65+
66+
ValidateActivityList(contextActivities.parent, 2);
67+
ValidateActivityList(contextActivities.grouping, 2);
68+
ValidateActivityList(contextActivities.category, 2);
69+
ValidateActivityList(contextActivities.other, 2);
70+
}
71+
72+
private void ValidateActivityList(List<Activity> list, int expectedLength)
73+
{
74+
Assert.IsTrue(list.Count == expectedLength);
75+
76+
for (int i = 0; i < list.Count; i++)
77+
{
78+
Assert.IsTrue(list[i].id == "http://" + i + ".bar/");
79+
}
80+
}
81+
}
82+
}

TinCanTests/TinCanTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<ItemGroup>
8989
<Compile Include="ActivityTest.cs" />
9090
<Compile Include="AgentTest.cs" />
91+
<Compile Include="ContextActivitiesTest.cs" />
9192
<Compile Include="ContextTest.cs" />
9293
<Compile Include="ResultTest.cs" />
9394
<Compile Include="Support.cs" />

0 commit comments

Comments
 (0)