aboutsummaryrefslogtreecommitdiff
path: root/DebugUtils/GTMDebugSelectorValidation.h
blob: b3f1e73a4badefa288d8290b00d7d5b832339a87 (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
//
//  GTMDebugSelectorValidation.h
//
//  This file should only be included within an implimation file.  In any
//  function that takes an object and selector to invoke, you should call:
//
//    GTMAssertSelectorNilOrImplementedWithArguments(obj, sel, @encode(arg1type), ..., NULL)
//
//  This will then validate that the selector is defined and using the right
//  type(s), this can help catch errors much earlier then waiting for the
//  selector to actually fire (and in the case of error selectors, might never
//  really be tested until in the field).
//
//  Copyright 2007-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.
//

#if DEBUG

#import <stdarg.h>
#import "GTMDefines.h"

static void GTMAssertSelectorNilOrImplementedWithArguments(id obj, SEL sel, ...) {
  
  // verify that the object's selector is implemented with the proper
  // number and type of arguments
  va_list argList;
  va_start(argList, sel);
  
  if (obj && sel) {
    // check that the selector is implemented
    if (![obj respondsToSelector:sel]) {
      _GTMDevAssert(NO,
                    @"\"%@\" selector \"%@\" is unimplemented or misnamed", 
                    NSStringFromClass([obj class]), 
                    NSStringFromSelector(sel));
    } else {
      const char *expectedArgType;
      int argCount = 2; // skip self and _cmd
      NSMethodSignature *sig = [obj methodSignatureForSelector:sel];
      
      // check that each expected argument is present and of the correct type
      while ((expectedArgType = va_arg(argList, const char*)) != 0) {
        
        if ([sig numberOfArguments] > argCount) {
          const char *foundArgType = [sig getArgumentTypeAtIndex:argCount];
          
          _GTMDevAssert(0 == strncmp(foundArgType, expectedArgType, strlen(expectedArgType)),
                        @"\"%@\" selector \"%@\" argument %d should be type %s", 
                        NSStringFromClass([obj class]), 
                        NSStringFromSelector(sel),
                        (argCount - 2),
                        expectedArgType);
        }
        argCount++;
      }
      
      // check that the proper number of arguments are present in the selector
      _GTMDevAssert(argCount == [sig numberOfArguments],
                    @"\"%@\" selector \"%@\" should have %d arguments",
                    NSStringFromClass([obj class]), 
                    NSStringFromSelector(sel),
                    (argCount - 2));
    }
  }
  
  va_end(argList);
}

#else // DEBUG

// make it go away if not debug
#define GTMAssertSelectorNilOrImplementedWithArguments(obj, sel, ...) do { } while (0)

#endif // DEBUG