{"id":3653,"date":"2013-03-19T16:15:49","date_gmt":"2013-03-19T23:15:49","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=3653"},"modified":"2019-07-03T10:13:06","modified_gmt":"2019-07-03T17:13:06","slug":"producing-better-bindings-for-xamarin-ios-and-xamarin-mac","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/producing-better-bindings-for-xamarin-ios-and-xamarin-mac\/","title":{"rendered":"Producing Better Bindings for Xamarin.iOS and Xamarin.Mac"},"content":{"rendered":"<p>\t\t\t\tFor .NET developers with some knowledge of Objective-C, writing .NET bindings for Objective-C libraries is not very hard. It is well <a href=\"http:\/\/docs.xamarin.com\/ios\/Guides\/Advanced_Topics\/Binding_Objective-C_Libraries\">documented<\/a> and several <a href=\"https:\/\/github.com\/mono\/monotouch-bindings\">complete examples<\/a> are available to guide you.<\/p>\n<p>Still, the process can be quite error-prone. Many typos and mistakes will only be caught at runtime and only when you use the specific API affected. Due to those issues, many bugs won&#8217;t be found for days, months or even years after you released your bindings.<\/p>\n<p>Full test coverage would considerably reduce the number of bugs\u2014but it would require 2x-5x time to create the bindings. That might not be the best way to invest your time into a project. Fortunately, the Objective-C <a href=\"https:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Reference\/ObjCRuntimeRef\/Reference\/reference.html\">runtime<\/a> has introspection APIs available, even if limited compared to .NET. That makes it possible to run generalized test cases without knowing the purpose for which the bindings were intended.<\/p>\n<p>This blog series will go through different test fixtures to help test <a href=\"http:\/\/xamarin.com\/monotouch\">Xamarin.iOS<\/a> or <a href=\"http:\/\/xamarin.com\/mac\">Xamarin.Mac<\/a> Objective-C bindings. With a bit of customization (you need to inherit from them and override a few methods) you will be able to spot some of the most common binding mistakes. While this is not a pain-free approach, you should be able find a lot of bugs in a very short amount of time.<\/p>\n<p><strong>Basics<\/strong><\/p>\n<p>All the test fixtures inherit from a single base class: <a href=\"https:\/\/github.com\/mono\/maccore\/blob\/master\/tests\/bindings\/ApiBaseTest.cs\"><code>ApiBaseTest.cs<\/code><\/a>. It defines an <code>Assembly<\/code> property that you&#8217;ll need to override to point to your binding assembly. E.g. to test the <a href=\"https:\/\/github.com\/mono\/monotouch-bindings\/tree\/master\/cocos2d\">Cocos2D bindings<\/a> you would need this piece of code:<\/p>\n<pre class=\"lang:csharp decode:true\">\nprotected override Assembly Assembly {\n     get { return typeof (CCAccelAmplitude).Assembly; }\n}\n<\/pre>\n<p>This base fixture also allow us to <em>cheat<\/em> a bit by introducing two properties that you should, when you <strong>start<\/strong> testing, set to <code>true<\/code> in your own inherited fixtures. Once every failure has been fixed it&#8217;s safe to turn them off\u2014and let the test fixtures behave like any others.<\/p>\n<p><strong><code>ContinueOnFailure = true;<\/code><\/strong><\/p>\n<p>This property will avoid stopping the test execution when failing an assertion. Doing otherwise would require too many &#8220;build \/ run \/ debug&#8221; cycles to go through dozens (or thousands) of types, methods or parameters. You&#8217;ll often be able to fix several issues between builds, progressing a lot quicker toward your goal.<\/p>\n<p><strong><code>LogProgress = true;<\/code><\/strong><\/p>\n<p>It&#8217;s likely you&#8217;ll hit bugs that will crash your testing process\u2014for example, a bad API signature can corrupt the stack. When that occurs, you want to know what was being executed. Turning on the <code>LogProgress<\/code> property will give you this information (and a lot of lines in your console).<\/p>\n<p>Even with those properties to help you, there will be failures that require a bit more knowledges to solve\u2014ways to ignore (special cases) or when the test fixture requires a bit of extra help to get the most of out them. To help you get down to zero failures, each test fixture will be documented in it&#8217;s own blog entry. Stay tuned for the rest of the series!<\/p>\n<p><b>Read the rest of the series:<\/b><\/p>\n<ul>\n<li><a href=\"\/producing-better-bindings-1-constructors\/\">Part 1: Constructors<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-2-fields\/\">Part 2: Fields<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-3-selectors\/\">Part 3: Selectors<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-4-signatures\/\">Part 4: Signatures<\/a><\/li>\n<\/ul>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/2433\/producing-better-bindings\">Discuss this post in the Xamarin Forums<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For .NET developers with some knowledge of Objective-C, writing .NET bindings for Objective-C libraries is not very hard. It is well documented and several complete examples are available to guide you. Still, the process can be quite error-prone. Many typos and mistakes will only be caught at runtime and only when you use the specific [&hellip;]<\/p>\n","protected":false},"author":5741,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[303],"tags":[6,4],"class_list":["post-3653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>For .NET developers with some knowledge of Objective-C, writing .NET bindings for Objective-C libraries is not very hard. It is well documented and several complete examples are available to guide you. Still, the process can be quite error-prone. Many typos and mistakes will only be caught at runtime and only when you use the specific [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/5741"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=3653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=3653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=3653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=3653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}