Решение на Четвърта задача от Делян Димитров

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

Към профила на Делян Димитров

Резултати

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

Код

module UI
def label(text: nil, border: nil, style: nil)
@@result << Label.new(text, border, style)
end
def vertical(border: nil, style: nil)
old_result = @@result
result = []
@@result = result
yield
@@result = old_result
@@result << Vertical.new(border, style, result)
end
def horizontal(border: nil, style: nil)
old_result = @@result
result = []
@@result = result
yield
@@result = old_result
@@result << Horizontal.new(border, style, result)
end
class << self
def result
@@result
end
def result=(result)
@@result = result
end
end
class TextScreen
class << self
def draw
result = []
UI.result = result
yield
result.map { |item| item.draw }.join
end
end
end
class Label
def initialize(text, border, style)
@text = text
@border = border
@style = style
end
def draw
apply_style
add_border
@text
end
def set_style(style)
if @style == nil
@style = style
end
end
def apply_style
case @style
when :upcase
@text = @text.upcase
when :downcase
@text = @text.downcase
end
end
def add_border
if @border != nil
@text = @border + @text + @border
end
end
end
class Vertical
def initialize(border, style, children)
@border = border
@style = style
@children = children
end
def set_style style
if @style == nil
@style = style
end
end
def handle_border
if @border == nil
@children
else
add_border @children
end
end
def add_border children
max_size = children.map { |child| child.size }.max
children.map { |child| child + get_spaces(max_size + child.size) }
bordered = children.map do |child|
spacing = get_spaces max_size - child.size
@border + child + spacing + @border
end
end
def get_spaces(number)
Array.new(number, " ").join
end
def draw
@children.each { |child| child.set_style @style }
@children = @children.map { |child| child.draw }
handle_border.join("\n") + "\n"
end
end
class Horizontal
def initialize(border, style, children)
@border = border
@style = style
@children = children
end
def set_style style
if @style == nil
@style = style
end
end
def add_border(text)
if @border == nil
text
else
@border + text + @border
end
end
def draw
@children.each { |child| child.set_style @style }
add_border @children.map { |child| child.draw }.join
end
end
end
extend UI

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

FFFFFFFFFF.

Failures:

  1) Command Line Toolkit arranges components horizontally by default
     Failure/Error: label text: '1'
     NoMethodError:
       undefined method `label' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a97554>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:21:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:20: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 adding horizontal group does not change the behavior
     Failure/Error: horizontal do
     NoMethodError:
       undefined method `horizontal' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a96244>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:31:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  3) Command Line Toolkit verical group orders elements vertically
     Failure/Error: vertical do
     NoMethodError:
       undefined method `vertical' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a95010>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:43:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  4) Command Line Toolkit handles complex group nestings
     Failure/Error: vertical do
     NoMethodError:
       undefined method `vertical' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a8fd18>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: vertical border: '|' do
     NoMethodError:
       undefined method `vertical' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a8e760>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  6) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: vertical border: '|' do
     NoMethodError:
       undefined method `vertical' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a8d3b0>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  7) Command Line Toolkit applies upcase to simple components
     Failure/Error: label text: 'some', style: :upcase
     NoMethodError:
       undefined method `label' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a8c0a0>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:129:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  8) Command Line Toolkit propagates upcase to child components
     Failure/Error: horizontal do
     NoMethodError:
       undefined method `horizontal' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a86b78>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  9) Command Line Toolkit applies downcase to simple components
     Failure/Error: label text: 'SOME'
     NoMethodError:
       undefined method `label' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a8541c>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:164:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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)>'

  10) Command Line Toolkit propagates downcase to child components
     Failure/Error: horizontal do
     NoMethodError:
       undefined method `horizontal' for #<RSpec::Core::ExampleGroup::Nested_1:0xb9a766b0>
     # /tmp/d20141126-26053-1dbmfby/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/solution.rb:39:in `draw'
     # /tmp/d20141126-26053-1dbmfby/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1dbmfby/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.0171 seconds
11 examples, 10 failures

Failed examples:

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

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

Делян обнови решението на 25.11.2014 22:36 (преди над 9 години)

+module UI
+ def label(text: nil, border: nil, style: nil)
+ @@result << Label.new(text, border, style)
+ end
+
+ class << self
+ def result
+ @@result
+ end
+
+ def result=(result)
+ @@result = result
+ end
+ end
+
+ class TextScreen
+ class << self
+ def draw
+ result = []
+ UI.result = result
+ yield
+ result.map { |item| item.draw }.join '\n'
+ end
+ end
+ end
+
+ class Label
+ def initialize(text, border, style)
+ @text = text
+ @border = border
+ @style = style
+ end
+
+ def draw
+ apply @style
+ add_border
+ @text
+ end
+
+ def apply_style(style)
+ if @style == nil
+ apply style
+ else
+ apply @style
+ end
+ end
+
+ def apply(style)
+ case style
+ when :upcase
+ @text = @text.upcase
+ when :downcase
+ @text = @text.downcase
+ end
+ end
+
+ def add_border
+ if @border != nil
+ @text = @border + @text + @border
+ end
+ end
+ end
+end
+
+extend UI

Делян обнови решението на 25.11.2014 23:41 (преди над 9 години)

module UI
def label(text: nil, border: nil, style: nil)
@@result << Label.new(text, border, style)
end
+ def vertical(border: nil, style: nil)
+ old_result = @@result
+ result = []
+ @@result = result
+ yield
+ @@result = old_result
+ @@result << Vertical.new(border, style, result)
+ end
+
+ def horizontal(border: nil, style: nil)
+ old_result = @@result
+ result = []
+ @@result = result
+ yield
+ @@result = old_result
+ @@result << Horizontal.new(border, style, result)
+ end
+
class << self
def result
@@result
end
def result=(result)
@@result = result
end
end
class TextScreen
class << self
def draw
result = []
UI.result = result
yield
- result.map { |item| item.draw }.join '\n'
+ result.map { |item| item.draw }.join
end
end
end
class Label
def initialize(text, border, style)
@text = text
@border = border
@style = style
end
def draw
- apply @style
+ apply_style
add_border
@text
end
- def apply_style(style)
+ def set_style(style)
if @style == nil
- apply style
- else
- apply @style
+ @style = style
end
end
- def apply(style)
- case style
+ def apply_style
+ case @style
when :upcase
@text = @text.upcase
when :downcase
@text = @text.downcase
end
end
def add_border
if @border != nil
@text = @border + @text + @border
end
+ end
+ end
+
+ class Vertical
+ def initialize(border, style, children)
+ @border = border
+ @style = style
+ @children = children
+ end
+
+ def set_style style
+ if @style == nil
+ @style = style
+ end
+ end
+
+ def handle_border
+ if @border == nil
+ @children
+ else
+ add_border @children
+ end
+ end
+
+ def add_border children
+ max_size = children.map { |child| child.size }.max
+ children.map { |child| child + get_spaces(max_size + child.size) }
+
+ bordered = children.map do |child|
+ spacing = get_spaces max_size - child.size
+ @border + child + spacing + @border
+ end
+ end
+
+ def get_spaces(number)
+ Array.new(number, " ").join
+ end
+
+ def draw
+ @children.each { |child| child.set_style @style }
+ @children = @children.map { |child| child.draw }
+ handle_border.join("\n") + "\n"
+ end
+ end
+
+ class Horizontal
+ def initialize(border, style, children)
+ @border = border
+ @style = style
+ @children = children
+ end
+
+ def set_style style
+ if @style == nil
+ @style = style
+ end
+ end
+
+ def add_border(text)
+ if @border == nil
+ text
+ else
+ @border + text + @border
+ end
+ end
+
+ def draw
+ @children.each { |child| child.set_style @style }
+ add_border @children.map { |child| child.draw }.join
end
end
end
extend UI