From 7371e3efce267a4926b9e2171819850e66dfb38f Mon Sep 17 00:00:00 2001 From: Rasmus E Date: Thu, 19 Jul 2018 22:25:54 +0200 Subject: [PATCH 1/3] ContextActivities was unable to parse when items was object and not array. --- TinCan/ContextActivities.cs | 47 ++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/TinCan/ContextActivities.cs b/TinCan/ContextActivities.cs index e329cd7..d94ce03 100644 --- a/TinCan/ContextActivities.cs +++ b/TinCan/ContextActivities.cs @@ -35,29 +35,60 @@ public ContextActivities(JObject jobj) if (jobj["parent"] != null) { parent = new List(); - foreach (JObject jactivity in jobj["parent"]) { - parent.Add((Activity)jactivity); + if(jobj["parent"].Type == JTokenType.Array) + { + foreach (JObject jactivity in jobj["parent"]) { + parent.Add((Activity)jactivity); + } + } + else + { + parent.Add((Activity)jobj["parent"]); } } if (jobj["grouping"] != null) { grouping = new List(); - foreach (JObject jactivity in jobj["grouping"]) { - grouping.Add((Activity)jactivity); + if (jobj["grouping"].Type == JTokenType.Array) + { + foreach (JObject jactivity in jobj["grouping"]) + { + grouping.Add((Activity)jactivity); + } + } + else + { + grouping.Add((Activity)jobj["grouping"]); } } if (jobj["category"] != null) { category = new List(); - foreach (JObject jactivity in jobj["category"]) { - category.Add((Activity)jactivity); + if (jobj["category"].Type == JTokenType.Array) + { + foreach (JObject jactivity in jobj["category"]) + { + category.Add((Activity)jactivity); + } + } + else + { + category.Add((Activity)jobj["category"]); } } if (jobj["other"] != null) { other = new List(); - foreach (JObject jactivity in jobj["other"]) { - other.Add((Activity)jactivity); + if (jobj["other"].Type == JTokenType.Array) + { + foreach (JObject jactivity in jobj["other"]) + { + other.Add((Activity)jactivity); + } + } + else + { + other.Add((Activity)jobj["other"]); } } } From 5730a813e4014bf7a23111f886c0830757574f15 Mon Sep 17 00:00:00 2001 From: reedmclean Date: Mon, 3 Dec 2018 14:12:54 -0600 Subject: [PATCH 2/3] Added tests to cover object/array --- TinCanTests/ContextActivitiesTest.cs | 82 ++++++++++++++++++++++++++++ TinCanTests/TinCanTests.csproj | 1 + 2 files changed, 83 insertions(+) create mode 100644 TinCanTests/ContextActivitiesTest.cs diff --git a/TinCanTests/ContextActivitiesTest.cs b/TinCanTests/ContextActivitiesTest.cs new file mode 100644 index 0000000..b069313 --- /dev/null +++ b/TinCanTests/ContextActivitiesTest.cs @@ -0,0 +1,82 @@ +/* + Copyright 2018 Rustici Software + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +namespace TinCanTests +{ + using System.Collections.Generic; + using NUnit.Framework; + using TinCan; + using TinCan.Json; + + [TestFixture] + class ContextActivitiesTest + { + private Activity sampleActivity1 = new Activity + { + id = "http://0.bar/" + }; + + private Activity sampleActivity2 = new Activity + { + id = "http://1.bar/" + }; + + [Test] + public void ConstructorWithObject() + { + var json = "{" + + "\"parent\": " + sampleActivity1.ToJSON() + "," + + "\"grouping\": " + sampleActivity1.ToJSON() + "," + + "\"category\": " + sampleActivity1.ToJSON() + "," + + "\"other\": " + sampleActivity1.ToJSON() + + "}"; + + ContextActivities contextActivities = new ContextActivities(new StringOfJSON(json)); + + ValidateActivityList(contextActivities.parent, 1); + ValidateActivityList(contextActivities.grouping, 1); + ValidateActivityList(contextActivities.category, 1); + ValidateActivityList(contextActivities.other, 1); + } + + [Test] + public void ConstructorWithArray() + { + var json = "{" + + "\"parent\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," + + "\"grouping\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," + + "\"category\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]," + + "\"other\": [" + sampleActivity1.ToJSON() + ", " + sampleActivity2.ToJSON() + "]" + + "}"; + + ContextActivities contextActivities = new ContextActivities(new StringOfJSON(json)); + + ValidateActivityList(contextActivities.parent, 2); + ValidateActivityList(contextActivities.grouping, 2); + ValidateActivityList(contextActivities.category, 2); + ValidateActivityList(contextActivities.other, 2); + } + + private void ValidateActivityList(List list, int expectedLength) + { + Assert.IsTrue(list.Count == expectedLength); + + for (int i = 0; i < list.Count; i++) + { + Assert.IsTrue(list[i].id == "http://" + i + ".bar/"); + } + } + } +} diff --git a/TinCanTests/TinCanTests.csproj b/TinCanTests/TinCanTests.csproj index d43e39e..7ebab24 100644 --- a/TinCanTests/TinCanTests.csproj +++ b/TinCanTests/TinCanTests.csproj @@ -88,6 +88,7 @@ + From e73e9bf448e60c24d2668b4b25d93d42ca901890 Mon Sep 17 00:00:00 2001 From: reedmclean Date: Mon, 17 Dec 2018 11:59:16 -0600 Subject: [PATCH 3/3] Style fix --- TinCan/ContextActivities.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TinCan/ContextActivities.cs b/TinCan/ContextActivities.cs index d94ce03..a024c9f 100644 --- a/TinCan/ContextActivities.cs +++ b/TinCan/ContextActivities.cs @@ -35,7 +35,7 @@ public ContextActivities(JObject jobj) if (jobj["parent"] != null) { parent = new List(); - if(jobj["parent"].Type == JTokenType.Array) + if (jobj["parent"].Type == JTokenType.Array) { foreach (JObject jactivity in jobj["parent"]) { parent.Add((Activity)jactivity);