Решение на Четвърта задача - Unit тестване от Симеон Гергинов

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

Към профила на Симеон Гергинов

Резултати

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

Код

RSpec.describe 'Version' do
describe Version do
describe '#to_s' do
it 'raises an error for wrong input' do
expect { Version.new('1.1.') }
.to raise_error(Version::ArgumentError, /Invalid version string '1.1.'/)
end
it 'converts version to string' do
version = Version.new('1.1.0')
expect(version.to_s).to eq('1.1')
end
it 'converts version without zeroes to string' do
version = Version.new('2.4.9')
expect(version.to_s).to eq('2.4.9')
end
it 'converts version with two zeroes to string' do
version = Version.new('1.0.0')
expect(version.to_s).to eq('1')
end
it 'converts version with empty string to string' do
version = Version.new('')
expect(version.to_s).to eq('')
end
it 'converts version with one zero to string' do
version = Version.new('0')
expect(version.to_s).to eq('')
end
end
describe '#<==>' do
it 'raises an error for wrong input' do
expect { Version.new('10.') }
.to raise_error(Version::ArgumentError, /Invalid version string '10.'/)
end
it 'returns the comparison of two versions' do
version_first = Version.new('1.2.3')
version_second = Version.new('1.3.1')
expect(version_first < version_second).to eq(true)
end
it 'returns the comparison of two versions with different length' do
version_first = Version.new('2.1')
version_second = Version.new('2.0.9')
expect(version_first > version_second).to eq(true)
end
it 'returns the comparison of two versions with zero components' do
version_first = Version.new('1.1.0')
version_second = Version.new('1.1')
expect(version_first == version_second).to eq(true)
end
it 'returns if the comparison is wrong' do
version_first = Version.new('2.2.6')
version_second = Version.new('3.4.3')
expect(version_first > version_second).to eq(false)
end
it 'returns if the comparison is wrong with zero components' do
version_first = Version.new('1.1')
version_second = Version.new('1.1.0')
expect(version_first > version_second).to eq(false)
end
end
describe '#components' do
it 'raises an error for wrong input' do
expect { Version.new('2.') }
.to raise_error(Version::ArgumentError, /Invalid version string '2.'/)
end
it 'returns the components of version as an array' do
version = Version.new('1.3.5')
expect(version.components).to eq([1, 3, 5])
end
it 'returns the number of components of version as an array' do
version = Version.new('1.3.5')
expect(version.components(2)).to eq([1, 3])
end
it 'returns the components of version with added zeroes as an array' do
version = Version.new('1.3.5')
expect(version.components(5)).to eq([1, 3, 5, 0, 0])
end
it 'returns the components of version with zeroes as an array' do
version = Version.new('1.0.0')
expect(version.components).to eq([1])
end
end
end
describe Range do
describe '#include?' do
it 'raises an error for wrong input' do
expect { Version.new('4.9.') }
.to raise_error(Version::ArgumentError, /Invalid version string '4.9.'/)
end
it 'checks if version is in interval' do
version = Version.new('1.5')
range = Version::Range.new('1', '2').include? version
expect(range).to eq(true)
end
it 'checks if version is not in interval' do
version = Version.new('4.4')
range = Version::Range.new('3.2.1', '4.3.4').include? version
expect(range).to eq(false)
end
it 'checks if version equal to the start is in interval' do
version = Version.new('1.1.0')
range = Version::Range.new('1.1', '3').include? version
expect(range).to eq(true)
end
it 'checks if version equal to the end is in interval' do
version = Version.new('2.1.0')
range = Version::Range.new('1.5', '2.1').include? version
expect(range).to eq(false)
end
it 'checks if version is not in interval' do
version = Version.new('2.10')
range = Version::Range.new('1.5', '2.1').include? version
expect(range).to eq(false)
end
end
describe '#to_a' do
it 'raises an error for wrong input' do
expect { Version.new('4.3.') }
.to raise_error(Version::ArgumentError, /Invalid version string '4.3.'/)
end
it 'generates versions in interval' do
range = Version::Range.new('1.1.0', '1.2.2').to_a
arr = [
'1.1',
'1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5',
'1.1.6', '1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(range).to eq(arr)
end
it 'generates versions with three components in interval' do
range = Version::Range.new('1.1', '1.2').to_a
arr = [
'1.1',
'1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5',
'1.1.6', '1.1.7', '1.1.8', '1.1.9'
]
expect(range).to eq(arr)
end
it 'generates versions from 1 to 1 in interval' do
range = Version::Range.new('1', '1').to_a
expect(range).to eq([])
end
end
end
end

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

/tmp/d20161119-6945-u83whh/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-u83whh/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-u83whh/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-u83whh/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-u83whh/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-bfg74x/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-bfg74x/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-bfg74x/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-bfg74x/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-bfg74x/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-15zgmr6/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-15zgmr6/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-15zgmr6/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-15zgmr6/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-15zgmr6/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-rxwsgw/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-rxwsgw/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-rxwsgw/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-rxwsgw/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-rxwsgw/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-11rglht/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-11rglht/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-11rglht/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-11rglht/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-11rglht/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-8male7/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-8male7/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-8male7/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-8male7/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-8male7/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-9jqr/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-9jqr/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-9jqr/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-9jqr/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-9jqr/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1roea3l/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1roea3l/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1roea3l/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1roea3l/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1roea3l/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1q6wk4w/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1q6wk4w/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1q6wk4w/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1q6wk4w/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1q6wk4w/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-1kvcw1b/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1kvcw1b/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1kvcw1b/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1kvcw1b/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1kvcw1b/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-mj5pkf/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-mj5pkf/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-mj5pkf/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-mj5pkf/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-mj5pkf/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-zoe0nc/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zoe0nc/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zoe0nc/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zoe0nc/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zoe0nc/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-ibnyb/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ibnyb/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ibnyb/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ibnyb/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ibnyb/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-zt51be/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zt51be/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zt51be/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zt51be/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zt51be/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-1ezs48t/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1ezs48t/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1ezs48t/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1ezs48t/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1ezs48t/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-1er2km6/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1er2km6/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1er2km6/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1er2km6/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1er2km6/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-1o73o3q/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1o73o3q/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1o73o3q/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1o73o3q/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1o73o3q/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-p94kjo/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-p94kjo/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-p94kjo/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-p94kjo/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-p94kjo/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-ul0ajc/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ul0ajc/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ul0ajc/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ul0ajc/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-ul0ajc/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-1srln2f/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1srln2f/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1srln2f/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1srln2f/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1srln2f/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-jlwkye/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-jlwkye/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-jlwkye/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-jlwkye/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-jlwkye/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
F/tmp/d20161119-6945-1i1r4ja/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1i1r4ja/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1i1r4ja/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1i1r4ja/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-1i1r4ja/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
./tmp/d20161119-6945-zca00w/solution_spec.rb:6: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zca00w/solution_spec.rb:38: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zca00w/solution_spec.rb:75: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zca00w/solution_spec.rb:104: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
/tmp/d20161119-6945-zca00w/solution_spec.rb:141: warning: toplevel constant ArgumentError referenced by Version::ArgumentError
.

Failures:

  1) 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-1a84d9p/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)>'

  2) 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-1a84d9p/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)>'

  3) 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-1a84d9p/spec.rb:368: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 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-1a84d9p/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)>'

  5) spec Version::Range tests constructing ranges with versions
     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)
               raise 'error' if start_version.is_a?(Version) || end_version.is_a?(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-1a84d9p/spec.rb:492: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::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-1a84d9p/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 16.28 seconds
19 examples, 6 failures

Failed examples:

rspec /tmp/d20161119-19072-1a84d9p/spec.rb:298 # spec Version checks that initialize can be given no arguments
rspec /tmp/d20161119-19072-1a84d9p/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-1a84d9p/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-1a84d9p/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-1a84d9p/spec.rb:482 # spec Version::Range tests constructing ranges with versions
rspec /tmp/d20161119-19072-1a84d9p/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Симеон обнови решението на 18.11.2016 10:28 (преди почти 2 години)

+RSpec.describe 'Version' do
+ describe Version do
+ describe '#to_s' do
+ it 'raises an error for wrong input' do
+ expect { Version.new('1.1.') }
+ .to raise_error(Version::ArgumentError, /Invalid version string '1.1.'/)
+ end
+
+ it 'converts version to string' do
+ version = Version.new('1.1.0')
+ expect(version.to_s).to eq('1.1')
+ end
+
+ it 'converts version without zeroes to string' do
+ version = Version.new('2.4.9')
+ expect(version.to_s).to eq('2.4.9')
+ end
+
+ it 'converts version with two zeroes to string' do
+ version = Version.new('1.0.0')
+ expect(version.to_s).to eq('1')
+ end
+
+ it 'converts version with empty string to string' do
+ version = Version.new('')
+ expect(version.to_s).to eq('')
+ end
+
+ it 'converts version with one zero to string' do
+ version = Version.new('0')
+ expect(version.to_s).to eq('')
+ end
+ end
+
+ describe '#<==>' do
+ it 'raises an error for wrong input' do
+ expect { Version.new('10.') }
+ .to raise_error(Version::ArgumentError, /Invalid version string '10.'/)
+ end
+
+ it 'returns the comparison of two versions' do
+ version_first = Version.new('1.2.3')
+ version_second = Version.new('1.3.1')
+ expect(version_first < version_second).to eq(true)
+ end
+
+ it 'returns the comparison of two versions with different length' do
+ version_first = Version.new('2.1')
+ version_second = Version.new('2.0.9')
+ expect(version_first > version_second).to eq(true)
+ end
+
+ it 'returns the comparison of two versions with zero components' do
+ version_first = Version.new('1.1.0')
+ version_second = Version.new('1.1')
+ expect(version_first == version_second).to eq(true)
+ end
+
+ it 'returns if the comparison is wrong' do
+ version_first = Version.new('2.2.6')
+ version_second = Version.new('3.4.3')
+ expect(version_first > version_second).to eq(false)
+ end
+
+ it 'returns if the comparison is wrong with zero components' do
+ version_first = Version.new('1.1')
+ version_second = Version.new('1.1.0')
+ expect(version_first > version_second).to eq(false)
+ end
+ end
+
+ describe '#components' do
+ it 'raises an error for wrong input' do
+ expect { Version.new('2.') }
+ .to raise_error(Version::ArgumentError, /Invalid version string '2.'/)
+ end
+
+ it 'returns the components of version as an array' do
+ version = Version.new('1.3.5')
+ expect(version.components).to eq([1, 3, 5])
+ end
+
+ it 'returns the number of components of version as an array' do
+ version = Version.new('1.3.5')
+ expect(version.components(2)).to eq([1, 3])
+ end
+
+ it 'returns the components of version with added zeroes as an array' do
+ version = Version.new('1.3.5')
+ expect(version.components(5)).to eq([1, 3, 5, 0, 0])
+ end
+
+ it 'returns the components of version with zeroes as an array' do
+ version = Version.new('1.0.0')
+ expect(version.components).to eq([1])
+ end
+ end
+ end
+
+ describe Range do
+ describe '#include?' do
+ it 'raises an error for wrong input' do
+ expect { Version.new('4.9.') }
+ .to raise_error(Version::ArgumentError, /Invalid version string '4.9.'/)
+ end
+
+ it 'checks if version is in interval' do
+ version = Version.new('1.5')
+ range = Version::Range.new('1', '2').include? version
+ expect(range).to eq(true)
+ end
+
+ it 'checks if version is not in interval' do
+ version = Version.new('4.4')
+ range = Version::Range.new('3.2.1', '4.3.4').include? version
+ expect(range).to eq(false)
+ end
+
+ it 'checks if version equal to the start is in interval' do
+ version = Version.new('1.1.0')
+ range = Version::Range.new('1.1', '3').include? version
+ expect(range).to eq(true)
+ end
+
+ it 'checks if version equal to the end is in interval' do
+ version = Version.new('2.1.0')
+ range = Version::Range.new('1.5', '2.1').include? version
+ expect(range).to eq(false)
+ end
+
+ it 'checks if version is not in interval' do
+ version = Version.new('2.10')
+ range = Version::Range.new('1.5', '2.1').include? version
+ expect(range).to eq(false)
+ end
+ end
+
+ describe '#to_a' do
+ it 'raises an error for wrong input' do
+ expect { Version.new('4.3.') }
+ .to raise_error(Version::ArgumentError, /Invalid version string '4.3.'/)
+ end
+
+ it 'generates versions in interval' do
+ range = Version::Range.new('1.1.0', '1.2.2').to_a
+ arr = [
+ '1.1',
+ '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5',
+ '1.1.6', '1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
+ ]
+ expect(range).to eq(arr)
+ end
+
+ it 'generates versions with three components in interval' do
+ range = Version::Range.new('1.1', '1.2').to_a
+ arr = [
+ '1.1',
+ '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5',
+ '1.1.6', '1.1.7', '1.1.8', '1.1.9'
+ ]
+ expect(range).to eq(arr)
+ end
+
+ it 'generates versions from 1 to 1 in interval' do
+ range = Version::Range.new('1', '1').to_a
+ expect(range).to eq([])
+ end
+ end
+ end
+end