Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSRegularExpression: Improved Error and Exception handling to match macOS behaviour #343

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion Source/NSRegularExpression.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#import "Foundation/NSCoder.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSNotification.h"
#import "Foundation/FoundationErrors.h"
#import "Foundation/NSError.h"


/**
Expand Down Expand Up @@ -114,6 +116,15 @@ - (id) initWithPattern: (NSString*)aPattern
options: (NSRegularExpressionOptions)opts
error: (NSError**)e
{
// Raise an NSInvalidArgumentException to match macOS behaviour.
if (!aPattern) {
NSException *exp;

exp = [NSException exceptionWithName: NSInvalidArgumentException
reason: @"nil argument"];
[exp raise];
}

uint32_t flags = NSRegularExpressionOptionsToURegexpFlags(opts);
UText p = UTEXT_INITIALIZER;
UParseError pe = {0};
Expand All @@ -131,7 +142,27 @@ - (id) initWithPattern: (NSString*)aPattern
utext_close(&p);
if (U_FAILURE(s))
{
// FIXME: Do something sensible with the error parameter.
// Match macOS behaviour if the pattern is invalid.
// Example:
// Domain=NSCocoaErrorDomain
// Code=2048 "The value “<PATTERN>” is invalid."
// UserInfo={NSInvalidValue=<PATTERN>}
if (e) {
NSDictionary *userInfo;
NSString *description;

description = [NSString stringWithFormat:
@"The value “%@” is invalid.",
aPattern];

userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
aPattern, @"NSInvalidValue", description, NSLocalizedDescriptionKey, nil];

*e = [NSError errorWithDomain: NSCocoaErrorDomain
code: NSFormattingError
userInfo: userInfo];
}

[self release];
return nil;
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/base/NSRegularExpression/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSError.h>
#import <Foundation/FoundationErrors.h>

@interface DegeneratePatternTest : NSObject
{
Expand Down Expand Up @@ -169,6 +171,38 @@ int main()
beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
}
PASS(NO == [thread isExecuting], "Faulty regular expression terminated");

/* Testing the error handling when an invalid pattern is passed
* The error should look like this:
*
* Error Domain=NSCocoaErrorDomain Code=2048 "The value “(abc” is
* invalid." UserInfo={NSInvalidValue=(abc}
*/

NSError *error = nil;
NSRegularExpression *testObj3 =
[[NSRegularExpression alloc] initWithPattern: @"(abc"
options: 0
error: &error];
PASS(testObj3 == nil, "Invalid pattern: returns nil");
PASS(error != nil, "Invalid pattern: error is set");
PASS_EQUAL([error domain], NSCocoaErrorDomain,
"Invalid pattern: error domain is NSCocoaErrorDomain");
PASS_EQUAL([error code], NSFormattingError,
"Invalid pattern: error code is NSFormattingError");
PASS_EQUAL([[error userInfo] objectForKey: @"NSInvalidValue"],
@"(abc", "Invalid pattern: error message is correct");
PASS_EQUAL([[error userInfo] objectForKey: NSLocalizedDescriptionKey],
@"The value “(abc” is invalid.",
"Invalid pattern: localized description is correct");

/* Testing exception handling when nil is passed as pattern */
PASS_EXCEPTION([NSRegularExpression regularExpressionWithPattern: nil
options: 0
error: NULL],
NSInvalidArgumentException,
"nil pattern: throws NSInvalidArgumentException");

#endif

END_SET("NSRegularExpression")
Expand Down
Loading