Skip to content

Commit 0b70805

Browse files
committed
better post session event handling
1 parent bccbda8 commit 0b70805

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

Assets/UXF/Scripts/Session.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public class Session : MonoBehaviour
2929
[Tooltip("Enable to automatically safely end the session when this object is destroyed.")]
3030
public bool endOnDestroy = true;
3131

32+
/// <summary>
33+
/// Enable to automatically end the session when the final trial has ended.
34+
/// </summary>
35+
[Tooltip("Enable to automatically end the session when the final trial has ended.")]
36+
public bool endAfterLastTrial = true;
37+
3238
/// <summary>
3339
/// List of blocks for this experiment
3440
/// </summary>
@@ -640,13 +646,14 @@ public void End()
640646
// end FileIOManager - forces immediate writing of all files
641647
fileIOManager.End();
642648

649+
onSessionEnd.Invoke(this);
650+
643651
currentTrialNum = 0;
644652
currentBlockNum = 0;
645653
blocks = new List<Block>();
646654
_hasInitialised = false;
647655

648656
Debug.Log("Ended session.");
649-
onSessionEnd.Invoke(this);
650657
}
651658
}
652659

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
10+
namespace UXF.Tests
11+
{
12+
13+
public class TestEvents
14+
{
15+
16+
GameObject gameObject;
17+
Session session;
18+
FileIOManager fileIOManager;
19+
SessionLogger sessionLogger;
20+
21+
[SetUp]
22+
public void SetUp()
23+
{
24+
25+
}
26+
27+
[TearDown]
28+
public void TearDown()
29+
{
30+
31+
}
32+
33+
[Test]
34+
public void SessionEndEvent()
35+
{
36+
37+
gameObject = new GameObject();
38+
fileIOManager = gameObject.AddComponent<FileIOManager>();
39+
sessionLogger = gameObject.AddComponent<SessionLogger>();
40+
session = gameObject.AddComponent<Session>();
41+
42+
session.AttachReferences(
43+
fileIOManager
44+
);
45+
46+
sessionLogger.AttachReferences(
47+
fileIOManager,
48+
session
49+
);
50+
51+
session.onSessionEnd.AddListener(UseSession);
52+
53+
sessionLogger.Initialise();
54+
55+
fileIOManager.debug = true;
56+
fileIOManager.Begin();
57+
58+
string experimentName = "unit_test";
59+
string ppid = "test_trials";
60+
session.Begin(experimentName, ppid, "example_output");
61+
session.customHeaders.Add("observation");
62+
63+
// generate trials
64+
session.CreateBlock(2);
65+
session.CreateBlock(3);
66+
67+
int i = 0;
68+
foreach (var trial in session.Trials)
69+
{
70+
trial.Begin();
71+
trial.result["observation"] = ++i;
72+
trial.End();
73+
}
74+
75+
session.End();
76+
77+
78+
79+
80+
}
81+
82+
83+
void UseSession(UXF.Session session)
84+
{
85+
int i = 0;
86+
foreach (var trial in session.Trials)
87+
{
88+
Assert.AreEqual(trial.result["observation"], ++i);
89+
}
90+
}
91+
92+
93+
}
94+
95+
}

Assets/UXF/Scripts/Tests/Editor/TestEvents.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UXF/Scripts/UI/Extensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public static void Shuffle<T>(this IList<T> list)
6767
list.Shuffle(rng);
6868
}
6969

70+
/// <summary>
71+
/// Swaps the order of the elements at indeces `indexA` and `indexB` within `list`
72+
/// </summary>
73+
public static void Swap<T>(IList<T> list, int indexA, int indexB)
74+
{
75+
T tmp = list[indexA];
76+
list[indexA] = list[indexB];
77+
list[indexB] = tmp;
78+
}
7079

7180
/// <summary>
7281
/// Combine many path parts into a single path.

0 commit comments

Comments
 (0)