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

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

Към профила на Божидар Горов

Резултати

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

Код

module UI
class TextScreen
@@string = ""
class << self
def draw(&block)
if block_given?
class_eval(&block)
StringParser.new(@@string).handle_string
end
end
def label(args)
@@string << "<label #{args}>#{args[:text]}</label>"
end
def vertical(*args, &block)
@@string << "<vertical #{args}>"
if block_given?
class_eval(&block)
end
@@string << "</vertical>"
end
def horizontal(*args, &block)
@@string << "<horizontal #{args}>"
if block_given?
class_eval(&block)
end
@@string << "</horizontal>"
end
end
end
class StringParser
def initialize(string)
@work_string = string
end
attr_reader :work_string
def handle_string
start
end
private
def start
data = /^<(\w+).*?\1>/.match(@work_string)
send(data[1], @work_string)
end
def vertical(string)
Vertical.new(string).parse
end
def horizontal(string)
Horizontal.new(string).parse
end
def label(string)
Label.new(string).parse
end
end
class Vertical
def initialize(string)
@string = string
@new_string = ''
end
def parse
data = /^<vertical \[(.*?)\]>(.*)/.match(@string)
level = data[2]
generate_string(level)
end
private
def generate_string(level)
while level != '</vertical>'
elem = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[1]
@new_string << StringParser.new(elem).handle_string << "\n"
level = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[3]
end
@new_string
end
end
class Horizontal
def initialize(string)
@string = string
@new_string = ''
end
def parse
data = /^<horizontal \[(.*?)\]>(.*)/.match(@string)
attributes = eval(data[1])
level = data[2]
set_attributes(attributes, generate_string(level))
end
private
def generate_string(level)
while level != '</horizontal>'
elem = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[1]
@new_string << StringParser.new(elem).handle_string
level = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[3]
end
@new_string
end
def set_attributes(attributes, elem)
border = (attributes.has_key?(:border) ? attributes[:border] : '' )
border + elem + border
end
end
class Label
def initialize(string)
@string = string
@new_string = ''
end
def parse
data = /^(<label ({.*?})>.*?<\/label>)(.*)/.match(@string)
generate_string(data[0])
end
private
def generate_string(level)
while level != ''
elem = /^(<(\w+) (.*?})>(.+?)<\/\2>)(.*)/.match(level)[4]
attributes = eval(/^(<(\w+) (.*?})>(.+?)<\/\2>)(.*)/.match(level)[3])
@new_string << set_attributes(attributes, elem)
level = /^(<(\w+) .*?}>.+?<\/\2>)(.*)/.match(level)[3]
end
@new_string
end
def set_attributes(attributes, elem)
border = (attributes.has_key?(:border) ? attributes[:border] : '' )
border + elem + border
end
end
end

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

.FFFFFFFFF.

Failures:

  1) Command Line Toolkit adding horizontal group does not change the behavior
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/spec.rb:30: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 verical group orders elements vertically
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  3) Command Line Toolkit handles complex group nestings
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  4) Command Line Toolkit wraps vertically-aligned components correctly in border
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  5) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  6) Command Line Toolkit applies upcase to simple components
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  7) Command Line Toolkit propagates upcase to child components
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  8) Command Line Toolkit applies downcase to simple components
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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)>'

  9) Command Line Toolkit propagates downcase to child components
     Failure/Error: actual_text   = rstrip_lines.call(UI::TextScreen.draw(&actual).to_s)
     SyntaxError:
       (eval):1: syntax error, unexpected '<'
       []><label {:text=>"1"}
           ^
       (eval):1: syntax error, unexpected =>, expecting '}'
       []><label {:text=>"1"}
                         ^
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `eval'
     # /tmp/d20141126-26053-1f3spes/solution.rb:134:in `generate_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:126:in `parse'
     # /tmp/d20141126-26053-1f3spes/solution.rb:61:in `label'
     # /tmp/d20141126-26053-1f3spes/solution.rb:49:in `start'
     # /tmp/d20141126-26053-1f3spes/solution.rb:43:in `handle_string'
     # /tmp/d20141126-26053-1f3spes/solution.rb:9:in `draw'
     # /tmp/d20141126-26053-1f3spes/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20141126-26053-1f3spes/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.03219 seconds
11 examples, 9 failures

Failed examples:

rspec /tmp/d20141126-26053-1f3spes/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-1f3spes/spec.rb:41 # Command Line Toolkit verical group orders elements vertically
rspec /tmp/d20141126-26053-1f3spes/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1f3spes/spec.rb:84 # Command Line Toolkit wraps vertically-aligned components correctly in border
rspec /tmp/d20141126-26053-1f3spes/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1f3spes/spec.rb:127 # Command Line Toolkit applies upcase to simple components
rspec /tmp/d20141126-26053-1f3spes/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1f3spes/spec.rb:162 # Command Line Toolkit applies downcase to simple components
rspec /tmp/d20141126-26053-1f3spes/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

Божидар обнови решението на 26.11.2014 01:54 (преди почти 10 години)

+module UI
+ class TextScreen
+ @@string = ""
+
+ class << self
+ def draw(&block)
+ if block_given?
+ class_eval(&block)
+ StringParser.new(@@string).handle_string
+ end
+ end
+
+ def label(args)
+ @@string << "<label #{args}>#{args[:text]}</label>"
+ end
+
+ def vertical(*args, &block)
+ @@string << "<vertical #{args}>"
+ if block_given?
+ class_eval(&block)
+ end
+ @@string << "</vertical>"
+ end
+
+ def horizontal(*args, &block)
+ @@string << "<horizontal #{args}>"
+ if block_given?
+ class_eval(&block)
+ end
+ @@string << "</horizontal>"
+ end
+ end
+ end
+
+ class StringParser
+ def initialize(string)
+ @work_string = string
+ end
+
+ attr_reader :work_string
+
+ def handle_string
+ start
+ end
+ private
+
+ def start
+ data = /^<(\w+).*?\1>/.match(@work_string)
+ send(data[1], @work_string)
+ end
+
+ def vertical(string)
+ Vertical.new(string).parse
+ end
+
+ def horizontal(string)
+ Horizontal.new(string).parse
+ end
+
+ def label(string)
+ Label.new(string).parse
+ end
+ end
+
+ class Vertical
+ def initialize(string)
+ @string = string
+ @new_string = ''
+ end
+
+ def parse
+ data = /^<vertical \[(.*?)\]>(.*)/.match(@string)
+ level = data[2]
+ generate_string(level)
+ end
+
+ private
+ def generate_string(level)
+ while level != '</vertical>'
+ elem = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[1]
+ @new_string << StringParser.new(elem).handle_string << "\n"
+ level = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[3]
+ end
+ @new_string
+ end
+ end
+
+ class Horizontal
+ def initialize(string)
+ @string = string
+ @new_string = ''
+ end
+
+ def parse
+ data = /^<horizontal \[(.*?)\]>(.*)/.match(@string)
+ attributes = eval(data[1])
+ level = data[2]
+ set_attributes(attributes, generate_string(level))
+ end
+
+ private
+
+ def generate_string(level)
+ while level != '</horizontal>'
+ elem = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[1]
+ @new_string << StringParser.new(elem).handle_string
+ level = /^(<(\w+) .*?>.+?<\/\2>)(.*)/.match(level)[3]
+ end
+ @new_string
+ end
+
+ def set_attributes(attributes, elem)
+ border = (attributes.has_key?(:border) ? attributes[:border] : '' )
+ border + elem + border
+ end
+ end
+
+ class Label
+ def initialize(string)
+ @string = string
+ @new_string = ''
+ end
+
+ def parse
+ data = /^(<label ({.*?})>.*?<\/label>)(.*)/.match(@string)
+ generate_string(data[0])
+ end
+
+ private
+
+ def generate_string(level)
+ while level != ''
+ elem = /^(<(\w+) (.*?})>(.+?)<\/\2>)(.*)/.match(level)[4]
+ attributes = eval(/^(<(\w+) (.*?})>(.+?)<\/\2>)(.*)/.match(level)[3])
+ @new_string << set_attributes(attributes, elem)
+ level = /^(<(\w+) .*?}>.+?<\/\2>)(.*)/.match(level)[3]
+ end
+ @new_string
+ end
+
+ def set_attributes(attributes, elem)
+ border = (attributes.has_key?(:border) ? attributes[:border] : '' )
+ border + elem + border
+ end
+ end
+end

Знам , че е далече от завършено, но така се случиха нещата. Междодругото някои от тестовете гърмят без да има причина например за вертикална група, когато си го рънвам ръчно няма проблем.