11using UnityEngine ;
2- using UnityEditor ;
3- using UnityEngine . Events ;
4- using UnityEngine . TestTools ;
52using System . Collections . Generic ;
63using NUnit . Framework ;
7- using System . Collections ;
84using System . IO ;
95
106namespace UXF . Tests
117{
12-
138 public class TestFileSaver
149 {
15-
1610 string experiment = "fileSaver_test" ;
1711 string ppid = "test_ppid" ;
1812 int sessionNum = 1 ;
19- FileSaver fileSaver ;
13+ FileSaver fileSaver ;
2014
2115 [ SetUp ]
2216 public void SetUp ( )
2317 {
24- var gameObject = new GameObject ( ) ;
25- fileSaver = gameObject . AddComponent < FileSaver > ( ) ;
26- fileSaver . verboseDebug = true ;
18+ var gameObject = new GameObject ( ) ;
19+ fileSaver = gameObject . AddComponent < FileSaver > ( ) ;
20+ fileSaver . verboseDebug = true ;
2721 }
2822
2923
30- [ TearDown ]
31- public void TearDown ( )
32- {
33- GameObject . DestroyImmediate ( fileSaver . gameObject ) ;
34- }
24+ [ TearDown ]
25+ public void TearDown ( )
26+ {
27+ GameObject . DestroyImmediate ( fileSaver . gameObject ) ;
28+ }
3529
3630
3731 [ Test ]
@@ -41,32 +35,34 @@ public void WriteManyFiles()
4135 fileSaver . SetUp ( ) ;
4236
4337 // generate a large dictionary
44- var dict = new Dictionary < string , object > ( ) ;
38+ var dict = new Dictionary < string , object > ( ) ;
4539
4640 var largeArray = new string [ 100 ] ;
4741 string largeString = new string ( '*' , 50000 ) ;
4842
49- // write lots and lots of JSON files
50- int n = 100 ;
43+ // write lots and lots of JSON files
44+ int n = 100 ;
5145 string [ ] fpaths = new string [ n ] ;
52- for ( int i = 0 ; i < n ; i ++ )
53- {
54- string fileName = string . Format ( "{0}" , i ) ;
55- Debug . LogFormat ( "Queueing {0}" , fileName ) ;
56- string fpath = fileSaver . HandleText ( largeString , experiment , ppid , sessionNum , fileName , UXFDataType . OtherSessionData ) ;
46+ for ( int i = 0 ; i < n ; i ++ )
47+ {
48+ string fileName = string . Format ( "{0}" , i ) ;
49+ Debug . LogFormat ( "Queueing {0}" , fileName ) ;
50+ string fpath = fileSaver . HandleText ( largeString , experiment , ppid , sessionNum , fileName ,
51+ UXFDataType . OtherSessionData ) ;
5752 fpaths [ i ] = fpath ;
58- }
53+ }
5954
6055 Debug . Log ( "###########################################" ) ;
6156 Debug . Log ( "############## CLEANING UP ################" ) ;
6257 Debug . Log ( "###########################################" ) ;
6358 fileSaver . CleanUp ( ) ;
6459
65- Assert . Throws < System . InvalidOperationException > ( ( ) => {
60+ Assert . Throws < System . InvalidOperationException > ( ( ) =>
61+ {
6662 fileSaver . HandleText ( largeString , experiment , ppid , sessionNum , "0" , UXFDataType . OtherSessionData ) ;
6763 } ) ;
6864
69- // cleanup files
65+ // cleanup files
7066 foreach ( var fpath in fpaths )
7167 {
7268 System . IO . File . Delete ( Path . Combine ( fileSaver . StoragePath , fpath ) ) ;
@@ -80,12 +76,10 @@ public void EarlyExit()
8076 fileSaver . StoragePath = "example_output" ;
8177 fileSaver . SetUp ( ) ;
8278 fileSaver . CleanUp ( ) ;
83-
84- Assert . Throws < System . InvalidOperationException > (
85- ( ) => {
86- fileSaver . ManageInWorker ( ( ) => Debug . Log ( "Code enqueued after FileSaver ended" ) ) ;
87- }
88- ) ;
79+
80+ Assert . Throws < System . InvalidOperationException > (
81+ ( ) => { fileSaver . ManageInWorker ( ( ) => Debug . Log ( "Code enqueued after FileSaver ended" ) ) ; }
82+ ) ;
8983
9084 fileSaver . SetUp ( ) ;
9185 fileSaver . ManageInWorker ( ( ) => Debug . Log ( "Code enqueued after FileSaver re-opened" ) ) ;
@@ -95,10 +89,18 @@ public void EarlyExit()
9589 [ Test ]
9690 public void AbsolutePath ( )
9791 {
98- fileSaver . StoragePath = "C:/example_output" ;
92+ if ( SystemInfo . operatingSystemFamily == OperatingSystemFamily . Windows )
93+ {
94+ fileSaver . StoragePath = "C:/example_output" ;
95+ }
96+ else
97+ {
98+ fileSaver . StoragePath = "/tmp/example_output" ;
99+ }
99100 fileSaver . SetUp ( ) ;
100-
101- string outString = fileSaver . HandleText ( "abc" , experiment , ppid , sessionNum , "test" , UXFDataType . OtherSessionData ) ;
101+
102+ string outString =
103+ fileSaver . HandleText ( "abc" , experiment , ppid , sessionNum , "test" , UXFDataType . OtherSessionData ) ;
102104
103105 Assert . AreEqual ( outString , @"fileSaver_test/test_ppid/S001/other/test.txt" ) ;
104106
@@ -115,18 +117,18 @@ public void PersistentDataPath()
115117 string dataOutput = "abc" ;
116118 fileSaver . HandleText ( dataOutput , experiment , ppid , sessionNum , "test" , UXFDataType . OtherSessionData ) ;
117119 fileSaver . CleanUp ( ) ;
118- string outFile = Path . Combine ( Application . persistentDataPath , @"fileSaver_test/test_ppid/S001/other/test.txt" ) ;
120+ string outFile = Path . Combine ( Application . persistentDataPath ,
121+ @"fileSaver_test/test_ppid/S001/other/test.txt" ) ;
119122
120123 string readData = File . ReadAllText ( outFile ) ;
121124 Assert . AreEqual ( dataOutput , readData ) ;
122-
125+
123126 if ( File . Exists ( outFile ) ) File . Delete ( outFile ) ;
124127 }
125128
126129 [ Test ]
127130 public void FileSaverRelPath ( )
128131 {
129-
130132 Assert . AreEqual (
131133 FileSaver . GetRelativePath ( "C:\\ base" , "C:\\ base\\ 123" ) ,
132134 "123"
@@ -139,15 +141,13 @@ public void FileSaverRelPath()
139141
140142 Assert . AreEqual (
141143 FileSaver . GetRelativePath ( "base/" , "base\\ 123" ) ,
142- " 123"
144+ SystemInfo . operatingSystemFamily == OperatingSystemFamily . Windows ? "123" : "../ 123"
143145 ) ;
144146
145147 Assert . AreEqual (
146148 FileSaver . GetRelativePath ( "C:/base/" , "C:/base\\ 123" ) ,
147- "123"
149+ SystemInfo . operatingSystemFamily == OperatingSystemFamily . Windows ? "123" : "../123"
148150 ) ;
149151 }
150-
151152 }
152-
153- }
153+ }
0 commit comments