Permalink
Browse files

Spec the moving of a task's position and fix the functionality

  • Loading branch information...
1 parent cbaf214 commit f91dbae79ab2604cf294b9a3939c8fa3f8e05028 @langhorst committed May 3, 2011
Showing with 47 additions and 11 deletions.
  1. +9 −10 lib/pomo/list.rb
  2. +38 −1 spec/list_spec.rb
View
@@ -71,18 +71,14 @@ def add task
alias :<< :add
##
- ## Move _task_ from current position to new position (requres saving).
+ ## Move task at _from_ position to new _to_ position (requres saving).
def move from, to
- list = []
+ list = Array.new(@tasks.size)
+ list[position(to)] = @tasks[position(from)]
@tasks.each_with_index do |task, index|
next if index == position(from)
- if index == position(to)
- list << @tasks[position(from)]
- list << task
- else
- list << task
- end
+ list[list.find_index(nil)] = task
end
@tasks = list
end
@@ -104,12 +100,15 @@ def load
@tasks = YAML.load_file path
self
end
-
+
+ ##
+ # Determine position from argument.
+
def position arg
return case arg.downcase
when 'first' then 0
when 'last' then @tasks.size-1
- else arg.to_i
+ else arg.to_i > @tasks.size-1 ? @tasks.size-1 : arg.to_i
end
end
private :position
View
@@ -30,7 +30,7 @@
@list.save
@list = Pomo::List.new(@path)
@list.load # not specifically needed, called upon initialization
- @list.tasks.first.should == 'Task 0'
+ @list.tasks.first.name.should == 'Task 0'
end
it "can find all tasks" do
@@ -77,5 +77,42 @@
task.name.should == names.shift
end
end
+
+ it "can move a task's position from first to last" do
+ @list.move('first', 'last')
+ @list.tasks.first.name.should == 'Task 1'
+ @list.tasks.last.name.should == 'Task 0'
+ end
+
+ it "can move a task's position from last to first" do
+ @list.move('last', 'first')
+ @list.tasks.first.name.should == 'Task 4'
+ @list.tasks.last.name.should == 'Task 3'
+ end
+
+ it "can move a task's position with numbers forwards" do
+ @list.move('1', '3')
+ @list.tasks[0].name.should == 'Task 0'
+ @list.tasks[1].name.should == 'Task 2'
+ @list.tasks[2].name.should == 'Task 3'
+ @list.tasks[3].name.should == 'Task 1'
+ @list.tasks[4].name.should == 'Task 4'
+ end
+
+ it "can move a task's position with numbers backwards" do
+ @list.move('3', '1')
+ @list.tasks[0].name.should == 'Task 0'
+ @list.tasks[1].name.should == 'Task 3'
+ @list.tasks[2].name.should == 'Task 1'
+ @list.tasks[3].name.should == 'Task 2'
+ @list.tasks[4].name.should == 'Task 4'
+ end
+
+ it "uses equivalent of 'last' for all arguments larger than size of task list" do
+ @list.move('first', '100')
+ @list.tasks.first.name.should == 'Task 1'
+ @list.tasks[4].name.should == 'Task 0'
+ @list.tasks.size.should == 5
+ end
end

0 comments on commit f91dbae

Please sign in to comment.