• Stars
    star
    100
  • Rank 329,736 (Top 7 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Asserts on roids, test all your assumptions with ease.

KZAsserts - Asserts on roids, test all your assumptions with ease.

Version Platform

There are many ways in which we can improve quality of our code-base, Assertions are one of them. Yet very few people actually use asserts, why is that?

An assert is used to make certain that a specific condition is true, because our apps don't exist in a vacuum and we often work with other API's (or our own), we need to make sure our assumptions about it are actually valid or we might have really weird bugs.

If our assumption is wrong, assert will crash on the line that checks that condition thus allowing us to find bugs in our code very quickly.

This is great concept, crash early and crash often, it makes it easy to have clean and bug free code.

On the other hand Release build should avoid crashing whenever possible, unless you like 1-star rating?

Because of that normal assertions are not enough, you need to make sure your code handles errors even in release builds, if you just strip assertions from release you are still going to crash if your assumption was wrong (eg. after striping asserts you'll have code that mismatches types etc.)!

How great would it be to assert all assumptions regarding server API responses, so that if backend changes you know it immediately and you can fix your code? So people end up with something like this:

NSParameterAssert([dataFromServer isKindOfClass:[NSDictionary class]]);
if ([dataFromServer isKindOfClass:[NSDictionary class]]) {
  //! create NSError, handle it
}

NSParameterAssert([something isKindOfClass:[NSString class]]);
if ([something isKindOfClass:[NSString class]]) {
  //! create NSError, handle it
}

Obviously they could store that condition once and reuse it in assert and if statement, it still sucks!

Now imagine testing your whole response format, that would be so much unnecesary and hard to read code!

That's why I've come up with KZAsserts around 2 years ago, this is how that could can look:

AssertTrueOrReturnError([dataFromServer isKindOfClass:[NSDictionary class]]);
AssertTrueOrReturnError([something isKindOfClass:[NSString class]]);

This will crash in debug, but in release it will automatically generate NSError for you, it will then return that error object from the current scope. It can also log message to your logger / server / console: IMAGE

And that's not all, you probably write a lot of async code in your apps? KZAsserts handles that as easily:

- (void)downloadFromURL:(NSURL*)url withCompletion:(void (^)(NSData *, NSError *))completionBlock
{
	AssertTrueOrReturnNilBlock([something isKindOfClass:[NSString class]], ^(NSError *error) {
  		completionBlock(nil, error);
	});
	//! ...
}

With those kind of macros, you can now assert all your assumptions. I'd also encourage you to use Asserts for enforcing API contracts: eg. if you have a method that downloads NSData * and the only way to get the results is via completion block, you should assert there actually is completion block. Otherwise you are wasting your user battery/network.

Assert macros

KZAsserts provies following asserts:

  AssertTrueOr[X](condition) - if condition fails to be true, on debug builds it will crash by using Assertion, on Release builds it calls error creation and perform specific action. Asserts with block param will execute ^(NSError *){} passed in block with auto-generated NSError.

  AssertTrueOrReturnError
  AssertTrueOrReturnErrorBlock

  AssertTrueOrReturn
  AssertTrueOrReturnBlock

  AssertTrueOrReturnNo
  AssertTrueOrReturnNoBlock

  AssertTrueOrReturnNil
  AssertTrueOrReturnNilBlock

  AssertTrueOrContinue
  AssertTrueOrContinueBlock

  AssertTrueOrBreak
  AssertTrueOrBreakBlock

  AssertTrueOrIgnore
  AssertTrueOrIgnoreBlock

Installation

KZAsserts is available through CocoaPods for macOS, iOS and tvOS, to install it simply add the following line to your Podfile:

pod "KZAsserts"

Supplying your own NSError creation function

If you want to have your own NSError creation function you can just add following line on top of your applicationDidFinishLaunching:

[KZAsserts registerErrorFunction:myErrorCreationFunction];

You can also change whole format of logging by defining your own KZAMakeError macro.

Test Assertion flow during debugging

In debug version, the assert aborts the program, sometimes we want quick look, what's will happens in release version. If you adding symbolic breakpoint with action you can check it:

+[KZAsserts debugPass:]    p shouldPass = YES

To copy paste, symbol and action:

+[KZAsserts debugPass:]
p shouldPass = YES

Author

Krzysztof Zablocki, @merowing_ my blog

License

KZAsserts is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

Sourcery

Meta-programming for Swift, stop writing boilerplate code.
Swift
7,544
star
2

LifetimeTracker

Find retain cycles / memory leaks sooner.
Swift
3,052
star
3

Playgrounds

Better playgrounds that work both for Objective-C and Swift
Objective-C
2,632
star
4

Bootstrap

iOS project bootstrap aimed at high quality coding.
Objective-C
2,047
star
5

Inject

Hot Reloading for Swift applications!
Swift
1,914
star
6

Swift-Macros

A curated list of awesome Swift Macros
Swift
1,863
star
7

LineDrawing

Beatiful and fast smooth line drawing algorithm for iOS - as seen in Foldify.
Objective-C
1,287
star
8

Difference

Simple way to identify what is different between 2 instances of any type. Must have for TDD.
Swift
1,213
star
9

PropertyMapper

Property mapping for Objective-C iOS apps.
Objective-C
1,126
star
10

KZFileWatchers

A micro-framework for observing file changes, both local and remote. Helpful in building developer tools.
Swift
1,074
star
11

LinkedConsole

Clickable links in your Xcode console, so you never wonder which class logged the message.
Swift
934
star
12

Traits

Modify your native iOS app in real time.
Swift
905
star
13

IconOverlaying

Build informations on top of your app icon.
Shell
650
star
14

crafter

Crafter - Xcode project configuration CLI made easy.
Ruby
547
star
15

Strongify

Strongify is a 1-file µframework providing a nicer API for avoiding weak-strong dance.
Swift
444
star
16

KZNodes

Have you ever wonder how you could make Origami like editor in 1h ?
Objective-C
335
star
17

SFObservers

NSNotificationCenter and KVO auto removal of observers.
Objective-C
309
star
18

AutomaticSettings

Data driven settings UI generation.
Swift
300
star
19

CCNode-SFGestureRecognizers

Adding UIGestureRecognizers to cocos2d, painless.
Objective-C
202
star
20

DetailsMatter

Objective-C
198
star
21

Pinch-to-reveal

Pinch to reveal animation transition built with Layer masking, as seen in boeing app for iPad.
Objective-C
193
star
22

ViewModelOwners

Protocols that help make your MVVM setup more consistent
Swift
143
star
23

SFContainerViewController

UIViewControllers containment predating Apple implementation. Works in both 4.x and 5.x iOS, no memory or hierarchy issues.
Objective-C
82
star
24

OhSnap

Reproduce bugs your user saw by capturing and replaying data snapshots with ease.
Swift
81
star
25

Versionable

Migration for `Codable` objects.
Swift
79
star
26

Swift-Observable

Native KVO like behaviour build in Swift.
Swift
64
star
27

BehavioursExample

Objective-C
58
star
28

SourceryWorkshops

Swift
45
star
29

Learn-iOS-GameDev-Level-0

Teeter clone accompanying tutorial at http://merowing.info/2013/04/learn-ios-game-dev-level-0/
Objective-C
24
star
30

XibReferencing

Simple category and sample showing how you can reference one Xib view from another
Objective-C
20
star
31

NSObject-SFExecuteOnDealloc

A simple category on NSObject that allows you to execute block when object is deallocated
Objective-C
18
star
32

KZImageSplitView

Objective-C
17
star
33

jenkins_jobs_to_statusboard

Ruby script that generates html table for embedding in StatusBoard by Panic http://panic.com/statusboard/
Ruby
15
star
34

SourceryPro-Feedback

Repository for discussing https://merowing.info/sourcery-pro/
12
star
35

krzysztofzablocki

2
star
36

krzysztofzablocki.github.io

Blog
HTML
2
star
37

starter-hugo-academic

Jupyter Notebook
1
star