2015-07-29 12:54:59 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-07-29 12:54:59 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-07-29 12:54:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTModuleMethod.h>
|
2015-07-29 12:54:59 +00:00
|
|
|
|
|
|
|
@interface RCTMethodArgumentTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTMethodArgumentTests
|
|
|
|
|
2017-11-03 23:28:47 +00:00
|
|
|
extern NSString *RCTParseMethodSignature(const char *methodSignature, NSArray **argTypes);
|
2015-07-29 12:54:59 +00:00
|
|
|
|
|
|
|
- (void)testOneArgument
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSInteger)foo";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)1);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSInteger");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testTwoArguments
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSInteger)foo bar:(BOOL)bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSInteger");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testSpaces
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo : (NSInteger)foo bar : (BOOL) bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSInteger");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNewlines
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo : (NSInteger)foo\nbar : (BOOL) bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSInteger");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testUnnamedArgs
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSInteger)foo:(BOOL)bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo::");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSInteger");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testUntypedUnnamedArgs
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:foo:bar:bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:::");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)3);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"id");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"id");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[2]).type, @"id");
|
|
|
|
}
|
|
|
|
|
2017-08-14 12:16:45 +00:00
|
|
|
- (void)testNamespacedCxxStruct
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-09-07 16:11:27 +00:00
|
|
|
const char *methodSignature = "foo:(foo::type &)foo bar:(bar::type &)bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2017-09-07 16:11:27 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
2017-08-14 12:16:45 +00:00
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"foo::type");
|
2017-09-07 16:11:27 +00:00
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"bar::type");
|
2017-08-14 12:16:45 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 12:54:59 +00:00
|
|
|
- (void)testAttributes
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(__attribute__((unused)) NSString *)foo bar:(__unused BOOL)bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSString");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNullability
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(nullable NSString *)foo bar:(nonnull NSNumber *)bar baz:(id)baz";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:baz:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)3);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSString");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"NSNumber");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[2]).type, @"id");
|
|
|
|
XCTAssertEqual(((RCTMethodArgument *)arguments[0]).nullability, RCTNullable);
|
|
|
|
XCTAssertEqual(((RCTMethodArgument *)arguments[1]).nullability, RCTNonnullable);
|
|
|
|
XCTAssertEqual(((RCTMethodArgument *)arguments[2]).nullability, RCTNullabilityUnspecified);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testSemicolonStripping
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSString *)foo bar:(BOOL)bar;";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-29 12:54:59 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSString");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"BOOL");
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
- (void)testUnused
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(__unused NSString *)foo bar:(NSNumber *)bar";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:bar:");
|
2015-07-31 13:55:47 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)2);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSString");
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[1]).type, @"NSNumber");
|
|
|
|
XCTAssertTrue(((RCTMethodArgument *)arguments[0]).unused);
|
|
|
|
XCTAssertFalse(((RCTMethodArgument *)arguments[1]).unused);
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:09:04 +00:00
|
|
|
- (void)testGenericArray
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSArray<NSString *> *)foo;";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:");
|
2015-12-10 18:09:04 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)1);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSStringArray");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNestedGenericArray
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSArray<NSArray<NSString *> *> *)foo;";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:");
|
2015-12-10 18:09:04 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)1);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSStringArrayArray");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testGenericSet
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSSet<NSNumber *> *)foo;";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:");
|
2015-12-10 18:09:04 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)1);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSNumberSet");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testGenericDictionary
|
|
|
|
{
|
|
|
|
NSArray *arguments;
|
2017-07-24 13:46:01 +00:00
|
|
|
const char *methodSignature = "foo:(NSDictionary<NSString *, NSNumber *> *)foo;";
|
2017-11-03 23:28:47 +00:00
|
|
|
NSString *selector = RCTParseMethodSignature(methodSignature, &arguments);
|
|
|
|
XCTAssertEqualObjects(selector, @"foo:");
|
2015-12-10 18:09:04 +00:00
|
|
|
XCTAssertEqual(arguments.count, (NSUInteger)1);
|
|
|
|
XCTAssertEqualObjects(((RCTMethodArgument *)arguments[0]).type, @"NSNumberDictionary");
|
|
|
|
}
|
|
|
|
|
2015-07-29 12:54:59 +00:00
|
|
|
@end
|