Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit 3c2bc18

Browse files
committed
fix extension spec
1 parent 7948e1a commit 3c2bc18

3 files changed

Lines changed: 67 additions & 39 deletions

File tree

Example/Specs/OFExtensionSpec.m

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,110 @@
55
QuickSpecBegin(OFExtensionSpec)
66

77
describe(@"NSBlockOperation+AsyncBlock", ^{
8-
context(@"when several operations in queue", ^{
9-
it(@"is max concurent operation in queue equal one", ^{
8+
context(@"when use serial queue", ^{
9+
it(@"run async operation in series", ^{
1010
__block NSString *testString = @"";
1111
NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc]init];
1212
testOperationQueue.maxConcurrentOperationCount = 1;
1313
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
14-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
15-
testString = @"first operation";
14+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
15+
testString = [testString stringByAppendingString:@"1"];
1616
finish();
1717
});
1818
}];
1919
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
20-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
21-
testString = @"second operation";
20+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
21+
testString = [testString stringByAppendingString:@"2"];
2222
finish();
2323
});
2424
}];
2525
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
2626
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
27-
testString = @"third operation";
27+
testString = [testString stringByAppendingString:@"3"];
2828
finish();
2929
});
3030
}];
31-
expect(testString).withTimeout(1.5).toEventually(equal(@"third operation"));
31+
expect(testString).toEventually(equal(@"123"));
3232
});
33-
34-
it(@"is max concurent operation in queue equal two", ^{
33+
});
34+
context(@"when use parallel queue", ^{
35+
it(@"run async operation in parallel", ^{
3536
__block NSString *testString = @"";
3637
NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc]init];
37-
testOperationQueue.maxConcurrentOperationCount = 2;
38+
testOperationQueue.maxConcurrentOperationCount = 3;
3839
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
39-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
40-
testString = @"first operation";
40+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
41+
testString = [testString stringByAppendingString:@"1"];
4142
finish();
4243
});
4344
}];
4445
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
45-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
46-
testString = @"second operation";
46+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
47+
testString = [testString stringByAppendingString:@"2"];
4748
finish();
4849
});
4950
}];
5051
[testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) {
51-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
52-
testString = @"third operation";
52+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
53+
testString = [testString stringByAppendingString:@"3"];
5354
finish();
5455
});
5556
}];
56-
expect(testString).withTimeout(1.5).toEventually(equal(@"second operation"));
57+
expect(testString).toEventually(equal(@"132"));
5758
});
5859
});
5960
});
6061

6162
describe(@"NSString+Regex", ^{
6263
context(@"when the string is checked for regex", ^{
6364
NSString *testString = @"This is test string.";
64-
it(@"is match regex", ^{
65+
it(@"match regex", ^{
6566
NSString *testRegex = @".*";
6667
expect(@([testString matchRegex:testRegex])).to(beTrue());
6768
});
6869

69-
it(@"is first match of regex", ^{
70-
NSString *testRegex = @"[t]";
71-
NSString *testResult = @"t";
72-
expect([testString firstMatchOfRegex:testRegex]).to(equal(testResult));
70+
it(@"has first match of regex", ^{
71+
NSString *testRegex = @"is";
72+
expect([testString firstMatchOfRegex:testRegex]).toNot(beNil());
7373
});
7474

75-
it(@"is all matches of regex", ^{
76-
NSString *testRegex = @"[t]";
77-
NSString *testResult = @"ttt";
78-
expect([[testString allMatchesOfRegex:testRegex] componentsJoinedByString:@""]).to(equal(testResult));
75+
it(@"has all matches of regex", ^{
76+
NSString *testRegex = @"is";
77+
NSInteger resultCount = 2;
78+
expect(@([testString allMatchesOfRegex:testRegex].count)).to(equal(@(resultCount)));
7979
});
8080

81-
it(@"is match email", ^{
82-
NSString *testEmail = @"test@gmail.com";
81+
it(@"has match email", ^{
82+
NSString *testEmail = @"test@mail.com";
8383
expect(@([testEmail matchEmailString])).to(beTrue());
8484
});
8585
});
86+
context(@"when the string is not checked for regex", ^{
87+
NSString *testString = @"This is test string.";
88+
it(@"not match regex", ^{
89+
NSString *testRegex = @"This";
90+
expect(@([testString matchRegex:testRegex])).toNot(beTrue());
91+
});
92+
it(@"has not first match of regex", ^{
93+
NSString *testRegex = @"testing";
94+
expect([testString firstMatchOfRegex:testRegex]).to(beNil());
95+
});
96+
97+
it(@"has not all matches of regex", ^{
98+
NSString *testRegex = @"testing";
99+
expect([testString firstMatchOfRegex:testRegex]).to(beNil());
100+
});
101+
102+
it(@"has not match email", ^{
103+
NSString *testEmail = @"testmail.com";
104+
expect(@([testEmail matchEmailString])).toNot(beTrue());
105+
});
106+
});
86107
});
87108

88109
describe(@"UIColor+HexString", ^{
89-
context(@"when color setted by hex string", ^{
90-
91-
it(@"is converting hex string to color", ^{
92-
// alizarin color
93-
// hex(#e74c3c)
94-
// rgba(231, 76, 60,1.0)
110+
context(@"when valid hex string", ^{
111+
it(@"create color", ^{
95112
NSString *testStringColor = @"#e74c3c";
96113
CGFloat testR = 231/256.0f, testG = 76/256.0f,testB = 60/256.0f,testA = 1.0f;
97114
UIColor *testColor = [UIColor colorWithHexString:testStringColor alpha:1.0f];
@@ -102,7 +119,15 @@
102119
expect(@(b)).to(equal(@(testB)));
103120
expect(@(a)).to(equal(@(testA)));
104121
});
122+
context(@"when NOT valid hex string", ^{
123+
it(@"create NIL color", ^{
124+
NSString *testStringColor = @"#888e74c3c";
125+
expect([UIColor colorWithHexString:testStringColor alpha:1.0f]).to(beNil());
126+
});
127+
});
128+
105129
});
106130
});
107131

108132
QuickSpecEnd
133+

OrangeFramework/Extension/UIKit/UIColor+OFExtension.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ NS_ASSUME_NONNULL_BEGIN
77
+ (instancetype)colorWith8BitRed:(NSUInteger)red green:(NSUInteger)green blue:(NSUInteger)blue NS_SWIFT_UNAVAILABLE("Use swift init instead");
88
+ (instancetype)colorWith8BitRed:(NSUInteger)red green:(NSUInteger)green blue:(NSUInteger)blue alpha:(CGFloat)alpha NS_SWIFT_UNAVAILABLE("Use swift init instead");
99

10-
+ (instancetype)colorWithHexString:(NSString *)hexString;
11-
+ (instancetype)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha;
10+
+ (nullable instancetype)colorWithHexString:(NSString *)hexString;
11+
+ (nullable instancetype)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha;
1212

1313
@end
1414

OrangeFramework/Extension/UIKit/UIColor+OFExtension.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ + (instancetype)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
1818
unsigned rgbValue = 0;
1919
NSScanner *scanner = [NSScanner scannerWithString:hexString];
2020
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
21-
[scanner scanHexInt:&rgbValue];
22-
return [self colorWith8BitRed:(rgbValue & 0xFF0000) >> 16 green:(rgbValue & 0xFF00) >> 8 blue:rgbValue & 0xFF alpha:alpha];
21+
if ([scanner scanHexInt:&rgbValue] && [NSString stringWithFormat:@"%x",rgbValue].length == 6) {
22+
return [self colorWith8BitRed:(rgbValue & 0xFF0000) >> 16 green:(rgbValue & 0xFF00) >> 8 blue:rgbValue & 0xFF alpha:alpha];
23+
} else {
24+
return nil;
25+
}
2326
}
2427

2528
@end

0 commit comments

Comments
 (0)