Решение на Четвърта задача от Светослав Кръстев

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

Към профила на Светослав Кръстев

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 2 успешни тест(а)
  • 9 неуспешни тест(а)

Код

module UI
class TextScreen
@text = ""
def self.draw(&block)
self.instance_eval(&block)
return @text
end
def self.label(text: '', border: '', style: lambda{ |x| x })
@text += border + '' + style.to_proc.call(text) + '' + border
end
def self.horizontal(style: lambda{ |x| x }, border: '', &block)
group = HorizontalGroup.new group_style: style, group_border: border
group.instance_eval(&block)
@text += group.to_s
end
def self.vertical(style: lambda{ |x| x }, border: '', &block)
group = VerticalGroup.new group_style: style, group_border: border
group.instance_eval(&block)
@text += group.to_s
end
end
class Group
def initialize(group_style: ,group_border:)
@components = []
@bordered = []
@group_style = group_style
@group_border = group_border
end
def label(text: '', border: '', style: lambda{ |x| x })
text_group = @group_style.to_proc.call(text)
@components << border + '' + style.to_proc.call(text_group) + '' + border
end
def horizontal(style: lambda{ |x| x }, border: '', &block)
group = HorizontalGroup.new group_style: style, group_border: border
group.instance_eval(&block)
@components << group.to_s
end
def vertical(style: lambda{ |x| x }, border: '', &block)
group = VerticalGroup.new group_style: style, group_border: border
group.instance_eval(&block)
@components << group.to_s
end
end
class VerticalGroup < Group
def to_s
self.border_components
@bordered.join("\n") + "\n"
end
def border_components
max_length = @components.zip(@components.map { |x| x = x.length }).max[1]
@components.each do |current|
gap = max_length - current.length
@bordered << @group_border + current + ' ' * gap + @group_border
end
end
end
class HorizontalGroup < Group
def to_s
@group_border + @components.join('') + @group_border
end
end
end

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

.FFFFFFFFF.

Failures:

  1) Command Line Toolkit adding horizontal group does not change the behavior
     Failure/Error: expect do
       expected #<Proc:0xb8dbd3c4@/tmp/d20141126-26053-lj4fpz/spec.rb:30> to render as "      123\n"
     # /tmp/d20141126-26053-lj4fpz/spec.rb:30: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) Command Line Toolkit verical group orders elements vertically
     Failure/Error: expect do
       expected #<Proc:0xb8db767c@/tmp/d20141126-26053-lj4fpz/spec.rb:42> to render as "      1\n      2\n      3\n"
     # /tmp/d20141126-26053-lj4fpz/spec.rb:42: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) Command Line Toolkit handles complex group nestings
     Failure/Error: vertical do
     ArgumentError:
       negative argument
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `*'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `block in border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `each'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:55:in `to_s'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:23:in `vertical'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/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)>'

  4) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: vertical border: '|' do
     ArgumentError:
       negative argument
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `*'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `block in border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `each'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:55:in `to_s'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:23:in `vertical'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:85: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) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb8d9ebe0@/tmp/d20141126-26053-lj4fpz/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-lj4fpz/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)>'

  6) Command Line Toolkit applies upcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb8d8a910@/tmp/d20141126-26053-lj4fpz/spec.rb:128> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-lj4fpz/spec.rb:128: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) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb8d77680@/tmp/d20141126-26053-lj4fpz/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-lj4fpz/spec.rb:137: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) Command Line Toolkit applies downcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb8d621cc@/tmp/d20141126-26053-lj4fpz/spec.rb:163> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-lj4fpz/spec.rb:163: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) Command Line Toolkit propagates downcase to child components
     Failure/Error: vertical style: :downcase do
     ArgumentError:
       negative argument
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `*'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:63:in `block in border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `each'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:61:in `border_components'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:55:in `to_s'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:49:in `vertical'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:177:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:16:in `instance_eval'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:16:in `horizontal'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `instance_eval'
     # /tmp/d20141126-26053-lj4fpz/solution.rb:6:in `draw'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-lj4fpz/spec.rb:172: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.02385 seconds
11 examples, 9 failures

Failed examples:

rspec /tmp/d20141126-26053-lj4fpz/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:41 # Command Line Toolkit verical group orders elements vertically
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:84 # Command Line Toolkit wraps vertically-aligned components correctly in border
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:127 # Command Line Toolkit applies upcase to simple components
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:162 # Command Line Toolkit applies downcase to simple components
rspec /tmp/d20141126-26053-lj4fpz/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

Светослав обнови решението на 26.11.2014 16:00 (преди почти 10 години)

+module UI
+ class TextScreen
+ @text = ""
+
+ def self.draw(&block)
+ self.instance_eval(&block)
+ return @text
+ end
+
+ def self.label(text: '', border: '', style: lambda{ |x| x })
+ @text += border + '' + style.to_proc.call(text) + '' + border
+ end
+
+ def self.horizontal(style: lambda{ |x| x }, border: '', &block)
+ group = HorizontalGroup.new group_style: style, group_border: border
+ group.instance_eval(&block)
+ @text += group.to_s
+ end
+
+ def self.vertical(style: lambda{ |x| x }, border: '', &block)
+ group = VerticalGroup.new group_style: style, group_border: border
+ group.instance_eval(&block)
+ @text += group.to_s
+ end
+ end
+
+ class Group
+ def initialize(group_style: ,group_border:)
+ @components = []
+ @bordered = []
+ @group_style = group_style
+ @group_border = group_border
+ end
+
+ def label(text: '', border: '', style: lambda{ |x| x })
+ text_group = @group_style.to_proc.call(text)
+ @components << border + '' + style.to_proc.call(text_group) + '' + border
+ end
+
+ def horizontal(style: lambda{ |x| x }, border: '', &block)
+ group = HorizontalGroup.new group_style: style, group_border: border
+ group.instance_eval(&block)
+ @components << group.to_s
+ end
+
+ def vertical(style: lambda{ |x| x }, border: '', &block)
+ group = VerticalGroup.new group_style: style, group_border: border
+ group.instance_eval(&block)
+ @components << group.to_s
+ end
+ end
+
+ class VerticalGroup < Group
+ def to_s
+ self.border_components
+ @bordered.join("\n") + "\n"
+ end
+
+ def border_components
+ max_length = @components.zip(@components.map { |x| x = x.length }).max[1]
+ @components.each do |current|
+ gap = max_length - current.length
+ @bordered << @group_border + current + ' ' * gap + @group_border
+ end
+ end
+ end
+
+ class HorizontalGroup < Group
+ def to_s
+ @group_border + @components.join('') + @group_border
+ end
+ end
+end