Skip to content

Commit 92ef3d5

Browse files
committed
Merge pull request #135 from ws233/more_unittests
Added unit tests for G8RecognizedBlock.
2 parents c258b2d + bc61350 commit 92ef3d5

File tree

3 files changed

+126
-19
lines changed

3 files changed

+126
-19
lines changed

TesseractOCR/G8RecognizedBlock.m

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
@interface G8RecognizedBlock ()
1313

14-
@property (nonatomic, copy) NSString *text;
15-
@property (nonatomic, assign) CGRect boundingBox;
16-
@property (nonatomic, assign) CGFloat confidence;
17-
@property (nonatomic, assign) G8PageIteratorLevel level;
18-
1914
@end
2015

2116
@implementation G8RecognizedBlock
@@ -63,22 +58,23 @@ - (BOOL)isEqual:(id)other
6358
if (other == self) {
6459
return YES;
6560
}
66-
else if ([super isEqual:other] == NO) {
67-
return NO;
68-
}
69-
else if ([other isKindOfClass:[self class]] == NO) {
70-
return NO;
71-
}
72-
else {
61+
else if ([other isKindOfClass:[self class]] == YES) {
62+
7363
G8RecognizedBlock *otherBlock = other;
74-
75-
BOOL result = self.level == otherBlock.level;
76-
result = result && ABS(self.confidence - otherBlock.confidence) < FLT_EPSILON;
77-
result = result && CGRectEqualToRect(self.boundingBox, otherBlock.boundingBox);
78-
result = result && (self.text == otherBlock.text || [self.text isEqual:otherBlock.text]);
79-
80-
return result;
64+
if (self.hash == otherBlock.hash) {
65+
66+
if (self.level == otherBlock.level &&
67+
ABS(self.confidence - otherBlock.confidence) < FLT_EPSILON &&
68+
CGRectEqualToRect(self.boundingBox, otherBlock.boundingBox) &&
69+
(self.text == otherBlock.text || [self.text isEqualToString:otherBlock.text])
70+
)
71+
{
72+
return YES;
73+
}
74+
}
8175
}
76+
77+
return NO;
8278
}
8379

8480
- (NSUInteger)hash

TestsProject/TestsProject.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
732C54761A514DA6000322DA /* InitializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 732C54751A514DA5000322DA /* InitializationTests.m */; };
2929
732C54791A5288CC000322DA /* Defaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 732C54781A5288CC000322DA /* Defaults.m */; };
3030
733DD5CD1A5E7CE50042374D /* TesseractOCR.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 733DD5CC1A5E7CE50042374D /* TesseractOCR.framework */; };
31+
733DD5E71A64300E0042374D /* RecognizedBlock.m in Sources */ = {isa = PBXBuildFile; fileRef = 733DD5E61A64300E0042374D /* RecognizedBlock.m */; };
3132
736EFF271A5882730031B432 /* tessdata-rus in Resources */ = {isa = PBXBuildFile; fileRef = 736EFF231A5872CA0031B432 /* tessdata-rus */; };
3233
73BE4C311A5B404C002C15F1 /* well_scaned_page.hOCR in Resources */ = {isa = PBXBuildFile; fileRef = 73BE4C301A5B404C002C15F1 /* well_scaned_page.hOCR */; };
3334
73BE4C351A5BD324002C15F1 /* image_sample.hOCR in Resources */ = {isa = PBXBuildFile; fileRef = 73BE4C341A5BD324002C15F1 /* image_sample.hOCR */; };
@@ -79,6 +80,7 @@
7980
732C54771A5288CC000322DA /* Defaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Defaults.h; sourceTree = "<group>"; };
8081
732C54781A5288CC000322DA /* Defaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Defaults.m; sourceTree = "<group>"; };
8182
733DD5CC1A5E7CE50042374D /* TesseractOCR.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = TesseractOCR.framework; path = "../build/Debug-iphoneos/TesseractOCR.framework"; sourceTree = "<group>"; };
83+
733DD5E61A64300E0042374D /* RecognizedBlock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RecognizedBlock.m; sourceTree = "<group>"; };
8284
736EFF231A5872CA0031B432 /* tessdata-rus */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "tessdata-rus"; path = "TestsProjectTests/tessdata-rus"; sourceTree = SOURCE_ROOT; };
8385
73BE4C301A5B404C002C15F1 /* well_scaned_page.hOCR */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = well_scaned_page.hOCR; sourceTree = "<group>"; };
8486
73BE4C341A5BD324002C15F1 /* image_sample.hOCR */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = image_sample.hOCR; sourceTree = "<group>"; };
@@ -163,6 +165,7 @@
163165
children = (
164166
732C54751A514DA5000322DA /* InitializationTests.m */,
165167
4115B97A1A3EF8E90004EC0A /* RecognitionTests.m */,
168+
733DD5E61A64300E0042374D /* RecognizedBlock.m */,
166169
4115B9781A3EF8E90004EC0A /* Supporting Files */,
167170
732C54771A5288CC000322DA /* Defaults.h */,
168171
732C54781A5288CC000322DA /* Defaults.m */,
@@ -380,6 +383,7 @@
380383
4115B97B1A3EF8E90004EC0A /* RecognitionTests.m in Sources */,
381384
732C54761A514DA6000322DA /* InitializationTests.m in Sources */,
382385
41C68DAF1A41825500848AE1 /* UIImage+G8Equal.m in Sources */,
386+
733DD5E71A64300E0042374D /* RecognizedBlock.m in Sources */,
383387
);
384388
runOnlyForDeploymentPostprocessing = 0;
385389
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// RecognizedBlock.m
3+
// TestsProject
4+
//
5+
// Created by Kirill Makankov on 12.01.15.
6+
// Copyright (c) 2015 Daniele Galiotto - www.g8production.com. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <TesseractOCR/TesseractOCR.h>
11+
#import <Kiwi/Kiwi.h>
12+
#import "Defaults.h"
13+
14+
SPEC_BEGIN(RecognizedBlockTests)
15+
16+
describe(@"RecognizedBlock Tests", ^{
17+
18+
context(@"empty block", ^{
19+
let(emptyBlock, ^id{
20+
G8RecognizedBlock *block = [[G8RecognizedBlock alloc] init];
21+
[[block shouldNot] beNil];
22+
return block;
23+
});
24+
25+
it(@"Should equal another empty block", ^{
26+
G8RecognizedBlock *block2 = [[G8RecognizedBlock alloc] init];
27+
[[block2 shouldNot] beNil];
28+
29+
BOOL isEqual = [emptyBlock isEqual:block2];
30+
[[theValue(isEqual) should] beYes];
31+
});
32+
33+
it(@"Should copy", ^{
34+
G8RecognizedBlock *block2 = [emptyBlock copy];
35+
[[block2 should] equal:emptyBlock];
36+
37+
BOOL isEqual = [emptyBlock isEqual:block2];
38+
[[theValue(isEqual) should] beYes];
39+
});
40+
41+
it(@"Should print description", ^{
42+
[[emptyBlock description] isEqualToString:@"(0.00%) '(null)'"];
43+
});
44+
45+
it(@"Should compare with nil", ^{
46+
BOOL isEqual = [emptyBlock isEqual:nil];
47+
[[theValue(isEqual) should] beNo];
48+
});
49+
50+
it(@"Should have the empty hash", ^{
51+
[[theValue([emptyBlock hash]) should] equal:theValue(0)];
52+
});
53+
});
54+
55+
context(@"Not empty block", ^{
56+
NSString *originalText = @"not empty block";
57+
CGRect originalRect = CGRectMake(10.f, 10.7f, 134.95f, 5.2f);
58+
CGFloat originalConfidence = 0.29f;
59+
G8PageIteratorLevel originalLevel = G8PageIteratorLevelWord;
60+
61+
let(notEmptyWordBlock, ^id{
62+
G8RecognizedBlock *block = [[G8RecognizedBlock alloc] initWithText:originalText
63+
boundingBox:originalRect
64+
confidence:originalConfidence
65+
level:originalLevel];
66+
[[block shouldNot] beNil];
67+
return block;
68+
});
69+
70+
it(@"should isEqual", ^{
71+
BOOL isEqual = [notEmptyWordBlock isEqual:notEmptyWordBlock];
72+
[[theValue(isEqual) should] beYes];
73+
});
74+
75+
it(@"Should not isEqual", ^{
76+
77+
BOOL isEqual = [notEmptyWordBlock isEqual:[[G8RecognizedBlock alloc] init]];
78+
[[theValue(isEqual) should] beNo];
79+
80+
isEqual = [notEmptyWordBlock isEqual:[[G8RecognizedBlock alloc] initWithText:@"different text"
81+
boundingBox:originalRect
82+
confidence:originalConfidence
83+
level:originalLevel]];
84+
[[theValue(isEqual) should] beNo];
85+
86+
isEqual = [notEmptyWordBlock isEqual:[[G8RecognizedBlock alloc] initWithText:originalText
87+
boundingBox:CGRectMake(10.00001f, 10.69999f, 134.95001f, 5.19999f)
88+
confidence:originalConfidence
89+
level:originalLevel]];
90+
[[theValue(isEqual) should] beNo];
91+
92+
isEqual = [notEmptyWordBlock isEqual:[[G8RecognizedBlock alloc] initWithText:originalText
93+
boundingBox:originalRect
94+
confidence:0.78f
95+
level:originalLevel]];
96+
[[theValue(isEqual) should] beNo];
97+
98+
isEqual = [notEmptyWordBlock isEqual:[[G8RecognizedBlock alloc] initWithText:originalText
99+
boundingBox:originalRect
100+
confidence:originalConfidence
101+
level:G8PageIteratorLevelParagraph]];
102+
[[theValue(isEqual) should] beNo];
103+
});
104+
});
105+
});
106+
107+
SPEC_END

0 commit comments

Comments
 (0)