Skip to content

Commit 17ff82c

Browse files
committed
Fix Result by allowing "duration" to be nullable
* "duration" property was always populated otherwise with 0 duration * Add basic tests for serialization/deserialization
1 parent 8d17e61 commit 17ff82c

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

TinCan/Result.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Result : JsonModel
2525
public Nullable<Boolean> completion { get; set; }
2626
public Nullable<Boolean> success { get; set; }
2727
public String response { get; set; }
28-
public TimeSpan duration { get; set; }
28+
public Nullable<TimeSpan> duration { get; set; }
2929
public Score score { get; set; }
3030
public Extensions extensions { get; set; }
3131

@@ -78,7 +78,7 @@ public override JObject ToJObject(TCAPIVersion version) {
7878
}
7979
if (duration != null)
8080
{
81-
result.Add("duration", XmlConvert.ToString(duration));
81+
result.Add("duration", XmlConvert.ToString((TimeSpan)duration));
8282
}
8383
if (score != null)
8484
{

TinCanTests/ResultTest.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2015 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 NUnit.Framework;
20+
using Newtonsoft.Json.Linq;
21+
using TinCan;
22+
using TinCan.Json;
23+
24+
[TestFixture]
25+
class ResultTest
26+
{
27+
[Test]
28+
public void TestEmptyCtr()
29+
{
30+
var obj = new Result();
31+
Assert.IsInstanceOf<Result>(obj);
32+
Assert.IsNull(obj.completion);
33+
Assert.IsNull(obj.success);
34+
Assert.IsNull(obj.response);
35+
Assert.IsNull(obj.duration);
36+
Assert.IsNull(obj.score);
37+
Assert.IsNull(obj.extensions);
38+
39+
StringAssert.AreEqualIgnoringCase("{}", obj.ToJSON());
40+
}
41+
42+
[Test]
43+
public void TestJObjectCtr()
44+
{
45+
var cfg = new JObject();
46+
cfg.Add("completion", true);
47+
cfg.Add("success", true);
48+
cfg.Add("response", "Yes");
49+
50+
var obj = new Result(cfg);
51+
Assert.IsInstanceOf<Result>(obj);
52+
Assert.That(obj.completion, Is.EqualTo(true));
53+
Assert.That(obj.success, Is.EqualTo(true));
54+
Assert.That(obj.response, Is.EqualTo("Yes"));
55+
}
56+
57+
[Test]
58+
public void TestStringOfJSONCtr()
59+
{
60+
var json = "{\"success\": true, \"completion\": true, \"response\": \"Yes\"}";
61+
var strOfJson = new StringOfJSON(json);
62+
63+
var obj = new Result(strOfJson);
64+
Assert.IsInstanceOf<Result>(obj);
65+
Assert.That(obj.success, Is.EqualTo(true));
66+
Assert.That(obj.completion, Is.EqualTo(true));
67+
Assert.That(obj.response, Is.EqualTo("Yes"));
68+
}
69+
}
70+
}

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="ResultTest.cs" />
7475
<Compile Include="Support.cs" />
7576
<Compile Include="SubStatementTest.cs" />
7677
<Compile Include="StatementTest.cs" />

0 commit comments

Comments
 (0)