Skip to content

Commit c133c22

Browse files
committed
NoSuchTrialException when accessing trials that do not exist
1 parent e97082c commit c133c22

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

Assets/UXF/Scripts/Session.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,17 @@ Trial GetPrevTrial()
618618
/// </summary>
619619
/// <returns></returns>
620620
Trial GetFirstTrial()
621-
{
622-
var firstBlock = blocks[0];
621+
{
622+
Block firstBlock;
623+
try
624+
{
625+
firstBlock = blocks[0];
626+
}
627+
catch (ArgumentOutOfRangeException)
628+
{
629+
throw new NoSuchTrialException("There is no first trial because no blocks have been created!");
630+
}
631+
623632
try
624633
{
625634
return firstBlock.trials[0];
@@ -636,7 +645,16 @@ Trial GetFirstTrial()
636645
/// <returns></returns>
637646
Trial GetLastTrial()
638647
{
639-
var lastBlock = blocks[blocks.Count - 1];
648+
Block lastBlock;
649+
try
650+
{
651+
lastBlock = blocks[0];
652+
}
653+
catch (ArgumentOutOfRangeException)
654+
{
655+
throw new NoSuchTrialException("There is no last trial because no blocks have been created!");
656+
}
657+
640658
try
641659
{
642660
return lastBlock.trials[lastBlock.trials.Count - 1];

Assets/UXF/Scripts/Tests/Editor/TestSessionBuilding.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,35 @@ public void SwapTrials()
129129
session.blocks = new List<Block>();
130130

131131
}
132+
133+
[Test]
134+
public void InvalidTrialAccess()
135+
{
136+
Block block = session.CreateBlock();
137+
138+
Assert.Throws<NoSuchTrialException>(
139+
delegate { Trial t = session.FirstTrial; }
140+
);
141+
142+
Assert.Throws<NoSuchTrialException>(
143+
delegate { Trial t = session.LastTrial; }
144+
);
145+
146+
// reset blocks
147+
session.blocks = new List<Block>();
148+
}
149+
150+
[Test]
151+
public void InvalidBlockAccess()
152+
{
153+
Assert.Throws<NoSuchTrialException>(
154+
delegate { Trial t = session.FirstTrial; }
155+
);
156+
157+
Assert.Throws<NoSuchTrialException>(
158+
delegate { Trial t = session.LastTrial; }
159+
);
160+
}
132161

133162
}
134163

0 commit comments

Comments
 (0)