Skip to content

Commit

Permalink
[Tests] Expanded tests, added code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jhersh committed Feb 25, 2015
1 parent 993235b commit e298315
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .slather.yml
@@ -0,0 +1,3 @@
coverage_service: coveralls
xcodeproj: Example/Espressos.xcodeproj
source_directory: TTTAttributedLabel
7 changes: 5 additions & 2 deletions .travis.yml
@@ -1,10 +1,13 @@
osx_image: xcode61
language: objective-c
before_install: gem install cocoapods xcpretty -N
before_install: gem install cocoapods xcpretty slather -N
cache: cocoapods
podfile: Example/Podfile
env:
- LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8
script:
- set -o pipefail && xcodebuild -workspace 'Example/Espressos.xcworkspace' -scheme 'Espressos' -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6" clean test | xcpretty -c
- set -o pipefail && xcodebuild -workspace 'Example/Espressos.xcworkspace' -scheme 'Espressos'
-sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6"
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test | xcpretty -c
- pod lib lint --quick
after_success: slather
161 changes: 161 additions & 0 deletions Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m
Expand Up @@ -77,6 +77,8 @@ - (void)setUp {

- (void)tearDown {
[super tearDown];

label = nil;
}

#pragma mark - Logic tests
Expand All @@ -85,11 +87,39 @@ - (void)testInitializable {
XCTAssertNotNil(label, @"Label should be initializable");
}

- (void)testContentSize {
label.text = TTTAttributedTestString();
expect([label intrinsicContentSize]).to.equal([label sizeThatFits:CGSizeZero]);
label.text = kTestLabelText;
expect([label intrinsicContentSize]).to.equal([label sizeThatFits:CGSizeZero]);
}

- (void)testHighlighting {
label.text = TTTAttributedTestString();
[label setHighlighted:YES];
expect(label.highlighted).to.beTruthy();
}

- (void)testAttributedTextAccess {
label.text = TTTAttributedTestString();
XCTAssertTrue([label.attributedText isEqualToAttributedString:TTTAttributedTestString()], @"Attributed strings should match");
}

- (void)testLinkTintColor {
label.tintColor = [UIColor whiteColor];

label.inactiveLinkAttributes = @{ kTTTBackgroundFillColorAttributeName : (id)[UIColor grayColor].CGColor };
label.activeLinkAttributes = @{ kTTTBackgroundFillColorAttributeName : (id)[UIColor redColor].CGColor };
label.text = TTTAttributedTestString();
[label addLinkToURL:testURL withRange:NSMakeRange(0, 4)];

// Set active
label.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
label.tintColor = [UIColor redColor];

expect([label.attributedText attribute:kTTTBackgroundFillColorAttributeName atIndex:0 effectiveRange:NULL]).to.beNil();
}

- (void)testDerivedAttributedString {
label.font = [UIFont italicSystemFontOfSize:15.f];
label.textColor = [UIColor purpleColor];
Expand Down Expand Up @@ -149,6 +179,13 @@ - (void)testMultilineLabelSizing {

UIFont *font = [testString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
XCTAssertGreaterThan(size.height, font.pointSize, @"Text should size to more than one line");

size = [TTTAttributedLabel sizeThatFitsAttributedString:testString
withConstraints:kTestLabelSize
limitedToNumberOfLines:2];

font = [testString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
XCTAssertGreaterThan(size.height, font.pointSize, @"Text should size to more than one line");
}

- (void)testContainsLinkAtPoint {
Expand Down Expand Up @@ -347,6 +384,18 @@ - (void)testComplexAttributedString {
FBSnapshotVerifyView(label, nil);
}

#pragma mark - UIAccessibility

- (void)testAccessibilityElement {
label.text = TTTAttributedTestString();
[label addLinkToURL:testURL withRange:NSMakeRange(0, 4)];

expect(label.isAccessibilityElement).to.beFalsy();
expect(label.accessibilityElementCount).will.equal(2);
expect([label accessibilityElementAtIndex:0]).toNot.beNil();
expect([label indexOfAccessibilityElement:nil]).to.equal(NSNotFound);
}

#pragma mark - TTTAttributedLabelLink tests

- (void)testDesignatedInitializer {
Expand Down Expand Up @@ -546,6 +595,24 @@ - (void)testLongDatePressCallsDelegate {
[TTTDelegateMock verify];
}

- (void)testLongDateTimeZonePressCallsDelegate {
label.text = TTTAttributedTestString();

NSDate *date = [NSDate date];
[label addLinkToDate:date timeZone:[NSTimeZone defaultTimeZone] duration:1 withRange:NSMakeRange(0, 4)];
TTTSizeAttributedLabel(label);

[[TTTDelegateMock reject] attributedLabel:label didSelectLinkWithDate:date];
[[TTTDelegateMock expect] attributedLabel:label didLongPressLinkWithDate:date
timeZone:[NSTimeZone defaultTimeZone]
duration:1
atPoint:CGPointMake(5, 5)];

TTTSimulateLongPressOnLabelAtPointWithDuration(label, CGPointMake(5, 5), 0.6f);

[TTTDelegateMock verify];
}

- (void)testAddressPressCallsDelegate {
label.text = TTTAttributedTestString();

Expand Down Expand Up @@ -649,4 +716,98 @@ - (void)testLongTextCheckingPressCallsDelegate {
[TTTDelegateMock verify];
}

#pragma mark - UIPasteboard

- (void)testCopyingLabelText {
label.text = kTestLabelText;

[label copy:nil];

expect([UIPasteboard generalPasteboard].string).to.equal(label.text);
}

- (void)testPerformActions {
expect([label canPerformAction:@selector(copy:) withSender:nil]).to.beTruthy();
expect([label canPerformAction:@selector(paste:) withSender:nil]).to.beFalsy();
}

#pragma mark - NSCoding

- (void)testEncodingLabel {
label.text = TTTAttributedTestString();

NSData *encodedLabel = [NSKeyedArchiver archivedDataWithRootObject:label];

TTTAttributedLabel *newLabel = [NSKeyedUnarchiver unarchiveObjectWithData:encodedLabel];

expect(newLabel.text).to.equal(label.text);
}

#pragma mark - TTTAttributedLabelLink

- (void)testAddSingleLink {
TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:
[NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]];

[label addLink:link];

expect(label.links.count).to.equal(1);
}

- (void)testEncodingLink {
TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:
[NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]];

NSData *encodedLink = [NSKeyedArchiver archivedDataWithRootObject:link];

TTTAttributedLabelLink *newLink = [NSKeyedUnarchiver unarchiveObjectWithData:encodedLink];

expect(newLink.result.URL).to.equal(link.result.URL);
}

- (void)testLinkAccessibility {
TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:
[NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]];

expect(link.accessibilityValue).to.equal(testURL.absoluteString);

link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:
[NSTextCheckingResult phoneNumberCheckingResultWithRange:NSMakeRange(0, 1) phoneNumber:@"415-555-1212"]];

expect(link.accessibilityValue).to.equal(@"415-555-1212");

NSDate *date = [NSDate date];

link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label
textCheckingResult:
[NSTextCheckingResult dateCheckingResultWithRange:NSMakeRange(0, 1) date:date]];

expect(link.accessibilityValue).to.equal([NSDateFormatter localizedStringFromDate:date
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterLongStyle]);
}

#pragma mark - Deprecated Methods

- (void)testLeading {
// Deprecated
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[label setLeading:1.f];
#pragma clang diagnostic pop
expect(label.lineSpacing).to.equal(1.f);
}

- (void)testDataDetectorTypes {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
label.dataDetectorTypes = NSTextCheckingTypeLink;
expect(label.dataDetectorTypes).will.equal(NSTextCheckingTypeLink);
#pragma clang diagnostic pop
}

@end
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# TTTAttributedLabel

[![Build Status](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel.svg)](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel)
[![Build Status](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel.svg)](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel) [![Coverage Status](https://coveralls.io/repos/TTTAttributedLabel/TTTAttributedLabel/badge.svg)](https://coveralls.io/r/TTTAttributedLabel/TTTAttributedLabel)

**A drop-in replacement for `UILabel` that supports attributes, data detectors, links, and more**

Expand Down

0 comments on commit e298315

Please sign in to comment.