Решение на Трета задача от Методи Димитров

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

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

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 34 успешни тест(а)
  • 10 неуспешни тест(а)

Код

module RBFS
class File
attr_reader :data, :data_type
def initialize(new_data = nil)
@data = new_data
set_data_type
end
def data=(new_data)
@data = new_data
set_data_type
end
def serialize
"#{@data_type}:#{@data}"
end
def File.parse(string_data)
type, data = *string_data.split(":")
case type
when 'nil' then File.new
when 'string' then File.new data
when 'number' then File.new data.to_f
when ('boolean' and data == "true") then File.new true
when ('boolean' and data == "false") then File.new false
end
end
private
def set_data_type
@data_type = case
when @data.is_a?(NilClass) then :nil
when @data.is_a?(String) then :string
when @data.is_a?(Numeric) then :number
when (@data.is_a?(TrueClass) or data.is_a?(FalseClass)) then :boolean
end
end
end
class Directory
attr_reader :files, :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
@directories[name] = directory ? directory : Directory.new
end
def [](name)
@directories[name] or @files[name]
end
def serialize
@serialized = "#{@files.count}:"
@files.each do |name, data|
@serialized << "#{name}:#{data.serialize.size}:#{data.serialize}"
end
@serialized << "#{@directories.count}:"
@directories.each do |name, data|
@serialized << "#{name}:#{data.serialize.size}:#{data.serialize}"
end
@serialized
end
def Directory.parse(string_data)
Directory.new()
end
end
end

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

.........F.FFF..............FFFF........F..F

Failures:

  1) RBFS Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: expect(directory.serialize).to eq recursive_serialized_string
       
       expected: "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS2:rbfs:64:1:solution.rb:13:symbol:hidden1:spec:24:1:test:12:boolean:true0:sample:4:0:0:"
            got: "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS2:rbfs:57:1:solution.rb:7::hidden1:spec:24:1:test:12:boolean:true0:sample:4:0:0:"
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:95:in `block (5 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) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: expect(parsed_directory.files.size     ).to eq    2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:110:in `block (5 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) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: expect(parsed_directory['dir1']        ).to be_an RBFS::Directory
       expected nil to be a kind of RBFS::Directory
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:119:in `block (5 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) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: expect(parsed_directory.files.size     ).to eq 2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:127:in `block (5 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) RBFS File data type string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: "Hay "
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:257:in `block (5 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) RBFS File data type symbol can be detected
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:266:in `block (5 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) RBFS File data type symbol can be serialized
     Failure/Error: expect(file.serialize).to eq 'symbol:yo'
       
       expected: "symbol:yo"
            got: ":yo"
       
       (compared using ==)
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:270:in `block (5 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) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
     NoMethodError:
       undefined method `data' for nil:NilClass
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:276:in `block (5 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) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
     NoMethodError:
       undefined method `data' for nil:NilClass
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:334:in `block (6 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)>'

  10) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
     NoMethodError:
       undefined method `data' for nil:NilClass
     # /tmp/d20141111-26053-1wmf9mg/spec.rb:353:in `block (6 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.04246 seconds
44 examples, 10 failures

Failed examples:

rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:269 # RBFS File data type symbol can be serialized
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1wmf9mg/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Методи обнови решението на 10.11.2014 16:45 (преди около 10 години)

+module RBFS
+ class File
+ attr_reader :data, :data_type
+
+ def initialize(new_data = nil)
+ @data = new_data
+ set_data_type
+ end
+
+ def data=(new_data)
+ @data = new_data
+ set_data_type
+ end
+
+ def serialize
+ "#{@data_type}:#{@data}"
+ end
+
+ def File.parse(string_data)
+ type, data = *string_data.split(":")
+ case type
+ when 'nil' then File.new
+ when 'string' then File.new data
+ when 'number' then File.new data.to_f
+ when ('boolean' and data == "true") then File.new true
+ when ('boolean' and data == "false") then File.new false
+ end
+ end
+
+ private
+
+ def set_data_type
+ @data_type = case
+ when @data.is_a?(NilClass) then :nil
+ when @data.is_a?(String) then :string
+ when @data.is_a?(Numeric) then :number
+ when (@data.is_a?(TrueClass) or data.is_a?(FalseClass)) then :boolean
+ end
+ end
+ end
+
+ class Directory
+ attr_reader :files, :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = nil)
+ @directories[name] = directory ? directory : Directory.new
+ end
+
+ def [](name)
+ @directories[name] or @files[name]
+ end
+
+ def serialize
+ @serialized = "#{@files.count}:"
+ @files.each do |name, data|
+ @serialized << "#{name}:#{data.serialize.size}:#{data.serialize}"
+ end
+ @serialized << "#{@directories.count}:"
+ @directories.each do |name, data|
+ @serialized << "#{name}:#{data.serialize.size}:#{data.serialize}"
+ end
+ @serialized
+ end
+
+ def Directory.parse(string_data)
+ Directory.new()
+ end
+ end
+end