Skip to content

Instantly share code, notes, and snippets.

@etsai

etsai/list.rb Secret

Forked from dbc-challenges/list.rb
Last active December 23, 2015 07:39
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 etsai/8ae709913c77aff6ccf3 to your computer and use it in GitHub Desktop.
Save etsai/8ae709913c77aff6ccf3 to your computer and use it in GitHub Desktop.
require_relative "task"
class List
attr_reader :title, :tasks
def initialize(title, tasks = [])
@title = title
@tasks = tasks
end
def add_task(task)
tasks << task
end
def complete_task(index)
return false unless tasks[index]
if tasks[index].complete!
return true
end
end
def delete_task(index)
tasks.delete_at(index)
end
def completed_tasks
tasks.select { |task| task.complete? }
end
def incomplete_tasks
tasks.select { |task| !task.complete? }
end
end
require "rspec"
require_relative "list"
require_relative "task"
describe List do
let(:title) { "Silly List" }
let(:task1) { Task.new("jiggle") }
let(:task2) { Task.new("jiggle") }
let(:list) { List.new(title, [task1, task2]) }
describe "#initialize" do
it "takes a title for its first argument" do
List.new("Little List").should be_an_instance_of List
end
it "requires one argument" do
expect { List.new }.to raise_error(ArgumentError)
end
end
describe "#add_task" do
it "adds task to collection of task" do
expect {
list.add_task("eat")
}.to change{ list.tasks.count }.by(1)
end
end
describe "#complete_task" do
it "will be false of task index doesn't exist" do
expect(list.complete_task(200)).to be_false
end
it "will be complete task if index exists" do
expect(list.complete_task(0)).to be_true
end
end
describe "#delete_task" do
it "will delete task from list" do
list.delete_task(0)
expect(list.tasks).not_to include [task1]
end
end
describe "#completed_tasks" do
it "will return completed tasks" do
list.complete_task(0)
expect(list.completed_tasks).to eql [task1]
end
end
describe "#incomplete_tasks" do
it "will return incomplete tasks" do
list.complete_task(0)
expect(list.incomplete_tasks).to eql [task2]
end
end
end
class Task
attr_reader :description
def initialize(description)
@description = description
@complete = false
end
def complete?
@complete
end
def complete!
@complete = true
end
end
require "rspec"
require_relative "task"
describe Task do
let(:description) { "Walk the giraffe" }
let(:task) { Task.new(description) }
describe "#initialize" do
it "takes a description for its first argument" do
Task.new("Feed the parakeet").should be_an_instance_of Task
end
it "requires one argument" do
expect { Task.new }.to raise_error(ArgumentError)
end
end
describe "#description" do
it "returns the correct description for the task" do
task.description.should eq description
end
end
describe "#complete?" do
it "returns false for incomplete tasks" do
task.complete?.should be_false
end
it "returns true for completed tasks" do
task.complete!
task.complete?.should be_true
end
end
describe "#complete!" do
it "changes the task from incomplete to completed" do
#
# Note the use of the `be_complete` method here.
# This is some RSpec goodness which expects a
# method `complete?` to be defined which returns
# true or false.
#
task.should_not be_complete
task.complete!
task.should be_complete
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment