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+ using System ;
9+ using System . Threading ;
10+ using System . Globalization ;
11+
12+ namespace UXF . Tests
13+ {
14+
15+ public class TestLocale
16+ {
17+ [ Test ]
18+ public void TestUS ( ) { TestSingleLocale ( new CultureInfo ( "en-US" ) ) ; }
19+
20+ [ Test ]
21+ public void TestGB ( ) { TestSingleLocale ( new CultureInfo ( "en-GB" ) ) ; }
22+
23+ [ Test ]
24+ public void TestDE ( ) { TestSingleLocale ( new CultureInfo ( "de-DE" ) ) ; }
25+
26+ public void TestSingleLocale ( CultureInfo culture )
27+ {
28+ // https://www.csharp-examples.net/culture-names/
29+ Thread . CurrentThread . CurrentCulture = culture ;
30+
31+ ( Session session , FileSaver fileSaver ) = CreateSession ( culture . DisplayName ) ;
32+ session . blocks [ 0 ] . trials [ 0 ] . Begin ( ) ;
33+ session . blocks [ 0 ] . trials [ 0 ] . result [ "number" ] = 123.456f ;
34+ session . blocks [ 0 ] . trials [ 0 ] . End ( ) ;
35+
36+ string path = fileSaver . GetSessionPath ( session . experimentName , session . ppid , session . number ) ;
37+ session . End ( ) ;
38+ GameObject . DestroyImmediate ( session . gameObject ) ;
39+
40+ // read the file to check data
41+ string [ ] lines = File . ReadAllLines ( Path . Combine ( path , "trial_results.csv" ) ) ;
42+ var header = lines [ 0 ] . Split ( new string [ ] { culture . TextInfo . ListSeparator } , StringSplitOptions . None ) ;
43+ int idx = Array . FindIndex ( header , ( v ) => v == "number" ) ;
44+
45+ var firstRow = lines [ 1 ] . Split ( new string [ ] { culture . TextInfo . ListSeparator } , StringSplitOptions . None ) ;
46+ string num = firstRow [ idx ] ;
47+
48+ Assert . AreEqual ( 123.456f . ToString ( ) , num ) ;
49+
50+ // json parsing never uses locale, as it does not allow commas within numbers
51+ var newObject = ( Dictionary < string , object > ) MiniJSON . Json . Deserialize ( "{ \" num\" : 123.456 }" ) ;
52+ Assert . AreEqual ( 123.456d , newObject [ "num" ] ) ;
53+ }
54+
55+ Tuple < Session , FileSaver > CreateSession ( string ppidExtra )
56+ {
57+ GameObject gameObject = new GameObject ( ) ;
58+ FileSaver fileSaver = gameObject . AddComponent < FileSaver > ( ) ;
59+ SessionLogger sessionLogger = gameObject . AddComponent < SessionLogger > ( ) ;
60+ Session session = gameObject . AddComponent < Session > ( ) ;
61+ fileSaver . storagePath = "example_output" ;
62+
63+ sessionLogger . AttachReferences (
64+ session
65+ ) ;
66+
67+ session . dataHandlers = new DataHandler [ ] { fileSaver } ;
68+
69+ sessionLogger . Initialise ( ) ;
70+
71+ fileSaver . verboseDebug = false ;
72+
73+ string experimentName = "unit_test" ;
74+ string ppid = "test_locale_" + ppidExtra ;
75+ session . Begin ( experimentName , ppid ) ;
76+
77+ // generate trials
78+ session . CreateBlock ( 2 ) ;
79+ session . CreateBlock ( 3 ) ;
80+
81+ return new Tuple < Session , FileSaver > ( session , fileSaver ) ;
82+ }
83+
84+ [ TearDown ]
85+ public void TearDown ( )
86+ {
87+ Thread . CurrentThread . CurrentCulture = new CultureInfo ( "en-GB" ) ;
88+ }
89+
90+ }
91+
92+ }
0 commit comments