Skip to content

Commit 8460ae2

Browse files
author
Kirill Makankov
committed
1. more unit tests.
2. fixed unit tests 3. removed all Documets
1 parent 0e4b705 commit 8460ae2

File tree

3 files changed

+67
-49
lines changed

3 files changed

+67
-49
lines changed
-16 Bytes
Binary file not shown.

TesseractOCR/G8Tesseract.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ - (id)initWithLanguage:(NSString *)language
102102
_monitor->cancel_this = (__bridge void*)self;
103103

104104
if (_absoluteDataPath != nil) {
105-
// config Tesseract to search trainedData in tessdata folder of the Documents folder];
105+
// config Tesseract to search trainedData in tessdata folder of the Caches folder
106106
NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
107107
NSString *cachesPath = cachesPaths.firstObject;
108108

109109
_absoluteDataPath = [cachesPath stringByAppendingPathComponent:_absoluteDataPath].copy;
110110

111-
[self moveTessdataToDocumentsDirectoryIfNecessary];
111+
[self moveTessdataToCachesDirectoryIfNecessary];
112112
}
113113
else {
114114
// config Tesseract to search trainedData in tessdata folder of the application bundle];
@@ -190,7 +190,7 @@ - (BOOL)resetEngine
190190
return isInitDone;
191191
}
192192

193-
- (BOOL)moveTessdataToDocumentsDirectoryIfNecessary
193+
- (BOOL)moveTessdataToCachesDirectoryIfNecessary
194194
{
195195
NSFileManager *fileManager = [NSFileManager defaultManager];
196196

TestsProject/TestsProjectTests/InitializationTests.m

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@
2020
return [NSFileManager defaultManager];
2121
});
2222

23+
let(resourcePath, ^id{
24+
return [[NSBundle bundleForClass:G8Tesseract.class].resourcePath stringByAppendingString:@"/"];
25+
});
26+
27+
let(tessdataFolderName, ^id{
28+
return @"tessdata";
29+
});
30+
31+
let(tessdataFolderPathFromTheBundle, ^id{
32+
return [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"];
33+
});
34+
2335
context(@"nil dataPath", ^{
2436

2537
it(@"", ^{
2638
[[fileManager shouldNot] receive:@selector(createSymbolicLinkAtPath:withDestinationPath:error:)];
2739
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
2840
[[tesseract shouldNot] beNil];
41+
42+
[[tesseract.absoluteDataPath should] equal:resourcePath];
2943
});
3044
});
3145

@@ -34,25 +48,43 @@
3448
NSString *cachesPath = cachesPaths.firstObject;
3549
return cachesPath;
3650
});
51+
52+
let(tessdataPath, ^id{
53+
return @"foo/bar";
54+
});
55+
56+
let(cachesTessDataPath, ^id{
57+
return [cashesPath stringByAppendingPathComponent:tessdataPath];
58+
});
3759

3860
context(@"not nil dataPath", ^{
3961

40-
let(tessdataPath, ^id{
41-
return @"tes";
42-
});
62+
// helper
63+
BOOL (^doFoldersContainTheSameElements)(void) = ^(void){
64+
NSError *error = nil;
65+
NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:tessdataFolderPathFromTheBundle error:&error];
66+
[[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1];
67+
if (error != nil) {
68+
NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error);
69+
}
70+
71+
NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] error:&error];
72+
[[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1];
73+
if (error != nil) {
74+
NSLog(@"Error getting the content of the Tessdata folder from the Caches folder: %@", error);
75+
}
76+
77+
return [contentsOfTheTessdataPathFolder isEqualToArray:contentsOfTessdataFromTheBundle];
78+
};
4379

4480
let(tessdataFolderName, ^id{
4581
return @"tessdata";
4682
});
4783

48-
let(bundle, ^id{
49-
return [NSBundle bundleForClass:G8Tesseract.class];
50-
});
51-
52-
it(@"no tessdata folder in the Documents yet", ^{
84+
it(@"no tessdata folder in the Caches yet", ^{
5385

54-
// proof Documents folder is empty
55-
BOOL folderExists = [fileManager fileExistsAtPath:[cashesPath stringByAppendingPathComponent:tessdataFolderName]];
86+
// proof Caches folder is empty
87+
BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath];
5688
[[theValue(folderExists) should] equal:theValue(NO)];
5789

5890
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
@@ -62,36 +94,20 @@
6294
engineMode:G8OCREngineModeTesseractOnly];
6395
[[tesseract shouldNot] beNil];
6496

65-
NSError *error = nil;
66-
NSString *path = [[NSBundle bundleForClass:[G8Tesseract class]].bundlePath stringByAppendingPathComponent:@"tessdata"];
67-
NSArray *contentsOfTessdataFromTheBundle = [fileManager contentsOfDirectoryAtPath:path error:&error];
68-
[[contentsOfTessdataFromTheBundle should] haveCountOfAtLeast:1];
69-
if (error != nil) {
70-
NSLog(@"Error getting the content of the Tessdata folder from the app bundle: %@", error);
71-
}
72-
73-
NSArray *contentsOfTheTessdataPathFolder = [fileManager contentsOfDirectoryAtPath:[tesseract.absoluteDataPath stringByAppendingPathComponent:@"tessdata"] error:&error];
74-
[[contentsOfTheTessdataPathFolder should] haveCountOfAtLeast:1];
75-
if (error != nil) {
76-
NSLog(@"Error getting the content of the Tessdata folder from the Documents folder: %@", error);
77-
}
97+
[[tesseract.absoluteDataPath should] equal:cachesTessDataPath];
7898

79-
[[contentsOfTessdataFromTheBundle should] containObjectsInArray:contentsOfTheTessdataPathFolder];
99+
[[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)];
80100
});
81101

82-
it(@"tessdata are already in the Documents", ^{
102+
it(@"tessdata are already in the Caches", ^{
83103
// helper
84-
BOOL (^moveTessdataToDocumentsDirectoryIfNecessary)(NSString *dataPath) = ^(NSString *dataPath){
104+
BOOL (^moveTessdataToCachesDirectoryIfNecessary)(NSString *dataPath) = ^(NSString *dataPath){
85105

86106
NSFileManager *fileManager = [NSFileManager defaultManager];
87107

88108
// Useful paths
89-
NSString *tessdataFolderName = @"tessdata";
90-
NSString *tessdataDestinationPath = [[NSBundle bundleForClass:G8Tesseract.class].resourcePath
91-
stringByAppendingPathComponent:tessdataFolderName];
92-
NSString *destinationPath = [[[NSBundle bundleForClass:G8Tesseract.class].resourcePath
93-
stringByAppendingPathComponent:dataPath]
94-
stringByAppendingPathComponent:tessdataFolderName];
109+
NSString *tessdataSourcePath = [resourcePath stringByAppendingPathComponent:tessdataFolderName];
110+
NSString *destinationPath = [[cashesPath stringByAppendingPathComponent:dataPath] stringByAppendingPathComponent:tessdataFolderName];
95111

96112
if ([fileManager fileExistsAtPath:destinationPath] == NO) {
97113
NSError *error = nil;
@@ -103,29 +119,29 @@
103119
}
104120

105121
NSError *error = nil;
106-
NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataDestinationPath error:&error];
122+
NSArray *files = [fileManager contentsOfDirectoryAtPath:tessdataSourcePath error:&error];
107123
if (error != nil) {
108124
NSLog(@"Test: ERROR! %@", error.description);
109125
return NO;
110126
}
111127
for (NSString *filename in files) {
112128

113129
NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filename];
114-
if ([fileManager fileExistsAtPath:destinationFileName]) {
130+
if (![fileManager fileExistsAtPath:destinationFileName]) {
115131

116-
NSString *filePath = [tessdataDestinationPath stringByAppendingPathComponent:filename];
132+
NSString *sourceFilePath = [tessdataSourcePath stringByAppendingPathComponent:filename];
117133
//NSLog(@"found %@", filePath);
118134
//NSLog(@"symlink in %@", destinationFileName);
119135

120136
BOOL res = [fileManager createSymbolicLinkAtPath:destinationFileName
121-
withDestinationPath:filePath
137+
withDestinationPath:sourceFilePath
122138
error:&error];
123139
if (res == NO) {
124140
NSLog(@"Test: The result of createSymbolicLinkAtPath == NO");
125141
return NO;
126142
}
127143
if (error != nil) {
128-
NSLog(@"Test: Error creating symlink %@: %@", filePath, error);
144+
NSLog(@"Test: Error creating symlink %@: %@", sourceFilePath, error);
129145
return NO;
130146
}
131147
}
@@ -134,33 +150,35 @@
134150
return YES;
135151
};
136152

137-
// copy files to the Documents dir first
138-
BOOL res = moveTessdataToDocumentsDirectoryIfNecessary(tessdataPath);
153+
// copy files to the Caches dir first
154+
BOOL res = moveTessdataToCachesDirectoryIfNecessary(tessdataPath);
139155
[[theValue(res) should] equal:theValue(YES)];
156+
157+
[[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)];
140158

141159
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
142160
configDictionary:nil
143161
configFileNames:nil
144162
cachesRelatedDataPath:tessdataPath
145163
engineMode:G8OCREngineModeTesseractOnly];
146164
[[tesseract shouldNot] beNil];
165+
[[tesseract.absoluteDataPath should] equal:cachesTessDataPath];
147166
});
148167

149168
afterEach(^{
150-
NSLog(@"Removing previous tessdata folder from Documents");
169+
NSLog(@"Removing previous tessdata folder from Caches folder");
151170
NSError *error = nil;
152-
NSString *path = [cashesPath stringByAppendingPathComponent:tessdataPath];
153-
BOOL fileIsRemoved = [fileManager removeItemAtPath:path error:&error];
171+
BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error];
154172
[[theValue(fileIsRemoved) should] equal:theValue(YES)];
155173
if (error != nil) {
156-
NSLog(@"Error deleting tessdata folder from the Documents: %@", error);
174+
NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error);
157175
}
158176

159177
// check tessdata folder was deleted
160-
NSArray *documentsContent = [fileManager contentsOfDirectoryAtPath:cashesPath error:&error];
161-
[[documentsContent shouldNot] contain:tessdataPath];
178+
NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cashesPath error:&error];
179+
[[cachesContent shouldNot] contain:tessdataPath];
162180
if (error != nil) {
163-
NSLog(@"Error getting the contents of the Documents folder: %@", error);
181+
NSLog(@"Error getting the contents of the Caches folder: %@", error);
164182
}
165183
});
166184
});

0 commit comments

Comments
 (0)