Skip to content

Commit a378544

Browse files
committed
Add support for having warnings vs errors
A basic framework for dealing with issues that users may want to treat as either warnings or errors. It supports setting a default from compile time, modifying it at runtime using an environment variable, and push/popping states off of a stack. It also changes some exceptions that were being thrown to issues specifically because those exceptions are making it very difficult for me to transition my codebase from OCMock 3.4.1 to OCMock 3.6, and I know it will also be an issue for https://github.com/TextureGroup/Texture moving to OCMock 3.6.
1 parent 9b0e314 commit a378544

File tree

11 files changed

+404
-12
lines changed

11 files changed

+404
-12
lines changed

OCMock.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Pod::Spec.new do |s|
2929
s.public_header_files = ["OCMock.h", "OCMockObject.h", "OCMArg.h", "OCMConstraint.h",
3030
"OCMLocation.h", "OCMMacroState.h", "OCMRecorder.h",
3131
"OCMStubRecorder.h", "NSNotificationCenter+OCMAdditions.h",
32-
"OCMFunctions.h", "OCMVerifier.h", "OCMQuantifier.h" ]
32+
"OCMFunctions.h", "OCMVerifier.h", "OCMQuantifier.h",
33+
"OCMIssueReporter.h"]
3334
.map { |file| "Source/OCMock/" + file }
3435

3536
end

Source/OCMock.xcodeproj/project.pbxproj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,20 @@
279279
817EB15C1BD765130047E85A /* OCMBlockArgCaller.h in Headers */ = {isa = PBXBuildFile; fileRef = 2FA2891034E7B73AA3511D17 /* OCMBlockArgCaller.h */; };
280280
817EB15D1BD765130047E85A /* OCMArgAction.h in Headers */ = {isa = PBXBuildFile; fileRef = 2FA2833B48908EAD36444671 /* OCMArgAction.h */; };
281281
817EB1661BD7674D0047E85A /* OCMFunctionsPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 03F370CA1BAA1DE800CAD3E8 /* OCMFunctionsPrivate.h */; };
282+
8B911C8C247B97A500A3581F /* OCMIssueReporterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B911C8B247B97A500A3581F /* OCMIssueReporterTests.m */; };
283+
8B911C8D247B97A500A3581F /* OCMIssueReporterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B911C8B247B97A500A3581F /* OCMIssueReporterTests.m */; };
282284
8BF73E53246CA75E00B9A52C /* OCMNoEscapeBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF73E52246CA75E00B9A52C /* OCMNoEscapeBlockTests.m */; settings = {COMPILER_FLAGS = "-Xclang -fexperimental-optimized-noescape"; }; };
283285
8BF73E54246CA75E00B9A52C /* OCMNoEscapeBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF73E52246CA75E00B9A52C /* OCMNoEscapeBlockTests.m */; settings = {COMPILER_FLAGS = "-Xclang -fexperimental-optimized-noescape"; }; };
286+
8BF74028247AE3F100B9A52C /* OCMIssueReporter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
287+
8BF74029247AE3F100B9A52C /* OCMIssueReporter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
288+
8BF7402A247AE3F100B9A52C /* OCMIssueReporter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
289+
8BF7402B247AE3F100B9A52C /* OCMIssueReporter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
290+
8BF7402C247AE3F100B9A52C /* OCMIssueReporter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
291+
8BF7402D247AE3F100B9A52C /* OCMIssueReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */; };
292+
8BF7402E247AE3F100B9A52C /* OCMIssueReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */; };
293+
8BF7402F247AE3F100B9A52C /* OCMIssueReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */; };
294+
8BF74030247AE3F100B9A52C /* OCMIssueReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */; };
295+
8BF74031247AE3F100B9A52C /* OCMIssueReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */; };
284296
8DE97C5522B43EE60098C63F /* OCMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B3159E146333BF0052CD09 /* OCMockObject.m */; };
285297
8DE97C5622B43EE60098C63F /* OCClassMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B3158C146333BF0052CD09 /* OCClassMockObject.m */; };
286298
8DE97C5722B43EE60098C63F /* OCPartialMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B315AA146333BF0052CD09 /* OCPartialMockObject.m */; };
@@ -571,7 +583,10 @@
571583
3CFBDD751BB3DB200050D9C5 /* TestClassWithCustomReferenceCounting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestClassWithCustomReferenceCounting.h; sourceTree = "<group>"; };
572584
3CFBDD761BB3DB200050D9C5 /* TestClassWithCustomReferenceCounting.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestClassWithCustomReferenceCounting.m; sourceTree = "<group>"; };
573585
817EB1621BD765130047E85A /* OCMock.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OCMock.framework; sourceTree = BUILT_PRODUCTS_DIR; };
586+
8B911C8B247B97A500A3581F /* OCMIssueReporterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCMIssueReporterTests.m; sourceTree = "<group>"; };
574587
8BF73E52246CA75E00B9A52C /* OCMNoEscapeBlockTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCMNoEscapeBlockTests.m; sourceTree = "<group>"; };
588+
8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCMIssueReporter.h; sourceTree = "<group>"; };
589+
8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCMIssueReporter.m; sourceTree = "<group>"; };
575590
8DE97CA022B43EE60098C63F /* OCMock.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OCMock.framework; sourceTree = BUILT_PRODUCTS_DIR; };
576591
A02926811CA0725A00594AAF /* TestObjects.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = TestObjects.xcdatamodel; sourceTree = "<group>"; };
577592
D31108AD1828DB8700737925 /* OCMockLibTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OCMockLibTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -751,6 +766,7 @@
751766
037ECD5318FAD84100AF0E4C /* OCMInvocationMatcherTests.m */,
752767
031E50571BB4A56300E257C3 /* OCMBoxedReturnValueProviderTests.m */,
753768
03B316211463350E0052CD09 /* OCMConstraintTests.m */,
769+
8B911C8B247B97A500A3581F /* OCMIssueReporterTests.m */,
754770
2FA28EDBF243639C57F88A1B /* OCMArgTests.m */,
755771
036865631D3571A8005E6BEE /* OCMQuantifierTests.m */,
756772
03B316291463350E0052CD09 /* OCObserverMockObjectTests.m */,
@@ -786,6 +802,8 @@
786802
037ECD5618FB0D2E00AF0E4C /* Helper */ = {
787803
isa = PBXGroup;
788804
children = (
805+
8BF74026247AE3F100B9A52C /* OCMIssueReporter.h */,
806+
8BF74027247AE3F100B9A52C /* OCMIssueReporter.m */,
789807
03E98D4918F308B400522D42 /* OCMLocation.h */,
790808
03E98D4A18F308B400522D42 /* OCMLocation.m */,
791809
2FA28006D043CBDBBAEF6E3F /* OCMMacroState.h */,
@@ -966,6 +984,7 @@
966984
0368656D1D357317005E6BEE /* OCMQuantifier.h in Headers */,
967985
3C0FF06A1BAA3FD10021AD20 /* OCMFunctionsPrivate.h in Headers */,
968986
03B31613146333C00052CD09 /* OCProtocolMockObject.h in Headers */,
987+
8BF74028247AE3F100B9A52C /* OCMIssueReporter.h in Headers */,
969988
2FA287ACE547BB41937BDEC3 /* NSObject+OCMAdditions.h in Headers */,
970989
2FA28641AAD0AC2F876C9E48 /* OCMInvocationMatcher.h in Headers */,
971990
0322DA6919118B4600CACAF1 /* OCMVerifier.h in Headers */,
@@ -1007,6 +1026,7 @@
10071026
03B315FB146333C00052CD09 /* OCMRealObjectForwarder.h in Headers */,
10081027
03DCED6F183406DA0059089E /* NSObject+OCMAdditions.h in Headers */,
10091028
0368656E1D357318005E6BEE /* OCMQuantifier.h in Headers */,
1029+
8BF74029247AE3F100B9A52C /* OCMIssueReporter.h in Headers */,
10101030
817EB1661BD7674D0047E85A /* OCMFunctionsPrivate.h in Headers */,
10111031
03B31605146333C00052CD09 /* OCObserverMockObject.h in Headers */,
10121032
03B3160A146333C00052CD09 /* OCPartialMockObject.h in Headers */,
@@ -1062,6 +1082,7 @@
10621082
817EB15A1BD765130047E85A /* NSValue+OCMAdditions.h in Headers */,
10631083
817EB15B1BD765130047E85A /* OCMFunctions.h in Headers */,
10641084
817EB15C1BD765130047E85A /* OCMBlockArgCaller.h in Headers */,
1085+
8BF7402B247AE3F100B9A52C /* OCMIssueReporter.h in Headers */,
10651086
817EB15D1BD765130047E85A /* OCMArgAction.h in Headers */,
10661087
2FA28806443827E286F12F6F /* OCMNonRetainingObjectReturnValueProvider.h in Headers */,
10671088
);
@@ -1098,6 +1119,7 @@
10981119
8DE97C9122B43EE60098C63F /* OCMFunctionsPrivate.h in Headers */,
10991120
8DE97C9222B43EE60098C63F /* OCObserverMockObject.h in Headers */,
11001121
8DE97C9322B43EE60098C63F /* OCMObserverRecorder.h in Headers */,
1122+
8BF7402C247AE3F100B9A52C /* OCMIssueReporter.h in Headers */,
11011123
8DE97C9422B43EE60098C63F /* OCMPassByRefSetter.h in Headers */,
11021124
8DE97C9522B43EE60098C63F /* NSInvocation+OCMAdditions.h in Headers */,
11031125
8DE97C9622B43EE60098C63F /* NSMethodSignature+OCMAdditions.h in Headers */,
@@ -1151,6 +1173,7 @@
11511173
F0B951491B00810C00942C38 /* NSValue+OCMAdditions.h in Headers */,
11521174
F0B9514A1B00810C00942C38 /* OCMFunctions.h in Headers */,
11531175
2FA28B7BDB3319A499E90525 /* OCMBlockArgCaller.h in Headers */,
1176+
8BF7402A247AE3F100B9A52C /* OCMIssueReporter.h in Headers */,
11541177
2FA280E60213BA09F007C173 /* OCMArgAction.h in Headers */,
11551178
2FA28AFBD67EAB9DD1F23BF5 /* OCMNonRetainingObjectReturnValueProvider.h in Headers */,
11561179
);
@@ -1406,6 +1429,7 @@
14061429
03B315B6146333BF0052CD09 /* NSMethodSignature+OCMAdditions.m in Sources */,
14071430
03618D85195B553400389166 /* OCMRecorder.m in Sources */,
14081431
03C7BF0C195DA2F200A545DD /* OCMInvocationStub.m in Sources */,
1432+
8BF7402D247AE3F100B9A52C /* OCMIssueReporter.m in Sources */,
14091433
03B315BB146333BF0052CD09 /* NSNotificationCenter+OCMAdditions.m in Sources */,
14101434
0322DA6B19118B4600CACAF1 /* OCMVerifier.m in Sources */,
14111435
03B315C0146333BF0052CD09 /* OCClassMockObject.m in Sources */,
@@ -1448,6 +1472,7 @@
14481472
03B315B8146333BF0052CD09 /* NSMethodSignature+OCMAdditions.m in Sources */,
14491473
03618D86195B553400389166 /* OCMRecorder.m in Sources */,
14501474
03C7BF0D195DA2F200A545DD /* OCMInvocationStub.m in Sources */,
1475+
8BF7402E247AE3F100B9A52C /* OCMIssueReporter.m in Sources */,
14511476
03B315BD146333BF0052CD09 /* NSNotificationCenter+OCMAdditions.m in Sources */,
14521477
0322DA6C19118B4600CACAF1 /* OCMVerifier.m in Sources */,
14531478
03B315C2146333BF0052CD09 /* OCClassMockObject.m in Sources */,
@@ -1494,6 +1519,7 @@
14941519
03E98D5018F310EE00522D42 /* OCMockObjectMacroTests.m in Sources */,
14951520
03565A4A18F05721003AE91E /* OCObserverMockObjectTests.m in Sources */,
14961521
03565A4318F05721003AE91E /* OCMockObjectClassMethodMockingTests.m in Sources */,
1522+
8B911C8C247B97A500A3581F /* OCMIssueReporterTests.m in Sources */,
14971523
03565A4918F05721003AE91E /* OCMArgTests.m in Sources */,
14981524
036865641D3571A8005E6BEE /* OCMQuantifierTests.m in Sources */,
14991525
0322DA65191188D100CACAF1 /* OCMockObjectVerifyAfterRunTests.m in Sources */,
@@ -1519,6 +1545,7 @@
15191545
817EB1181BD765130047E85A /* OCClassMockObject.m in Sources */,
15201546
817EB1191BD765130047E85A /* OCPartialMockObject.m in Sources */,
15211547
817EB11A1BD765130047E85A /* OCProtocolMockObject.m in Sources */,
1548+
8BF74030247AE3F100B9A52C /* OCMIssueReporter.m in Sources */,
15221549
817EB11B1BD765130047E85A /* OCMRecorder.m in Sources */,
15231550
817EB11C1BD765130047E85A /* OCMStubRecorder.m in Sources */,
15241551
817EB11D1BD765130047E85A /* OCMExpectationRecorder.m in Sources */,
@@ -1561,6 +1588,7 @@
15611588
8DE97C5622B43EE60098C63F /* OCClassMockObject.m in Sources */,
15621589
8DE97C5722B43EE60098C63F /* OCPartialMockObject.m in Sources */,
15631590
8DE97C5822B43EE60098C63F /* OCProtocolMockObject.m in Sources */,
1591+
8BF74031247AE3F100B9A52C /* OCMIssueReporter.m in Sources */,
15641592
8DE97C5922B43EE60098C63F /* OCMRecorder.m in Sources */,
15651593
03A1CC9F23F8A045005ADA04 /* OCMQuantifier.m in Sources */,
15661594
8DE97C5A22B43EE60098C63F /* OCMStubRecorder.m in Sources */,
@@ -1607,6 +1635,7 @@
16071635
D31108C51828DBD600737925 /* OCMockObjectClassMethodMockingTests.m in Sources */,
16081636
D31108C71828DBD600737925 /* OCMConstraintTests.m in Sources */,
16091637
D31108C91828DBD600737925 /* OCObserverMockObjectTests.m in Sources */,
1638+
8B911C8D247B97A500A3581F /* OCMIssueReporterTests.m in Sources */,
16101639
037ECD5518FAD84100AF0E4C /* OCMInvocationMatcherTests.m in Sources */,
16111640
D31108C41828DBD600737925 /* OCMockObjectPartialMocksTests.m in Sources */,
16121641
D31108C81828DBD600737925 /* OCMStubRecorderTests.m in Sources */,
@@ -1632,6 +1661,7 @@
16321661
F0B9510D1B0080EC00942C38 /* OCClassMockObject.m in Sources */,
16331662
F0B9510E1B0080EC00942C38 /* OCPartialMockObject.m in Sources */,
16341663
F0B9510F1B0080EC00942C38 /* OCProtocolMockObject.m in Sources */,
1664+
8BF7402F247AE3F100B9A52C /* OCMIssueReporter.m in Sources */,
16351665
F0B951101B0080EC00942C38 /* OCMRecorder.m in Sources */,
16361666
F0B951111B0080EC00942C38 /* OCMStubRecorder.m in Sources */,
16371667
F0B951121B0080EC00942C38 /* OCMExpectationRecorder.m in Sources */,

Source/OCMock/OCMIssueReporter.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2014-2020 Erik Doernenburg and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use these files except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
#import "OCMFunctions.h"
19+
20+
// Use this define to be able to control the default issue treatment from a build.
21+
// It can be overridden by using `OCMIssueTreatmentDefaultEnvironmentVariable`.
22+
#ifndef OCMIssueTreatmentDefault
23+
#define OCMIssueTreatmentDefault OCMIssueTreatmentWarnings
24+
#endif
25+
26+
// Use this to control the default issue treatment through the environment.
27+
// It can be set to 0 for warnings or 1 for errors.
28+
#define OCMIssueTreatmentDefaultEnvironmentVariable @"OCMIssueTreatmentDefault"
29+
30+
// The name of NSExceptions thrown by default when an issue it treated as an error.
31+
OCMOCK_EXTERN NSExceptionName const OCMIssueException;
32+
33+
typedef NS_ENUM(NSUInteger, OCMIssueTreatment)
34+
{
35+
// Warnings are printed to stderr.
36+
OCMIssueTreatmentWarnings = 0,
37+
// Errors are thrown an NSExceptions.
38+
OCMIssueTreatmentErrors,
39+
};
40+
41+
@interface OCMIssueReporter : NSObject
42+
{
43+
NSMutableArray *issueTreatmentStack;
44+
}
45+
46+
+ (instancetype)defaultReporter;
47+
48+
- (void)reportIssueInFile:(const char *)file line:(NSUInteger)line format:(NSString *)format, ... NS_FORMAT_FUNCTION(3,4);
49+
- (void)reportIssueInFilev:(const char *)file line:(NSUInteger)line format:(NSString *)format arguments:(va_list)args NS_FORMAT_FUNCTION(3,0);
50+
- (void)reportIssueInFile:(const char *)file line:(NSUInteger)line exceptionName:(NSExceptionName)name reason:(NSString *)reason;
51+
52+
// Pushes/Pops an issue treatment on the stack. Push and Pop can only be called from the main thread and must be balanced.
53+
- (void)pushIssueTreatment:(OCMIssueTreatment)treatment;
54+
- (void)popIssueTreatment;
55+
56+
// The current issue treatment on the top of the stack.
57+
- (OCMIssueTreatment)issueTreatment;
58+
59+
@end
60+
61+
#define OCM_REPORT_ISSUE(_format, ...) ([[OCMIssueReporter defaultReporter] reportIssueInFile:__FILE__ line:__LINE__ format:_format, __VA_ARGS__ ])

Source/OCMock/OCMIssueReporter.m

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2014-2020 Erik Doernenburg and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use these files except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
#import "OCMIssueReporter.h"
18+
19+
NSExceptionName const OCMIssueException = @"OCMIssueException";
20+
21+
@implementation OCMIssueReporter
22+
23+
+ (instancetype)defaultReporter
24+
{
25+
static dispatch_once_t onceToken;
26+
static OCMIssueReporter *defaultReporter;
27+
dispatch_once(&onceToken, ^{
28+
defaultReporter = [[OCMIssueReporter alloc] init];
29+
atexit_b(^{
30+
if([defaultReporter->issueTreatmentStack count] != 1)
31+
{
32+
[NSException raise:NSInternalInconsistencyException format:@"Unmatched push/pops on OCMIssueRecorder"];
33+
}
34+
});
35+
});
36+
return defaultReporter;
37+
}
38+
39+
- (instancetype)init
40+
{
41+
if((self = [super init]))
42+
{
43+
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
44+
OCMIssueTreatment value;
45+
NSString *stringValue = [environment objectForKey:OCMIssueTreatmentDefaultEnvironmentVariable];
46+
if(stringValue)
47+
{
48+
value = [stringValue integerValue];
49+
}
50+
else
51+
{
52+
value = OCMIssueTreatmentDefault;
53+
}
54+
if(value > OCMIssueTreatmentErrors)
55+
{
56+
[NSException raise:NSInvalidArgumentException format:@"OCMIssueReporter has invalid default issue treatment: %d", (int)value];
57+
}
58+
issueTreatmentStack = [[NSMutableArray alloc] init];
59+
[issueTreatmentStack addObject:[NSNumber numberWithInteger:value]];
60+
}
61+
return self;
62+
}
63+
64+
- (void)dealloc
65+
{
66+
[issueTreatmentStack release];
67+
[super dealloc];
68+
}
69+
70+
- (void)reportIssueInFile:(const char *)file line:(NSUInteger)line format:(NSString *)format, ...
71+
{
72+
va_list arguments;
73+
va_start(arguments, format);
74+
[self reportIssueInFilev:file line:line format:format arguments:arguments];
75+
va_end(arguments);
76+
}
77+
78+
- (void)reportIssueInFilev:(const char *)file line:(NSUInteger)line format:(NSString *)format arguments:(va_list)arguments
79+
{
80+
NSString *description = [[NSString alloc] initWithFormat:format arguments:arguments];
81+
[self reportIssueInFile:file line:line exceptionName:OCMIssueException reason:description];
82+
[description release];
83+
}
84+
85+
- (void)reportIssueInFile:(const char *)file line:(NSUInteger)line exceptionName:(NSExceptionName)name reason:(NSString *)reason
86+
{
87+
OCMIssueTreatment treatment = [self issueTreatment];
88+
NSString *fullDescription;
89+
NSString *type;
90+
switch(treatment)
91+
{
92+
case OCMIssueTreatmentWarnings:
93+
type = @"warning";
94+
break;
95+
96+
case OCMIssueTreatmentErrors:
97+
type = @"error";
98+
break;
99+
100+
default:
101+
[NSException raise:NSInternalInconsistencyException format:@"Unknown issue treatment: %d", (int)treatment];
102+
break;
103+
}
104+
if(file)
105+
{
106+
fullDescription = [[NSString alloc] initWithFormat:@"%s:%d:0: %@: %@", file, (int)line, type, reason];
107+
}
108+
else
109+
{
110+
fullDescription = [[NSString alloc] initWithFormat:@"%@: %@", type, reason];
111+
}
112+
switch(treatment)
113+
{
114+
case OCMIssueTreatmentWarnings:
115+
fprintf(stderr, "%s\n", [fullDescription UTF8String]);
116+
break;
117+
118+
case OCMIssueTreatmentErrors:
119+
[NSException raise:name format:@"%@", fullDescription];
120+
break;
121+
122+
default:
123+
[NSException raise:NSInternalInconsistencyException format:@"Unknown issue treatment: %d", (int)treatment];
124+
break;
125+
}
126+
}
127+
128+
- (void)pushIssueTreatment:(OCMIssueTreatment)treatment
129+
{
130+
if(![NSThread isMainThread])
131+
{
132+
[NSException raise:NSInternalInconsistencyException format:@"pushIssueTreatment can only be called on main thread"];
133+
}
134+
@synchronized(self)
135+
{
136+
[issueTreatmentStack addObject:[NSNumber numberWithInteger:treatment]];
137+
}
138+
}
139+
140+
- (void)popIssueTreatment
141+
{
142+
if(![NSThread isMainThread])
143+
{
144+
[NSException raise:NSInternalInconsistencyException format:@"popIssueTreatment can only be called on main thread"];
145+
}
146+
147+
@synchronized(self)
148+
{
149+
if([issueTreatmentStack count] == 1)
150+
{
151+
[NSException raise:NSInternalInconsistencyException format:@"unbalanced calls to pushIssueTreatment/popIssueTreatment"];
152+
}
153+
[issueTreatmentStack removeLastObject];
154+
}
155+
}
156+
157+
- (OCMIssueTreatment)issueTreatment
158+
{
159+
OCMIssueTreatment treatment;
160+
@synchronized(self)
161+
{
162+
treatment = [[issueTreatmentStack lastObject] integerValue];
163+
}
164+
return treatment;
165+
}
166+
@end

0 commit comments

Comments
 (0)