|
5 | 5 | QuickSpecBegin(OFExtensionSpec) |
6 | 6 |
|
7 | 7 | describe(@"NSBlockOperation+AsyncBlock", ^{ |
8 | | - |
9 | | -}) |
| 8 | + context(@"when use serial queue", ^{ |
| 9 | + it(@"run async operation in series", ^{ |
| 10 | + __block NSString *testString = @""; |
| 11 | + NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc]init]; |
| 12 | + testOperationQueue.maxConcurrentOperationCount = 1; |
| 13 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 14 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 15 | + testString = [testString stringByAppendingString:@"1"]; |
| 16 | + finish(); |
| 17 | + }); |
| 18 | + }]; |
| 19 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 20 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 21 | + testString = [testString stringByAppendingString:@"2"]; |
| 22 | + finish(); |
| 23 | + }); |
| 24 | + }]; |
| 25 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 26 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 27 | + testString = [testString stringByAppendingString:@"3"]; |
| 28 | + finish(); |
| 29 | + }); |
| 30 | + }]; |
| 31 | + expect(testString).toEventually(equal(@"123")); |
| 32 | + }); |
| 33 | + }); |
| 34 | + context(@"when use parallel queue", ^{ |
| 35 | + it(@"run async operation in parallel", ^{ |
| 36 | + __block NSString *testString = @""; |
| 37 | + NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc]init]; |
| 38 | + testOperationQueue.maxConcurrentOperationCount = 3; |
| 39 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 40 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 41 | + testString = [testString stringByAppendingString:@"1"]; |
| 42 | + finish(); |
| 43 | + }); |
| 44 | + }]; |
| 45 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 46 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 47 | + testString = [testString stringByAppendingString:@"2"]; |
| 48 | + finish(); |
| 49 | + }); |
| 50 | + }]; |
| 51 | + [testOperationQueue addOperationWithAsyncBlock:^(OFAsyncBlockFinish _Nonnull finish) { |
| 52 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 53 | + testString = [testString stringByAppendingString:@"3"]; |
| 54 | + finish(); |
| 55 | + }); |
| 56 | + }]; |
| 57 | + expect(testString).toEventually(equal(@"132")); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
10 | 61 |
|
11 | 62 | describe(@"NSString+Regex", ^{ |
12 | | - |
13 | | -}) |
| 63 | + context(@"when the string contains regex pattern", ^{ |
| 64 | + NSString *testString = @"This is test string."; |
| 65 | + it(@"match regex", ^{ |
| 66 | + NSString *testRegex = @".*"; |
| 67 | + expect(@([testString matchRegex:testRegex])).to(beTrue()); |
| 68 | + }); |
| 69 | + |
| 70 | + it(@"has first match of regex", ^{ |
| 71 | + NSString *testRegex = @"is"; |
| 72 | + expect([testString firstMatchOfRegex:testRegex]).toNot(beNil()); |
| 73 | + }); |
| 74 | + |
| 75 | + it(@"has all matches of regex", ^{ |
| 76 | + NSString *testRegex = @"is"; |
| 77 | + NSInteger resultCount = 2; |
| 78 | + expect(@([testString allMatchesOfRegex:testRegex].count)).to(equal(@(resultCount))); |
| 79 | + }); |
| 80 | + |
| 81 | + it(@"has match email", ^{ |
| 82 | + NSString *testEmail = @"test@mail.com"; |
| 83 | + expect(@([testEmail matchEmailString])).to(beTrue()); |
| 84 | + }); |
| 85 | + }); |
| 86 | + context(@"when the string NOT contains regex pattern", ^{ |
| 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 | + }); |
| 107 | +}); |
14 | 108 |
|
15 | 109 | describe(@"UIColor+HexString", ^{ |
16 | | - |
17 | | -}) |
| 110 | + context(@"when valid hex string", ^{ |
| 111 | + it(@"create color", ^{ |
| 112 | + NSString *testStringColor = @"#e74c3c"; |
| 113 | + CGFloat testR = 231/256.0f, testG = 76/256.0f, testB = 60/256.0f; |
| 114 | + UIColor *testColor = [UIColor colorWithHexString:testStringColor]; |
| 115 | + CGFloat r, g, b; |
| 116 | + [testColor getRed:&r green:&g blue:&b alpha:NULL]; |
| 117 | + expect(@(r)).to(equal(@(testR))); |
| 118 | + expect(@(g)).to(equal(@(testG))); |
| 119 | + expect(@(b)).to(equal(@(testB))); |
| 120 | + }); |
| 121 | + context(@"when NOT valid hex string", ^{ |
| 122 | + it(@"create NIL color", ^{ |
| 123 | + NSString *testStringColor = @"888e74c3c"; |
| 124 | + expect([UIColor colorWithHexString:testStringColor]).to(beNil()); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + }); |
| 129 | +}); |
18 | 130 |
|
19 | 131 | QuickSpecEnd |
| 132 | + |
0 commit comments