|
12 | 12 | #import <Kiwi/Kiwi.h> |
13 | 13 | #import "Defaults.h" |
14 | 14 |
|
| 15 | +@interface G8Tesseract (Tests) |
| 16 | +- (BOOL)configEngine; |
| 17 | +- (BOOL)resetEngine; |
| 18 | +@end |
| 19 | + |
15 | 20 | SPEC_BEGIN(TesseractInitialization) |
16 | 21 |
|
17 | 22 | describe(@"Tesseract initialization", ^{ |
|
43 | 48 | [[recognizedText should] equal:@"1234567890\n\n"]; |
44 | 49 | }; |
45 | 50 |
|
| 51 | + context(@"Should check common function", ^{ |
| 52 | + |
| 53 | + it(@"Should check version", ^{ |
| 54 | + [[[G8Tesseract version] should] equal:@"3.03"]; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
46 | 58 | context(@"nil cachesRelatedDataPath", ^{ |
47 | 59 |
|
48 | 60 | it(@"Should initialize simple", ^{ |
|
51 | 63 | [[tesseract shouldNot] beNil]; |
52 | 64 |
|
53 | 65 | [[tesseract.absoluteDataPath should] equal:resourcePath]; |
| 66 | + |
| 67 | + tesseract = [G8Tesseract alloc]; |
| 68 | + [[tesseract shouldNot] beNil]; |
| 69 | + NSAssert([tesseract respondsToSelector:@selector(configEngine)] == YES, @"Error! G8Tesseract instance does not contain configEngine selector"); |
| 70 | + [[tesseract should] receive:@selector(configEngine) andReturn:theValue(NO)]; |
| 71 | + tesseract = [tesseract init]; |
| 72 | + |
| 73 | + [[tesseract should] beNil]; |
| 74 | + |
| 75 | + tesseract = [[G8Tesseract alloc] init]; |
| 76 | + NSAssert([tesseract respondsToSelector:@selector(resetEngine)] == YES, @"Error! G8Tesseract instance does not contain resetEngine selector"); |
| 77 | + [[tesseract should] receive:@selector(configEngine) andReturn:theValue(NO)]; |
| 78 | + [[theValue([tesseract resetEngine]) should] beNo]; |
| 79 | + }); |
| 80 | + |
| 81 | + it(@"Should initialize simple with engine mode", ^{ |
| 82 | + [[fileManager shouldNot] receive:@selector(createSymbolicLinkAtPath:withDestinationPath:error:)]; |
| 83 | + G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages engineMode:G8OCREngineModeTesseractOnly]; |
| 84 | + [[tesseract shouldNot] beNil]; |
| 85 | + |
| 86 | + [[tesseract.absoluteDataPath should] equal:resourcePath]; |
54 | 87 | }); |
55 | 88 |
|
56 | 89 | NSString *debugConfigsFilePathFromTheBundle = [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName]; |
|
114 | 147 | checkVariablesAreSetForTesseract(tesseract); |
115 | 148 | }); |
116 | 149 | }); |
117 | | - |
| 150 | + |
118 | 151 | NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); |
119 | 152 | NSString *cachesPath = cachesPaths.firstObject; |
120 | 153 | NSString *tessdataPath = @"foo/bar"; |
121 | 154 | 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); |
122 | 191 |
|
| 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 | + |
123 | 202 | context(@"not nil cachesRelatedDataPath", ^{ |
124 | 203 |
|
125 | 204 | // helper |
126 | 205 | BOOL (^doFoldersContainTheSameElements)(void) = ^(void){ |
127 | 206 | NSError *error = nil; |
128 | 207 | NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:tessdataFolderPathFromTheBundle error:&error]; |
129 | | - [[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1]; |
130 | | - if (error != nil) { |
131 | | - NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error); |
132 | | - } |
| 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); |
133 | 210 |
|
134 | 211 | NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] error:&error]; |
135 | 212 | [[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1]; |
|
148 | 225 | it(@"Should simple init, download rus language files and reinitialize tess with them", ^{ |
149 | 226 | // proof Caches folder is empty |
150 | 227 | BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath]; |
151 | | - [[theValue(folderExists) should] beNo]; |
| 228 | + NSAssert(folderExists == NO, @"Error! Tessdata folder is already here: %@", cachesTessDataPath); |
152 | 229 |
|
153 | 230 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
154 | 231 | configDictionary:nil |
|
196 | 273 | }); |
197 | 274 |
|
198 | 275 | it(@"Should initialize with config dictionary", ^{ |
199 | | - |
| 276 | + |
200 | 277 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
201 | 278 | configDictionary:@{ |
202 | 279 | kG8ParamTessdataManagerDebugLevel : @"1", |
|
235 | 312 |
|
236 | 313 | it(@"Should initialize with 2 config files", ^{ |
237 | 314 |
|
238 | | - [[[fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:nil] should] beNil]; |
239 | | - [[[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); |
240 | 317 |
|
241 | 318 | G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
242 | 319 | configDictionary:nil |
|
380 | 457 | }); |
381 | 458 |
|
382 | 459 | afterEach(^{ |
383 | | - //NSLog(@"Removing previous tessdata folder from Caches folder"); |
384 | | - NSError *error = nil; |
385 | | - BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error]; |
386 | | - if (error != nil) { |
387 | | - NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error); |
388 | | - } |
389 | | - NSAssert(fileIsRemoved == YES, @"Error cleaning tessdata from the Caches folder"); |
390 | | - |
391 | | - // check tessdata folder was deleted |
392 | | - NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cachesPath error:&error]; |
393 | | - if (error != nil) { |
394 | | - NSLog(@"Error getting the contents of the Caches folder: %@", error); |
395 | | - } |
396 | | - NSAssert([cachesContent containsObject:tessdataPath] == NO, @"Assert! Tessdata path was not removed from the Caches folder"); |
| 460 | + cleanTessdataFolder(); |
397 | 461 | }); |
398 | 462 | }); |
399 | 463 | }); |
|
0 commit comments