PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/MTBlockTableViewTests/MTBlockTableViewTests.m

https://github.com/pita5/MTBlockTableView
Objective C | 410 lines | 307 code | 95 blank | 8 comment | 32 complexity | c0e5db618170fdb254f66b6d79e56b2a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // MTBlockTableViewTests.m
  3. // MTBlockTableViewTests
  4. //
  5. // Created by Parker Wightman on 8/11/12.
  6. // Copyright (c) 2012 Mysterious Trousers. All rights reserved.
  7. //
  8. #import "MTBlockTableViewTests.h"
  9. @implementation MTBlockTableViewTests
  10. - (void)setUp
  11. {
  12. [super setUp];
  13. _tableView = [[MTBlockTableView alloc] initWithFrame:CGRectZero];
  14. }
  15. - (void)tearDown
  16. {
  17. // Tear-down code here.
  18. [super tearDown];
  19. }
  20. #pragma mark - UITableViewDataSource Protocol Method Tests
  21. - (void)testNumberOfSectionsInTableViewBlock
  22. {
  23. [_tableView setNumberOfSectionsInTableViewBlock:^NSInteger(UITableView *tableView) {
  24. return 10;
  25. }];
  26. STAssertEquals([_tableView.dataSource numberOfSectionsInTableView:_tableView], 10, nil);
  27. }
  28. - (void)testNumberOfRowsInSectionBlock
  29. {
  30. [_tableView setNumberOfSectionsInTableViewBlock:^NSInteger(UITableView *tableView) {
  31. return 2;
  32. }];
  33. [_tableView setNumberOfRowsInSectionBlock:^NSInteger(UITableView *tableView, NSInteger section) {
  34. return (section == 0 ? 2 : 1);
  35. }];
  36. STAssertEquals([_tableView.dataSource tableView:_tableView numberOfRowsInSection:0], 2, nil);
  37. STAssertEquals([_tableView.dataSource tableView:_tableView numberOfRowsInSection:1], 1, nil);
  38. }
  39. - (void)testCellForRowAtIndexPathBlock {
  40. UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
  41. [_tableView setCellForRowAtIndexPathBlock:^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
  42. if (indexPath.section == 1 && indexPath.row == 2) {
  43. return cell;
  44. }
  45. return nil;
  46. }];
  47. STAssertEquals([_tableView.dataSource tableView:_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]], cell, nil);
  48. }
  49. - (void)testTitleForHeaderInSectionBlock {
  50. STAssertEqualObjects([_tableView.dataSource tableView:_tableView titleForHeaderInSection:3], nil, nil);
  51. NSString *title = @"Foobar";
  52. [_tableView setTitleForHeaderInSectionBlock:^NSString *(UITableView *tableView, NSInteger section) {
  53. if (section == 3) {
  54. return title;
  55. }
  56. return nil;
  57. }];
  58. STAssertEquals([_tableView.dataSource tableView:_tableView titleForHeaderInSection:3], title, nil);
  59. }
  60. - (void)testTitleForFooterInSectionBlock {
  61. STAssertEqualObjects([_tableView.dataSource tableView:_tableView titleForFooterInSection:3], nil, nil);
  62. NSString *title = @"Foobar";
  63. [_tableView setTitleForFooterInSectionBlock:^NSString *(UITableView *tableView, NSInteger section) {
  64. if (section == 3) {
  65. return title;
  66. }
  67. return nil;
  68. }];
  69. STAssertEquals([_tableView.dataSource tableView:_tableView titleForFooterInSection:3], title, nil);
  70. }
  71. - (void)testCanEditRowAtIndexPath {
  72. STAssertEquals([_tableView.dataSource tableView:_tableView canEditRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]], NO, nil);
  73. [_tableView setCanEditRowAtIndexPathBlock:^BOOL(UITableView *tableView, NSIndexPath *indexPath) {
  74. if (indexPath.row == 1 && indexPath.section == 1){
  75. return YES;
  76. }
  77. return NO;
  78. }];
  79. STAssertEquals([_tableView.dataSource tableView:_tableView canEditRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]], YES, nil);
  80. }
  81. - (void)testCanMoveRowAtIndexPath {
  82. STAssertEquals([_tableView.dataSource tableView:_tableView canMoveRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]], NO, nil);
  83. [_tableView setCanMoveRowAtIndexPathBlock:^BOOL(UITableView *tableView, NSIndexPath *indexPath) {
  84. if (indexPath.row == 1 && indexPath.section == 1){
  85. return YES;
  86. }
  87. return NO;
  88. }];
  89. STAssertEquals([_tableView.dataSource tableView:_tableView canMoveRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]], YES, nil);
  90. }
  91. - (void)testSectionIndexTitlesBlock {
  92. STAssertEqualObjects([_tableView.dataSource sectionIndexTitlesForTableView:_tableView], nil, nil);
  93. NSArray *indexes = @[ ];
  94. [_tableView setSectionIndexTitlesBlock:^NSArray *(UITableView *tableView) {
  95. return indexes;
  96. }];
  97. STAssertEqualObjects([_tableView.dataSource sectionIndexTitlesForTableView:_tableView], indexes, nil);
  98. }
  99. - (void)testCommitEditingStyleForRowAtIndexPathBlock {
  100. }
  101. #pragma mark - UITableViewDelegate Protocol Method Tests
  102. - (void)testAccessoryButtonTappedForRowWithIndexPathBlock {
  103. __block NSInteger i = 0;
  104. _tableView.accessoryButtonTappedForRowWithIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {
  105. i++;
  106. };
  107. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  108. [_tableView.delegate tableView:_tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
  109. STAssertEquals(i, 1, nil);
  110. }
  111. - (void)testCanPerformActionForRowAtIndexPathWithSenderBlock {
  112. _tableView.canPerformActionForRowAtIndexPathWithSenderBlock = ^BOOL(UITableView *tableView, SEL action, NSIndexPath *indexPath, id sender) {
  113. if (indexPath.row == 0) {
  114. return NO;
  115. } else {
  116. return YES;
  117. }
  118. };
  119. STAssertFalse([_tableView.delegate tableView:_tableView
  120. canPerformAction:nil
  121. forRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  122. withSender:nil], nil);
  123. STAssertTrue([_tableView.delegate tableView:_tableView
  124. canPerformAction:nil
  125. forRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]
  126. withSender:nil], nil);
  127. }
  128. - (void)testDidDeselectRowAtIndexPathBlock {
  129. __block NSInteger i = 0;
  130. _tableView.didDeselectRowAtIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {
  131. i++;
  132. };
  133. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  134. [_tableView.delegate tableView:_tableView didDeselectRowAtIndexPath:indexPath];
  135. STAssertEquals(i, 1, nil);
  136. }
  137. - (void)testDidEndEditingRowAtIndexPathBlock {
  138. __block NSInteger i = 0;
  139. _tableView.didEndEditingRowAtIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {
  140. i++;
  141. };
  142. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  143. [_tableView.delegate tableView:_tableView didEndEditingRowAtIndexPath:indexPath];
  144. STAssertEquals(i, 1, nil);
  145. }
  146. - (void)testDidSelectRowAtIndexPathBlock {
  147. __block NSInteger i = 0;
  148. _tableView.didSelectRowAtIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {
  149. i++;
  150. };
  151. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  152. [_tableView.delegate tableView:_tableView didSelectRowAtIndexPath:indexPath];
  153. STAssertEquals(i, 1, nil);
  154. }
  155. - (void)testEditingStyleForRowAtIndexPathBlock {
  156. _tableView.editingStyleForRowAtIndexPathBlock = ^UITableViewCellEditingStyle(UITableView *tableView, NSIndexPath *indexPath) {
  157. if (indexPath.row == 0) {
  158. return UITableViewCellEditingStyleDelete;
  159. } else {
  160. return UITableViewCellEditingStyleInsert;
  161. }
  162. };
  163. STAssertEquals([_tableView.delegate tableView:_tableView editingStyleForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]],
  164. UITableViewCellEditingStyleDelete,
  165. nil);
  166. STAssertEquals([_tableView.delegate tableView:_tableView editingStyleForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]],
  167. UITableViewCellEditingStyleInsert,
  168. nil);
  169. }
  170. - (void)testHeightForFooterInSectionBlock {
  171. _tableView.heightForFooterInSectionBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
  172. return 15.0f;
  173. };
  174. STAssertEquals([_tableView.delegate tableView:_tableView heightForFooterInSection:0], 15.0f, nil);
  175. }
  176. - (void)testHeightForHeaderInSectionBlock {
  177. _tableView.heightForHeaderInSectionBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
  178. return 30.0f;
  179. };
  180. STAssertEquals([_tableView.delegate tableView:_tableView heightForHeaderInSection:0], 30.0f, nil);
  181. }
  182. - (void)testHeightForRowAtIndexPath {
  183. _tableView.heightForRowAtIndexPathBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
  184. return 10.0f;
  185. };
  186. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  187. STAssertEquals([_tableView.delegate tableView:_tableView heightForRowAtIndexPath:indexPath], 10.0f, nil);
  188. }
  189. - (void)testIndentationLevelForRowAtIndexPathBlock {
  190. _tableView.indentationLevelForRowAtIndexPathBlock = ^NSInteger(UITableView *tableView, NSIndexPath *indexPath) {
  191. return 20;
  192. };
  193. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  194. NSInteger indentation = [_tableView.delegate tableView:_tableView
  195. indentationLevelForRowAtIndexPath:indexPath];
  196. STAssertEquals(indentation, 20, nil);
  197. }
  198. - (void)testPerformActionForRowAtIndexPathWithSenderBlock {
  199. __block NSInteger i = 0;
  200. _tableView.performActionForRowAtIndexPathWithSenderBlock = ^(UITableView *tableView, SEL action, NSIndexPath *indexPath, id sender) {
  201. i++;
  202. };
  203. [_tableView.delegate tableView:_tableView performAction:nil forRowAtIndexPath:nil withSender:nil];
  204. STAssertEquals(i, 1, nil);
  205. }
  206. - (void)testShouldIndentWhileEditingRowAtIndexPathBlock {
  207. _tableView.shouldIndentWhileEditingRowAtIndexPathBlock = ^BOOL(UITableView *tableView, NSIndexPath *indexPath) {
  208. if (indexPath.row == 0) {
  209. return NO;
  210. } else {
  211. return YES;
  212. }
  213. };
  214. STAssertFalse([_tableView.delegate tableView:_tableView
  215. shouldIndentWhileEditingRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]],
  216. nil);
  217. STAssertTrue([_tableView.delegate tableView:_tableView
  218. shouldIndentWhileEditingRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]],
  219. nil);
  220. }
  221. - (void)testShouldShowMenuForRowAtIndexPathBlock {
  222. _tableView.shouldShowMenuForRowAtIndexPathBlock = ^BOOL(UITableView *tableView, NSIndexPath *indexPath) {
  223. if (indexPath.row == 0) {
  224. return NO;
  225. } else {
  226. return YES;
  227. }
  228. };
  229. STAssertFalse([_tableView.delegate tableView:_tableView
  230. shouldShowMenuForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]],
  231. nil);
  232. STAssertTrue([_tableView.delegate tableView:_tableView
  233. shouldShowMenuForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]],
  234. nil);
  235. }
  236. - (void)testTargetIndexPathForMoveFromRowAtIndexPathToProposedIndexPath {
  237. NSIndexPath *indexPath = [_tableView.delegate tableView:_tableView
  238. targetIndexPathForMoveFromRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  239. toProposedIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
  240. STAssertEqualObjects(indexPath, [NSIndexPath indexPathForRow:1 inSection:1], nil);
  241. _tableView.targetIndexPathForMoveFromRowAtIndexPathToProposedIndexPathBlock = ^NSIndexPath *(UITableView *tableView, NSIndexPath *sourceIndexPath, NSIndexPath *proposedDestinationIndexPath) {
  242. return [NSIndexPath indexPathForRow:0 inSection:0];
  243. };
  244. indexPath = [_tableView.delegate tableView:_tableView
  245. targetIndexPathForMoveFromRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  246. toProposedIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
  247. STAssertEqualObjects(indexPath, [NSIndexPath indexPathForRow:0 inSection:0], nil);
  248. }
  249. - (void)testTitleForDeleteConfirmationButtonForRowAtIndexPathBlock {
  250. _tableView.titleForDeleteConfirmationButtonForRowAtIndexPathBlock = ^NSString *(UITableView *tableView, NSIndexPath *indexPath) {
  251. if (indexPath.row == 0) {
  252. return @"Hey Dawg";
  253. } else {
  254. return @"I heard you like blocks";
  255. }
  256. };
  257. STAssertEqualObjects(@"Hey Dawg",
  258. [_tableView.delegate tableView:_tableView
  259. titleForDeleteConfirmationButtonForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]],
  260. nil);
  261. STAssertEqualObjects(@"I heard you like blocks",
  262. [_tableView.delegate tableView:_tableView
  263. titleForDeleteConfirmationButtonForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]],
  264. nil);
  265. }
  266. - (void)testViewForFooterInSectionBlock {
  267. _tableView.viewForFooterInSectionBlock = ^UIView *(UITableView *tableView, NSInteger section) {
  268. return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
  269. };
  270. UIView *view = [_tableView.delegate tableView:_tableView viewForFooterInSection:0];
  271. STAssertEquals(view.frame.size.height, 10.0f, nil);
  272. }
  273. - (void)testViewForHeaderInSectionBlock {
  274. _tableView.viewForHeaderInSectionBlock = ^UIView *(UITableView *tableView, NSInteger section) {
  275. return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
  276. };
  277. UIView *view = [_tableView.delegate tableView:_tableView viewForHeaderInSection:0];
  278. STAssertEquals(view.frame.size.height, 20.0f, nil);
  279. }
  280. - (void)testWillBeginEditingRowAtIndexPath {
  281. __block NSInteger i = 0;
  282. _tableView.willBeginEditingRowAtIndexPathBlock = ^(UITableView *tableView, NSIndexPath *indexPath) {
  283. i++;
  284. };
  285. [_tableView.delegate tableView:_tableView willBeginEditingRowAtIndexPath:nil];
  286. STAssertEquals(i, 1, nil);
  287. }
  288. - (void)testWillDeselectRowAtIndexPath {
  289. _tableView.willDeselectRowAtIndexPathBlock = ^NSIndexPath *(UITableView *tableView, NSIndexPath *indexPath) {
  290. return [NSIndexPath indexPathForRow:0 inSection:0];
  291. };
  292. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
  293. STAssertEqualObjects([_tableView.delegate tableView:_tableView willDeselectRowAtIndexPath:indexPath],
  294. [NSIndexPath indexPathForRow:0 inSection:0],
  295. nil);
  296. }
  297. - (void)testWillDisplayCellForRowAtIndexPathBlock {
  298. __block NSInteger i = 0;
  299. _tableView.willDisplayCellForRowAtIndexPathBlock = ^(UITableView *tableView, UITableViewCell *cell, NSIndexPath *indexPath) {
  300. i++;
  301. };
  302. [_tableView.delegate tableView:_tableView willDisplayCell:nil forRowAtIndexPath:nil];
  303. STAssertEquals(i, 1, nil);
  304. }
  305. - (void)testWillSelectRowAtIndexPathBlock {
  306. _tableView.willSelectRowAtIndexPathBlock = ^NSIndexPath *(UITableView *tableView, NSIndexPath *indexPath) {
  307. return [NSIndexPath indexPathForRow:10 inSection:10];
  308. };
  309. [_tableView.delegate tableView:_tableView willDisplayCell:nil forRowAtIndexPath:nil];
  310. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
  311. STAssertEqualObjects([_tableView.delegate tableView:_tableView willSelectRowAtIndexPath:indexPath],
  312. [NSIndexPath indexPathForRow:10 inSection:10],
  313. nil);
  314. }
  315. @end