Fixed wrong number of arguments issue, extensive tests

This commit is contained in:
Jamon Holmgren
2013-05-27 18:28:48 -07:00
parent 9d7b7467c7
commit 36232eefe9
4 changed files with 215 additions and 72 deletions

View File

@@ -11,10 +11,10 @@ module ProMotion
if v.is_a?(Hash) && element.respond_to?(k)
sub_element = element.send(k)
set_attributes(sub_element, v) if sub_element
elsif v.is_a?(Array) && element.respond_to?("#{k}")
element.send("#{k}", *v)
elsif element.respond_to?("#{k}=")
element.send("#{k}=", v)
elsif v.is_a?(Array) && element.respond_to?("#{k}") && element.method("#{k}").arity == v.length
element.send("#{k}", *v)
else
# Doesn't respond. Check if snake case.
if k.to_s.include?("_")

View File

@@ -9,9 +9,10 @@ module ProMotion
# TODO: Some of these need to go away. Unnecessary overhead.
set_cell_attributes
set_background_color
set_class_attributes
set_accessory_view
set_label
set_subtitle
set_image
set_remote_image
set_subviews
@@ -27,6 +28,10 @@ module ProMotion
self
end
def set_background_color
self.backgroundView = UIView.new.tap{|v| v.backgroundColor = data_cell[:background_color]} if data_cell[:background_color]
end
def set_class_attributes
if data_cell[:cell_class_attributes]
PM.logger.deprecated "`cell_class_attributes` is deprecated. Just add the attributes you want to set right into your cell hash."
@@ -51,7 +56,7 @@ module ProMotion
self
end
def set_label
def set_subtitle
if data_cell[:subtitle] && self.detailTextLabel
self.detailTextLabel.text = data_cell[:subtitle]
self.detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth

View File

@@ -0,0 +1,108 @@
describe "PM::Table module" do
def cell_factory(args={})
{ title: "Basic", action: :basic_cell_tapped, arguments: { id: 1 } }.merge!(args)
end
def custom_cell
cell_factory({
title: "Crazy Full Featured Cell",
subtitle: "This is way too huge..see note",
arguments: { data: [ "lots", "of", "data" ] },
action: :tapped_cell_1,
height: 50, # manually changes the cell's height
cell_style: UITableViewCellStyleSubtitle,
cell_identifier: "Cell",
cell_class: PM::TableViewCell,
masks_to_bounds: true,
background_color: UIColor.whiteColor,
selection_style: UITableViewCellSelectionStyleGray,
cell_class_attributes: {
# any Obj-C attributes to set on the cell
backgroundColor: UIColor.whiteColor
},
accessory: :switch, # currently only :switch is supported
accessory_view: @some_accessory_view,
accessory_type: UITableViewCellAccessoryCheckmark,
accessory_checked: true, # whether it's "checked" or not
image: { image: UIImage.imageNamed("something"), radius: 15 },
remote_image: { # remote image, requires SDWebImage CocoaPod
url: "http://placekitten.com/200/300", placeholder: "some-local-image",
size: 50, radius: 15
},
subviews: [ @some_view, @some_other_view ] # arbitrary views added to the cell
})
end
before do
@subject = TestTableScreen.new
@subject.mock! :table_data do
[{
title: "Table cell group 1", cells: [ ]
},{
title: "Table cell group 2", cells: [ cell_factory ]
},{
title: "Table cell group 3", cells: [ cell_factory(title: "3-1"), cell_factory(title: "3-2") ]
},{
title: "Table cell group 4", cells: [ custom_cell, cell_factory(title: "4-2"), cell_factory(title: "4-3"), cell_factory(title: "4-4") ]
}]
end
@subject.on_load
@ip = NSIndexPath.indexPathForRow(1, inSection: 2) # Cell 3-2
@custom_ip = NSIndexPath.indexPathForRow(0, inSection: 3) # Cell "Crazy Full Featured Cell"
@subject.update_table_data
end
it "should have the right number of sections" do
@subject.numberOfSectionsInTableView(@subject.table_view).should == 4
end
it "should set the section titles" do
@subject.tableView(@subject.table_view, titleForHeaderInSection:0).should == "Table cell group 1"
@subject.tableView(@subject.table_view, titleForHeaderInSection:1).should == "Table cell group 2"
@subject.tableView(@subject.table_view, titleForHeaderInSection:2).should == "Table cell group 3"
@subject.tableView(@subject.table_view, titleForHeaderInSection:3).should == "Table cell group 4"
end
it "should create the right number of cells" do
@subject.tableView(@subject.table_view, numberOfRowsInSection:0).should == 0
@subject.tableView(@subject.table_view, numberOfRowsInSection:1).should == 1
@subject.tableView(@subject.table_view, numberOfRowsInSection:2).should == 2
@subject.tableView(@subject.table_view, numberOfRowsInSection:3).should == 4
end
it "should create the jumplist" do
@subject.mock! :table_data_index, do
Array("A".."Z")
end
@subject.sectionIndexTitlesForTableView(@subject.table_view).should == Array("A".."Z")
end
it "should return the proper cell" do
cell = @subject.tableView(@subject.table_view, cellForRowAtIndexPath: @ip)
cell.should.be.kind_of(UITableViewCell)
cell.textLabel.text.should == "3-2"
end
it "should return the table's cell height if none is given" do
@subject.tableView(@subject.table_view, heightForRowAtIndexPath:@ip).should == 44.0 # Built-in default
end
it "should allow setting a custom cell height" do
@subject.tableView(@subject.table_view, heightForRowAtIndexPath:@custom_ip).should.be > 0.0
@subject.tableView(@subject.table_view, heightForRowAtIndexPath:@custom_ip).should == custom_cell[:height].to_f
end
it "should trigger the right action on select and pass in the right arguments" do
@subject.mock! :tapped_cell_1 do |args|
args[:data].should == [ "lots", "of", "data" ]
end
@subject.tableView(@subject.table_view, didSelectRowAtIndexPath:@custom_ip)
end
end

View File

@@ -1,76 +1,106 @@
describe "PM::TableViewCell" do
describe "PM::TableViewCellModule" do
def custom_cell
{
title: "Crazy Full Featured Cell",
subtitle: "This is way too huge...",
arguments: { data: [ "lots", "of", "data" ] },
action: :tapped_cell_1,
height: 50, # manually changes the cell's height
cell_style: UITableViewCellStyleSubtitle,
cell_identifier: "Cell",
cell_class: PM::TableViewCell,
layer: { masks_to_bounds: true },
background_color: UIColor.redColor,
selection_style: UITableViewCellSelectionStyleGray,
accessory: :switch, # currently only :switch is supported
accessory_checked: true, # whether it's "checked" or not
image: { image: UIImage.imageNamed("list"), radius: 15 },
subviews: [ UIView.alloc.initWithFrame(CGRectZero), UILabel.alloc.initWithFrame(CGRectZero) ] # arbitrary views added to the cell
}
end
before do
@subject = TestTableScreen.new
@subject.on_load
@basic_cell = { title: "Basic", action: :basic_cell_tapped, arguments: { id: 1 } }
end
it "should do nothing" do
1.should == 1
end
it "should set the section title" do
@subject.mock! :table_data do
[{
title: "Table cell group 1",
cells: [ @basic_cell.dup ]
},{
title: "Table cell group 2",
cells: [ @basic_cell.dup ]
},{
title: "Table cell group 3",
cells: [ @basic_cell.dup ]
}]
@screen = TestTableScreen.new
button = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap{|b| b.titleLabel.text = "ACC" }
@screen.mock! :table_data do
[
{ title: "", cells: [] },
{ title: "", cells: [
{title: "Test 1", accessory_type: UITableViewCellStateShowingEditControlMask },
custom_cell,
{ title: "Test2", accessory_view: button } ] }
]
end
@subject.update_table_data
@subject.tableView(@subject.table_view, titleForHeaderInSection:0).should == "Table cell group 1"
@subject.tableView(@subject.table_view, titleForHeaderInSection:1).should == "Table cell group 2"
@subject.tableView(@subject.table_view, titleForHeaderInSection:2).should == "Table cell group 3"
@screen.on_load
custom_ip = NSIndexPath.indexPathForRow(1, inSection: 1) # Cell "Crazy Full Featured Cell"
@screen.update_table_data
@subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: custom_ip)
end
it "should be a PM::TableViewCell" do
@subject.should.be.kind_of(PM::TableViewCell)
end
it "should have the right title" do
@subject.textLabel.text.should == "Crazy Full Featured Cell"
end
it "should have the right subtitle" do
@subject.detailTextLabel.text.should == "This is way too huge..."
end
it "should have the right re-use identifier" do
@subject.reuseIdentifier.should == "Cell"
end
it "should set the layer.masksToBounds" do
@subject.layer.masksToBounds.should == true
end
it "should set the background color" do
@subject.backgroundView.backgroundColor.should == UIColor.redColor
end
it "should set the selection color style" do
@subject.selectionStyle.should == UITableViewCellSelectionStyleGray
end
it "should set the accessory view to a switch" do
@subject.accessoryView.should.be.kind_of(UISwitch)
end
it "should set the accessory view to a button" do
ip = NSIndexPath.indexPathForRow(2, inSection: 1)
subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: ip)
subject.accessoryView.should.be.kind_of(UIRoundedRectButton)
end
it "should set the accessory type to edit" do
ip = NSIndexPath.indexPathForRow(0, inSection: 1)
subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: ip)
subject.accessoryView.should.be.nil
subject.accessoryType.should == UITableViewCellStateShowingEditControlMask
end
it "should set an image with a radius" do
@subject.imageView.should.be.kind_of(UIImageView)
@subject.imageView.image.should == UIImage.imageNamed("list")
@subject.imageView.layer.cornerRadius.should == 15.0
end
it "should create two extra subviews" do
@subject.subviews.length.should == 4
@subject.subviews[2].class.should == UIView
@subject.subviews[3].class.should == UILabel
end
end
# def table_data
# [{
# title: "Table cell group 1",
# cells: [{
# title: "Simple cell",
# action: :this_cell_tapped,
# arguments: { id: 4 }
# }, {
# title: "Crazy Full Featured Cell",
# subtitle: "This is way too huge..see note",
# arguments: { data: [ "lots", "of", "data" ] },
# action: :tapped_cell_1,
# height: 50, # manually changes the cell's height
# cell_style: UITableViewCellStyleSubtitle,
# cell_identifier: "Cell",
# cell_class: PM::TableViewCell,
# masks_to_bounds: true,
# background_color: UIColor.whiteColor,
# selection_style: UITableViewCellSelectionStyleGray,
# cell_class_attributes: {
# # any Obj-C attributes to set on the cell
# backgroundColor: UIColor.whiteColor
# },
# accessory: :switch, # currently only :switch is supported
# accessory_view: @some_accessory_view,
# accessory_type: UITableViewCellAccessoryCheckmark,
# accessory_checked: true, # whether it's "checked" or not
# image: { image: UIImage.imageNamed("something"), radius: 15 },
# remote_image: { # remote image, requires SDWebImage CocoaPod
# url: "http://placekitten.com/200/300", placeholder: "some-local-image",
# size: 50, radius: 15
# },
# subviews: [ @some_view, @some_other_view ] # arbitrary views added to the cell
# }]
# }, {
# title: "Table cell group 2",
# cells: [{
# title: "Log out",
# action: :log_out
# }]
# }]
# end