Skip to content

Commit b865b39

Browse files
committed
Adjust SubStatement object handling in Statement/StatementBase
* Add unit test for detecting non-null SubStatement as Statement object
1 parent 55173fa commit b865b39

4 files changed

Lines changed: 121 additions & 12 deletions

File tree

TinCan/Statement.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public Statement(JObject jobj) : base(jobj) {
5050
{
5151
version = (TCAPIVersion)jobj.Value<String>("version");
5252
}
53+
54+
//
55+
// handle SubStatement as target which isn't provided by StatementBase
56+
// because SubStatements are not allowed to nest
57+
//
58+
if (jobj["object"] != null && (String)jobj["object"]["objectType"] == SubStatement.OBJECT_TYPE)
59+
{
60+
target = (SubStatement)jobj.Value<JObject>("object");
61+
}
5362
}
5463

5564
public override JObject ToJObject(TCAPIVersion version)

TinCan/StatementBase.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@ public StatementBase(JObject jobj)
7070
{
7171
target = (StatementRef)jobj.Value<JObject>("object");
7272
}
73-
else if ((String)jobj["object"]["objectType"] == SubStatement.OBJECT_TYPE)
74-
{
75-
if ((string)jobj["objectType"] != "SubStatement")
76-
{
77-
target = (SubStatement)jobj.Value<JObject>("object");
78-
}
79-
else
80-
{
81-
// nested statement not allowed exception here?
82-
target = (Activity)jobj.Value<JObject>("object");
83-
}
84-
}
8573
}
8674
else
8775
{

TinCanTests/StatementTest.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2014 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;
19+
using System.Collections.Generic;
20+
using NUnit.Framework;
21+
using Newtonsoft.Json.Linq;
22+
using TinCan;
23+
using TinCan.Json;
24+
25+
[TestFixture]
26+
class StatementTest
27+
{
28+
Agent agent;
29+
Verb verb;
30+
Activity activity;
31+
Activity parent;
32+
Context context;
33+
Result result;
34+
Score score;
35+
StatementRef statementRef;
36+
SubStatement subStatement;
37+
38+
[SetUp]
39+
public void Init()
40+
{
41+
Console.WriteLine("Running " + TestContext.CurrentContext.Test.FullName);
42+
43+
agent = new Agent();
44+
agent.mbox = "mailto:tincancsharp@tincanapi.com";
45+
46+
verb = new Verb("http://adlnet.gov/expapi/verbs/experienced");
47+
48+
activity = new Activity();
49+
activity.id = new Uri("http://tincanapi.com/TinCanCSharp/Test/Unit/0");
50+
parent = new Activity();
51+
parent.id = new Uri("http://tincanapi.com/TinCanCSharp/Test");
52+
53+
statementRef = new StatementRef(Guid.NewGuid());
54+
55+
context = new Context();
56+
context.registration = Guid.NewGuid();
57+
context.statement = statementRef;
58+
context.contextActivities = new ContextActivities();
59+
context.contextActivities.parent = new List<Activity>();
60+
context.contextActivities.parent.Add(parent);
61+
62+
score = new Score();
63+
score.raw = 97;
64+
score.scaled = 0.97;
65+
score.max = 100;
66+
score.min = 0;
67+
68+
result = new Result();
69+
result.score = score;
70+
result.success = true;
71+
result.completion = true;
72+
result.duration = new TimeSpan(1, 2, 16, 43);
73+
74+
subStatement = new SubStatement();
75+
subStatement.actor = agent;
76+
subStatement.verb = verb;
77+
subStatement.target = parent;
78+
}
79+
80+
[Test]
81+
public void TestEmptyCtr()
82+
{
83+
Statement obj = new Statement();
84+
Assert.IsInstanceOf<Statement>(obj);
85+
Assert.IsNull(obj.id);
86+
Assert.IsNull(obj.actor);
87+
Assert.IsNull(obj.verb);
88+
Assert.IsNull(obj.target);
89+
Assert.IsNull(obj.result);
90+
Assert.IsNull(obj.context);
91+
Assert.IsNull(obj.version);
92+
Assert.IsNull(obj.timestamp);
93+
Assert.IsNull(obj.stored);
94+
95+
StringAssert.AreEqualIgnoringCase("{\"version\":\"1.0.1\"}", obj.ToJSON());
96+
}
97+
98+
[Test]
99+
public void TestJObjectCtrSubStatement()
100+
{
101+
JObject cfg = new JObject();
102+
cfg.Add("actor", agent.ToJObject());
103+
cfg.Add("verb", verb.ToJObject());
104+
cfg.Add("object", subStatement.ToJObject());
105+
106+
Statement obj = new Statement(cfg);
107+
Assert.IsInstanceOf<Statement>(obj);
108+
Assert.IsNotNull(obj.target);
109+
}
110+
}
111+
}

TinCanTests/TinCanTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="AgentTest.cs" />
74+
<Compile Include="StatementTest.cs" />
7475
<Compile Include="RemoteLRSResourceTest.cs" />
7576
<Compile Include="RemoteLRSTest.cs" />
7677
<Compile Include="LanguageMapTest.cs" />

0 commit comments

Comments
 (0)