Skip to content

Commit fdd5ffa

Browse files
committed
fix issues with blank entries
1 parent 111a1d5 commit fdd5ffa

File tree

4 files changed

+95
-28
lines changed

4 files changed

+95
-28
lines changed

Assets/UXF/Scripts/Etc/SessionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void BuildFromTable(this Session session, UXFDataTable table)
6161
if (kvp.Key == "block_num") continue;
6262

6363
// empty values do not assign a setting
64-
if (kvp.Value.ToString() == string.Empty) continue;
64+
if (kvp.Value.ToString().Trim() == string.Empty) continue;
6565

6666
// add trial setting
6767
newTrial.settings.SetValue(kvp.Key, kvp.Value);

Assets/UXF/Scripts/Etc/UXFDataTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public static UXFDataTable FromCSV(string[] csvLines)
5454
// traverse down rows
5555
for (int i = 1; i < csvLines.Length; i++)
5656
{
57-
string[] values = csvLines[i].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
57+
string[] values = csvLines[i].Split(',');
5858

59-
// if last line is blank, ignore it
60-
if (values.Length == 0 && i == csvLines.Length - 1) break;
59+
// if last line, just 1 item in the row, and it is blank, then ignore it
60+
if (i == csvLines.Length - 1 && values.Length == 1 && values[0].Trim() == string.Empty ) break;
6161

6262
// check if number of columns is correct
6363
if (values.Length != headers.Length) throw new Exception($"CSV line {i} has {values.Length} columns, but expected {headers.Length}");

Assets/UXF/Tests/Editor/TestBuildingFromCSV.cs

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public void BuildFromTable()
4343
}
4444

4545
session.BuildFromTable(table);
46-
Assert.AreEqual(10, session.Trials.Count());
47-
Assert.AreEqual(1, session.blocks.Count);
46+
Assert.AreEqual(10, session.Trials.Count());
47+
Assert.AreEqual(1, session.blocks.Count);
4848

4949
foreach (var trial in session.Trials)
5050
{
51-
Assert.AreEqual("hello", trial.settings.GetString("some_text"));
52-
Assert.AreEqual(123, trial.settings.GetInt("an_integer"));
53-
Assert.AreEqual(3.14f, trial.settings.GetFloat("a_float"));
51+
Assert.AreEqual("hello", trial.settings.GetString("some_text"));
52+
Assert.AreEqual(123, trial.settings.GetInt("an_integer"));
53+
Assert.AreEqual(3.14f, trial.settings.GetFloat("a_float"));
5454
}
5555

5656
}
@@ -83,22 +83,22 @@ public void BuildFromTableWithBlockNum()
8383
}
8484

8585
session.BuildFromTable(table);
86-
Assert.AreEqual(20, session.Trials.Count());
87-
Assert.AreEqual(2, session.blocks.Count);
86+
Assert.AreEqual(20, session.Trials.Count());
87+
Assert.AreEqual(2, session.blocks.Count);
8888

8989
foreach (var trial in session.Trials)
9090
{
9191
Assert.IsFalse(trial.settings.ContainsKey("block_num"));
9292
Assert.AreEqual("hello", trial.settings.GetString("some_text"));
93-
Assert.AreEqual(123, trial.settings.GetInt("an_integer"));
94-
Assert.AreEqual(3.14f, trial.settings.GetFloat("a_float"));
93+
Assert.AreEqual(123, trial.settings.GetInt("an_integer"));
94+
Assert.AreEqual(3.14f, trial.settings.GetFloat("a_float"));
9595
}
9696

9797
}
9898

99-
[Test]
100-
public void BuildTableFromCSV()
101-
{
99+
[Test]
100+
public void BuildTableFromCSV()
101+
{
102102
string testCsv = "some_text,an_integer,a_float\n" +
103103
"hello,123,3.14\n" +
104104
"hello,123,3.14\n" +
@@ -110,54 +110,121 @@ public void BuildTableFromCSV()
110110
"hello,123,3.14\n" +
111111
"hello,123,3.14\n";
112112

113-
string[] csvLines = testCsv.Split('\n');
113+
string[] csvLines = testCsv.Split('\n');
114114

115115
// build a table from the CSV
116116
UXFDataTable table = UXFDataTable.FromCSV(csvLines);
117117

118-
// test that the table contains the correct data
119-
Assert.AreEqual(3, table.Headers.Count());
118+
// test that the table contains the correct data
119+
Assert.AreEqual(3, table.Headers.Count());
120120

121121
var rows = table.GetAsListOfDict();
122122
foreach (var row in rows)
123-
{
124-
Assert.AreEqual("hello", row["some_text"]);
125-
Assert.AreEqual("123", row["an_integer"]);
126-
Assert.AreEqual("3.14", row["a_float"]);
127-
}
123+
{
124+
Assert.AreEqual("hello", row["some_text"]);
125+
Assert.AreEqual("123", row["an_integer"]);
126+
Assert.AreEqual("3.14", row["a_float"]);
127+
}
128+
}
129+
130+
[Test]
131+
public void BuildTableFromCSVithBlankEntries()
132+
{
133+
string testCsv = "some_text,an_integer,a_float\n" +
134+
"hello,123,3.14\n" +
135+
"hello,,3.14\n" +
136+
"hello,123,3.14\n";
137+
138+
string[] csvLines = testCsv.Split('\n');
139+
140+
// build a table from the CSV
141+
UXFDataTable table = UXFDataTable.FromCSV(csvLines);
142+
143+
// test that the table contains the correct data
144+
Assert.AreEqual(3, table.Headers.Count());
145+
146+
// check row 2
147+
var rows = table.GetAsListOfDict();
148+
Assert.AreEqual(string.Empty, rows[1]["an_integer"]);
128149
}
129150

130151
[Test]
131152
public void BuildFromTableWithBlankEntries()
132153
{
133-
UXFDataTable table = new UXFDataTable("some_text");
154+
UXFDataTable table = new UXFDataTable("an_integer", "some_text", "a_float");
134155

135156
var row1 = new UXFDataRow();
157+
row1.Add(("an_integer", "2"));
136158
row1.Add(("some_text", "trial_string"));
159+
row1.Add(("a_float", "3.14"));
137160
table.AddCompleteRow(row1);
138161

139162
var row2 = new UXFDataRow();
163+
row2.Add(("an_integer", "2"));
140164
row2.Add(("some_text", "")); // blank entry
165+
row2.Add(("a_float", "3.14"));
141166
table.AddCompleteRow(row2);
167+
168+
var row3 = new UXFDataRow();
169+
row3.Add(("an_integer", "2"));
170+
row3.Add(("some_text", " ")); // blank entry with whitespace
171+
row3.Add(("a_float", "3.14"));
172+
table.AddCompleteRow(row3);
142173

143174
session.BuildFromTable(table);
144175

145176
// set a session setting. session needs to be started to create session settings
146177
session.Begin("test", "test_ppid");
147178
session.settings.SetValue("some_text", "session_string");
148179

149-
// should be 2 trials
150-
Assert.AreEqual(2, session.Trials.Count());
180+
// should be 3 trials
181+
Assert.AreEqual(3, session.Trials.Count());
151182

152183
// pull out the text
153184
string trial1text = session.GetTrial(1).settings.GetString("some_text");
154185
string trial2text = session.GetTrial(2).settings.GetString("some_text");
186+
string trial3text = session.GetTrial(3).settings.GetString("some_text");
155187

156188
// check that the trial setting is correct
157189
Assert.AreEqual("trial_string", trial1text);
158190

159191
// check that the blank entry was ignored in place of the session setting
160192
Assert.AreEqual("session_string", trial2text);
193+
194+
// and the whitespace was also ignored
195+
Assert.AreEqual("session_string", trial3text);
196+
}
197+
198+
199+
[Test]
200+
public void BuildTableFromCSVWithoutBlankLastLine()
201+
{
202+
string testCsv = "some_text,an_integer,a_float\n" +
203+
"hello,123,3.14\n" +
204+
"hello,123,3.14\n" +
205+
"hello,123,3.14\n" +
206+
"hello,123,3.14\n" +
207+
"hello,123,3.14\n" +
208+
"hello,123,3.14\n" +
209+
"hello,123,3.14\n" +
210+
"hello,123,3.14\n" +
211+
"hello,123,3.14";
212+
213+
string[] csvLines = testCsv.Split('\n');
214+
215+
// build a table from the CSV
216+
UXFDataTable table = UXFDataTable.FromCSV(csvLines);
217+
218+
// test that the table contains the correct data
219+
Assert.AreEqual(3, table.Headers.Count());
220+
221+
var rows = table.GetAsListOfDict();
222+
foreach (var row in rows)
223+
{
224+
Assert.AreEqual("hello", row["some_text"]);
225+
Assert.AreEqual("123", row["an_integer"]);
226+
Assert.AreEqual("3.14", row["a_float"]);
227+
}
161228
}
162229

163230
}

Assets/UXF/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0
1+
2.3.1

0 commit comments

Comments
 (0)