Best Swift-snapshot-testing code snippet using TableViewController.tableView
TableViewCellTests.swift
Source:TableViewCellTests.swift
...8import XCTest9@testable import Mew10class TableViewCellTests: XCTestCase {11 func testDequeueTableViewCellWithViewController() {12 let tableViewController = TableViewController(with: [1, 2, 3], environment: ())13 _ = tableViewController.view // load view14 INJECTABLE_VC: do {15 let cell = ViewController.dequeueAsTableViewCell(from: tableViewController.tableView, for: IndexPath(row: 0, section: 0), input: 39, parentViewController: tableViewController) as! TableViewCell<ViewController>16 XCTAssertEqual(cell.content.parameter, 39)17 XCTAssertTrue(cell.contentView.subviewTreeContains(with: cell.content.view))18 XCTAssertEqual(cell.accessoryType, .none)19 XCTAssertEqual(cell.editingAccessoryType, .none)20 XCTAssertEqual(cell.selectionStyle, .none)21 }22 INJECTABLE_VIEW: do {23 let cell = View.dequeueAsTableViewCell(from: tableViewController.tableView, for: IndexPath(row: 0, section: 0), input: 39, parentViewController: tableViewController) as! TableViewCell<Mew.ViewController<View, Void>>24 XCTAssertEqual(cell.content.content.parameter, 39)25 XCTAssertTrue(cell.contentView.subviewTreeContains(with: cell.content.view))26 XCTAssertEqual(cell.accessoryType, .none)27 XCTAssertEqual(cell.editingAccessoryType, .none)28 XCTAssertEqual(cell.selectionStyle, .none)29 }30 INTERACTABLE_VC: do {31 var expected: Int?32 let cell = ViewController.dequeueAsTableViewCell(from: tableViewController.tableView, for: IndexPath(row: 0, section: 0), input: 48, output: { expected = $0 }, parentViewController: tableViewController) as! TableViewCell<ViewController>33 XCTAssertEqual(cell.content.parameter, 48)34 XCTAssertTrue(cell.contentView.subviewTreeContains(with: cell.content.view))35 XCTAssertEqual(cell.accessoryType, .none)36 XCTAssertEqual(cell.editingAccessoryType, .none)37 XCTAssertEqual(cell.selectionStyle, .none)38 XCTAssertNil(expected)39 cell.content.fire()40 XCTAssertEqual(expected, 48)41 }42 INTERACTABLE_VIEW: do {43 var expected: Int?44 let cell = View.dequeueAsTableViewCell(from: tableViewController.tableView, for: IndexPath(row: 0, section: 0), input: 48, output: { expected = $0 }, parentViewController: tableViewController) as! TableViewCell<Mew.ViewController<View, Void>>45 XCTAssertEqual(cell.content.content.parameter, 48)46 XCTAssertTrue(cell.contentView.subviewTreeContains(with: cell.content.view))47 XCTAssertEqual(cell.accessoryType, .none)48 XCTAssertEqual(cell.editingAccessoryType, .none)49 XCTAssertEqual(cell.selectionStyle, .none)50 XCTAssertNil(expected)51 cell.content.content.fire()52 XCTAssertEqual(expected, 48)53 }54 }55 func testDequeueTableViewHeaderFooterWithViewController() {56 let tableViewController = TableViewController(with: [1, 2, 3], environment: ())57 _ = tableViewController.view // load view58 INJECTABLE_VC: do {59 let view = ViewController.dequeueAsTableViewHeaderFooterView(from: tableViewController.tableView, input: 39, parentViewController: tableViewController) as! TableViewHeaderFooterView<ViewController>60 XCTAssertEqual(view.content.parameter, 39)61 XCTAssertTrue(view.contentView.subviewTreeContains(with: view.content.view))62 }63 INJECTABLE_VIEW: do {64 let view = View.dequeueAsTableViewHeaderFooterView(from: tableViewController.tableView, input: 39, parentViewController: tableViewController) as! TableViewHeaderFooterView<Mew.ViewController<View, Void>>65 XCTAssertEqual(view.content.content.parameter, 39)66 XCTAssertTrue(view.contentView.subviewTreeContains(with: view.content.view))67 }68 INTERACTABLE_VC: do {69 var expected: Int?70 let view = ViewController.dequeueAsTableViewHeaderFooterView(from: tableViewController.tableView, input: 48, output: { expected = $0 }, parentViewController: tableViewController) as! TableViewHeaderFooterView<ViewController>71 XCTAssertEqual(view.content.parameter, 48)72 XCTAssertTrue(view.contentView.subviewTreeContains(with: view.content.view))73 XCTAssertNil(expected)74 view.content.fire()75 XCTAssertEqual(expected, 48)76 }77 INTERACTABLE_VIEW: do {78 var expected: Int?79 let view = View.dequeueAsTableViewHeaderFooterView(from: tableViewController.tableView, input: 48, output: { expected = $0 }, parentViewController: tableViewController) as! TableViewHeaderFooterView<Mew.ViewController<View, Void>>80 XCTAssertEqual(view.content.content.parameter, 48)81 XCTAssertTrue(view.contentView.subviewTreeContains(with: view.content.view))82 XCTAssertNil(expected)83 view.content.content.fire()84 XCTAssertEqual(expected, 48)85 }86 }87 func testViewControllerLifeCycle() {88 let exp = expectation(description: #function + "\(#line)")89 let tableViewController = TableViewController(with: Array(0..<10), environment: ())90 let parent = UIViewController()91 UIApplication.shared.keyWindow?.rootViewController = parent92 parent.present(tableViewController, animated: true, completion: {93 let viewControllers = tableViewController.tableView.visibleCells94 .compactMap { $0 as? TableViewCell<ViewController> }95 .map { $0.content }96 viewControllers.forEach {97 XCTAssertEqual($0.parent, tableViewController)98 }99 XCTAssertEqual(100 (tableViewController.tableView.headerView(forSection: 0) as? TableViewHeaderFooterView<ViewController>)?.content.parent,101 tableViewController102 )103 XCTAssertEqual(104 (tableViewController.tableView.headerView(forSection: 1) as? TableViewHeaderFooterView<Mew.ViewController<View, Void>>)?.content.parent,105 tableViewController106 )107 parent.dismiss(animated: true, completion: {108 exp.fulfill()109 })110 })111 self.wait(for: [exp], timeout: 5.0)112 }113 func testAutosizingCell() {114 let tableViewController = AutolayoutTableViewController(with: [], environment: ())115 _ = tableViewController.view // load view116 let data = [117 [118 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100))),119 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100))),120 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100))),121 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100))),122 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100))),123 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<100)), additionalHeight: CGFloat(Int.random(in: 0..<100)))124 ],125 [126 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),127 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),128 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),129 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),130 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),131 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000))),132 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 200..<1000)), additionalHeight: CGFloat(Int.random(in: 200..<1000)))133 ],134 [135 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),136 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),137 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),138 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),139 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),140 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000))),141 AutolayoutViewController.Input(additionalWidth: CGFloat(Int.random(in: 0..<1000)), additionalHeight: CGFloat(Int.random(in: 0..<1000)))142 ]143 ]144 UIApplication.shared.keyWindow?.rootViewController = tableViewController145 for expects in data {146 tableViewController.input(expects)147 let cells = tableViewController.tableView.visibleCells148 zip(expects, cells).forEach { expect, cell in149 XCTAssertEqual(cell.frame.size, CGSize(width: tableViewController.tableView.frame.width, height: 200 + expect.additionalHeight + 0.5), accurancy: 1.0)150 XCTAssertEqual(cell.contentView.frame.size, CGSize(width: tableViewController.tableView.frame.width, height: 200 + expect.additionalHeight), accurancy: 1.0)151 let childViewController = tableViewController.children.first(where: { $0.view.superview == cell.contentView }) as! AutolayoutViewController152 XCTAssertEqual(childViewController.view.frame.size, CGSize(width: min(tableViewController.tableView.frame.width, 200 + expect.additionalWidth), height: 200 + expect.additionalHeight), accurancy: 1.0)153 XCTAssertFalse(cell.hasAmbiguousLayout)154 }155 }156 }157 static var allTests = [158 ("testDequeueTableViewCellWithViewController", testDequeueTableViewCellWithViewController),159 ("testDequeueTableViewHeaderFooterWithViewController", testDequeueTableViewHeaderFooterWithViewController),160 ("testViewControllerLifeCycle", testViewControllerLifeCycle),161 ("testAutosizingCell", testAutosizingCell)162 ]163}...
TableViewController.swift
Source:TableViewController.swift
...30import UIKit31public protocol TableViewDelegate: UITableViewDelegate {}32public protocol TableViewDataSource: UITableViewDataSource {33 /**34 Retrieves the data source items for the tableView.35 - Returns: An Array of DataSourceItem objects.36 */37 var dataSourceItems: [DataSourceItem] { get }38}39extension UIViewController {40 /**41 A convenience property that provides access to the TableViewController.42 This is the recommended method of accessing the TableViewController43 through child UIViewControllers.44 */45 public var tableViewController: TableViewController? {46 var viewController: UIViewController? = self47 while nil != viewController {48 if viewController is TableViewController {49 return viewController as? TableViewController50 }51 viewController = viewController?.parent52 }53 return nil54 }55}56open class TableViewController: UIViewController {57 /// A reference to a Reminder.58 open let tableView = TableView()59 60 /// An Array of DataSourceItems.61 open var dataSourceItems = [DataSourceItem]()62 63 open override func viewDidLoad() {64 super.viewDidLoad()65 prepare()66 }67 68 /**69 Prepares the view instance when intialized. When subclassing,70 it is recommended to override the prepareView method71 to initialize property values and other setup operations.72 The super.prepareView method should always be called immediately73 when subclassing.74 */75 open func prepare() {76 view.clipsToBounds = true77 view.backgroundColor = .white78 view.contentScaleFactor = Screen.scale79 prepareTableView()80 }81}82extension TableViewController {83 /// Prepares the tableView.84 fileprivate func prepareTableView() {85 tableView.delegate = self86 tableView.dataSource = self87 view.layout(tableView).edges()88 }89}90extension TableViewController: TableViewDelegate {}91extension TableViewController: TableViewDataSource {92 @objc93 open func numberOfSections(in tableView: UITableView) -> Int {94 return 195 }96 97 @objc98 open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {99 return dataSourceItems.count100 }101 102 @objc103 open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {104 return UITableViewCell()105 }106}...
tableView
Using AI Code Generation
1override func viewDidLoad() {2 super.viewDidLoad()3 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")4}5override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {6}7override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {8 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)9}10override func viewDidLoad() {11 super.viewDidLoad()12 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")13}14override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {15}16override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {17 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)18}19override func viewDidLoad() {20 super.viewDidLoad()21 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")22}23override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {24}25override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {26 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)27}28override func viewDidLoad() {29 super.viewDidLoad()30 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")31}32override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {33}34override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {35 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)36}37override func viewDidLoad() {38 super.viewDidLoad()39 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")40}41override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {42}43override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath
tableView
Using AI Code Generation
1override func viewDidLoad() {2 super.viewDidLoad()3 let path = Bundle.main.path(forResource: "data", ofType: "plist")!4 let dict = NSDictionary(contentsOfFile: path)5 for item in array {6 let contact = Contact(name: name, phone: phone, email: email, address: address)7 contacts.append(contact)8 }9}10override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {11}12override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {13 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)14}15override func prepare(for segue: UIStoryboardSegue, sender: Any?) {16 if segue.identifier == "showContact" {17 }18}19class Contact {20 init(name: String, phone: String, email: String, address: String) {21 }22}23class ContactViewController: UIViewController {
tableView
Using AI Code Generation
1 import UIKit2 class TableViewController: UITableViewController {3 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {4 }5 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {6 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)7 cell.textLabel?.text = "Row \(indexPath.row)"8 }9 }10 import UIKit11 class TableViewController: UITableViewController {12 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {13 }14 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {15 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)16 cell.textLabel?.text = "Row \(indexPath.row)"17 }18 }19 import UIKit20 class TableViewController: UITableViewController {21 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {22 }23 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {24 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)25 cell.textLabel?.text = "Row \(indexPath.row)"26 }27 }28 import UIKit29 class TableViewController: UITableViewController {30 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {31 }32 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {33 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)34 cell.textLabel?.text = "Row \(indexPath.row)"35 }36 }37 import UIKit38 class TableViewController: UITableViewController {39 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {40 }41 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {42 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)43 cell.textLabel?.text = "Row \(indexPath.row
tableView
Using AI Code Generation
1class TableViewController: UITableViewController {2 override func viewDidLoad() {3 super.viewDidLoad()4 }5}6class TableViewController: UITableViewController {7 override func viewDidLoad() {8 super.viewDidLoad()9 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")10 }11 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {12 }13 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {14 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)15 }16}17class TableViewController: UITableViewController {18 override func viewDidLoad() {19 super.viewDidLoad()20 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")21 }22 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {23 }24 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {25 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)26 }27}28class TableViewController: UITableViewController {29 override func viewDidLoad() {30 super.viewDidLoad()31 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")32 }33 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {34 }35 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {36 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)37 }38}39class TableViewController: UITableViewController {40 override func viewDidLoad() {41 super.viewDidLoad()42 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")43 }44 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {45 }46 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {47 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
tableView
Using AI Code Generation
1class TableViewController: UITableViewController {2 override func viewDidLoad() {3 super.viewDidLoad()4 }5 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {6 }7 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {8 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)9 }10 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {11 }12}13class ViewController: UIViewController {14 override func viewDidLoad() {15 super.viewDidLoad()16 }17 @IBAction func showTable(_ sender: Any) {18 let tableViewController = self.storyboard?.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController19 self.navigationController?.pushViewController(tableViewController, animated: true)20 }21}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!