Решение на Втора задача от Станимир Килявков

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

Към профила на Станимир Килявков

Резултати

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

Код

class List
attr_reader :status, :description, :priority, :tags
def initialize(status,description,priority,tags)
@status,@description, @tags = status,description,tags
@priority=priority
end
def matches?(criteria,type)
if type==:status and status==criteria then return true end
if type==:description and description ==criteria then return true end
if type==:priority and priority==criteria then return true end
if type==:tags and @tags.any?{|x| ["#{x}"]==criteria } then return true end
end
end
class Criteria < Object
def tags(item)
item
end
def status(item)
if item==:todo then "TODO" end
if item==:current then "CURRENT" end
if item==:done then "DONE" end
end
def priority(item)
case item
when :high then return "High"
when :low then return "Low"
when :normal then return "Normal"
end
end
def description(item)
item
end
end
class Object
$type
def tags(item)
$type=:tags
self.new.tags(item)
end
def priority(item)
$type=:priority
self.new.priority(item)
end
def status(item)
$type=:status
self.new.status(item)
end
def description(item)
$type=:description
self.new.description(item)
end
def filter(z)
self.select{|item| item.matches?(z,$type)}
end
def adjoin(z)
self+z
end
end
class TodoList < Object
include Enumerable
def TodoList::parse(text)
@input=text.lines.map{|item| item.split("|").map(&:strip)}
@input.map! do |status,description,priority,alltags|
tags=alltags.split(",").map(&:strip)
List.new(status,description,priority,tags)
end
end
end

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

...FFFFFFF.FFFFF......

Failures:

  1) TodoList filters tasks by multiple tags
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Grok Ruby."]
     # /tmp/d20131107-4393-tvrkle/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:    []
       the missing elements were:      ["Do the 5th Ruby challenge."]
     # /tmp/d20131107-4393-tvrkle/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.map(&:description).should =~ ['Eat spaghetti.', 'Destroy Facebook and Google.', 'Occupy Sofia University.']
       expected collection contained:  ["Destroy Facebook and Google.", "Eat spaghetti.", "Occupy Sofia University."]
       actual collection contained:    []
       the missing elements were:      ["Destroy Facebook and Google.", "Eat spaghetti.", "Occupy Sofia University."]
     # /tmp/d20131107-4393-tvrkle/spec.rb:52: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 "DONE":String
     # /tmp/d20131107-4393-tvrkle/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: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby.", "Have some tea.", "Party animal."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Grok Ruby.", "Have some tea.", "Party animal."]
     # /tmp/d20131107-4393-tvrkle/spec.rb:72: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 "High":String
     # /tmp/d20131107-4393-tvrkle/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.tags(['development'])
     NoMethodError:
       undefined method `&' for "Normal":String
     # /tmp/d20131107-4393-tvrkle/spec.rb:84: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 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-tvrkle/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)>'

  9) TodoList returns the number of the tasks todo
     Failure/Error: todo_list.tasks_todo.should eq 5
     NoMethodError:
       undefined method `tasks_todo' for #<Array:0xb9c6d8d8>
     # /tmp/d20131107-4393-tvrkle/spec.rb:119: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 returns the number of the tasks in progress
     Failure/Error: todo_list.tasks_in_progress.should eq 2
     NoMethodError:
       undefined method `tasks_in_progress' for #<Array:0xb9c6b45c>
     # /tmp/d20131107-4393-tvrkle/spec.rb:123: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)>'

  11) TodoList returns the number of the completed tasks
     Failure/Error: todo_list.tasks_completed.should eq 2
     NoMethodError:
       undefined method `tasks_completed' for #<Array:0xb9c69224>
     # /tmp/d20131107-4393-tvrkle/spec.rb:127: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)>'

  12) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `completed?' for #<Array:0xb9c62cbc>
     # /tmp/d20131107-4393-tvrkle/spec.rb:131: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.03967 seconds
22 examples, 12 failures

Failed examples:

rspec /tmp/d20131107-4393-tvrkle/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-tvrkle/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-tvrkle/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-tvrkle/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-tvrkle/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-tvrkle/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-tvrkle/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-tvrkle/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-tvrkle/spec.rb:118 # TodoList returns the number of the tasks todo
rspec /tmp/d20131107-4393-tvrkle/spec.rb:122 # TodoList returns the number of the tasks in progress
rspec /tmp/d20131107-4393-tvrkle/spec.rb:126 # TodoList returns the number of the completed tasks
rspec /tmp/d20131107-4393-tvrkle/spec.rb:130 # TodoList checks if all tasks are completed

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

Станимир обнови решението на 06.11.2013 08:43 (преди над 10 години)

+class List
+ attr_reader :status, :description, :priority, :tags
+ def initialize(status,description,priority,tags)
+ @status,@description, @tags = status,description,tags
+ @priority=priority
+ end
+
+ def matches?(criteria,type)
+ if type==:status and status==criteria then return true end
+ if type==:description and description ==criteria then return true end
+ if type==:priority and priority==criteria then return true end
+ if type==:tags and @tags.any?{|x| ["#{x}"]==criteria } then return true end
+ end
+end
+
+class Criteria < Object
+ def tags(item)
+ item
+ end
+
+ def status(item)
+ if item==:todo then "TODO" end
+ if item==:current then "CURRENT" end
+ if item==:done then "DONE" end
+ end
+
+ def priority(item)
+ case item
+ when :high then return "High"
+ when :low then return "Low"
+ when :normal then return "Normal"
+ end
+ end
+
+ def description(item)
+ item
+ end
+ end
+
+class Object
+ $type
+ def tags(item)
+ $type=:tags
+ self.new.tags(item)
+ end
+
+ def priority(item)
+ $type=:priority
+ self.new.priority(item)
+ end
+
+ def status(item)
+ $type=:status
+ self.new.status(item)
+ end
+
+ def description(item)
+ $type=:description
+ self.new.description(item)
+ end
+
+ def filter(z)
+ self.select{|item| item.matches?(z,$type)}
+ end
+ def adjoin(z)
+ self+z
+ end
+end
+
+class TodoList < Object
+ include Enumerable
+ def TodoList::parse(text)
+ @input=text.lines.map{|item| item.split("|").map(&:strip)}
+ @input.map! do |status,description,priority,alltags|
+ tags=alltags.split(",").map(&:strip)
+ List.new(status,description,priority,tags)
+ end
+ end
+end