aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSAppleScript+HandlerTest.m
blob: 5a9d52b6f319a5c75fd198e57bdd9ede7b7a9117 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
//  NSAppleScript+HandlerTest.m
//
//  Copyright 2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import <Carbon/Carbon.h>
#import "GTMNSAppleScript+Handler.h"
#import "GTMNSAppleEventDescriptor+Foundation.h"
#import "GTMUnitTestDevLog.h"

@interface GTMNSAppleScript_HandlerTest : GTMTestCase {
  NSAppleScript *script_; 
}
@end

@implementation GTMNSAppleScript_HandlerTest
- (void)setUp {
  NSBundle *bundle = [NSBundle bundleForClass:[GTMNSAppleScript_HandlerTest class]];
  STAssertNotNil(bundle, nil);
  NSString *path = [bundle pathForResource:@"GTMNSAppleEvent+HandlerTest" 
                                    ofType:@"scpt"
                               inDirectory:@"Scripts"];
  STAssertNotNil(path, [bundle description]);
  NSDictionary *error = nil;
  script_ = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
                                                   error:&error];
  STAssertNotNil(script_, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
}

- (void)tearDown {
  [script_ release];
  script_ = nil;
}

- (void)testHandlerNoParamsNoReturn {
  NSDictionary *error = nil;
  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"test" 
                                                            parameters:nil 
                                                                 error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
  desc = [script_ gtm_executePositionalHandler:@"test" 
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
  
  //Applescript doesn't appear to get upset about extra params
  desc = [script_ gtm_executePositionalHandler:@"test" 
                                    parameters:[NSArray arrayWithObject:@"foo"] 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeNull, nil);
}
  
- (void)testHandlerNoParamsWithReturn {
  NSDictionary *error = nil;
  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"testReturnOne" 
                                                            parameters:nil 
                                                                 error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)1, nil);
  desc = [script_ gtm_executePositionalHandler:@"testReturnOne" 
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)1, nil);
  
  //Applescript doesn't appear to get upset about extra params
  desc = [script_ gtm_executePositionalHandler:@"testReturnOne" 
                                    parameters:[NSArray arrayWithObject:@"foo"] 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)1, nil);
}

- (void)testHandlerOneParamWithReturn {
  NSDictionary *error = nil;
  // Note case change in executeHandler call
  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"testreturnParam" 
                                                            parameters:nil 
                                                                error:&error];
  STAssertNil(desc, @"Desc should by nil %@", desc);
  STAssertNotNil(error, nil);
  error = nil;
  
  desc = [script_ gtm_executePositionalHandler:@"testReturnParam" 
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNil(desc, @"Desc should by nil %@", desc);
  STAssertNotNil(error, nil);
  error = nil;
  
  desc = [script_ gtm_executePositionalHandler:@"testReturnParam" 
                                    parameters:[NSArray arrayWithObject:@"foo"]
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeUnicodeText, nil);
  STAssertEqualObjects([desc gtm_objectValue], @"foo", nil);
}

- (void)testHandlerTwoParamsWithReturn {
  NSDictionary *error = nil;
  // Note case change in executeHandler call
  // Test case and empty params
  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"testADDPArams" 
                                                            parameters:nil 
                                                                 error:&error];
  STAssertNil(desc, @"Desc should by nil %@", desc);
  STAssertNotNil(error, nil);
  
  // Test empty params
  error = nil;
  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNil(desc, @"Desc should by nil %@", desc);
  STAssertNotNil(error, nil);
  
  error = nil;
  NSArray *args = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:2],
    nil];
  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
                                    parameters:args 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)3, nil);

  // Test bad params
  error = nil;
  args = [NSArray arrayWithObjects:
    @"foo",
    @"bar",
    nil];
  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
                                    parameters:args 
                                         error:&error];
  STAssertNil(desc, @"Desc should by nil %@", desc);
  STAssertNotNil(error, nil);

  // Test too many params. Currently Applescript allows this so it should pass
  error = nil;
  args = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:2],
    [NSNumber numberWithInt:3],
    nil];
  desc = [script_ gtm_executePositionalHandler:@"testAddParams" 
                                    parameters:args 
                                         error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)3, nil);}

- (void)testLabeledHandler {
  NSDictionary *error = nil;
  AEKeyword labels[] = { keyDirectObject, 
                         keyASPrepositionOnto, 
                         keyASPrepositionGiven };
  id params[3];
  params[0] = [NSNumber numberWithInt:1];
  params[1] = [NSNumber numberWithInt:3];
  params[2] = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:4] 
                                          forKey:@"othervalue"];
  
  NSAppleEventDescriptor *desc = [script_ gtm_executeLabeledHandler:@"testAdd" 
                                                             labels:labels
                                                         parameters:params
                                                              count:sizeof(params) / sizeof(id)
                                                              error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)8, nil);
  
  // Test too many params. Currently Applescript allows this so it should pass
  AEKeyword labels2[] = { keyDirectObject, 
                         keyASPrepositionOnto, 
                         keyASPrepositionBetween,
                         keyASPrepositionGiven };
  id params2[4];
  params2[0] = [NSNumber numberWithInt:1];
  params2[1] = [NSNumber numberWithInt:3];
  params2[2] = [NSNumber numberWithInt:5];
  params2[3] = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:4] 
                                            forKey:@"othervalue"];

  error = nil;
  desc = [script_ gtm_executeLabeledHandler:@"testAdd" 
                                     labels:labels2
                                 parameters:params2
                                      count:sizeof(params2) / sizeof(id)
                                      error:&error];
  STAssertNotNil(desc, [error description]);
  STAssertNil(error, @"Error should be nil. Error = %@", [error description]);
  STAssertEquals([desc descriptorType], (DescType)typeSInt32, nil);
  STAssertEquals([desc int32Value], (SInt32)8, nil);}

- (void)testHandlers {
  NSSet *handlers = [script_ gtm_handlers];
  NSSet *expected = [NSSet setWithObjects:
                     @"aevtodoc",
                     @"test",
                     @"testreturnone",
                     @"testreturnparam",
                     @"testaddparams",
                     @"testadd",
                     nil];
  STAssertEqualObjects(handlers, expected, @"Unexpected handlers?");
}

- (void)testProperties {
  NSSet *properties = [script_ gtm_properties];
  NSSet *expected = [NSSet setWithObjects:
                     @"foo", 
                     @"asdscriptuniqueidentifier", 
                     nil];
  STAssertEqualObjects(properties, expected, @"Unexpected properties?");
  id value = [script_ gtm_valueForProperty:@"foo"];
  STAssertEqualObjects(value, [NSNumber numberWithInt:1], @"bad property?");
  BOOL goodSet = [script_ gtm_setValue:@"bar" forProperty:@"foo"];
  STAssertTrue(goodSet, @"Couldn't set property");
  value = [script_ gtm_valueForProperty:@"foo"];
  STAssertEqualObjects(value, @"bar", @"bad property?");
  
  [GTMUnitTestDevLog expectPattern:@"Unable to setValue:bar forProperty:"
   "\\(null\\) from <NSAppleScript: 0x[0-9a-f]+> \\(-50\\)"];
  goodSet = [script_ gtm_setValue:@"bar" forProperty:nil];
  STAssertFalse(goodSet, @"Set property?");
  [GTMUnitTestDevLog expectPattern:@"Unable to get valueForProperty:gargle "
   "from <NSAppleScript: 0x[0-9a-f]+> \\(-1753\\)"];
  value = [script_ gtm_valueForProperty:@"gargle"];
  STAssertNil(value, @"Property named gargle?");
}

- (void)testFailures {
  NSDictionary *error = nil;
  NSAppleEventDescriptor *desc = [script_ gtm_executePositionalHandler:@"noSuchTest" 
                                                            parameters:nil 
                                                                 error:&error];
  STAssertNil(desc, nil);
  STAssertNotNil(error, nil);

  // Test with empty handler name
  error = nil;
  desc = [script_ gtm_executePositionalHandler:@"" 
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNil(desc, nil);
  STAssertNotNil(error, nil);
  
  // Test with nil handler
  error = nil;
  desc = [script_ gtm_executePositionalHandler:nil
                                    parameters:[NSArray array] 
                                         error:&error];
  STAssertNil(desc, nil);
  STAssertNotNil(error, nil);
  
  // Test with nil handler and nil error
  desc = [script_ gtm_executePositionalHandler:nil
                                    parameters:nil 
                                         error:nil];
  STAssertNil(desc, nil);
  
  // Test with a bad script
  NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:@"david hasselhoff"] autorelease];
  [GTMUnitTestDevLog expectPattern:@"Unable to compile script: .*"];
  [GTMUnitTestDevLog expectPattern:@"Error getting handlers: -[0-9]+"];
  NSSet *handlers = [script gtm_handlers];
  STAssertEquals([handlers count], (NSUInteger)0, @"Should have no handlers");
  [GTMUnitTestDevLog expectPattern:@"Unable to compile script: .*"];
  [GTMUnitTestDevLog expectPattern:@"Error getting properties: -[0-9]+"];
  NSSet *properties = [script gtm_properties];
  STAssertEquals([properties count], (NSUInteger)0, @"Should have no properties");
}

- (void)testScriptDescriptors {
  NSAppleEventDescriptor *desc = [script_ gtm_appleEventDescriptor];
  STAssertNotNil(desc, @"Couldn't make a script desc");
  NSAppleScript *script = [desc gtm_objectValue];
  STAssertNotNil(script, @"Couldn't get a script back");
  NSSet *handlers = [script gtm_handlers];
  STAssertNotNil(handlers, @"Couldn't get handlers");
}

@protocol ScriptInterface
- (id)test;
- (id)testReturnParam:(id)param;
- (id)testAddParams:(id)param1 :(id)param2;
@end

- (void)testForwarding {
  id<ScriptInterface> foo = (id<ScriptInterface>)script_;
  [foo test];
  NSNumber *val = [foo testReturnParam:[NSNumber numberWithInt:2]];
  STAssertEquals([val intValue], 2, @"should be 2");
  val = [foo testAddParams:[NSNumber numberWithInt:2] :[NSNumber numberWithInt:3]];
  STAssertEquals([val intValue], 5, @"should be 5");
}
@end