We were returning a cannedErrorResponse here which wasn't needed and was actually returning before the method completed
BTJSON_Tests.testLargerMixedJSONWithEmoji()
"aString": "Hello, JSON 😍!" and "anInvalidURL": ":™£¢://://://???!!!" were failing to evaluate as nil when calling BTJSON.asURL() - neither of these are URLs so we expect that method to evaluate as nil
Adding a check of urlString.utf8.count == urlString.utf16.count allows us to evaluate the string in the same way that we were in Obj-C
More details on this test as a whole below...
The Why
I found this super interesting so adding it here if other folks are also interested:
The testLargerMixedJSONWithEmoji test has been failing because "😍" is 2 extra bytes in UTF-8 vs UTF-16 and "™£¢" is 4 extra bytes in UTF-8 vs UTF-16
Lots of discussions about eventually adding some logic around this to Foundation directly in the Swift forums
Alternatives
There are some alternatives to the check I added here that we can explore, but the approach I took was the closest to our v5 implementation here which used asString to see if it could be cast to a NSString (UTF-16) and if not returned null. Some alternatives:
Cast urlString to NSString - downside, we would then need to cast it back to a string in return URL(string: urlString) because NSString is not implicitly convertible to String
Using something like UIApplication.shared.canOpenURL could be an option but we would need to replace our URLs in tests with actual URLs. This also seems like overkill a bit imo, but open to opinions!
Summary of changes
BTSEPADirectDebitClient_Tests.testTokenizeWithPresentationContext_handleCreateMandateReturnsInvalidURL_returnsError_andSendsAnalytics()
cannedErrorResponse
here which wasn't needed and was actually returning before the method completedBTJSON_Tests.testLargerMixedJSONWithEmoji()
"aString": "Hello, JSON 😍!"
and"anInvalidURL": ":™£¢://://://???!!!"
were failing to evaluate asnil
when callingBTJSON.asURL()
- neither of these are URLs so we expect that method to evaluate asnil
urlString.utf8.count == urlString.utf16.count
allows us to evaluate the string in the same way that we were in Obj-CThe Why
I found this super interesting so adding it here if other folks are also interested:
NSString
- these are UTF-16 (more reading here)String
- these are UTF-8 (more reading here)testLargerMixedJSONWithEmoji
test has been failing because "😍" is 2 extra bytes in UTF-8 vs UTF-16 and "™£¢" is 4 extra bytes in UTF-8 vs UTF-16Alternatives
There are some alternatives to the check I added here that we can explore, but the approach I took was the closest to our v5 implementation here which used
asString
to see if it could be cast to aNSString
(UTF-16) and if not returnednull
. Some alternatives:urlString
toNSString
- downside, we would then need to cast it back to a string inreturn URL(string: urlString)
becauseNSString
is not implicitly convertible toString
UIApplication.shared.canOpenURL
could be an option but we would need to replace our URLs in tests with actual URLs. This also seems like overkill a bit imo, but open to opinions!Checklist
Authors