diff --git a/Homework/Denisa Sandu/code/todo_list.rb b/Homework/Denisa Sandu/code/todo_list.rb new file mode 100644 index 0000000..ffa2f4d --- /dev/null +++ b/Homework/Denisa Sandu/code/todo_list.rb @@ -0,0 +1,49 @@ +class TodoItem + attr_accessor :done + attr_accessor :description + def initialize(description, done = false) + @description = description + @done = done + end + def done! + @done = true + end + def done? + return @done + end +end + +class TodoList + attr_accessor :name, :color,:items, :items_pending, :items_done + def initialize(name, color = {:color => :none}) + @name = name + @color = color[:color] + @items = [] + @items_pending = [] + @items_done = [] + end + def add(item) + if item.to_s == item + auxItem = TodoItem.new(item) + @items.push(auxItem) + @items_pending.push(auxItem) + elsif item.done? + @items.push(item) + @items_done.push(item) + else + @items.push(item) + @items_pending.push(item) + end + end + def find_by_description(description) + return @items.each{|x| x.description == description}[0] + end + def set_as_done(description) + if (find_by_description(description)) + find_by_description(description).done! + return true + else + return false + end + end +end \ No newline at end of file diff --git a/Homework/Denisa Sandu/test/test_deps.rb b/Homework/Denisa Sandu/test/test_deps.rb new file mode 100644 index 0000000..681107a --- /dev/null +++ b/Homework/Denisa Sandu/test/test_deps.rb @@ -0,0 +1,55 @@ +gem 'minitest' +require 'minitest/unit' +require 'minitest/autorun' +require 'stringio' + +class MiniTest::Unit::TestCase + def _ + flunk "Replace _ with an actual value" + end + + def truthy?(value) + !!value + end + + def assert_print(expected) + temp_io = StringIO.new + + $stdout = temp_io + + yield + + temp_io.rewind + result = temp_io.read + + assert_equal expected, result.strip + + ensure + $stdout = STDOUT + end +end + +class MiniTest::Unit + def location e # :nodoc: + last_before_assertion = "" + clean_bt = e.backtrace.reverse_each.take_while do |s| + !(s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/) + end + + if clean_bt[-1] =~ /test_deps.rb/ + last_before_assertion = clean_bt[-2] + else + last_before_assertion = clean_bt[-1] + end + + last_before_assertion.sub(/:in .*$/, '') + end +end + +class ErrUnderscore < StandardError; end + +class Object + def _ + raise ErrUnderscore + end +end diff --git a/Homework/Denisa Sandu/test/todo_list.rb b/Homework/Denisa Sandu/test/todo_list.rb new file mode 100644 index 0000000..95886ee --- /dev/null +++ b/Homework/Denisa Sandu/test/todo_list.rb @@ -0,0 +1,112 @@ +require_relative 'test_deps' +require_relative '../code/todo_list' + +class TodoTest < MiniTest::Unit::TestCase + def test_there_should_be_a_todo_class + assert defined?(TodoItem), "A TodoItem class should exist" + end + + def test_the_todo_class_should_take_a_description + item = TodoItem.new("Get Milk And Cookies") + refute_nil item + end + + def test_the_todo_class_should_have_a_getter_for_its_done_state + item = TodoItem.new("Get Milk And Cookies") + + refute item.done?, "Items should not be done by default" + end + + def test_the_todo_class_can_be_initialized_to_done + item = TodoItem.new("Do the thing", true) + + assert item.done?, "This particular item, however, should be done" + end + + def test_a_todo_can_be_set_to_done + item = TodoItem.new("Do the thing") + refute item.done?, "Item should not be done" + + item.done! + + assert item.done?, "Item should be done" + end + + def test_a_todo_can_get_its_description + item = TodoItem.new("Do the thing") + assert_equal "Do the thing", item.description + end + + def test_a_todo_can_set_its_description_after_creation + item = TodoItem.new("Do the thing") + assert_equal "Do the thing", item.description + + item.description = "Flex muscles" + assert_equal "Flex muscles", item.description + end +end + +class TodoListTest < MiniTest::Unit::TestCase + def test_there_should_be_a_todo_class + assert defined?(TodoListTest), "A TodoList class should exist" + end + + def test_a_todo_list_should_have_a_name + todo_list = TodoList.new("Really gross things I have to wash") + assert_equal "Really gross things I have to wash", todo_list.name + end + + def test_a_todo_list_should_have_a_color + todo_list = TodoList.new("Pokemon to collect", :color => :red) + assert_equal :red, todo_list.color + end + + def test_should_be_able_to_add_a_todo_to_a_list + todo_list = TodoList.new("Pokemon to collect", :color => :red) + + todo_list.add(TodoItem.new("Charizard")) + + assert_equal 1, todo_list.items.length + end + + def test_should_be_able_to_add_a_todo_to_a_list_via_a_string + todo_list = TodoList.new("Pokemon to collect", :color => :red) + + todo_list.add("Charizard") + + assert_equal 1, todo_list.items.length + end + + def test_should_be_able_to_get_a_list_of_tasks_pending + todo_list = TodoList.new("Pokemon to collect", :color => :red) + + todo_list.add("Charizard") + + assert_equal 1, todo_list.items_pending.length + end + + def test_should_be_able_to_get_a_list_of_tasks_done + todo_list = TodoList.new("Pokemon to collect", :color => :red) + + todo_list.add(TodoItem.new("Charizard", true)) + + assert_equal 1, todo_list.items_done.length + end + + def test_should_be_able_to_get_a_task_by_description + todo_list = TodoList.new("Pokemon to collect", :color => :red) + + todo_list.add("Charizard") + + refute_nil todo_list.find_by_description("Charizard") + end + + def test_should_have_a_method_that_sets_a_task_as_done_by_description + todo_list = TodoList.new("Pokemon to collect", :color => :red) + item = TodoItem.new("Charizard") + todo_list.add(item) + + assert todo_list.set_as_done("Charizard"), "Should return true when the task is found" + assert item.done?, "Task should be set as done" + end +end