File: PopplerTextSearchTest.m

package info (click to toggle)
popplerkit.framework 0.0.20051227svn-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,632 kB
  • sloc: objc: 2,303; cpp: 672; sh: 475; ansic: 55; makefile: 29
file content (186 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
//  PopplerTextSearchTest.m
//  PopplerKit
//
//  Created by Stefan Kleine Stegemann on 8/29/05.
//  Copyright 2005 . All rights reserved.
//

#import "PopplerTextSearchTest.h"
#import "TestSettings.h"

@implementation PopplerTextSearchTest

- (void) setUp
{
   document = [[PopplerDocument alloc] initWithPath: kTestDocument];
}

- (void) tearDown
{
   [document release];
}

- (void) testSearch
{
   PopplerTextSearch* search = [PopplerTextSearch searchWithDocument: document];
   
   NSArray* hits = [search searchFor: @"NEXTSTEP" from: 2];
   [self assertNotNil: hits];
   [self assertTrue: [hits count] > 1 message: @"not enought hits"];
   [self assertFalse: [search running] message: @"search should not be running"];

   id delegate = [MockObject mockForClass: [DummyDelegate class] testCase: self];

   [[[delegate expect: @selector(searchWillStart:)]
      once]
      with: [Arg sameAs: search]];
   
   [[[delegate expect: @selector(search:didFoundHit:)]
      times: [hits count]]
      with: [Arg sameAs: search] with: [Arg isA: [PopplerTextHit class]]];
   
   [[[delegate expect: @selector(search:didCompletePage:)]
      times: [document countPages]]
       with: [Arg sameAs: search]
       with: [ArgNextPageIndexRule ruleWithStartPage: 2 document: document]];

   [[[delegate expect: @selector(searchDidFinish:)]
      once]
      with: [Arg sameAs: search]];
   
   [search searchFor: @"NEXTSTEP" from: 2 delegate: delegate];
   
   [delegate verify];
   [self assertFalse: [search running] message: @"search should not be running"];
}

- (void) testStopSearch
{
   PopplerTextSearch* search = [PopplerTextSearch searchWithDocument: document];
   
   id delegate = [[[CancelDelegate alloc] init] autorelease];
   id mockDelegate = [MockObject mockForClass: [delegate class] testCase: self];
   
   [[[mockDelegate expect: @selector(searchWillStart:)]
      once]
      with: [Arg sameAs: search]];
   
   [[[[mockDelegate expect: @selector(search:didFoundHit:)]
      once]
      with: [Arg sameAs: search] with: [Arg isA: [PopplerTextHit class]]]
      delegate: delegate];
   
   [[[mockDelegate expect: @selector(search:didCompletePage:)]
      never]
       with: [Arg sameAs: search]
       with: [ArgNextPageIndexRule ruleWithStartPage: 2 document: document]];
   
   [[[mockDelegate expect: @selector(searchDidFinish:)]
      once]
      with: [Arg sameAs: search]];
   
   [search searchFor: @"NEXTSTEP" from: 2 delegate: mockDelegate];
   
   [mockDelegate verify];
   [self assertFalse: [search running] message: @"search should not be running"];
}

- (void) testSearchPageRange
{
   PopplerTextSearch* search = [PopplerTextSearch searchWithDocument: document];
   
   NSArray* hits = [search searchFor: @"NEXTSTEP" from: 4 to: 2];
   [self assertNotNil: hits];
   [self assertTrue: [hits count] > 1 message: @"not enought hits"];
   [self assertFalse: [search running] message: @"search should not be running"];
   
   id delegate = [MockObject mockForClass: [DummyDelegate class] testCase: self];
   
   [[[delegate expect: @selector(searchWillStart:)]
      once]
      with: [Arg sameAs: search]];
   
   [[[delegate expect: @selector(search:didFoundHit:)]
      times: [hits count]]
      with: [Arg sameAs: search] with: [Arg isA: [PopplerTextHit class]]];
   
   [[[delegate expect: @selector(search:didCompletePage:)]
      times: ([document countPages] - 4) + 1 + 2]
       with: [Arg sameAs: search]
       with: [ArgNextPageIndexRule ruleWithStartPage: 4 document: document]];
   
   [[[delegate expect: @selector(searchDidFinish:)]
      once]
      with: [Arg sameAs: search]];
   
   [search searchFor: @"NEXTSTEP" from: 4 to: 2 delegate: delegate];
   
   [delegate verify];
   [self assertFalse: [search running] message: @"search should not be running"];
}

@end

/* ----------------------------------------------------- */
/*  class DummyDelegate                                  */
/* ----------------------------------------------------- */

@implementation DummyDelegate
- (void) searchWillStart: (PopplerTextSearch*)aSearch; {}
- (void) search: (PopplerTextSearch*)search didFoundHit: (PopplerTextHit*)hit; {}
- (void) search: (PopplerTextSearch*)search didCompletePage: (PopplerPage*)page; {}
- (void) searchDidFinish: (PopplerTextSearch*)aSearch; {}
@end

/* ----------------------------------------------------- */
/*  class CancelDelegate                                 */
/* ----------------------------------------------------- */

@implementation CancelDelegate

- (void) search: (PopplerTextSearch*)search didFoundHit: (PopplerTextHit*)hit;
{
   [search stop];
}

@end

/* ----------------------------------------------------- */
/*  class ArgNextPageIndexRule                           */
/* ----------------------------------------------------- */

@implementation ArgNextPageIndexRule

- (id) initWithStartPage: (unsigned)aStartPageIndex document: (PopplerDocument*)aDocument;
{
   if (![super init])
      return nil;
   
   startPageIndex = aStartPageIndex;
   currentPageIndex = 0;
   document = aDocument;
   
   return self;
}

+ (ArgNextPageIndexRule*) ruleWithStartPage: (unsigned)aStartPageIndex document: (PopplerDocument*)aDocument;
{
   return [[[self alloc] initWithStartPage: aStartPageIndex document: aDocument] autorelease];
}

- (BOOL) satisfiedForValue: (id)value error: (NSString**)error
{
   unsigned expectedPageIndex = 
      (currentPageIndex != 0 ? [document nextPageIndex: currentPageIndex] : startPageIndex);

   if ([value index] != expectedPageIndex) {
      *error = [NSString stringWithFormat: @"expected page with index %d after %d but got %d", expectedPageIndex, currentPageIndex, [value index]];
      return NO;
   }
   
   currentPageIndex = expectedPageIndex;
   return YES;
}

@end