Решение на Втора задача от Елена Орешарова

Обратно към всички решения

Към профила на Елена Орешарова

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 12 успешни тест(а)
  • 10 неуспешни тест(а)

Код

class Criteria
def initialize(argument)
@argument = argument
end
def argument
[@argument].flatten
end
def self.status(status)
Criteria.new(status.upcase.to_sym)
end
def self.priority(priority)
Criteria.new(priority.downcase.to_sym)
end
def self.tags(labels)
Criteria.new(labels)
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize (tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def self.parse (text)
arguments = []
text.lines{ |i| arguments << Task.new(*i.split("|"))}
TodoList.new(arguments)
end
def filter(criteria)
self.tasks.select{ |i|
(criteria.argument.include?(i.status)) ||
(criteria.argument.include?(i.priority)) ||
(!((i.tags & criteria.argument).empty?))
}
end
def tasks_todo
todo_symbol = :TODO
self.count{ |i| i.status == todo_symbol }
end
def tasks_in_progress
current_symbol = :CURRENT
self.count{ |i| i.status == current_symbol }
end
def tasks_completed
done_symbol = :DONE
self.count{ |i| i.status == done_symbol }
end
def completed?
done_symbol = :DONE
self.all? { |i| i.status == done_symbol}
end
end
class Task
attr_reader :description
def initialize(status, description, priority, labels = "")
@status = status.strip
@description = description.strip
@priority = priority.strip
@labels = labels.strip
end
def status
@status.to_sym
end
def priority
@priority.downcase.to_sym
end
def tags
@labels.split(",")
end
end

Лог от изпълнението

...FFFFFFFFF...F......

Failures:

  1) TodoList filters tasks by multiple tags
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby."]
       actual collection contained:    ["Grok Ruby."]
       the missing elements were:      ["Do the 5th Ruby challenge."]
     # /tmp/d20131107-4393-1iabb20/spec.rb:43:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) TodoList filtering by multiple tags matches only tasks with all the tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development FMI]).map(&:description).should =~ ['Do the 5th Ruby challenge.']
       expected collection contained:  ["Do the 5th Ruby challenge."]
       actual collection contained:    ["Grok Ruby."]
       the missing elements were:      ["Do the 5th Ruby challenge."]
       the extra elements were:        ["Grok Ruby."]
     # /tmp/d20131107-4393-1iabb20/spec.rb:47:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) TodoList supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<Criteria:0xb9844f2c @argument=:TODO>
     # /tmp/d20131107-4393-1iabb20/spec.rb:51:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `|' for #<Criteria:0xb98befd4 @argument=:DONE>
     # /tmp/d20131107-4393-1iabb20/spec.rb:56:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `argument' for false:FalseClass
     # /tmp/d20131107-4393-1iabb20/solution.rb:45:in `block in filter'
     # /tmp/d20131107-4393-1iabb20/solution.rb:44:in `select'
     # /tmp/d20131107-4393-1iabb20/solution.rb:44:in `filter'
     # /tmp/d20131107-4393-1iabb20/spec.rb:66:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) TodoList supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `&' for #<Criteria:0xb9873c78 @argument=:high>
     # /tmp/d20131107-4393-1iabb20/spec.rb:76:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Criteria:0xb98714c8 @argument=:TODO>
     # /tmp/d20131107-4393-1iabb20/spec.rb:82:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) TodoList can be adjoined with another to-do list
     Failure/Error: adjoined    = development.adjoin food
     NoMethodError:
       undefined method `adjoin' for #<Array:0xb97430c4>
     # /tmp/d20131107-4393-1iabb20/spec.rb:99:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) TodoList constructs an object for each task
     Failure/Error: task.status.should      eq :todo
       
       expected: :todo
            got: :TODO
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:todo
       +:TODO
     # /tmp/d20131107-4393-1iabb20/spec.rb:112:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
     NoMethodError:
       undefined method `completed?' for #<Array:0xb972c040>
     # /tmp/d20131107-4393-1iabb20/spec.rb:132:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03794 seconds
22 examples, 10 failures

Failed examples:

rspec /tmp/d20131107-4393-1iabb20/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-1iabb20/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-1iabb20/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-1iabb20/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-1iabb20/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-1iabb20/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-1iabb20/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-1iabb20/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-1iabb20/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-1iabb20/spec.rb:130 # TodoList checks if all tasks are completed

История (1 версия и 0 коментара)

Елена обнови решението на 06.11.2013 09:15 (преди над 10 години)

+class Criteria
+
+ def initialize(argument)
+ @argument = argument
+ end
+
+ def argument
+ [@argument].flatten
+ end
+
+ def self.status(status)
+ Criteria.new(status.upcase.to_sym)
+ end
+
+ def self.priority(priority)
+ Criteria.new(priority.downcase.to_sym)
+ end
+
+ def self.tags(labels)
+ Criteria.new(labels)
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_reader :tasks
+
+ def initialize (tasks)
+ @tasks = tasks
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def self.parse (text)
+ arguments = []
+ text.lines{ |i| arguments << Task.new(*i.split("|"))}
+ TodoList.new(arguments)
+ end
+
+ def filter(criteria)
+ self.tasks.select{ |i|
+ (criteria.argument.include?(i.status)) ||
+ (criteria.argument.include?(i.priority)) ||
+ (!((i.tags & criteria.argument).empty?))
+ }
+ end
+
+ def tasks_todo
+ todo_symbol = :TODO
+ self.count{ |i| i.status == todo_symbol }
+ end
+
+ def tasks_in_progress
+ current_symbol = :CURRENT
+ self.count{ |i| i.status == current_symbol }
+ end
+
+ def tasks_completed
+ done_symbol = :DONE
+ self.count{ |i| i.status == done_symbol }
+ end
+
+ def completed?
+ done_symbol = :DONE
+ self.all? { |i| i.status == done_symbol}
+ end
+end
+
+class Task
+ attr_reader :description
+
+ def initialize(status, description, priority, labels = "")
+ @status = status.strip
+ @description = description.strip
+ @priority = priority.strip
+ @labels = labels.strip
+ end
+
+ def status
+ @status.to_sym
+ end
+
+ def priority
+ @priority.downcase.to_sym
+ end
+
+ def tags
+ @labels.split(",")
+ end
+end