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

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

Към профила на Константин Тодоров

Резултати

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

Код

module UI
class TextScreen
def self.draw(&block)
@result = StyledText.new
instance_eval &block if block_given?
@result.text
end
def self.label(text:, border: nil, style: nil)
@result.add_text "#{border}#{text}#{border}"
end
def self.vertical(border: nil, style: nil, &block)
@result.vertical = true
@result = @result.inner_level
@result.text += "#{border}"
instance_eval &block if block_given?
@result.text += "#{border}"
@result = @result.upper_level
end
def self.horizontal(border: nil, style: nil, &block)
@result.vertical = false
@result = @result.inner_level
@result.text += "#{border}"
instance_eval &block if block_given?
@result.text += "#{border}"
@result = @result.upper_level
end
def self.max_length
end
end
class StyledText
attr_accessor :vertical
attr_accessor :text
attr_accessor :inner_level
def initialize(given_parent = nil, is_vertical = false)
@parent = given_parent
@inner_level = nil
@vertical = is_vertical
@text = ''
end
def inner_level
@inner_level = StyledText.new(self, @vertical)
end
def upper_level
return @text if @parent == nil
@parent.text += @text
@parent.text += "\n" if @parent.vertical
@parent.inner_level = nil
@inner_level = nil
@parent
end
def add_text(given_text)
@text += given_text
@text += "\n" if @vertical
end
end
end

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

..FFFFFFFF.

Failures:

  1) Command Line Toolkit verical group orders elements vertically
     Failure/Error: expect do
       expected #<Proc:0xba03509c@/tmp/d20141126-26053-12u3049/spec.rb:42> to render as "      1\n      2\n      3\n"
     # /tmp/d20141126-26053-12u3049/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)>'

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

  3) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: expect do
       expected #<Proc:0xba02b560@/tmp/d20141126-26053-12u3049/spec.rb:85> to render as "      |something|\n      |some     |\n      |soommee  |\n"
     # /tmp/d20141126-26053-12u3049/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)>'

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

  5) Command Line Toolkit applies upcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xba003704@/tmp/d20141126-26053-12u3049/spec.rb:128> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-12u3049/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)>'

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

  7) Command Line Toolkit applies downcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb9fedd64@/tmp/d20141126-26053-12u3049/spec.rb:163> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-12u3049/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)>'

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

Failed examples:

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

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

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

+module UI
+ class TextScreen
+ def self.draw(&block)
+ @result = StyledText.new
+ instance_eval &block if block_given?
+ @result.text
+ end
+
+ def self.label(text:, border: nil, style: nil)
+ @result.add_text "#{border}#{text}#{border}"
+ end
+
+ def self.vertical(border: nil, style: nil, &block)
+ @result.vertical = true
+ @result = @result.inner_level
+ @result.text += "#{border}"
+ instance_eval &block if block_given?
+ @result.text += "#{border}"
+ @result = @result.upper_level
+
+ end
+
+ def self.horizontal(border: nil, style: nil, &block)
+ @result.vertical = false
+ @result = @result.inner_level
+ @result.text += "#{border}"
+ instance_eval &block if block_given?
+ @result.text += "#{border}"
+ @result = @result.upper_level
+ end
+
+ def self.max_length
+
+ end
+ end
+
+ class StyledText
+ attr_accessor :vertical
+ attr_accessor :text
+ attr_accessor :inner_level
+
+ def initialize(given_parent = nil, is_vertical = false)
+ @parent = given_parent
+ @inner_level = nil
+ @vertical = is_vertical
+ @text = ''
+ end
+
+ def inner_level
+ @inner_level = StyledText.new(self, @vertical)
+ end
+
+ def upper_level
+ return @text if @parent == nil
+ @parent.text += @text
+ @parent.text += "\n" if @parent.vertical
+ @parent.inner_level = nil
+ @inner_level = nil
+ @parent
+ end
+
+ def add_text(given_text)
+ @text += given_text
+ @text += "\n" if @vertical
+ end
+ end
+end

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

module UI
class TextScreen
def self.draw(&block)
@result = StyledText.new
instance_eval &block if block_given?
@result.text
end
def self.label(text:, border: nil, style: nil)
@result.add_text "#{border}#{text}#{border}"
end
def self.vertical(border: nil, style: nil, &block)
- @result.vertical = true
- @result = @result.inner_level
- @result.text += "#{border}"
+ @result.vertical = true
+ @result = @result.inner_level
+ @result.text += "#{border}"
instance_eval &block if block_given?
@result.text += "#{border}"
@result = @result.upper_level
end
def self.horizontal(border: nil, style: nil, &block)
- @result.vertical = false
- @result = @result.inner_level
+ @result.vertical = false
+ @result = @result.inner_level
@result.text += "#{border}"
instance_eval &block if block_given?
@result.text += "#{border}"
@result = @result.upper_level
end
def self.max_length
end
end
class StyledText
attr_accessor :vertical
attr_accessor :text
attr_accessor :inner_level
def initialize(given_parent = nil, is_vertical = false)
- @parent = given_parent
+ @parent = given_parent
@inner_level = nil
@vertical = is_vertical
@text = ''
end
def inner_level
@inner_level = StyledText.new(self, @vertical)
end
def upper_level
- return @text if @parent == nil
+ return @text if @parent == nil
@parent.text += @text
@parent.text += "\n" if @parent.vertical
@parent.inner_level = nil
@inner_level = nil
@parent
end
def add_text(given_text)
@text += given_text
@text += "\n" if @vertical
end
end
end