@@ -147,22 +147,66 @@ - (BOOL)resetEngine;
147147 checkVariablesAreSetForTesseract (tesseract);
148148 });
149149 });
150-
150+
151151 NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
152152 NSString *cachesPath = cachesPaths.firstObject ;
153153 NSString *tessdataPath = @" foo/bar" ;
154154 NSString *cachesTessDataPath = [cachesPath stringByAppendingPathComponent: tessdataPath];
155+
156+ void (^cleanTessdataFolder)() = ^{
157+ // NSLog(@"Removing previous tessdata folder from Caches folder");
158+ NSError *error = nil ;
159+ BOOL fileIsRemoved = [fileManager removeItemAtPath: cachesTessDataPath error: &error];
160+ if (error != nil ) {
161+ NSLog (@" Error deleting tessdata folder from the Caches folder: %@ " , error);
162+ }
163+ NSAssert (fileIsRemoved == YES , @" Error cleaning tessdata from the Caches folder" );
164+
165+ // check tessdata folder was deleted
166+ NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath: cachesPath error: &error];
167+ if (error != nil ) {
168+ NSLog (@" Error getting the contents of the Caches folder: %@ " , error);
169+ }
170+ NSAssert ([cachesContent containsObject: tessdataPath] == NO , @" Assert! Tessdata path was not removed from the Caches folder" );
171+ };
172+
173+ context (@" moveTessdataToCachesDirectoryIfNecessary" , ^{
174+
175+ void (^checkInitializationWithFailedSelectorReturnValueAndCount)(SEL selector, id returnValue, int count) = ^(SEL selector, id returnValue, int count){
176+ G8Tesseract *wrongTesseract = [G8Tesseract alloc ];
177+ [[wrongTesseract shouldNot ] beNil ];
178+ [[[NSFileManager defaultManager ] should ] receive: selector andReturn: returnValue withCount: count];
179+ wrongTesseract = [wrongTesseract initWithLanguage: kG8Languages configDictionary: nil configFileNames: nil cachesRelatedDataPath: tessdataPath engineMode: G8OCREngineModeTesseractOnly];
180+ [[wrongTesseract should ] beNil ];
181+ };
182+
183+ it (@" Should return nil if createDirectoryAtPath fails" , ^{
184+ checkInitializationWithFailedSelectorReturnValueAndCount (@selector (createDirectoryAtPath:withIntermediateDirectories:attributes:error: ), theValue (NO ), 1 );
185+ });
186+
187+ it (@" Should return nil if createSymbolicLinkAtPath fails" , ^{
188+ NSError *error = nil ;
189+ NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath: tessdataFolderPathFromTheBundle error: &error];
190+ NSAssert (error == nil , @" Error getting the content of the Tessdata folder from the app bundle: %@ " , error);
155191
192+ checkInitializationWithFailedSelectorReturnValueAndCount (@selector (createSymbolicLinkAtPath:withDestinationPath:error: ), theValue (NO ), contentsOfTessdataFromTheBundle.count );
193+ cleanTessdataFolder ();
194+ });
195+
196+ it (@" Should return nil if contentsOfDirectoryAtPath fails" , ^{
197+ checkInitializationWithFailedSelectorReturnValueAndCount (@selector (contentsOfDirectoryAtPath:error: ), nil , 2 );
198+ cleanTessdataFolder ();
199+ });
200+ });
201+
156202 context (@" not nil cachesRelatedDataPath" , ^{
157203
158204 // helper
159205 BOOL (^doFoldersContainTheSameElements)(void ) = ^(void ){
160206 NSError *error = nil ;
161207 NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath: tessdataFolderPathFromTheBundle error: &error];
162- [[contentsOfTessdataFromTheBundle should ] haveCountOfAtLeast: 1 ];
163- if (error != nil ) {
164- NSLog (@" Error getting the content of the Tessdata folder from the app bundle: %@ " , error);
165- }
208+ NSAssert (contentsOfTessdataFromTheBundle.count >= 1 , @" Error! Tessdata folder is empty" );
209+ NSAssert (error == nil , @" Error getting the content of the Tessdata folder from the app bundle: %@ " , error);
166210
167211 NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath: [cachesTessDataPath stringByAppendingPathComponent: tessdataFolderName] error: &error];
168212 [[contentsOfTheTessdataPathFolder should ] haveCountOfAtLeast: 1 ];
@@ -181,7 +225,7 @@ - (BOOL)resetEngine;
181225 it (@" Should simple init, download rus language files and reinitialize tess with them" , ^{
182226 // proof Caches folder is empty
183227 BOOL folderExists = [fileManager fileExistsAtPath: cachesTessDataPath];
184- [[ theValue (folderExists) should ] beNo ] ;
228+ NSAssert (folderExists == NO , @" Error! Tessdata folder is already here: %@ " , cachesTessDataPath) ;
185229
186230 G8Tesseract *tesseract = [[G8Tesseract alloc ] initWithLanguage: kG8Languages
187231 configDictionary: nil
@@ -229,7 +273,7 @@ - (BOOL)resetEngine;
229273 });
230274
231275 it (@" Should initialize with config dictionary" , ^{
232-
276+
233277 G8Tesseract *tesseract = [[G8Tesseract alloc ] initWithLanguage: kG8Languages
234278 configDictionary: @{
235279 kG8ParamTessdataManagerDebugLevel : @" 1" ,
@@ -268,8 +312,8 @@ - (BOOL)resetEngine;
268312
269313 it (@" Should initialize with 2 config files" , ^{
270314
271- [[[ fileManager attributesOfItemAtPath: debugConfigsFilePathFromTheCaches error: nil ] should ] beNil ] ;
272- [[[ fileManager attributesOfItemAtPath: recognitionConfigsFilePathFromTheCaches error: nil ] should ] beNil ] ;
315+ NSAssert ([ fileManager attributesOfItemAtPath: debugConfigsFilePathFromTheCaches error: nil ] == nil , @" Error! %@ is already here! " , debugConfigsFilePathFromTheCaches) ;
316+ NSAssert ([ fileManager attributesOfItemAtPath: recognitionConfigsFilePathFromTheCaches error: nil ] == nil , @" Error! %@ cannot is already here! " , recognitionConfigsFilePathFromTheCaches) ;
273317
274318 G8Tesseract *tesseract = [[G8Tesseract alloc ] initWithLanguage: kG8Languages
275319 configDictionary: nil
@@ -413,20 +457,7 @@ - (BOOL)resetEngine;
413457 });
414458
415459 afterEach (^{
416- // NSLog(@"Removing previous tessdata folder from Caches folder");
417- NSError *error = nil ;
418- BOOL fileIsRemoved = [fileManager removeItemAtPath: cachesTessDataPath error: &error];
419- if (error != nil ) {
420- NSLog (@" Error deleting tessdata folder from the Caches folder: %@ " , error);
421- }
422- NSAssert (fileIsRemoved == YES , @" Error cleaning tessdata from the Caches folder" );
423-
424- // check tessdata folder was deleted
425- NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath: cachesPath error: &error];
426- if (error != nil ) {
427- NSLog (@" Error getting the contents of the Caches folder: %@ " , error);
428- }
429- NSAssert ([cachesContent containsObject: tessdataPath] == NO , @" Assert! Tessdata path was not removed from the Caches folder" );
460+ cleanTessdataFolder ();
430461 });
431462 });
432463});
0 commit comments