<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">It is true that testing an assert is not really possible, as it basically crashes the app.</div><div class=""><br class=""></div><div class="">But we have to take into account that the behaviour of the assert method is not strait-forward : it depends on what is the optimisation level. Here is an extract of the inline doc of the assert method :</div><div class=""><div class=""><br class=""></div><div class="">* In playgrounds and -Onone builds (the default for Xcode's Debug</div><div class="">&nbsp; configuration): if `condition` evaluates to false, stop program</div><div class="">&nbsp; execution in a debuggable state after printing `message`.</div><div class="">* In -O builds (the default for Xcode's Release configuration),</div><div class="">&nbsp; `condition` is not evaluated, and there are no effects.</div><div class="">* In -Ounchecked builds, `condition` is not evaluated, but the</div><div class="">&nbsp; optimizer may assume that it *would* evaluate to `true`. Failure</div><div class="">&nbsp; to satisfy that assumption in -Ounchecked builds is a serious</div><div class="">&nbsp; programming error.</div></div><div class=""><br class=""></div><div class="">I have the feeling that assertions are not meant to be tested, as they are not meant to be executed in production code.</div><div class="">And I have to add that writing tests that would not behave the same depending on the optimisation level would make me feel rather uncomfortable.</div><div class=""><br class=""></div><div class="">Maybe throwing an error would be more adapted for a validation that must also occur on production builds ?</div><div class="">Then, testing it would be pretty easy, with an extension to XCTestCase like that :</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>XCTestCase<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> XCTAssertThrows(file: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">__FILE__</span>, line: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">UInt</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">__LINE__</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">_</span> closure:() <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">throws</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">do</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">try</span> closure();</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; XCTFail(<span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"Closure did not throw an error"</span>, file: file, line: line);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; } <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">catch</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; </span>// expected, nothing to do</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> XCTAssertNoThrow&lt;T&gt;(file: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">__FILE__</span>, line: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">UInt</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">__LINE__</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">_</span> closure:() <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">throws</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">do</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">try</span> closure();</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; } <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">catch</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> error {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; XCTFail(<span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">"Closure throw unexpected error </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">(</span>error<span style="font-variant-ligatures: no-common-ligatures; color: #b4261a" class="">)"</span>, file: file, line: line);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>nil<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">Jérôme</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 05 Jan 2016, at 01:42, Tony Parker via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Mohamed,<div class=""><br class=""></div><div class="">I agree it’s very difficult to test assertions in XCTest today. This approach looks interesting, but I’m not sure how it’s possible to implement within XCTest’s current architecture. Do you have any idea how this would be implemented?&nbsp;</div><div class=""><br class=""></div><div class="">- Tony</div><div class=""><br class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 31, 2015, at 1:35 AM, Mohamed Ebrahim Afifi via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Right now, we cannot easily test failed assertions (assert, assertionFailure, precondition, preconditionFailure, fatalError) in our own code without some hacks in the code under test. Like this example&nbsp;<a href="https://github.com/mohamede1945/AssertionsTestingExample" class="">https://github.com/mohamede1945/AssertionsTestingExample</a></div><div class=""><br class=""></div><div class="">So, my suggestion is to add for XCTest something very similar to&nbsp;<b class="">expectCrashLater</b> that is defined here&nbsp;<a href="https://github.com/apple/swift/blob/9b15d03b73b9e8a6dbd3f71b5c78660a359e8e26/stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb" class="">https://github.com/apple/swift/blob/9b15d03b73b9e8a6dbd3f71b5c78660a359e8e26/stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb</a></div><div class="">and used in tests like this example&nbsp;<a href="https://github.com/apple/swift/blob/master/validation-test/stdlib/Assert.swift" class="">https://github.com/apple/swift/blob/master/validation-test/stdlib/Assert.swift</a></div><div class=""><br class=""></div><div class="">What do you think?</div><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class="">Best Regards,<div class="">Mohamed Afifi</div></div></div></div></div></div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=U7z7oSqRMSwdl2rYt9JJ4zIFaeg4R-2FxExg9xndDuCbwNbdxz0U2-2FzrvmcSyuDynq9bISO-2BMyFrjnnW4NVXO90-2F86M0Laqvo71eSsSXdvJJdDKBdSeykbEqfIrlJIyw0VBhuZ8Qs71nh07gioa9Yb2UqJPEWye1XOgxvU-2BFTxLOWJMvjZvnku1wDHnFCRntuqfcCttzlCmkRRcPR-2Fd3kgTgNx8Mze5kMsH-2FpL1rxrE0E-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZfNDJ3c1KY40DpRPxaBpmw0PAd1TZW0h3lqFWlwcZNXMQrRhhT3ydcBjuxUom7RfZB9AcGHAXWt0S-2BQsgO63hZDC0uaQ9pe213HPP8Vj-2FtBCTWgiKuOtkq9x9mVFekMGIrZFTnrd-2FyjTvhr-2F0nRfOodNKH-2Bug5SumvnYuUi50Fh-2FrROKL-2FW4jDZmVeql2RO-2B2yk2LNFAT0AUFNfCiOnGAvpKvJVXeqMi-2B-2FdBbZSHUec-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>