Решение на Четвърта задача от Атанас Цанков

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

Към профила на Атанас Цанков

Резултати

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

Код

require 'delegate'
module UI
class TextScreen
attr_reader :string
def self.draw(&block)
screen = new
delegator = Delegator.new(screen)
delegator.instance_eval(&block)
screen.string.strip << "\n"
end
def initialize
@string = ""
end
def append(text, horizontal)
@string << text
@string << "\n" unless horizontal
end
class Delegator < SimpleDelegator
def initialize(object)
super(object)
@positions = []
end
def label(text:, border: nil)
@positions << true if @positions.empty?
horizontal = @positions[-1]
text = border + text << border unless border == nil
append(text, horizontal)
end
def horizontal(&block)
@positions << true
instance_eval(&block)
@positions.pop
horizontal = @positions[-1]
append("", horizontal)
end
def vertical(&block)
@positions << false
instance_eval(&block)
@positions.pop
horizontal = @positions[-1]
append("", horizontal)
end
end
end
end

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

...FFFFFFF.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb8e5cc08@/tmp/d20141126-26053-e3k6lz/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/solution.rb:46:in `vertical'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:86:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/solution.rb:46:in `vertical'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:100:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/spec.rb:129:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/solution.rb:46:in `vertical'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:142:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:40:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:40:in `horizontal'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:138:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/spec.rb:165:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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-e3k6lz/solution.rb:46:in `vertical'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:177:in `block (4 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:40:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:40:in `horizontal'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:173:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `instance_eval'
     # /tmp/d20141126-26053-e3k6lz/solution.rb:11:in `draw'
     # /tmp/d20141126-26053-e3k6lz/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-e3k6lz/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.02025 seconds
11 examples, 7 failures

Failed examples:

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

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

Атанас обнови решението на 26.11.2014 12:03 (преди почти 10 години)

+require 'delegate'
+
+module UI
+ class TextScreen
+
+ attr_reader :string
+
+ def self.draw(&block)
+ screen = new
+ delegator = Delegator.new(screen)
+ delegator.instance_eval(&block)
+ screen.string
+ end
+
+ def initialize
+ @string = ""
+ end
+
+ def append(text)
+ @string << text
+ end
+
+ class Delegator < SimpleDelegator
+ def label(text:)
+ append text
+ end
+ end
+ end
+end

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

require 'delegate'
module UI
class TextScreen
attr_reader :string
def self.draw(&block)
- screen = new
+ screen = new
delegator = Delegator.new(screen)
delegator.instance_eval(&block)
- screen.string
- end
+ screen.string
+ end
def initialize
- @string = ""
+ @string = ""
end
def append(text)
- @string << text
+ @string << text
end
class Delegator < SimpleDelegator
def label(text:)
append text
end
end
end
end

Атанас обнови решението на 26.11.2014 14:25 (преди почти 10 години)

require 'delegate'
module UI
class TextScreen
attr_reader :string
def self.draw(&block)
screen = new
delegator = Delegator.new(screen)
delegator.instance_eval(&block)
- screen.string
+ screen.string.strip << "\n"
end
def initialize
@string = ""
end
- def append(text)
+ def append(text, horizontal)
@string << text
+ @string << "\n" unless horizontal
end
class Delegator < SimpleDelegator
- def label(text:)
- append text
+
+ def initialize(object)
+ super(object)
+ @positions = []
+ end
+
+ def label(text:, border: nil)
+ @positions << true if @positions.empty?
+ horizontal = @positions[-1]
+ text = border + text << border unless border == nil
+ append(text, horizontal)
+ end
+
+ def horizontal(&block)
+ @positions << true
+ instance_eval(&block)
+ @positions.pop
+ horizontal = @positions[-1]
+ append("", horizontal)
+ end
+
+ def vertical(&block)
+ @positions << false
+ instance_eval(&block)
+ @positions.pop
+ horizontal = @positions[-1]
+ append("", horizontal)
end
end
end
end