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

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

Към профила на Мартина Радева

Резултати

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

Код

module UI
class TextScreen
def self.draw &block
screen = Screen.new
screen.instance_eval(&block)
puts screen.render.inspect
screen.render
end
end
class Screen
def initialize()
@screen = []
end
def horizontal &block
yield
@screen = [@screen, "\n"]
puts @screen.inspect
end
def vertical &block
yield
@screen.map! {|group| [group] << "\n"}
@screen.last.pop
end
def render
@screen.join
end
def label (text, style: nil, border: nil)
label = TextLabel.new(text)
@screen << label.label
end
end
class TextLabel
attr_reader :label
def initialize(text:)
@label = text
end
end
def border #border will be defined as a patch of method_missing
end
def style # style will be defined as a patch of method_missing
end
end

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

"123"
.[["1", "2", "3"], "\n"]
"123\n"
."1\n2\n3"
.[[["1", "\n"], ["2", "\n"], ["3", "\n"], ["4", "\n"], ["5", "\n"], ["6"]], "\n"]
[[[[["1", "\n"], ["2", "\n"], ["3", "\n"], ["4", "\n"], ["5", "\n"], ["6"]], "\n"], ["\n"]], "\n"]
"1\n2\n3\n4\n5\n6\n\n\n\n\n7"
FFFFFFF.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9354170@/tmp/d20141126-26053-124xdwi/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-124xdwi/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: vertical border: '|' do
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141126-26053-124xdwi/solution.rb:24:in `vertical'
     # /tmp/d20141126-26053-124xdwi/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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: vertical border: '|' do
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141126-26053-124xdwi/solution.rb:24:in `vertical'
     # /tmp/d20141126-26053-124xdwi/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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: label text: 'some', style: :upcase
     ArgumentError:
       unknown keyword: style
     # /tmp/d20141126-26053-124xdwi/solution.rb:35:in `new'
     # /tmp/d20141126-26053-124xdwi/solution.rb:35:in `label'
     # /tmp/d20141126-26053-124xdwi/spec.rb:129:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141126-26053-124xdwi/solution.rb:24:in `vertical'
     # /tmp/d20141126-26053-124xdwi/spec.rb:142:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:19:in `horizontal'
     # /tmp/d20141126-26053-124xdwi/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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: label text: 'VERY', style: :downcase
     ArgumentError:
       unknown keyword: style
     # /tmp/d20141126-26053-124xdwi/solution.rb:35:in `new'
     # /tmp/d20141126-26053-124xdwi/solution.rb:35:in `label'
     # /tmp/d20141126-26053-124xdwi/spec.rb:165:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141126-26053-124xdwi/solution.rb:24:in `vertical'
     # /tmp/d20141126-26053-124xdwi/spec.rb:177:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:19:in `horizontal'
     # /tmp/d20141126-26053-124xdwi/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `instance_eval'
     # /tmp/d20141126-26053-124xdwi/solution.rb:7:in `draw'
     # /tmp/d20141126-26053-124xdwi/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-124xdwi/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.02103 seconds
11 examples, 7 failures

Failed examples:

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

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

Мартина обнови решението на 26.11.2014 09:43 (преди почти 10 години)

+module UI
+
+ class TextScreen
+
+ def self.draw &block
+ screen = Screen.new
+ screen.instance_eval(&block)
+ puts screen.render.inspect
+ screen.render
+ end
+ end
+
+ class Screen
+ def initialize()
+ @screen = []
+ end
+
+ def horizontal &block
+ yield
+ @screen = [@screen, "\n"]
+ puts @screen.inspect
+ end
+
+ def vertical &block
+ yield
+ @screen.map! {|group| [group] << "\n"}
+ @screen.last.pop
+ end
+
+ def render
+ @screen.join
+ end
+
+ def label (text, style: nil, border: nil)
+ label = TextLabel.new(text)
+ @screen << label.label
+ end
+
+ end
+
+ class TextLabel
+ attr_reader :label
+
+ def initialize(text:)
+ @label = text
+ end
+
+ end
+
+ def border #border will be defined as a patch of method_missing
+ end
+
+ def style # style will be defined as a patch of method_missing
+ end
+
+end