Swift Unit Testing with Xctassertthrows Analogue

Swift Unit testing with XCTAssertThrows analogue

If you add the following three files to your tests:

//  ThrowsToBool.h
#import <Foundation/Foundation.h>

/// A 'pure' closure; has no arguments, returns nothing.
typedef void (^VoidBlock)(void);

/// Returns: true if the block throws an `NSException`, otherwise false
BOOL throwsToBool(VoidBlock block);

// ThrowsToBool.m
#import "ThrowsToBool.h"

BOOL throwsToBool(VoidBlock const block) {
@try {
block();
}
@catch (NSException * const notUsed) {
return YES;
}
return NO;
}

// xxxTests-Bridging-Header.h
#import "ThrowsToBool.h"

Then you can write:

XCTAssert(throwsToBool {
// test code that throws an NSException
})

But it doesn't work for assert or precondition :(

PS I got the idea from: http://modocache.io/xctest-the-good-parts

Testing Swift code with preconditions

You test Swift methods that have preconditions by only testing behavior with inputs that meet those preconditions. The behavior when the precondition is not met is clear without further testing, since preconditions are baked into the standard library.

If you doubt you've correctly specified a precondition, you can pull out the precondition expression into a Boolean function, then test that for correctly discerning valid from invalid inputs.

However, you can create for yourself what you wish for here:

An option to generate exceptions on precondition/assertion failures would do the trick: one would turn it on in debug builds, and turn it off in release builds.

Instead of calling Apple's precondition, you can write your own function, say, require. This could be conditionally compiled to act as you wish. You would then use this everywhere you'd otherwise be using precondition.

Is Scrum effective on a team where all of its members are amateurs?

The discussion is usually agile vs. waterfall, right? I am linking an article, but it is in Portuguese, so I'll try to transmit some of its ideas:

Waterfall is like chess. You think and plan a lot, try to foresee every possible issue as soon as possible. There's a lot of planning, but makes sense only on stable and well-known domains, where change isn't much expected.

Agile is like soccer (or many collective sports): decisions are made in-game and should be done fast. There's no much time to analyze every consequence. It is "ideal" for dynamic and unstable domains, where change is always expected (web applications, for instance, tend to fall in this category). Another point to note is: even if you have the best players, if they don't do well as a team, you won't be the winner.

IMHO, Scrum would be useful, because:

  • Once every two weeks (or every month, depending on iteration time) you'll be able to see what's working or not. And this is very valuable, specially as an "amateur" team, which is expected to be learning and finding things out much more constantly.
  • As amateurs, you probably won't be able to foresee everything (and that's something agile embraces)
  • There's more space for sharing experience (stand-up meeting, retrospective, and even planning meeting). And you share REAL experience (you must write code every week rather than just plan)

Help with a jquery country/state list

Maybe this could be a fine solution for you:

HTML selection and input fields:

<select name="country" id="country" class="textarealong  signup_good"/>
<option value=1001>Choose a Country</option>
<option value=238>Zimbabwe</option>
<option value=239>Rwanda</option>
</select>

USA state dropdown list
<select name="usstate" id="usstate" class="textarealong signup_good"/>
<option value=1001>Choose a State</option>
<option value=238>Florida</option>
</select>

<input id="otherstate"/>

Then the jQuery part with some 'ready'-magic: hide the otherstate field and trigger the change event on loading like Greg mentioned already. This makes sure, that if a country was already selected on page load the right form field will be selected:

<script type="text/javascript">
$(document).ready(function () {
$("#otherstate").hide();
$("#country").trigger("change");
});

$("#country").change(function () {
if ($("#country").val() != '1001') {
$("#usstate").hide();
$("#otherstate").show();
} else {
$("#usstate").show();
$("#otherstate").hide();
}
});
</script>

Hopes this will help you! ;)

How to provide a explicit specialization to only one method in a C++ template class?

Alternatively to Martin York's inline solution you could also do in your header file:

class D { };
template<> void C<D>::A(); // Don't implement here!

And supply a .cpp file with the implementation:

template<> void C<D>::A() { /* do code here */ }

So you avoid the multiple definitions by supplying a single one.
This is also good to hide implementations for specific Types away from the template header file when publishing the library.



Related Topics



Leave a reply



Submit