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

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

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

Резултати

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

Код

module UI
class TextScreen
def self.draw(&block)
p instance_eval(&block)
end
def self.label(text:, border: nil, style: nil)
text = text.send(style) if ! style.nil?
@label = '' if @label.nil?
if border.nil?
@label += text
else
@label += "#{border}#{text}#{border}"
end
end
def self.vertical(border: nil,style: nil, &block)
if border.nil?
Vertical.instance_eval(&block)
else
vertical_border(Vertical.instance_eval(&block), border)
end
end
def self.horizontal(border: nil,style: nil, &block)
if ! style.nil?
Horizontal.instance_eval(&block).send(style)
else
"#{border}#{Horizontal.instance_eval(&block)}#{border}"
end
end
private
def self.vertical_border(label, border)
longest = label.split("\n").group_by(&:size).max.last
a = label.split("\n").map do |e|
e + ' ' * (longest[0].length - e.length)
end
"#{border}#{a.join(border + "\n")}#{border}"
end
end
class Vertical
def self.label(text:, style: nil)
text = text.send(style) if ! style.nil?
@label = '' if @label.nil?
@label += "#{text}\n"
end
end
class Horizontal
def self.label(text:, style: nil)
text = text.send(style) if ! style.nil?
@label = '' if @label.nil?
@label += "#{text}"
end
end
end

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

"123"
."123"
."1\n2\n3\n"
.F"|1        |\n2        |\n3        |\nsomething|\nsome     |\nsoommee  |"
FF"123SOMEvery"
FF"123SOMEverySOMEvery"
FF.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: horizontal do
     NoMethodError:
       undefined method `horizontal' for UI::Vertical:Class
     # /tmp/d20141126-26053-kgvj8r/spec.rb:58:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:19:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:19:in `vertical'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:57:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `draw'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/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 wraps vertically-aligned components correctly in border
     Failure/Error: expect do
       expected #<Proc:0xb8a3a300@/tmp/d20141126-26053-kgvj8r/spec.rb:85> to render as "      |something|\n      |some     |\n      |soommee  |\n"
     # /tmp/d20141126-26053-kgvj8r/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)>'

  3) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: horizontal border: '|' do
     NoMethodError:
       undefined method `horizontal' for UI::Vertical:Class
     # /tmp/d20141126-26053-kgvj8r/spec.rb:101:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:21:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:21:in `vertical'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `draw'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/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)>'

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

  5) Command Line Toolkit propagates upcase to child components
     Failure/Error: vertical style: :upcase do
     NoMethodError:
       undefined method `vertical' for UI::Horizontal:Class
     # /tmp/d20141126-26053-kgvj8r/spec.rb:142:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:29:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:29:in `horizontal'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `draw'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/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)>'

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

  7) Command Line Toolkit propagates downcase to child components
     Failure/Error: vertical style: :downcase do
     NoMethodError:
       undefined method `vertical' for UI::Horizontal:Class
     # /tmp/d20141126-26053-kgvj8r/spec.rb:177:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:29:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:29:in `horizontal'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `instance_eval'
     # /tmp/d20141126-26053-kgvj8r/solution.rb:4:in `draw'
     # /tmp/d20141126-26053-kgvj8r/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-kgvj8r/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.0217 seconds
11 examples, 7 failures

Failed examples:

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

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

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

+module UI
+ class TextScreen
+ def self.draw(&block)
+ p instance_eval(&block)
+ end
+
+ def self.label(text:, border: nil, style: nil)
+ text = text.send(style) if ! style.nil?
+ @label = '' if @label.nil?
+ if border.nil?
+ @label += text
+ else
+ @label += "#{border}#{text}#{border}"
+ end
+ end
+
+ def self.vertical(border: nil,style: nil, &block)
+ if border.nil?
+ Vertical.instance_eval(&block)
+ else
+ vertical_border(Vertical.instance_eval(&block), border)
+ end
+ end
+
+ def self.horizontal(border: nil,style: nil, &block)
+ if ! style.nil?
+ Horizontal.instance_eval(&block).send(style)
+ else
+ "#{border}#{Horizontal.instance_eval(&block)}#{border}"
+ end
+ end
+
+ private
+ def self.vertical_border(label, border)
+ longest = label.split("\n").group_by(&:size).max.last
+ a = label.split("\n").map do |e|
+ e + ' ' * (longest[0].length - e.length)
+ end
+ "#{border}#{a.join(border + "\n")}#{border}"
+ end
+
+ end
+
+ class Vertical
+ def self.label(text:, style: nil)
+ text = text.send(style) if ! style.nil?
+ @label = '' if @label.nil?
+ @label += "#{text}\n"
+ end
+ end
+
+ class Horizontal
+ def self.label(text:, style: nil)
+ text = text.send(style) if ! style.nil?
+ @label = '' if @label.nil?
+ @label += "#{text}"
+ end
+ end
+end