-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(iOS): preserve null values in bridged types (#4072)
- Loading branch information
Showing
7 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// convenience wrappers to transform Arrays between NSNull and Optional values, for interoperability with Obj-C | ||
extension Array: CapacitorExtension {} | ||
extension CapacitorExtensionTypeWrapper where T == Array<JSValue> { | ||
public func replacingNullValues() -> Array<JSValue?> { | ||
return baseType.map({ (value) -> JSValue? in | ||
if value is NSNull { | ||
return nil | ||
} | ||
return value | ||
}) | ||
} | ||
|
||
public func replacingOptionalValues() -> Array<JSValue> { | ||
return baseType | ||
} | ||
} | ||
|
||
extension CapacitorExtensionTypeWrapper where T == Array<JSValue?> { | ||
public func replacingNullValues() -> Array<JSValue?> { | ||
return baseType | ||
} | ||
|
||
public func replacingOptionalValues() -> Array<JSValue> { | ||
return baseType.map({ (value) -> JSValue in | ||
if let value = value { | ||
return value | ||
} | ||
return NSNull() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Foundation | ||
@testable import Capacitor | ||
|
||
enum BridgeTypeError: Error { | ||
case badCast | ||
} | ||
|
||
@objc class BridgedTypesHelper: NSObject { | ||
@objc static let shared = BridgedTypesHelper() | ||
|
||
var untypedArray: [Any] { | ||
return [] | ||
} | ||
|
||
@objc func validTransformationOf(array: [Any]) -> [Any] { | ||
let result = JSTypes.coerceArrayToJSArray(array)!.capacitor.replacingNullValues() | ||
return result.capacitor.replacingOptionalValues() as [Any] | ||
} | ||
|
||
@objc func invalidTransformationOf(array: [Any]) -> [Any] { | ||
let result = JSTypes.coerceArrayToJSArray(array)!.capacitor.replacingNullValues() | ||
return result as [Any] | ||
} | ||
|
||
@objc func testCast(of array: [Any], atIndex index: Int) throws -> Any { | ||
if let castArray = array as? [JSValue] { | ||
return castArray[index] as Any | ||
} | ||
throw BridgeTypeError.badCast | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#import <XCTest/XCTest.h> | ||
#import <Capacitor/Capacitor.h> | ||
// forward declaration of internal capacitor classes that are exposed in the swift header via the @testable import. | ||
@interface CAPWebViewAssetHandler: NSObject | ||
@end | ||
@interface CapacitorBridge: NSObject | ||
@end | ||
@interface CAPWebViewDelegationHandler: NSObject | ||
@end | ||
// import that will fail without the declarations | ||
#import "CapacitorTests-Swift.h" | ||
|
||
// interface for this class | ||
@interface BridgedTypesTestsObjc : XCTestCase | ||
@end | ||
|
||
@implementation BridgedTypesTestsObjc | ||
|
||
- (void)setUp { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
- (void)tearDown { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
- (void)testNullHandling { | ||
NSArray* source = @[@"test", [NSNull null], @3]; | ||
NSArray* result = [[BridgedTypesHelper shared] validTransformationOfArray:source]; | ||
NSError *error = nil; | ||
// test that the replaced null value exists | ||
id value = [result objectAtIndex:1]; | ||
XCTAssertNotNil(value); | ||
XCTAssertTrue([value isKindOfClass:[NSNull class]]); | ||
// test that the null value casts to non-optional | ||
value = [[BridgedTypesHelper shared] testCastOf:result atIndex:1 error:&error]; | ||
XCTAssertNotNil(value); | ||
XCTAssertNil(error); | ||
} | ||
|
||
- (void)testOptionalHandling { | ||
NSArray* source = @[@"test", [NSNull null], @3]; | ||
NSArray* result = [[BridgedTypesHelper shared] invalidTransformationOfArray:source]; | ||
NSError *error = nil; | ||
// test that the removed null value, now optional, is automatically transformed back into a NSNull | ||
id value = [result objectAtIndex:1]; | ||
XCTAssertNotNil(value); | ||
XCTAssertTrue([value isKindOfClass:[NSNull class]]); | ||
// test that the optional value fails to cast to non-optional | ||
value = [[BridgedTypesHelper shared] testCastOf:result atIndex:1 error:&error]; | ||
XCTAssertNil(value); | ||
XCTAssertNotNil(error); | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters