Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 28, 2012 00:29
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 Pcushing/3007858 to your computer and use it in GitHub Desktop.
Save Pcushing/3007858 to your computer and use it in GitHub Desktop.
spec for todo app with Sung
require 'rspec'
require 'simplecov'
SimpleCov.start
require './list.rb'
require './task.rb'
describe Todo::List do
before :each do
@new_list = Todo::List.new('./todo.txt')
end
it "should be initialized" do
@new_list.should be_an_instance_of Todo::List
end
it "should not be initialized without arguments" do
lambda {Todo::List.new}.should raise_error(ArgumentError)
end
it "should parse tasks from file and contain existing tasks" do
old_tasks = File.readlines('./todo.txt')
@new_list.print_tasks({:status=>nil}).length.should equal old_tasks.length
end
[:add_task, :delete_task, :complete_task, :print_tasks].each do |method|
it "should respond to #{ method }" do
@new_list.should respond_to(method)
end
end
it "should add tasks" do
# old_contents = File.readlines('./todo.txt')
@new_list.print_tasks({:status=>:nil}).to_s.index("testing add and delete tasks").should be_nil
@new_list.add_task("testing add and delete tasks")
@new_list.print_tasks({:status=>:nil}).to_s.index("testing add and delete tasks").should_not be_nil
# old_contents.length.should be < File.readlines('./todo.txt').length
end
it "should delete tasks" do
# old_contents = File.readlines('./todo.txt')
@new_list.print_tasks({:status=>:nil}).to_s.index("testing add and delete tasks").should_not be_nil
@new_list.delete_task(@new_list.print_tasks({:status=>:nil}).length)
@new_list.print_tasks({:status=>:nil}).to_s.index("testing add and delete tasks").should be_nil
# old_contents.length.should be > File.readlines('./todo.txt').length
end
it "should complete tasks" do
# task_completer = Todo::Task.new("We want to complete this")
@new_list.complete_task(1)
File.readlines('./todo.txt')[1].split(' ; ')[2].chomp.should_not be ""
end
it "should print all tasks" do
@new_list.print_tasks({:status=>:nil})
$stdout.should_not be nil
end
it "should print complete tasks" do
count = 0
File.readlines('./todo.txt').each do |line|
if line.split(' ; ')[2].chomp != ""
count +=1
end
end
@new_list.print_tasks({:status=>:complete}).length.should equal count
end
it "should print incomplete tasks" do
count = 0
File.readlines('./todo.txt').each do |line|
if line.split(' ; ')[2].chomp == ""
count +=1
end
end
@new_list.print_tasks({:status=>:incomplete}).length.should equal count
end
end
describe Todo::Task do
before :each do
@new_task = Todo::Task.new("Task 1")
end
it "should be initialized" do
@new_task.should be_an_instance_of Todo::Task
end
[:to_s, :complete!, :complete?, :incomplete?].each do |method|
it "should respond to #{method}" do
@new_task.should respond_to(method)
end
end
it "should respond to self.from_string" do
Todo::Task.should respond_to :from_string
end
it "should create a string of 3 separate 'columns'" do
@new_task.to_s.split(';').length.should equal 3
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment