From c53ec5520e39096e0804ce8d89a21378c0904481 Mon Sep 17 00:00:00 2001 From: thomasvl Date: Fri, 13 Jun 2008 19:21:50 +0000 Subject: Landing a log of AppleScript/AppleEvent support code. Landing GTMHTTPServer as a simple server but mainly for use in unittesting. _GTMCompileAssert for doing compile time assertions to GTMDefines.h Lots of improvments for UnitTesting, Dave's gonna put up a wiki page shortly with the full details of what can be done. --- DebugUtils/GTMDebugSelectorValidation.h | 22 ++++++++++++++++++--- DebugUtils/GTMDevLog.m | 34 +++++++++++++++++++++++++++++++++ DebugUtils/GTMMethodCheck.m | 13 ++++++++++--- DebugUtils/GTMMethodCheckTest.m | 28 ++++----------------------- 4 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 DebugUtils/GTMDevLog.m (limited to 'DebugUtils') diff --git a/DebugUtils/GTMDebugSelectorValidation.h b/DebugUtils/GTMDebugSelectorValidation.h index 82d00e8..19d8b8e 100644 --- a/DebugUtils/GTMDebugSelectorValidation.h +++ b/DebugUtils/GTMDebugSelectorValidation.h @@ -5,6 +5,8 @@ // function that takes an object and selector to invoke, you should call: // // GTMAssertSelectorNilOrImplementedWithArguments(obj, sel, @encode(arg1type), ..., NULL) +// or +// GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments(obj, sel, @encode(returnType), @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 @@ -31,12 +33,12 @@ #import #import "GTMDefines.h" -static void GTMAssertSelectorNilOrImplementedWithArguments(id obj, SEL sel, ...) { - +static void GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments(id obj, SEL sel, const char *retType, ...) { + // verify that the object's selector is implemented with the proper // number and type of arguments va_list argList; - va_start(argList, sel); + va_start(argList, retType); if (obj && sel) { // check that the selector is implemented @@ -71,14 +73,28 @@ static void GTMAssertSelectorNilOrImplementedWithArguments(id obj, SEL sel, ...) NSStringFromClass([obj class]), NSStringFromSelector(sel), (argCount - 2)); + + // if asked, validate the return type + if (retType && (strcmp("gtm_skip_return_test", retType) != 0)) { + const char *foundRetType = [sig methodReturnType]; + _GTMDevAssert(0 == strncmp(foundRetType, retType, strlen(retType)), + @"\"%@\" selector \"%@\" return type should be type %s", + NSStringFromClass([obj class]), + NSStringFromSelector(sel), + retType); + } } va_end(argList); } +#define GTMAssertSelectorNilOrImplementedWithArguments(obj, sel, ...) \ + GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments((obj), (sel), "gtm_skip_return_test", __VA_ARGS__) + #else // DEBUG // make it go away if not debug +#define GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments(obj, sel, retType, ...) do { } while (0) #define GTMAssertSelectorNilOrImplementedWithArguments(obj, sel, ...) do { } while (0) #endif // DEBUG diff --git a/DebugUtils/GTMDevLog.m b/DebugUtils/GTMDevLog.m new file mode 100644 index 0000000..af30713 --- /dev/null +++ b/DebugUtils/GTMDevLog.m @@ -0,0 +1,34 @@ +// +// GTMDevLog.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. +// + +#include "GTMUnitTestDevLog.h" + +// This is the logging function that is called by default when building +// GTMFramework. If it can find GTMUnitTestDevLog class it will use it, +// otherwise it falls onto NSLog. +void _GTMUnittestDevLog(NSString *format, ...) { + Class devLogClass = NSClassFromString(@"GTMUnitTestDevLog"); + va_list argList; + va_start(argList, format); + if (devLogClass) { + [devLogClass log:format args:argList]; + } else { + NSLogv(format, argList); + } + va_end(argList); +} diff --git a/DebugUtils/GTMMethodCheck.m b/DebugUtils/GTMMethodCheck.m index f91b1a9..300022b 100644 --- a/DebugUtils/GTMMethodCheck.m +++ b/DebugUtils/GTMMethodCheck.m @@ -41,9 +41,14 @@ static BOOL ConformsToNSObjectProtocol(Class cls) { // work for you. // Some classes (like _NSZombie) start with _NS. // On Leopard we have to look for CFObject as well. - if ((strncmp(className, "NS", 2) == 0) || - (strncmp(className, "_NS", 3) == 0) || - (strcmp(className, "CFObject") == 0)) { + // On iPhone we check Object as well + if ((strncmp(className, "NS", 2) == 0) + || (strncmp(className, "_NS", 3) == 0) + || (strcmp(className, "CFObject") == 0) +#if GTM_IPHONE_SDK + || (strcmp(className, "Object") == 0) +#endif + ) { return YES; } @@ -88,10 +93,12 @@ void GTMMethodCheckMethodChecker(void) { // @protocol(NSObject), or else we will tumble into a _objc_msgForward // recursive loop when we try and call a function by name. if (!ConformsToNSObjectProtocol(cls)) { + // COV_NF_START _GTMDevLog(@"GTMMethodCheckMethodChecker: Class %s does not conform to " "@protocol(NSObject), so won't be checked", class_getName(cls)); continue; + // COV_NF_END } // Since we are looking for a class method (+xxGMMethodCheckMethod...) // we need to query the isa pointer to see what methods it support, but diff --git a/DebugUtils/GTMMethodCheckTest.m b/DebugUtils/GTMMethodCheckTest.m index b5e7e6b..91b7300 100644 --- a/DebugUtils/GTMMethodCheckTest.m +++ b/DebugUtils/GTMMethodCheckTest.m @@ -20,33 +20,13 @@ static BOOL gTestCheckVar = NO; -// This is a contrived class that doesn't inherit from NSObject, but does -// implement some of it's functionality to force test a case in -// GTMMethodCheck. -@interface GTMClassThatDoesntInheritFromNSObject -+ (BOOL)instancesRespondToSelector:(SEL)selector; -+ (BOOL)respondsToSelector:(SEL)selector; -@end - -@implementation GTMClassThatDoesntInheritFromNSObject -GTM_METHOD_CHECK(GTMClassThatDoesntInheritFromNSObject, GTMMethodCheckTestMethod); -- (void)GTMMethodCheckTestMethod { -} -+ (BOOL)instancesRespondToSelector:(SEL)selector { - return YES; -} - -+ (BOOL)respondsToSelector:(SEL)selector { - return YES; -} -@end - -@interface GTMMethodCheckTest : SenTestCase +@interface GTMMethodCheckTest : GTMTestCase ++ (void)GTMMethodCheckTestClassMethod; @end @implementation GTMMethodCheckTest -GTM_METHOD_CHECK(GTMMethodCheckTest, GTMMethodCheckTestMethod); -GTM_METHOD_CHECK(GTMMethodCheckTest, GTMMethodCheckTestClassMethod); +GTM_METHOD_CHECK(GTMMethodCheckTest, GTMMethodCheckTestMethod); // COV_NF_LINE +GTM_METHOD_CHECK(GTMMethodCheckTest, GTMMethodCheckTestClassMethod); // COV_NF_LINE - (void)GTMMethodCheckTestMethod { } -- cgit v1.2.3