Skip to content

Commit 45bd03a

Browse files
committed
Unit testing
1 parent e02511d commit 45bd03a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1404
-996
lines changed

Assets/UXF/Examples/Basic/BasicExample.unity

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,6 @@ Prefab:
160160
m_Modification:
161161
m_TransformParent: {fileID: 0}
162162
m_Modifications:
163-
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
164-
type: 2}
165-
propertyPath: customHeaders.Array.size
166-
value: 1
167-
objectReference: {fileID: 0}
168-
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
169-
type: 2}
170-
propertyPath: trackedObjects.Array.size
171-
value: 1
172-
objectReference: {fileID: 0}
173163
- target: {fileID: 4522850482104986, guid: 0a3b6392f04558844bd340e68ced1ff9, type: 2}
174164
propertyPath: m_LocalPosition.x
175165
value: 0
@@ -202,6 +192,11 @@ Prefab:
202192
propertyPath: m_RootOrder
203193
value: 3
204194
objectReference: {fileID: 0}
195+
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
196+
type: 2}
197+
propertyPath: trackedObjects.Array.data[0]
198+
value:
199+
objectReference: {fileID: 1294342340}
205200
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
206201
type: 2}
207202
propertyPath: onSessionBegin.m_PersistentCalls.m_Calls.Array.data[0].m_Target
@@ -222,61 +217,6 @@ Prefab:
222217
propertyPath: m_AnchorMax.y
223218
value: 1
224219
objectReference: {fileID: 0}
225-
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
226-
type: 2}
227-
propertyPath: m_AnchorMin.y
228-
value: 1
229-
objectReference: {fileID: 0}
230-
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
231-
type: 2}
232-
propertyPath: m_AnchorMax.y
233-
value: 1
234-
objectReference: {fileID: 0}
235-
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
236-
type: 2}
237-
propertyPath: m_AnchoredPosition.x
238-
value: 164
239-
objectReference: {fileID: 0}
240-
- target: {fileID: 224286096967890162, guid: 0a3b6392f04558844bd340e68ced1ff9,
241-
type: 2}
242-
propertyPath: m_SizeDelta.x
243-
value: 318
244-
objectReference: {fileID: 0}
245-
- target: {fileID: 224546371281550106, guid: 0a3b6392f04558844bd340e68ced1ff9,
246-
type: 2}
247-
propertyPath: m_SizeDelta.y
248-
value: 75
249-
objectReference: {fileID: 0}
250-
- target: {fileID: 224546371281550106, guid: 0a3b6392f04558844bd340e68ced1ff9,
251-
type: 2}
252-
propertyPath: m_AnchoredPosition.y
253-
value: -0.0000051259995
254-
objectReference: {fileID: 0}
255-
- target: {fileID: 114345737082279174, guid: 0a3b6392f04558844bd340e68ced1ff9,
256-
type: 2}
257-
propertyPath: m_Value
258-
value: 1
259-
objectReference: {fileID: 0}
260-
- target: {fileID: 224130118367839794, guid: 0a3b6392f04558844bd340e68ced1ff9,
261-
type: 2}
262-
propertyPath: m_AnchorMin.y
263-
value: 0
264-
objectReference: {fileID: 0}
265-
- target: {fileID: 114375821934039048, guid: 0a3b6392f04558844bd340e68ced1ff9,
266-
type: 2}
267-
propertyPath: quickStartMode
268-
value: 1
269-
objectReference: {fileID: 0}
270-
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
271-
type: 2}
272-
propertyPath: customHeaders.Array.data[0]
273-
value: some_variable
274-
objectReference: {fileID: 0}
275-
- target: {fileID: 114916036141644208, guid: 0a3b6392f04558844bd340e68ced1ff9,
276-
type: 2}
277-
propertyPath: trackedObjects.Array.data[0]
278-
value:
279-
objectReference: {fileID: 1294342340}
280220
m_RemovedComponents: []
281221
m_ParentPrefab: {fileID: 100100000, guid: 0a3b6392f04558844bd340e68ced1ff9, type: 2}
282222
m_IsPrefabParent: 0

Assets/UXF/Examples/Basic/BasicExampleScript.cs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,7 @@
1313
public class BasicExampleScript : MonoBehaviour {
1414

1515
UXF.Session session;
16-
float startNextTime;
17-
18-
19-
void Start()
20-
{
21-
// disable this behavior so the Update() function doesnt run just yet.
22-
enabled = false;
23-
}
24-
25-
void Update()
26-
{
27-
// here we are mimicking some experiment behaviour, e.g waiting for user to interact with scene
28-
if (Time.time > startNextTime && session.inTrial)
29-
{
30-
Debug.Log("Ending trial");
31-
session.EndCurrentTrial();
32-
33-
if (session.currentTrial == session.lastTrial)
34-
{
35-
#if UNITY_EDITOR
36-
UnityEditor.EditorApplication.isPlaying = false;
37-
#else
38-
Application.Quit();
39-
#endif
40-
}
41-
}
42-
}
43-
16+
4417
public void GenerateExperiment(Session experimentSession)
4518
{
4619
// save reference to session
@@ -71,7 +44,7 @@ of an ExperimentSession component as .settings */
7144
Block practiceBlock = session.CreateBlock(numPracticeTrials);
7245
practiceBlock.settings["practice"] = true;
7346

74-
// retrieve the n_main_trials setting, which was loaded from our .json file
47+
// retrieve the n_main_trials setting, which was loaded from our .json file into our session settings
7548
int numMainTrials = Convert.ToInt32(session.settings["n_main_trials"]);
7649
// create block 2
7750
Block mainBlock = session.CreateBlock(numMainTrials); // block 2
@@ -80,8 +53,6 @@ of an ExperimentSession component as .settings */
8053
mainBlock.GetRelativeTrial(2).settings["size"] = 10;
8154
mainBlock.GetRelativeTrial(1).settings["color"] = Color.red;
8255

83-
// setting this script to enabled allows the MonoBehaviour scripts to run e.g. Update()
84-
enabled = true;
8556
}
8657

8758
public void PresentStimulus(Trial trial)
@@ -102,8 +73,30 @@ public void PresentStimulus(Trial trial)
10273
Debug.Log(string.Format("We observed: {0}", observation));
10374
trial.result["some_variable"] = observation;
10475

105-
// wait 1 second until we end trial and run the next one
106-
startNextTime = Time.time + 1;
76+
// end trial and prepare next trial in 1 second
77+
Invoke("EndAndPrepare", 1);
78+
}
79+
80+
81+
public void EndAndPrepare()
82+
{
83+
Debug.Log("Ending trial");
84+
session.currentTrial.End();
85+
86+
if (session.currentTrial == session.lastTrial)
87+
{
88+
#if UNITY_EDITOR
89+
UnityEditor.EditorApplication.isPlaying = false;
90+
#else
91+
Application.Quit();
92+
#endif
93+
}
94+
else
95+
{
96+
session.BeginNextTrial();
97+
}
98+
10799
}
100+
108101
}
109102

Assets/UXF/Prefabs/ExperimentWithUI.prefab

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,8 @@ MonoBehaviour:
18911891
m_Script: {fileID: 11500000, guid: 325d8bc56dfbdf4439627ee58ed3a5a4, type: 3}
18921892
m_Name:
18931893
m_EditorClassIdentifier:
1894+
session: {fileID: 114916036141644208}
1895+
fileIOManager: {fileID: 114324480150258046}
18941896
--- !u!114 &114164683737562590
18951897
MonoBehaviour:
18961898
m_ObjectHideFlags: 1
@@ -2659,6 +2661,7 @@ MonoBehaviour:
26592661
m_Script: {fileID: 11500000, guid: 62bd92b5564a1d343b07363d3453a980, type: 3}
26602662
m_Name:
26612663
m_EditorClassIdentifier:
2664+
debug: 1
26622665
--- !u!114 &114344481789201668
26632666
MonoBehaviour:
26642667
m_ObjectHideFlags: 1
@@ -2770,7 +2773,7 @@ MonoBehaviour:
27702773
m_Script: {fileID: 11500000, guid: 67303b59714ae6141976cebbe8c7d961, type: 3}
27712774
m_Name:
27722775
m_EditorClassIdentifier:
2773-
quickStartMode: 0
2776+
quickStartMode: 1
27742777
saveDataLocation: example_output
27752778
experimentSettingsName: basic_example_1.json
27762779
_participantDataPoints:
@@ -4555,12 +4558,12 @@ MonoBehaviour:
45554558
m_Script: {fileID: 11500000, guid: fa9f2505d11cc9e4aa44357b47b12905, type: 3}
45564559
m_Name:
45574560
m_EditorClassIdentifier:
4558-
endExperimentOnQuit: 1
4559-
_settingsToLog:
4560-
- size
4561-
_customHeaders:
4561+
endOnDestroy: 1
4562+
settingsToLog: []
4563+
customHeaders:
45624564
- some_variable
4563-
_trackedObjects: []
4565+
trackedObjects:
4566+
- {fileID: 0}
45644567
onSessionBegin:
45654568
m_PersistentCalls:
45664569
m_Calls:
@@ -4606,26 +4609,17 @@ MonoBehaviour:
46064609
PublicKeyToken=null
46074610
onTrialEnd:
46084611
m_PersistentCalls:
4609-
m_Calls:
4610-
- m_Target: {fileID: 114916036141644208}
4611-
m_MethodName: BeginNextTrial
4612-
m_Mode: 1
4613-
m_Arguments:
4614-
m_ObjectArgument: {fileID: 0}
4615-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
4616-
m_IntArgument: 0
4617-
m_FloatArgument: 0
4618-
m_StringArgument:
4619-
m_BoolArgument: 0
4620-
m_CallState: 2
4612+
m_Calls: []
46214613
m_TypeName: UXF.TrialEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
46224614
PublicKeyToken=null
4623-
isQuitting: 0
4615+
_hasInitialised: 0
46244616
experimentName:
46254617
ppid:
46264618
number: 0
46274619
currentTrialNum: 0
46284620
currentBlockNum: 0
4621+
fileIOManager: {fileID: 114324480150258046}
4622+
logger: {fileID: 114155219626309100}
46294623
--- !u!114 &114922365913132848
46304624
MonoBehaviour:
46314625
m_ObjectHideFlags: 1
@@ -6261,7 +6255,7 @@ RectTransform:
62616255
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
62626256
m_AnchorMin: {x: 0, y: 1}
62636257
m_AnchorMax: {x: 1, y: 1}
6264-
m_AnchoredPosition: {x: 0, y: 0.0000059604645}
6258+
m_AnchoredPosition: {x: 0, y: -0.0000051259995}
62656259
m_SizeDelta: {x: 0, y: 0}
62666260
m_Pivot: {x: 0.5, y: 1}
62676261
--- !u!224 &224594001841352330

Assets/UXF/Scripts/CSVReader/Tests.meta

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

Assets/Editor.meta renamed to Assets/UXF/Scripts/CSVReader/Tests/Editor.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
9+
namespace UXF.Tests
10+
{
11+
public class TestSessionBuilding
12+
{
13+
GameObject gameObject;
14+
Session session;
15+
16+
[SetUp]
17+
public void SetUp()
18+
{
19+
gameObject = new GameObject();
20+
session = gameObject.AddComponent<Session>();
21+
session.AttachReferences(
22+
newFileIOManager: gameObject.AddComponent<FileIOManager>()
23+
);
24+
25+
}
26+
27+
[TearDown]
28+
public void TearDown()
29+
{
30+
GameObject.DestroyImmediate(gameObject);
31+
}
32+
33+
[Test]
34+
public void CreateBlocks()
35+
{
36+
Block block1 = session.CreateBlock(5);
37+
Assert.AreEqual(block1.trials.Count, 5);
38+
39+
Block block2 = session.CreateBlock();
40+
Assert.AreEqual(block2.trials.Count, 0);
41+
42+
Trial trial = block2.CreateTrial();
43+
Assert.AreEqual(block2.trials.Count, 1);
44+
Assert.AreEqual(trial.number, 6);
45+
46+
Assert.AreEqual(session.blocks.Count, 2);
47+
48+
// reset blocks
49+
session.blocks = new List<Block>();
50+
51+
}
52+
53+
[Test]
54+
public void ShuffleBlocks()
55+
{
56+
for (int i = 0; i < 5; i++)
57+
{
58+
Block block = session.CreateBlock(10);
59+
block.settings["order"] = i;
60+
}
61+
62+
var rng = new System.Random(0);
63+
64+
session.blocks.Shuffle(rng);
65+
66+
int[] expectedOrders = new int[]{ 3, 0, 2, 1, 4 };
67+
for (int i = 0; i < 5; i++)
68+
{
69+
var block = session.blocks[i];
70+
var expected = expectedOrders[i];
71+
Assert.AreEqual(block.settings["order"], expected);
72+
}
73+
74+
// reset blocks
75+
session.blocks = new List<Block>();
76+
77+
}
78+
79+
80+
[Test]
81+
public void ShuffleTrials()
82+
{
83+
Block block = session.CreateBlock(10);
84+
85+
for (int i = 0; i < block.trials.Count; i++)
86+
{
87+
block.trials[i].settings["order"] = i;
88+
}
89+
90+
var rng = new System.Random(10);
91+
int[] expectedOrders = new int[] { 9, 7, 2, 8, 0, 5, 1, 4, 6, 3 };
92+
93+
block.trials.Shuffle(rng);
94+
for (int i = 0; i < block.trials.Count; i++)
95+
{
96+
var trial = block.trials[i];
97+
var expected = expectedOrders[i];
98+
Assert.AreEqual(trial.settings["order"], expected);
99+
}
100+
101+
// reset blocks
102+
session.blocks = new List<Block>();
103+
104+
}
105+
106+
}
107+
108+
}

0 commit comments

Comments
 (0)