Решение на Четвърта задача - Unit тестване от Александър Илиев

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

Към профила на Александър Илиев

Резултати

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

Код

RSpec.describe 'Version' do
describe 'create' do
context 'when the input is valid' do
it 'can copy version from another version' do
other = Version.new('1.13.51')
expect(Version.new(other).to_s).to eq other.to_s
other = Version.new('')
expect(Version.new(other).to_s).to eq other.to_s
other = Version.new('1.0.0.2.0.0')
expect(Version.new(other).to_s).to eq other.to_s
end
it 'can create 0 versions' do
expect(Version.new('').to_s).to eq ''
expect(Version.new.to_s).to eq ''
expect(Version.new('0').to_s).to eq ''
end
it 'can create correct version from string input' do
expect(Version.new('1.2').to_s).to eq '1.2'
expect(Version.new('1.2.123').to_s).to eq '1.2.123'
end
end
context 'when invalid input is given' do
it 'throws error' do
invalid_version = '1..2'
expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
"Invalid version string '#{invalid_version}'"
)
invalid_version = '1.-1.2'
expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
"Invalid version string '#{invalid_version}'"
)
invalid_version = '.2'
expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
"Invalid version string '#{invalid_version}'"
)
invalid_version = '1.2.'
expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
"Invalid version string '#{invalid_version}'"
)
end
end
end
describe 'comparisons' do
it 'can compare two versions with same amount of version numbers' do
expect(Version.new('1.2')).to be < Version.new('1.3')
expect(Version.new('1')).to be <= Version.new('3')
expect(Version.new('1.2.5')).to be >= Version.new('1.1.9')
expect(Version.new('1') <=> Version.new('3')).to eq -1
end
it 'can compare two versions with different amount of version numbers' do
expect(Version.new('1.2.2.5.12')).to be < Version.new('1.3')
expect(Version.new('1.0')).to be <= Version.new('3')
expect(Version.new('1.0.54.5')).to be >= Version.new('1.0.9')
expect(Version.new('1')).to be == Version.new('1.0.0.0')
expect(Version.new('2.2.1') <=> Version.new('3.0.0.0.0.0')).to eq -1
end
end
describe 'to_s' do
it 'strips tailing 0 from the version' do
expect(Version.new('1.2.0.1.0.0.0.0.0').to_s).to eq '1.2.0.1'
expect(Version.new('1.0.00.0.0.0.0.2').to_s).to eq '1.0.0.0.0.0.0.2'
expect(Version.new('0.0.0.0.0.0.5').to_s).to eq '0.0.0.0.0.0.5'
end
end
describe 'components' do
it 'can fill up with 0' do
input = '1.2.0.1'
expect(Version.new(input).components(5)).to eq [1, 2, 0, 1, 0]
end
it 'can cut from components to fit' do
input = '1.2.0.1'
expect(Version.new(input).components(2)).to eq [1, 2]
input = '1.2.0.1'
expect(Version.new(input).components(4)).to eq [1, 2, 0, 1]
end
end
describe 'Range' do
describe'include' do
it 'can check with include' do
expect(
Version::Range.new(
Version.new('1'),
Version.new('2')
)
).to include '1.5'
expect(
Version::Range.new(
Version.new('0.0.0.2'),
Version.new('0.0.0.2.15.0')
)
).to include '0.0.0.2.12'
expect(
Version::Range.new(
Version.new('1.1.2.3.4'),
Version.new('2')
)
).to include '1.5'
end
end
describe 'to_a' do
it 'can print middle versions' do
expect(
Version::Range.new(
Version.new('1'),
Version.new('1.0.0')
).to_a
).to eq []
expect(
Version::Range.new(
Version.new('1'),
Version.new('1.0.1')
).to_a
).to eq ['1.0.0']
expect(
Version::Range.new(
Version.new('1.0'),
Version.new('1.0.1.3.2.0')
).to_a
).to eq ['1', '1.0.1']
expect(
Version::Range.new(
Version.new('1.1.0'),
Version.new('1.2.2')
).to_a
).to eq [
'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(
Version::Range.new(
Version.new('1.1'),
Version.new('1.2')
).to_a
).to eq [
'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'
]
end
end
end
end

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

.....FFF..F.F.FFF..

Failures:

  1) 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-1xnijo7/spec.rb:323: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 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-1xnijo7/spec.rb:359: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 tests #components without 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)
             padding_size = positions - @components.size
           
             if padding_size > 0
               @components + [0] * padding_size
             else
               @components.take(positions)
             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-1xnijo7/spec.rb:402: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-1xnijo7/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 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-1xnijo7/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)>'

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

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

  8) 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-1xnijo7/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 14.01 seconds
19 examples, 8 failures

Failed examples:

rspec /tmp/d20161119-19072-1xnijo7/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:389 # spec Version tests #components without arguments
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:471 # spec Version::Range tests constructing ranges with strings
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:495 # spec Version::Range smoke-tests include?
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:505 # spec Version::Range tests include? with versions greater than the start one
rspec /tmp/d20161119-19072-1xnijo7/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Александър обнови решението на 16.11.2016 13:26 (преди над 7 години)

+RSpec.describe 'Version' do
+ describe 'create' do
+ context 'when the input is valid' do
+ it 'can copy version from another version' do
+ other = Version.new('1.13.51')
+ expect(Version.new(other).to_s).to eq other.to_s
+
+ other = Version.new('')
+ expect(Version.new(other).to_s).to eq other.to_s
+
+ other = Version.new('1.0.0.2.0.0')
+ expect(Version.new(other).to_s).to eq other.to_s
+ end
+
+ it 'can create 0 versions' do
+ expect(Version.new('').to_s).to eq ''
+ expect(Version.new.to_s).to eq ''
+ expect(Version.new('0').to_s).to eq ''
+ end
+
+ it 'can create correct version from string input' do
+ valid = '1.2'
+ expect(Version.new(valid).to_s).to eq valid
+
+ valid = '1.2.123'
+ expect(Version.new(valid).to_s).to eq valid
+ end
+ end
+
+ context 'when invalid input is given' do
+ it 'throws error' do
+ invalid = '1..2'
+ expect { Version.new(invalid) }.to raise_error(
+ ArgumentError,
+ "Invalid version string '#{invalid}'"
+ )
+
+ invalid = '1.-1.2'
+ expect { Version.new(invalid) }.to raise_error(
+ ArgumentError,
+ "Invalid version string '#{invalid}'"
+ )
+
+ invalid = '.2'
+ expect { Version.new(invalid) }.to raise_error(
+ ArgumentError,
+ "Invalid version string '#{invalid}'"
+ )
+
+ invalid = '1.2.'
+ expect { Version.new(invalid) }.to raise_error(
+ ArgumentError,
+ "Invalid version string '#{invalid}'"
+ )
+ end
+ end
+ end
+
+ describe 'compare' do
+ it 'can compare two versions with same amount of version numbers' do
+ expect(Version.new('1.2') < Version.new('1.3')).to eq true
+ expect(Version.new('1') <= Version.new('3')).to eq true
+ expect(Version.new('1.2.5') >= Version.new('1.1.9')).to eq true
+ expect(Version.new('1') <=> Version.new('3')).to eq -1
+ end
+
+ it 'can compare two versions with different amount of version numbers' do
+ expect(Version.new('1.2.2.5.12') < Version.new('1.3')).to eq true
+ expect(Version.new('1.0') <= Version.new('3')).to eq true
+ expect(Version.new('1.0.54.5') >= Version.new('1.0.9')).to eq true
+ expect(Version.new('1') == Version.new('1.0.0.0')).to eq true
+ expect(Version.new('2.2.1') <=> Version.new('3.0.0.0.0.0')).to eq -1
+ end
+ end
+
+ describe 'to_s' do
+ it 'strips tailing 0 from the version' do
+ input = '1.2.0.1.0.0.0.0.0'
+ expect(Version.new(input).to_s).to eq '1.2.0.1'
+
+ input = '1.0.00.0.0.0.0.2'
+ expect(Version.new(input).to_s).to eq '1.0.0.0.0.0.0.2'
+
+ input = '0.0.0.0.0.0.5'
+ expect(Version.new(input).to_s).to eq input
+ end
+ end
+
+ describe 'components' do
+ it 'can fill up with 0' do
+ input = '1.2.0.1'
+ expect(Version.new(input).components(5)).to eq [1, 2, 0, 1, 0]
+ end
+
+ it 'can cut from components to fit' do
+ input = '1.2.0.1'
+ expect(Version.new(input).components(2)).to eq [1, 2]
+
+ input = '1.2.0.1'
+ expect(Version.new(input).components(4)).to eq [1, 2, 0, 1]
+ end
+ end
+
+ describe 'Range' do
+ describe 'create when valid input is given' do
+ it 'can create range' do
+ expect { Version::Range.new('1.2.3', '2.3.5') }.to_not raise_error
+
+ expect do
+ Version::Range.new(
+ Version.new('0.0.1'),
+ Version.new('0.0.0.0')
+ )
+ end.to_not raise_error
+
+ expect { Range.new(Version.new, Version.new) }.to_not raise_error
+ end
+ end
+
+ describe'include' do
+ it 'can check with include' do
+ expect(
+ Version::Range.new(
+ Version.new('1'),
+ Version.new('2')
+ ).include?('1.5')
+ ).to eq true
+
+ expect(
+ Version::Range.new(
+ Version.new('0.0.0.2'),
+ Version.new('0.0.0.2.15.0')
+ ).include?('0.0.0.2.12')
+ ).to eq true
+
+ expect(
+ Version::Range.new(
+ Version.new('1'),
+ Version.new('2')
+ ).include?(Version.new('1.5'))
+ ).to eq true
+ end
+ end
+
+ describe 'to_a' do
+ it 'can print middle versions' do
+ expect(
+ Version::Range.new(
+ Version.new('1'),
+ Version.new('1.0.0')
+ ).to_a
+ ).to eq []
+
+ expect(
+ Version::Range.new(
+ Version.new('1'),
+ Version.new('1.0.1')
+ ).to_a
+ ).to eq ['1.0.0']
+
+ expect(
+ Version::Range.new(
+ Version.new('1.0'),
+ Version.new('1.0.1.3.2.0')
+ ).to_a
+ ).to eq ['1', '1.0.1']
+
+ expect(
+ Version::Range.new(
+ Version.new('1.1.0'),
+ Version.new('1.2.2')
+ ).to_a
+ ).to eq [
+ '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(
+ Version::Range.new(
+ Version.new('1.1'),
+ Version.new('1.2')
+ ).to_a
+ ).to eq [
+ '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'
+ ]
+ end
+ end
+ end
+end

Александър обнови решението на 17.11.2016 09:23 (преди над 7 години)

RSpec.describe 'Version' do
describe 'create' do
context 'when the input is valid' do
it 'can copy version from another version' do
other = Version.new('1.13.51')
expect(Version.new(other).to_s).to eq other.to_s
other = Version.new('')
expect(Version.new(other).to_s).to eq other.to_s
other = Version.new('1.0.0.2.0.0')
expect(Version.new(other).to_s).to eq other.to_s
end
it 'can create 0 versions' do
expect(Version.new('').to_s).to eq ''
expect(Version.new.to_s).to eq ''
expect(Version.new('0').to_s).to eq ''
end
it 'can create correct version from string input' do
- valid = '1.2'
- expect(Version.new(valid).to_s).to eq valid
+ expect(Version.new('1.2').to_s).to eq '1.2'
- valid = '1.2.123'
- expect(Version.new(valid).to_s).to eq valid
+ expect(Version.new('1.2.123').to_s).to eq '1.2.123'
end
end
context 'when invalid input is given' do
it 'throws error' do
- invalid = '1..2'
- expect { Version.new(invalid) }.to raise_error(
+ invalid_version = '1..2'
+ expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
- "Invalid version string '#{invalid}'"
+ "Invalid version string '#{invalid_version}'"
)
- invalid = '1.-1.2'
- expect { Version.new(invalid) }.to raise_error(
+ invalid_version = '1.-1.2'
+ expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
- "Invalid version string '#{invalid}'"
+ "Invalid version string '#{invalid_version}'"
)
- invalid = '.2'
- expect { Version.new(invalid) }.to raise_error(
+ invalid_version = '.2'
+ expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
- "Invalid version string '#{invalid}'"
+ "Invalid version string '#{invalid_version}'"
)
- invalid = '1.2.'
- expect { Version.new(invalid) }.to raise_error(
+ invalid_version = '1.2.'
+ expect { Version.new(invalid_version) }.to raise_error(
ArgumentError,
- "Invalid version string '#{invalid}'"
+ "Invalid version string '#{invalid_version}'"
)
end
end
end
- describe 'compare' do
+ describe 'comparisons' do
it 'can compare two versions with same amount of version numbers' do
- expect(Version.new('1.2') < Version.new('1.3')).to eq true
- expect(Version.new('1') <= Version.new('3')).to eq true
- expect(Version.new('1.2.5') >= Version.new('1.1.9')).to eq true
+ expect(Version.new('1.2')).to be < Version.new('1.3')
+ expect(Version.new('1')).to be <= Version.new('3')
+ expect(Version.new('1.2.5')).to be >= Version.new('1.1.9')
expect(Version.new('1') <=> Version.new('3')).to eq -1
end
it 'can compare two versions with different amount of version numbers' do
- expect(Version.new('1.2.2.5.12') < Version.new('1.3')).to eq true
- expect(Version.new('1.0') <= Version.new('3')).to eq true
- expect(Version.new('1.0.54.5') >= Version.new('1.0.9')).to eq true
- expect(Version.new('1') == Version.new('1.0.0.0')).to eq true
+ expect(Version.new('1.2.2.5.12')).to be < Version.new('1.3')
+ expect(Version.new('1.0')).to be <= Version.new('3')
+ expect(Version.new('1.0.54.5')).to be >= Version.new('1.0.9')
+ expect(Version.new('1')).to be == Version.new('1.0.0.0')
expect(Version.new('2.2.1') <=> Version.new('3.0.0.0.0.0')).to eq -1
end
end
describe 'to_s' do
it 'strips tailing 0 from the version' do
- input = '1.2.0.1.0.0.0.0.0'
- expect(Version.new(input).to_s).to eq '1.2.0.1'
+ expect(Version.new('1.2.0.1.0.0.0.0.0').to_s).to eq '1.2.0.1'
- input = '1.0.00.0.0.0.0.2'
- expect(Version.new(input).to_s).to eq '1.0.0.0.0.0.0.2'
+ expect(Version.new('1.0.00.0.0.0.0.2').to_s).to eq '1.0.0.0.0.0.0.2'
- input = '0.0.0.0.0.0.5'
- expect(Version.new(input).to_s).to eq input
+ expect(Version.new('0.0.0.0.0.0.5').to_s).to eq '0.0.0.0.0.0.5'
end
end
describe 'components' do
it 'can fill up with 0' do
input = '1.2.0.1'
expect(Version.new(input).components(5)).to eq [1, 2, 0, 1, 0]
end
it 'can cut from components to fit' do
input = '1.2.0.1'
expect(Version.new(input).components(2)).to eq [1, 2]
input = '1.2.0.1'
expect(Version.new(input).components(4)).to eq [1, 2, 0, 1]
end
end
describe 'Range' do
- describe 'create when valid input is given' do
- it 'can create range' do
- expect { Version::Range.new('1.2.3', '2.3.5') }.to_not raise_error
-
- expect do
- Version::Range.new(
- Version.new('0.0.1'),
- Version.new('0.0.0.0')
- )
- end.to_not raise_error
-
- expect { Range.new(Version.new, Version.new) }.to_not raise_error
- end
- end
-
describe'include' do
it 'can check with include' do
expect(
Version::Range.new(
Version.new('1'),
Version.new('2')
- ).include?('1.5')
- ).to eq true
+ )
+ ).to include '1.5'
expect(
Version::Range.new(
Version.new('0.0.0.2'),
Version.new('0.0.0.2.15.0')
- ).include?('0.0.0.2.12')
- ).to eq true
+ )
+ ).to include '0.0.0.2.12'
expect(
Version::Range.new(
- Version.new('1'),
+ Version.new('1.1.2.3.4'),
Version.new('2')
- ).include?(Version.new('1.5'))
- ).to eq true
+ )
+ ).to include '1.5'
end
end
describe 'to_a' do
it 'can print middle versions' do
expect(
Version::Range.new(
Version.new('1'),
Version.new('1.0.0')
).to_a
).to eq []
expect(
Version::Range.new(
Version.new('1'),
Version.new('1.0.1')
).to_a
).to eq ['1.0.0']
expect(
Version::Range.new(
Version.new('1.0'),
Version.new('1.0.1.3.2.0')
).to_a
).to eq ['1', '1.0.1']
expect(
Version::Range.new(
Version.new('1.1.0'),
Version.new('1.2.2')
).to_a
).to eq [
'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(
Version::Range.new(
Version.new('1.1'),
Version.new('1.2')
).to_a
).to eq [
'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'
]
end
end
end
end