Skip to content

Instantly share code, notes, and snippets.

@perspectivezoom
Created June 27, 2012 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perspectivezoom/3007453 to your computer and use it in GitHub Desktop.
Save perspectivezoom/3007453 to your computer and use it in GitHub Desktop.
require 'rspec'
require 'simplecov'
require './task.rb'
SimpleCov.start
require './list.rb'
describe Todo::List do
before(:each) do
@file_path = './spec_todo.txt'
File.open(@file_path, 'w') { |file| file.write '' }
@list = Todo::List.new(@file_path)
@file = File.open(@file_path).read
end
subject { @list }
describe '#initialize' do
it 'should create a List instance' do
@list.should be_an_instance_of Todo::List
end
end
[:parse_tasks, :print_tasks, :add_task, :delete_task, :complete_task, :write_tasks, :task_strings].each do |attribute|
it { should respond_to attribute }
end
describe '#add_task' do
it "should add a task to the file" do
@list.add_task("bake a cake")
File.open(@file_path).read.include?("bake a cake").should be true
end
end
describe '#complete_task' do
it "should complete the task" do
time = Time.now
Time.stub(:now).and_return(time)
@list.add_task('blah')
@list.complete_task(1)
get_tasks(@list)[0].should == "1. blah ; #{time} ; #{time}"
end
end
describe '#write_tasks' do
it "should check if tasks is wrtten to file" do
@list.write_tasks
simulated_file_write = ""
@list.task_strings { |task_string| simulated_file_write << task_string }
File.open(@file_path).read.should eq simulated_file_write
end
end
describe '#delete_task' do
it "should delete the task from the file" do
@list.add_task("bake a cake")
@list.delete_task(1)
File.open(@file_path).read.include?("bake a cake").should be false
end
end
describe '#task_strings' do
it "should give a string of tasks" do
time = Time.now
Time.stub(:now).and_return(time)
@list.add_task("do laundry")
str = ""
@list.task_strings { |task| str << task.to_s }
puts str.inspect
str.chomp.rstrip.should eq "1. do laundry ; #{time} ;"
end
end
end
def get_tasks(list)
ar = []
list.task_strings { |task_string| ar << task_string.chomp.rstrip }
ar
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment