Решение на Четвърта задача - Unit тестване от Емине Башева

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

Към профила на Емине Башева

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
it 'initialize right with string' do
version = Version.new('1.8.1')
expect(version).to be_truthy
end
it 'initialize right with Version object' do
version = Version.new(Version.new('1.8.1'))
expect(version).to be_truthy
end
it 'initialize right with string witn many numbers' do
version = Version.new(Version.new('1.8.1.8.1.8.1.8.1.8.1.8.1.8.1'))
expect(version).to be_truthy
end
it 'raise error on initialize witn other symbols' do
expect { Version.new('1.8.a') }.to raise_error(ArgumentError)
end
it 'raise error on initialize with negative numbers' do
expect { Version.new('1.-8.1') }.to raise_error(ArgumentError)
end
it 'raise error on initialize with string without numbers' do
expect { Version.new('...') }.to raise_error(ArgumentError)
end
it 'raise error on initialize with string starting with dot' do
expect { Version.new('.8.1') }.to raise_error(ArgumentError)
end
it 'raise error on initialize with string starting with dot' do
expect { Version.new('.8.1') }.to raise_error(ArgumentError)
end
it 'raise error on initialize with string with missing number' do
expect { Version.new('1..1') }.to raise_error(ArgumentError)
end
end
describe '#<=>' do
it 'compare with = with true result with same values' do
version1 = Version.new('1.8.8')
version2 = Version.new('1.8.8')
expect(version1).to eq(version2)
end
it 'compare with < with true result' do
version1 = Version.new('1.8.1')
version2 = Version.new('1.8.8')
expect(version1).to be < version2
end
it 'compare with < with false result' do
version1 = Version.new('1.8.1')
version2 = Version.new('1.8.8')
expect(version2).not_to be < version1
end
it 'compare with > with true result' do
version1 = Version.new('1.8.1')
version2 = Version.new('1.8.8')
expect(version2).to be > version1
end
it 'compare with > with false result' do
version1 = Version.new('1.8.1')
version2 = Version.new('1.8.8')
expect(version1).not_to be > version2
end
end
describe '#to_s' do
it 'check the same string as string in .new' do
str_version = '1.8.1'
version = Version.new(str_version)
expect(version.to_s).to eq str_version
end
it 'check the string with 0 at end' do
version = Version.new('1.8.1.0')
expect(version.to_s).to eq '1.8.1'
end
end
describe '#components' do
it 'get components' do
version = Version.new('1.3.5')
expect(version.components).to match_array [1, 3, 5]
end
it 'get components for version ends with 0' do
version = Version.new('1.3.5.0')
expect(version.components).to match_array [1, 3, 5]
end
it 'get with n equal to exact number' do
version = Version.new('1.3.5')
expect(version.components(3)).to match_array [1, 3, 5]
end
it 'get with n lower than exact number' do
version = Version.new('1.3.5')
expect(version.components(2)).to match_array [1, 3]
end
it 'get with n grater than exact number' do
version = Version.new('1.3.5')
expect(version.components(4)).to match_array [1, 3, 5, 0]
end
end
describe 'Version::Range' do
describe '#include?' do
it 'include version in range of versions' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?(Version.new('1.5'))).to be true
end
it 'include string version in range of string versions' do
range = Version::Range.new('1', '2')
expect(range.include?('1.5')).to be true
end
it 'include string version in range of versions' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?('1.5')).to be true
end
it 'include version in range of string versions' do
range = Version::Range.new('1', '2')
expect(range.include?(Version.new('1.5'))).to be true
end
end
describe '#to_a' do
it 'get all versions in range' do
version1 = Version.new('1.1.0')
version2 = Version.new('1.2.2')
result = [
Version.new('1.1'), Version.new('1.1.1'),
Version.new('1.1.2'), Version.new('1.1.3'),
Version.new('1.1.4'), Version.new('1.1.5'),
Version.new('1.1.6'), Version.new('1.1.7'),
Version.new('1.1.8'), Version.new('1.1.9'),
Version.new('1.2'), Version.new('1.2.1')
]
range = Version::Range.new(version1, version2)
expect(range.to_a).to match_array result
end
end
end
end

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

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

Failures:

  1) spec Version checks for ArgumentError with the correct message
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string"
             end
           
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:257:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) spec Version checks that initialize can be given an empty string
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless /\A[0-9]+(\.[0-9]+)*\z/.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
           
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:295:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) spec Version checks that initialize can be given no arguments
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version)
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
           
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:314:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) spec Version checks for comparison operators positively
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
         def <=(other)
           false
         end
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:341:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  5) spec Version checks for comparison operators negatively
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
         def <=(other)
           true
         end
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:377:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  6) spec Version tests that #components cannot be used to modify the version
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
           
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:456:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  7) spec Version::Range tests constructing ranges with strings
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = start_version
               @end_version   = end_version
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:479:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  8) spec Version::Range smoke-tests include?
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               true
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:502:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  9) spec Version::Range tests include? with versions greater than the start one
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               @start_version <= version
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:512:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  10) spec Version::Range tests include? with versions lower than the start one
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               @end_version > version
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-8dydud/spec.rb:522:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 17 seconds
19 examples, 10 failures

Failed examples:

rspec /tmp/d20161119-19072-8dydud/spec.rb:241 # spec Version checks for ArgumentError with the correct message
rspec /tmp/d20161119-19072-8dydud/spec.rb:279 # spec Version checks that initialize can be given an empty string
rspec /tmp/d20161119-19072-8dydud/spec.rb:298 # spec Version checks that initialize can be given no arguments
rspec /tmp/d20161119-19072-8dydud/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-8dydud/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-8dydud/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-8dydud/spec.rb:471 # spec Version::Range tests constructing ranges with strings
rspec /tmp/d20161119-19072-8dydud/spec.rb:495 # spec Version::Range smoke-tests include?
rspec /tmp/d20161119-19072-8dydud/spec.rb:505 # spec Version::Range tests include? with versions greater than the start one
rspec /tmp/d20161119-19072-8dydud/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Емине обнови решението на 18.11.2016 23:47 (преди над 7 години)

+RSpec.describe 'Version' do
+ describe '#initialize' do
+ it 'initialize right with string' do
+ version = Version.new('1.8.1')
+ expect(version).to be_truthy
+ end
+
+ it 'initialize right with Version object' do
+ version = Version.new(Version.new('1.8.1'))
+ expect(version).to be_truthy
+ end
+
+ it 'initialize right with string witn many numbers' do
+ version = Version.new(Version.new('1.8.1.8.1.8.1.8.1.8.1.8.1.8.1'))
+ expect(version).to be_truthy
+ end
+
+ it 'raise error on initialize witn other symbols' do
+ expect { Version.new('1.8.a') }.to raise_error(ArgumentError)
+ end
+
+ it 'raise error on initialize with negative numbers' do
+ expect { Version.new('1.-8.1') }.to raise_error(ArgumentError)
+ end
+
+ it 'raise error on initialize with string without numbers' do
+ expect { Version.new('...') }.to raise_error(ArgumentError)
+ end
+
+ it 'raise error on initialize with string starting with dot' do
+ expect { Version.new('.8.1') }.to raise_error(ArgumentError)
+ end
+
+ it 'raise error on initialize with string starting with dot' do
+ expect { Version.new('.8.1') }.to raise_error(ArgumentError)
+ end
+
+ it 'raise error on initialize with string with missing number' do
+ expect { Version.new('1..1') }.to raise_error(ArgumentError)
+ end
+ end
+
+ describe '#<=>' do
+ it 'compare with = with true result with same values' do
+ version1 = Version.new('1.8.8')
+ version2 = Version.new('1.8.8')
+ expect(version1).to eq(version2)
+ end
+
+ it 'compare with < with true result' do
+ version1 = Version.new('1.8.1')
+ version2 = Version.new('1.8.8')
+ expect(version1).to be < version2
+ end
+
+ it 'compare with < with false result' do
+ version1 = Version.new('1.8.1')
+ version2 = Version.new('1.8.8')
+ expect(version2).not_to be < version1
+ end
+
+ it 'compare with > with true result' do
+ version1 = Version.new('1.8.1')
+ version2 = Version.new('1.8.8')
+ expect(version2).to be > version1
+ end
+
+ it 'compare with > with false result' do
+ version1 = Version.new('1.8.1')
+ version2 = Version.new('1.8.8')
+ expect(version1).not_to be > version2
+ end
+ end
+
+ describe '#to_s' do
+ it 'check the same string as string in .new' do
+ str_version = '1.8.1'
+ version = Version.new(str_version)
+ expect(version.to_s).to eq str_version
+ end
+
+ it 'check the string with 0 at end' do
+ version = Version.new('1.8.1.0')
+ expect(version.to_s).to eq '1.8.1'
+ end
+ end
+
+ describe '#components' do
+ it 'get components' do
+ version = Version.new('1.3.5')
+ expect(version.components).to match_array [1, 3, 5]
+ end
+
+ it 'get components for version ends with 0' do
+ version = Version.new('1.3.5.0')
+ expect(version.components).to match_array [1, 3, 5]
+ end
+
+ it 'get with n equal to exact number' do
+ version = Version.new('1.3.5')
+ expect(version.components(3)).to match_array [1, 3, 5]
+ end
+
+ it 'get with n lower than exact number' do
+ version = Version.new('1.3.5')
+ expect(version.components(2)).to match_array [1, 3]
+ end
+
+ it 'get with n grater than exact number' do
+ version = Version.new('1.3.5')
+ expect(version.components(4)).to match_array [1, 3, 5, 0]
+ end
+ end
+
+ describe 'Version::Range' do
+ describe '#include?' do
+ it 'include version in range of versions' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(range.include?(Version.new('1.5'))).to be true
+ end
+
+ it 'include string version in range of string versions' do
+ range = Version::Range.new('1', '2')
+ expect(range.include?('1.5')).to be true
+ end
+
+ it 'include string version in range of versions' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(range.include?('1.5')).to be true
+ end
+
+ it 'include version in range of string versions' do
+ range = Version::Range.new('1', '2')
+ expect(range.include?(Version.new('1.5'))).to be true
+ end
+ end
+
+ describe '#to_a' do
+ it 'get all versions in range' do
+ version1 = Version.new('1.1.0')
+ version2 = Version.new('1.2.2')
+ result = [
+ Version.new('1.1'), Version.new('1.1.1'),
+ Version.new('1.1.2'), Version.new('1.1.3'),
+ Version.new('1.1.4'), Version.new('1.1.5'),
+ Version.new('1.1.6'), Version.new('1.1.7'),
+ Version.new('1.1.8'), Version.new('1.1.9'),
+ Version.new('1.2'), Version.new('1.2.1')
+ ]
+ range = Version::Range.new(version1, version2)
+ expect(range.to_a).to match_array result
+ end
+ end
+ end
+end