Skip to content

Commit

Permalink
Add more tests around width calculations
Browse files Browse the repository at this point in the history
Had to make the C function used public to avoid build failures.
  • Loading branch information
dmiedema committed Jan 1, 2015
1 parent 9b1a96b commit a45afe2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
13 changes: 12 additions & 1 deletion CRToast/CRToastView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
//

#import <UIKit/UIKit.h>
#import "CRToast.h"

@class CRToast;
/**
Calculate the width of the view given all necessary values of the given `CRToastView`s properties
@param fullContentWidth full width of contentView to fill
@param fullContentHeight full height of the content view. It is assumed the image & activity indicators frame is a square with sides the length of the height of the contentView.
@param showingImage @c YES if an image is being shown and should be accounted for. @c NO otherwise.
@param imageAlignment alignment of image. Only used if @c showingImage is set to @c YES
@param showingActivityIndicator @c YES if an activity indicator is being shown and should be accounted for. @c NO otherwise.
@param activityIndicatorAlignment alignment of activity indicator. Only used if @c showingActivityIndicator is set to @c YES
*/
CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment);

@interface CRToastView : UIView
@property (nonatomic, strong) CRToast *toast;
Expand Down
2 changes: 1 addition & 1 deletion CRToast/CRToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static CGFloat CRToastWidthOfViewWithAlignment(CGFloat height, BOOL showing, CRT
height;
}

static CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment) {
CGFloat CRContentWidthForAccessoryViewsWithAlignments(CGFloat fullContentWidth, CGFloat fullContentHeight, BOOL showingImage, CRToastAccessoryViewAlignment imageAlignment, BOOL showingActivityIndicator, CRToastAccessoryViewAlignment activityIndicatorAlignment) {
CGFloat width = fullContentWidth;

width -= CRToastWidthOfViewWithAlignment(fullContentHeight, showingImage, imageAlignment);
Expand Down
14 changes: 12 additions & 2 deletions Tests/Unit Tests/CRToastViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,38 @@ - (void)testActivityFrameRightAlignment {
#pragma mark Width Calculations
- (void)testWidthWithOnlyLeftItem {

CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentLeft, NO, CRToastAccessoryViewAlignmentLeft);

XCTAssertTrue(width == 200, @"Width with only left item should be 200 (full width - (height x 1)). Instead was %f", width);
}

- (void)testWidthWithOnlyCenterItem {
CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentCenter, NO, CRToastAccessoryViewAlignmentLeft);

XCTAssertTrue(width == 300, @"Width with only center item should be 300 (full width). Instead was %f", width);
}

- (void)testWidthWithOnlyRightItem {
CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentRight, NO, CRToastAccessoryViewAlignmentLeft);

XCTAssertTrue(width == 200, @"Width with only center item should be 300 (full width - (height x 1)). Instead was %f", width);
}

- (void)testWidthWithLeftAndCenter {
CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentLeft, YES, CRToastAccessoryViewAlignmentCenter);

XCTAssertTrue(width == 200, @"Width with left & center item should be 200 (full width - (height x 1)). Instead was %f", width);
}

- (void)testWidthWithLeftAndRight {
CGFloat width = CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentLeft, YES, CRToastAccessoryViewAlignmentRight);

XCTAssertTrue(width == 100, @"Width with left & right item should be 200 (full width - (height x 2)). Instead was %f", width);
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
CRContentWidthForAccessoryViewsWithAlignments(300, 100, YES, CRToastAccessoryViewAlignmentLeft, YES, CRToastAccessoryViewAlignmentRight);
}];
}

Expand Down

0 comments on commit a45afe2

Please sign in to comment.