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

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

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

Резултати

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

Код

module UI
class TextScreen
attr_accessor :line
def label(text: ,border: nil, style: nil)
style = @case if style == nil
@line = [] if @line == nil
case style
when :upcase then text.upcase!
when :downcase then text.downcase!
end
@line << border.to_s + text + border.to_s
end
def vertical(border: nil, style: nil, &block)
@line = [] if @line == nil
old_line = @line.dup
layout(style, &block)
length = @line.max_by(&:length).length
@line = @line.map { |word| vertical_border(word, border, length) }
@line = old_line + [@line.join("\n")]
end
def horizontal(border: nil, style: nil, &block)
@line = [] if @line == nil
old_line = @line.dup
layout(style, &block)
@line.map {|word| border.to_s + word + border.to_s}
@line = old_line + [@line.join('')]
end
def layout(style, &block)
old_case = @case
@case = style
@line = []
@case = style unless style == nil
self.instance_eval(&block) if block_given?
@case = old_case
end
def vertical_border(word, border, length)
unless (word == "\n" or word == "")
border.to_s + word.ljust(length, ' ') + border.to_s
else
word
end
end
def self.draw(&block)
screen = self.new
screen.instance_eval(&block) if block_given?
screen.line.join('')
end
end
end

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

...F.F.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9a8b45c@/tmp/d20141126-26053-1azeu0e/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-1azeu0e/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)>'

  2) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9a81060@/tmp/d20141126-26053-1azeu0e/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-1azeu0e/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)>'

  3) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9a5b5b8@/tmp/d20141126-26053-1azeu0e/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-1azeu0e/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)>'

  4) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9a4e124@/tmp/d20141126-26053-1azeu0e/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-1azeu0e/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.02266 seconds
11 examples, 4 failures

Failed examples:

rspec /tmp/d20141126-26053-1azeu0e/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1azeu0e/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1azeu0e/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1azeu0e/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

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

+module UI
+ class TextScreen
+ attr_accessor :line
+
+ def label(text: ,border: nil, style: nil)
+ style = @case if style == nil
+ @line = [] if @line == nil
+ case style
+ when :upcase then text.upcase!
+ when :downcase then text.downcase!
+ end
+ @line << border.to_s + text + border.to_s
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ @line = [] if @line == nil
+ old_line = @line.dup
+ layout(style, &block)
+ length = @line.max_by(&:length).length
+ @line = @line.map { |word| vertical_border(word, border, length) }
+ @line = old_line + [@line.join("\n")]
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ @line = [] if @line == nil
+ old_line = @line.dup
+ layout(style, &block)
+ @line.map {|word| border.to_s + word + border.to_s}
+ @line = old_line + [@line.join('')]
+ end
+
+ def layout(style, &block)
+ old_case = @case
+ @case = style
+ @line = []
+ @case = style unless style == nil
+ self.instance_eval(&block) if block_given?
+ @case = old_case
+ end
+
+ def vertical_border(word, border, length)
+ unless (word == "\n" or word == "")
+ border.to_s + word.ljust(length, ' ') + border.to_s
+ else
+ word
+ end
+ end
+
+ def self.draw(&block)
+ screen = self.new
+ screen.instance_eval(&block) if block_given?
+ screen.line.join('')
+ end
+ end
+end

Прегледай примерите, които сме дискутирали в темата във форума.

Винаги ще подаваме блок на TextScreen.draw, horizontal и vertical. Няма нужда да се грижиш за друго поведение.

На 5-ти ред интервалът трябва да е след запетаята, а не преди.

Малко по-добро име за @line? :)