Skip to content

Commit f187787

Browse files
author
Kirill Makankov
committed
1. No tests for tests. NSAsserts instead.
2. no more let. variables instead. 3. it should everywhere.
1 parent 2a45ff3 commit f187787

File tree

1 file changed

+41
-83
lines changed

1 file changed

+41
-83
lines changed

TestsProject/TestsProjectTests/InitializationTests.m

Lines changed: 41 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,13 @@
1616

1717
describe(@"Tesseract initialization", ^{
1818

19-
let(fileManager, ^id{
20-
return [NSFileManager defaultManager];
21-
});
22-
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-
35-
let(debugConfigsFileName, ^id{
36-
return @"debugConfigs.txt";
37-
});
38-
39-
let(recognitionConfigsFileName, ^id{
40-
return @"recognitionConfis.txt";
41-
});
42-
43-
let(tessConfigsFolderName, ^id{
44-
return @"tessconfigs";
45-
});
19+
NSFileManager *fileManager = [NSFileManager defaultManager];
20+
NSString *resourcePath = [[NSBundle bundleForClass:G8Tesseract.class].resourcePath stringByAppendingString:@"/"];
21+
NSString *tessdataFolderName = @"tessdata";
22+
NSString *tessdataFolderPathFromTheBundle = [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"];
23+
NSString *debugConfigsFileName = @"debugConfigs.txt";
24+
NSString *recognitionConfigsFileName = @"recognitionConfis.txt";
25+
NSString *tessConfigsFolderName = @"tessconfigs";
4626

4727
void (^checkVariablesAreSetForTesseract)(G8Tesseract *tesseract) = ^(G8Tesseract *tesseract){
4828
[[[tesseract variableValueForKey:kG8ParamTessdataManagerDebugLevel] should] equal:@"1"];
@@ -52,36 +32,31 @@
5232
[[[tesseract variableValueForKey:kG8ParamUserPatternsSuffix] should] equal:@"user-patterns"];
5333
};
5434

55-
context(@"nil dataPath", ^{
35+
context(@"nil cachesRelatedDataPath", ^{
5636

57-
it(@"simple", ^{
37+
it(@"Should initialize simple", ^{
5838
[[fileManager shouldNot] receive:@selector(createSymbolicLinkAtPath:withDestinationPath:error:)];
5939
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
6040
[[tesseract shouldNot] beNil];
6141

6242
[[tesseract.absoluteDataPath should] equal:resourcePath];
6343
});
6444

65-
let(debugConfigsFilePathFromTheBundle, ^id{
66-
return [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName];
67-
});
45+
NSString *debugConfigsFilePathFromTheBundle = [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName];
46+
NSString *recognitionConfigsFilePathFromTheBundle = [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:recognitionConfigsFileName];
6847

69-
let(recognitionConfigsFilePathFromTheBundle, ^id{
70-
return [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:recognitionConfigsFileName];
71-
});
72-
73-
it(@"config file path", ^{
48+
it(@"Should initialize with config file path", ^{
7449
[[theBlock(^{
7550
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
7651
configDictionary:nil
77-
configFileNames:debugConfigsFilePathFromTheBundle
52+
configFileNames:(NSArray*)debugConfigsFilePathFromTheBundle
7853
cachesRelatedDataPath:nil
7954
engineMode:G8OCREngineModeTesseractOnly];
8055
[tesseract recognize];
8156
}) should] raise];
8257

83-
[[theValue([fileManager fileExistsAtPath:debugConfigsFilePathFromTheBundle]) should] equal:theValue(YES)];
84-
[[theValue([fileManager fileExistsAtPath:recognitionConfigsFilePathFromTheBundle]) should] equal:theValue(YES)];
58+
[[theValue([fileManager fileExistsAtPath:debugConfigsFilePathFromTheBundle]) should] beYes];
59+
[[theValue([fileManager fileExistsAtPath:recognitionConfigsFilePathFromTheBundle]) should] beYes];
8560

8661
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
8762
configDictionary:nil
@@ -94,7 +69,7 @@
9469
checkVariablesAreSetForTesseract(tesseract);
9570
});
9671

97-
it(@"config dictionary", ^{
72+
it(@"Should initialize with config dictionary", ^{
9873

9974
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
10075
configDictionary:@{
@@ -113,7 +88,7 @@
11388
checkVariablesAreSetForTesseract(tesseract);
11489
});
11590

116-
it(@"config dictionary and a file", ^{
91+
it(@"Should initialize with config dictionary and a file", ^{
11792

11893
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
11994
configDictionary:@{
@@ -129,21 +104,12 @@
129104
});
130105
});
131106

132-
let(cashesPath, ^id{
133-
NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
134-
NSString *cachesPath = cachesPaths.firstObject;
135-
return cachesPath;
136-
});
137-
138-
let(tessdataPath, ^id{
139-
return @"foo/bar";
140-
});
141-
142-
let(cachesTessDataPath, ^id{
143-
return [cashesPath stringByAppendingPathComponent:tessdataPath];
144-
});
107+
NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
108+
NSString *cachesPath = cachesPaths.firstObject;
109+
NSString *tessdataPath = @"foo/bar";
110+
NSString *cachesTessDataPath = [cachesPath stringByAppendingPathComponent:tessdataPath];
145111

146-
context(@"not nil dataPath", ^{
112+
context(@"not nil cachesRelatedDataPath", ^{
147113

148114
// helper
149115
BOOL (^doFoldersContainTheSameElements)(void) = ^(void){
@@ -163,23 +129,15 @@
163129
return [contentsOfTheTessdataPathFolder isEqualToArray:contentsOfTessdataFromTheBundle];
164130
};
165131

166-
let(tessdataFolderName, ^id{
167-
return @"tessdata";
168-
});
169-
170-
let(debugConfigsFilePathFromTheCaches, ^id{
171-
return [[[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName];
172-
});
173-
let(recognitionConfigsFilePathFromTheCaches, ^id{
174-
return [[[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:recognitionConfigsFileName];
175-
});
176-
132+
NSString *debugConfigsFilePathFromTheCaches = [[[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName];
133+
NSString *recognitionConfigsFilePathFromTheCaches = [[[cachesTessDataPath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:recognitionConfigsFileName];
134+
177135
context(@"no tessdata folder in the Caches yet", ^{
178136

179-
it(@"simple init", ^{
137+
it(@"Should simple init", ^{
180138
// proof Caches folder is empty
181139
BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath];
182-
[[theValue(folderExists) should] equal:theValue(NO)];
140+
[[theValue(folderExists) should] beNo];
183141

184142
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
185143
configDictionary:nil
@@ -190,10 +148,10 @@
190148

191149
[[tesseract.absoluteDataPath should] equal:cachesTessDataPath];
192150

193-
[[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)];
151+
[[theValue(doFoldersContainTheSameElements()) should] beYes];
194152
});
195153

196-
it(@"config dictionary", ^{
154+
it(@"Should initialize with config dictionary", ^{
197155

198156
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
199157
configDictionary:@{
@@ -212,7 +170,7 @@
212170
checkVariablesAreSetForTesseract(tesseract);
213171
});
214172

215-
it(@"config dictionary and a file", ^{
173+
it(@"Should initialize config dictionary and a file", ^{
216174

217175
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
218176
configDictionary:@{
@@ -228,7 +186,7 @@
228186
});
229187
});
230188

231-
it(@"config file path, while there is no tessdata folder yet", ^{
189+
it(@"Should initialize with config file path, while there is no tessdata folder yet", ^{
232190

233191
NSLog(@"debugConfigsFilePathFromTheCaches: %@", debugConfigsFilePathFromTheCaches);
234192
NSLog(@"recognitionConfigsFilePathFromTheCaches: %@", recognitionConfigsFilePathFromTheCaches);
@@ -255,7 +213,7 @@
255213

256214
// Useful paths
257215
NSString *tessdataSourcePath = [resourcePath stringByAppendingPathComponent:tessdataFolderName];
258-
NSString *destinationPath = [[cashesPath stringByAppendingPathComponent:dataPath] stringByAppendingPathComponent:tessdataFolderName];
216+
NSString *destinationPath = [[cachesPath stringByAppendingPathComponent:dataPath] stringByAppendingPathComponent:tessdataFolderName];
259217

260218
if ([fileManager fileExistsAtPath:destinationPath] == NO) {
261219
NSError *error = nil;
@@ -301,12 +259,12 @@
301259
beforeEach(^{
302260
// copy files to the Caches dir first
303261
BOOL res = moveTessdataToCachesDirectoryIfNecessary(tessdataPath);
304-
[[theValue(res) should] equal:theValue(YES)];
262+
NSAssert(res == YES, @"Error copying tessadata from the bundle to the Caches folder");
305263

306-
[[theValue(doFoldersContainTheSameElements()) should] equal:theValue(YES)];
264+
[[theValue(doFoldersContainTheSameElements()) should] beYes];
307265
});
308266

309-
it(@"no configs", ^{
267+
it(@"Should initialize with no configs", ^{
310268
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
311269
configDictionary:nil
312270
configFileNames:nil
@@ -317,7 +275,7 @@
317275
});
318276

319277

320-
it(@"config file path", ^{
278+
it(@"Should initialize with config file path", ^{
321279

322280
NSError *error = nil;
323281
[[[fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:&error] shouldNot] beNil];
@@ -340,7 +298,7 @@
340298
checkVariablesAreSetForTesseract(tesseract);
341299
});
342300

343-
it(@"config dictionary", ^{
301+
it(@"Should initialize with config dictionary", ^{
344302

345303
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
346304
configDictionary:@{
@@ -359,7 +317,7 @@
359317
checkVariablesAreSetForTesseract(tesseract);
360318
});
361319

362-
it(@"config dictionary and a file", ^{
320+
it(@"Should initialize with config dictionary and a file", ^{
363321

364322
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
365323
configDictionary:@{
@@ -379,17 +337,17 @@
379337
//NSLog(@"Removing previous tessdata folder from Caches folder");
380338
NSError *error = nil;
381339
BOOL fileIsRemoved = [fileManager removeItemAtPath:cachesTessDataPath error:&error];
382-
[[theValue(fileIsRemoved) should] equal:theValue(YES)];
383340
if (error != nil) {
384341
NSLog(@"Error deleting tessdata folder from the Caches folder: %@", error);
385342
}
343+
NSAssert(fileIsRemoved == YES, @"Error cleaning tessdata from the Caches folder");
386344

387345
// check tessdata folder was deleted
388-
NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cashesPath error:&error];
389-
[[cachesContent shouldNot] contain:tessdataPath];
346+
NSArray *cachesContent = [fileManager contentsOfDirectoryAtPath:cachesPath error:&error];
390347
if (error != nil) {
391348
NSLog(@"Error getting the contents of the Caches folder: %@", error);
392349
}
350+
NSAssert([cachesContent containsObject:tessdataPath] == NO, @"Assert! Tessdata path was not removed from the Caches folder");
393351
});
394352
});
395353
});

0 commit comments

Comments
 (0)