Skip to content

Commit 1f8faed

Browse files
author
Patrick Nollet
committed
Add test for new initializer
1 parent 0cf5a0a commit 1f8faed

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

TestsProject/TestsProjectTests/InitializationTests.m

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ - (BOOL)resetEngine;
2424

2525
NSFileManager *fileManager = [NSFileManager defaultManager];
2626
NSString *resourcePath = [NSBundle mainBundle].resourcePath;
27+
NSString *customDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"testDirectory"];
2728
NSString *tessdataFolderName = @"tessdata";
2829
NSString *tessdataFolderPathFromTheBundle = [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"];
2930
NSString *debugConfigsFileName = @"debugConfigs.txt";
@@ -85,6 +86,120 @@ - (BOOL)resetEngine;
8586
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:nil];
8687
});
8788
});
89+
90+
NSString *customTessDataPath = [customDirectoryPath stringByAppendingPathComponent:@"tessdata"];
91+
void(^cleanCustomTessdataFolder)() = ^{
92+
NSError *error = nil;
93+
BOOL fileIsRemoved = [fileManager removeItemAtPath:customTessDataPath error:&error];
94+
if (error != nil) {
95+
NSLog(@"Error deleting tessdata folder from the custom directory: %@", error);
96+
}
97+
NSAssert(fileIsRemoved == YES, @"Error cleaning tessdata from the custom directory");
98+
99+
// check tessdata folder was deleted
100+
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:customDirectoryPath error:&error];
101+
if (error != nil) {
102+
NSLog(@"Error getting the contents of the custom directory: %@", error);
103+
}
104+
NSAssert([directoryContent containsObject:customDirectoryPath] == NO, @"Assert! Tessdata path was not removed from the Caches folder");
105+
};
106+
107+
BOOL(^copyDataToCustomDirectory)() = ^{
108+
{
109+
// Useful paths
110+
NSString *tessdataFolderName = @"tessdata";
111+
NSString *tessdataPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:tessdataFolderName];
112+
NSString *destinationPath = customTessDataPath;
113+
NSLog(@"Tesseract destination path: %@", destinationPath);
114+
115+
if ([fileManager fileExistsAtPath:destinationPath] == NO) {
116+
NSError *error = nil;
117+
BOOL res = [fileManager createDirectoryAtPath:destinationPath withIntermediateDirectories:YES attributes:nil error:&error];
118+
if (res == NO) {
119+
NSLog(@"Error creating folder %@: %@", destinationPath, error);
120+
return NO;
121+
}
122+
}
123+
124+
BOOL result = YES;
125+
NSError *error = nil;
126+
NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataPath error:&error];
127+
if (files == nil) {
128+
NSLog(@"ERROR! %@", error.description);
129+
result = NO;
130+
} else {
131+
for (NSString *filename in files) {
132+
133+
NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filename];
134+
if (![fileManager fileExistsAtPath:destinationFileName]) {
135+
136+
NSString *filePath = [tessdataPath stringByAppendingPathComponent:filename];
137+
138+
// delete broken symlinks first
139+
[fileManager removeItemAtPath:destinationFileName error:&error];
140+
141+
// than recreate it
142+
error = nil; // don't care about previous error, that can happens if we tried to remove a symlink, which doesn't exist
143+
BOOL res = [fileManager createSymbolicLinkAtPath:destinationFileName
144+
withDestinationPath:filePath
145+
error:&error];
146+
if (res == NO) {
147+
NSLog(@"Error creating symlink %@: %@", destinationPath, error);
148+
result = NO;
149+
}
150+
}
151+
}
152+
}
153+
return result;
154+
}
155+
};
156+
157+
context(@"initialize with absoluteDataPath", ^{
158+
159+
it(@"Should initialize simple", ^{
160+
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages configDictionary:nil configFileNames:nil absoluteDataPath:nil engineMode:G8OCREngineModeTesseractOnly copyFilesFromResources:NO];
161+
[[tesseract shouldNot] beNil];
162+
163+
[[tesseract.absoluteDataPath should] equal:resourcePath];
164+
165+
tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages configDictionary:nil configFileNames:nil absoluteDataPath:customDirectoryPath engineMode:G8OCREngineModeTesseractOnly copyFilesFromResources:NO];
166+
[[tesseract should] beNil];
167+
168+
BOOL isDirectory = NO;
169+
[[theValue([fileManager fileExistsAtPath:customTessDataPath isDirectory:&isDirectory]) should] beNo];
170+
[[theValue(isDirectory) should] beNo];
171+
172+
copyDataToCustomDirectory();
173+
174+
isDirectory = NO;
175+
[[theValue([fileManager fileExistsAtPath:customTessDataPath isDirectory:&isDirectory]) should] beYes];
176+
[[theValue(isDirectory) should] beYes];
177+
178+
tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages configDictionary:nil configFileNames:nil absoluteDataPath:customDirectoryPath engineMode:G8OCREngineModeTesseractOnly copyFilesFromResources:NO];
179+
[[tesseract shouldNot] beNil];
180+
181+
[[tesseract.absoluteDataPath should] equal:customDirectoryPath];
182+
183+
isDirectory = NO;
184+
[[theValue([fileManager fileExistsAtPath:customTessDataPath isDirectory:&isDirectory]) should] beYes];
185+
[[theValue(isDirectory) should] beYes];
186+
187+
cleanCustomTessdataFolder();
188+
189+
tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages configDictionary:nil configFileNames:nil absoluteDataPath:customDirectoryPath engineMode:G8OCREngineModeTesseractOnly copyFilesFromResources:YES];
190+
[[tesseract shouldNot] beNil];
191+
192+
[[tesseract.absoluteDataPath should] equal:customDirectoryPath];
193+
194+
isDirectory = NO;
195+
[[theValue([fileManager fileExistsAtPath:[customDirectoryPath stringByAppendingPathComponent:@"tessdata"] isDirectory:&isDirectory]) should] beYes];
196+
[[theValue(isDirectory) should] beYes];
197+
198+
cleanCustomTessdataFolder();
199+
200+
});
201+
202+
});
88203

89204
context(@"nil cachesRelatedDataPath", ^{
90205

0 commit comments

Comments
 (0)