From dcdbf78daf5de61a5f33d66f1e6d86457eab9669 Mon Sep 17 00:00:00 2001 From: TJ Lambert Date: Mon, 13 Jun 2022 10:40:22 -0500 Subject: [PATCH 1/3] Validating Null ignores and adding tests --- src/passkit.cs | 18 ++++++----- tests/monotouch-test/PassKit/PKPassTest.cs | 24 +++++++++++++++ .../PassKit/PKPaymentRequestTest.cs | 22 ++++++++++++++ .../PassKit/PKPaymentSummaryItemTest.cs | 30 +++++++++++++++++++ .../common-PassKit.ignore | 14 +-------- tests/xtro-sharpie/common-PassKit.ignore | 14 +-------- 6 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 tests/monotouch-test/PassKit/PKPassTest.cs create mode 100644 tests/monotouch-test/PassKit/PKPaymentSummaryItemTest.cs diff --git a/src/passkit.cs b/src/passkit.cs index 7a0a6f46f92a..24fa9bb22a79 100644 --- a/src/passkit.cs +++ b/src/passkit.cs @@ -81,6 +81,7 @@ interface PKPassLibrary { PKPass[] GetPasses (); [Export ("passWithPassTypeIdentifier:serialNumber:")] + [return: NullAllowed] PKPass GetPass (string identifier, string serialNumber); [iOS (8,0)] @@ -264,7 +265,7 @@ interface PKPayment { [Deprecated (PlatformName.iOS, 9, 0, message: "Use 'ShippingContact' instead.")] ABRecord ShippingAddress { get; } - [Export ("shippingMethod", ArgumentSemantic.Strong)] + [NullAllowed, Export ("shippingMethod", ArgumentSemantic.Strong)] PKShippingMethod ShippingMethod { get; } @@ -813,7 +814,7 @@ interface PKPass : NSSecureCoding, NSCopying { [Export ("initWithData:error:")] NativeHandle Constructor (NSData data, out NSError error); - [Export ("authenticationToken", ArgumentSemantic.Copy)] + [NullAllowed, Export ("authenticationToken", ArgumentSemantic.Copy)] string AuthenticationToken { get; } [NoWatch] @@ -838,16 +839,17 @@ interface PKPass : NSSecureCoding, NSCopying { [Export ("passURL", ArgumentSemantic.Copy)] NSUrl PassUrl { get; } - [Export ("relevantDate", ArgumentSemantic.Copy)] + [NullAllowed, Export ("relevantDate", ArgumentSemantic.Copy)] NSDate RelevantDate { get; } [Export ("serialNumber", ArgumentSemantic.Copy)] string SerialNumber { get; } - [Export ("webServiceURL", ArgumentSemantic.Copy)] + [NullAllowed, Export ("webServiceURL", ArgumentSemantic.Copy)] NSUrl WebServiceUrl { get; } [Export ("localizedValueForFieldKey:")] + [return: NullAllowed] NSObject GetLocalizedValue (NSString key); // TODO: Should be enum for PKPassLibraryUserInfoKey #if !NET @@ -857,7 +859,7 @@ interface PKPass : NSSecureCoding, NSCopying { #endif [iOS (7,0)] - [Export ("userInfo", ArgumentSemantic.Copy)] + [NullAllowed, Export ("userInfo", ArgumentSemantic.Copy)] NSDictionary UserInfo { get; } [iOS (8,0)] @@ -1334,7 +1336,7 @@ interface PKPaymentAuthorizationResult { [Export ("status", ArgumentSemantic.Assign)] PKPaymentAuthorizationStatus Status { get; set; } - [Export ("errors", ArgumentSemantic.Copy)] + [NullAllowed, Export ("errors", ArgumentSemantic.Copy)] NSError[] Errors { get; set; } } @@ -1372,7 +1374,7 @@ interface PKPaymentRequestShippingContactUpdate { [Export ("shippingMethods", ArgumentSemantic.Copy)] PKShippingMethod[] ShippingMethods { get; set; } - [Export ("errors", ArgumentSemantic.Copy)] + [NullAllowed, Export ("errors", ArgumentSemantic.Copy)] NSError[] Errors { get; set; } } @@ -1400,7 +1402,7 @@ interface PKPaymentRequestPaymentMethodUpdate { NativeHandle Constructor ([NullAllowed] NSError[] errors, PKPaymentSummaryItem [] paymentSummaryItems); [Watch (6,0), iOS (13,0)] - [Export ("errors", ArgumentSemantic.Copy)] + [NullAllowed, Export ("errors", ArgumentSemantic.Copy)] NSError [] Errors { get; set; } // inlined diff --git a/tests/monotouch-test/PassKit/PKPassTest.cs b/tests/monotouch-test/PassKit/PKPassTest.cs new file mode 100644 index 000000000000..d2c055af8f7c --- /dev/null +++ b/tests/monotouch-test/PassKit/PKPassTest.cs @@ -0,0 +1,24 @@ +#if !__TVOS__ && !MONOMAC + +using System; +using Foundation; +using UIKit; +using PassKit; +using NUnit.Framework; + +namespace MonoTouchFixtures.PassKit { + + [TestFixture] + [Preserve (AllMembers = true)] + public class PKPassTest { + + [Test] + public void GetLocalizedValueNull () + { + using var pass = new PKPass (); + Assert.IsNull (pass.GetLocalizedValue (new NSString ()), "'PKPass.GetLocalizedValue' is not returning a null value"); + } + } +} + +#endif diff --git a/tests/monotouch-test/PassKit/PKPaymentRequestTest.cs b/tests/monotouch-test/PassKit/PKPaymentRequestTest.cs index 1a3b114af670..d0787f05afb1 100644 --- a/tests/monotouch-test/PassKit/PKPaymentRequestTest.cs +++ b/tests/monotouch-test/PassKit/PKPaymentRequestTest.cs @@ -64,6 +64,28 @@ public void WeakRequiredBillingContactFields () } } } + + [Test] + public void CheckDefaultNulls () + { + using var pr = new PKPaymentRequest (); + Assert.IsNull (pr.CountryCode, "'PKPaymentRequest.CountryCode' is not returning null by default."); + Assert.IsNull (pr.CurrencyCode, "'PKPaymentRequest.CurrencyCode' is not returning null by default."); + Assert.IsNull (pr.MerchantIdentifier, "'PKPaymentRequest.MerchantIdentifier' is not returning null by default."); + Assert.IsNull (pr.PaymentSummaryItems, "'PKPaymentRequest.PaymentSummaryItems' is not returning null by default."); + Assert.IsNull (pr.SupportedNetworks, "'PKPaymentRequest.SupportedNetworks' is not returning null by default."); + + Assert.DoesNotThrow (delegate { pr.CountryCode = null; }, + "'PKPaymentRequest.CountryCode' cannot be set to null."); + Assert.DoesNotThrow (delegate { pr.CurrencyCode = null; }, + "'PKPaymentRequest.CurrencyCode' cannot be set to null."); + Assert.DoesNotThrow (delegate { pr.MerchantIdentifier = null; }, + "'PKPaymentRequest.MerchantIdentifier' cannot be set to null."); + Assert.DoesNotThrow (delegate { pr.PaymentSummaryItems = null; }, + "'PKPaymentRequest.PaymentSummaryItems' cannot be set to null."); + Assert.DoesNotThrow (delegate { pr.SupportedNetworks = null; }, + "'PKPaymentRequest.SupportedNetworks' cannot be set to null."); + } } } diff --git a/tests/monotouch-test/PassKit/PKPaymentSummaryItemTest.cs b/tests/monotouch-test/PassKit/PKPaymentSummaryItemTest.cs new file mode 100644 index 000000000000..010360f137d2 --- /dev/null +++ b/tests/monotouch-test/PassKit/PKPaymentSummaryItemTest.cs @@ -0,0 +1,30 @@ +#if !__TVOS__ && !MONOMAC + +using System; +using Foundation; +using UIKit; +using PassKit; +using NUnit.Framework; + +namespace MonoTouchFixtures.PassKit { + + [TestFixture] + [Preserve (AllMembers = true)] + public class PKPaymentSummaryItemTest { + + [Test] + public void CheckDefaultNulls () + { + using var ps = new PKPaymentSummaryItem (); + Assert.IsNull (ps.Amount, "'PKPaymentSummaryItem.Amount' is not returning null by default."); + Assert.IsNull (ps.Label, "'PKPaymentSummaryItem.Label' is not returning null by default."); + + Assert.DoesNotThrow (delegate { ps.Amount = null; }, + "'PKPaymentSummaryItem.Amount' cannot be set to null."); + Assert.DoesNotThrow (delegate { ps.Label = null; }, + "'PKPaymentSummaryItem.Label' cannot be set to null."); + } + } +} + +#endif diff --git a/tests/xtro-sharpie/api-annotations-dotnet/common-PassKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/common-PassKit.ignore index 28fe5ad5e621..2e09a571d21c 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/common-PassKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/common-PassKit.ignore @@ -1,4 +1,4 @@ -# Initial result from new rule extra-null-allowed +# By default these are null !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CountryCode(System.String)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CurrencyCode(System.String)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_MerchantIdentifier(System.String)' has a extraneous [NullAllowed] on parameter #0 @@ -6,15 +6,3 @@ !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_SupportedNetworks(Foundation.NSString[])' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Amount(Foundation.NSDecimalNumber)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Label(System.String)' has a extraneous [NullAllowed] on parameter #0 - -# Initial result from new rule missing-null-allowed -!missing-null-allowed! 'Foundation.NSDate PassKit.PKPass::get_RelevantDate()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSDictionary PassKit.PKPass::get_UserInfo()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSObject PassKit.PKPass::GetLocalizedValue(Foundation.NSString)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSUrl PassKit.PKPass::get_WebServiceUrl()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'PassKit.PKPass PassKit.PKPassLibrary::GetPass(System.String,System.String)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'PassKit.PKShippingMethod PassKit.PKPayment::get_ShippingMethod()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'System.String PassKit.PKPass::get_AuthenticationToken()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'System.Void PassKit.PKPaymentAuthorizationResult::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestPaymentMethodUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestShippingContactUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 diff --git a/tests/xtro-sharpie/common-PassKit.ignore b/tests/xtro-sharpie/common-PassKit.ignore index 391b0195a351..6e0313d47124 100644 --- a/tests/xtro-sharpie/common-PassKit.ignore +++ b/tests/xtro-sharpie/common-PassKit.ignore @@ -5,7 +5,7 @@ !incorrect-protocol-member! PKPaymentAuthorizationControllerDelegate::paymentAuthorizationController:didAuthorizePayment:completion: is OPTIONAL and should NOT be abstract !incorrect-protocol-member! PKPaymentAuthorizationViewControllerDelegate::paymentAuthorizationViewControllerWillAuthorizePayment: is OPTIONAL and should NOT be abstract -# Initial result from new rule extra-null-allowed +# By default these are null !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CountryCode(System.String)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_CurrencyCode(System.String)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_MerchantIdentifier(System.String)' has a extraneous [NullAllowed] on parameter #0 @@ -13,15 +13,3 @@ !extra-null-allowed! 'System.Void PassKit.PKPaymentRequest::set_SupportedNetworks(Foundation.NSString[])' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Amount(Foundation.NSDecimalNumber)' has a extraneous [NullAllowed] on parameter #0 !extra-null-allowed! 'System.Void PassKit.PKPaymentSummaryItem::set_Label(System.String)' has a extraneous [NullAllowed] on parameter #0 - -# Initial result from new rule missing-null-allowed -!missing-null-allowed! 'Foundation.NSDate PassKit.PKPass::get_RelevantDate()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSDictionary PassKit.PKPass::get_UserInfo()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSObject PassKit.PKPass::GetLocalizedValue(Foundation.NSString)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSUrl PassKit.PKPass::get_WebServiceUrl()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'PassKit.PKPass PassKit.PKPassLibrary::GetPass(System.String,System.String)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'PassKit.PKShippingMethod PassKit.PKPayment::get_ShippingMethod()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'System.String PassKit.PKPass::get_AuthenticationToken()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'System.Void PassKit.PKPaymentAuthorizationResult::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestPaymentMethodUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void PassKit.PKPaymentRequestShippingContactUpdate::set_Errors(Foundation.NSError[])' is missing an [NullAllowed] on parameter #0 From e99179f34c2fcd7a459cc8dcd468b7223ed71227 Mon Sep 17 00:00:00 2001 From: TJ Lambert Date: Mon, 13 Jun 2022 14:36:20 -0500 Subject: [PATCH 2/3] add nullability --- src/PassKit/PKPaymentRequest.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PassKit/PKPaymentRequest.cs b/src/PassKit/PKPaymentRequest.cs index f23738b1ab66..75a99354b81b 100644 --- a/src/PassKit/PKPaymentRequest.cs +++ b/src/PassKit/PKPaymentRequest.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using Foundation; using ObjCRuntime; From 89acd37bbaa11a273ca4c20bbad0b6c781459a69 Mon Sep 17 00:00:00 2001 From: TJ Lambert Date: Mon, 13 Jun 2022 14:38:22 -0500 Subject: [PATCH 3/3] use is null --- src/PassKit/PKPaymentRequest.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PassKit/PKPaymentRequest.cs b/src/PassKit/PKPaymentRequest.cs index 75a99354b81b..2024586e5d82 100644 --- a/src/PassKit/PKPaymentRequest.cs +++ b/src/PassKit/PKPaymentRequest.cs @@ -11,13 +11,13 @@ public partial class PKContactFieldsExtensions { static public PKContactFields GetValue (NSSet set) { var fields = PKContactFields.None; - if (set == null) + if (set is null) return fields; foreach (PKContactFields value in Enum.GetValues (typeof (PKContactFields))) { var constant = value.GetConstant (); // None does not have an associated native value and Contains would throw an ANE - if ((constant != null) && set.Contains (constant)) + if ((constant is not null) && set.Contains (constant)) fields |= value; } return fields; @@ -33,7 +33,7 @@ static public NSSet GetSet (PKContactFields values) if (values.HasFlag (value)) { var constant = value.GetConstant (); // None does not have an associated native value and Contains would throw an ANE - if (constant != null) + if (constant is not null) set.Add (constant); } }