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

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

Към профила на Мила Бянкова

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
context 'when is given invalid argument' do
it 'raises ArgumentError when the argument starts with a dot' do
expect { Version.new('.2.3') }.to raise_error(
ArgumentError,
"Invalid version string '.2.3'"
)
end
it 'raises ArgumentError when argument ends with a dot' do
expect { Version.new('1.3.') }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
it 'raises ArgumentError when argument contains two consecutive dots' do
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
end
it 'raises ArgumentError when argument has negative number' do
expect { Version.new('1.-2.1') }.to raise_error(
ArgumentError,
"Invalid version string '1.-2.1'"
)
end
end
end
describe 'comparing two versions' do
it 'compres equal versions' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.2.3')
expect(first_version).not_to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres versions when one is ending with zeros' do
first_version = Version.new('1.2')
second_version = Version.new('1.2.0.0')
expect(first_version).not_to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres from left to right' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.3.1')
expect(first_version).not_to be > second_version
expect(first_version).to be < second_version
expect(first_version).not_to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'compres versions with different length' do
first_version = Version.new('1.2.3.4')
second_version = Version.new('1.3')
expect(first_version).not_to be > second_version
expect(first_version).to be < second_version
expect(first_version).not_to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'zero version is the smallest version' do
first_version = Version.new('0.0.1')
second_version = Version.new('')
expect(first_version).to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).not_to be <= second_version
expect(first_version).not_to be eq(second_version)
expect(first_version <=> second_version).to eq(1)
end
end
describe '#to_s' do
it 'converts simple version to string' do
expect(Version.new('1.0.1').to_s).to eq('1.0.1')
end
it 'converts version ending with zeros' do
expect(Version.new('1.0.0').to_s).to eq('1')
end
it 'converts empty version to empty string' do
expect(Version.new('').to_s).to eq('')
end
it 'converts version with zeros to empty string' do
expect(Version.new('0.0').to_s).to eq('')
end
end
describe '#components' do
it 'returns array of the components of version' do
expect(Version.new('1.3.15.6').components).to eq([1, 3, 15, 6])
end
it 'returns array of the components of version ending with zeros' do
expect(Version.new('1.3.0.0').components).to eq([1, 3])
end
it 'returns array of the components of version containing zeros' do
expect(Version.new('1.0.1').components).to eq([1, 0, 1])
end
it 'returns array of the components of version starting with zeros' do
expect(Version.new('0.0.1').components).to eq([0, 0, 1])
end
it 'returns hole array of components when given argument' do
expect(Version.new('1.3.5').components(3)).to eq([1, 3, 5])
end
it 'returns part of the array of components when given argument' do
expect(Version.new('1.3.5.6').components(2)).to eq([1, 3])
end
it 'returns array of components filled with zeros in the blank places' do
expect(Version.new('1.3').components(4)).to eq([1, 3, 0, 0])
end
it 'does not modify the version when changing components' do
version = Version.new('1.2.3')
version.components.shift
expect(version).to eq(Version.new('1.2.3'))
end
end
describe 'Range#include?' do
it 'returns true for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?(Version.new('1.5'))).to eq(true)
end
it 'returns false for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?(Version.new('2'))).to eq(false)
end
it 'returns true for string in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?('1.5')).to eq(true)
end
it 'returns false for string not in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(range.include?('2.5')).to eq(false)
end
it 'returns true for string in range of strings' do
expect(Version::Range.new('1', '2').include?('1.2')).to eq(true)
end
it 'returns false for string not in range of strings' do
expect(Version::Range.new('1', '2').include?('2.2')).to eq(false)
end
it 'returns true for instance in range of strings' do
range = Version::Range.new('1', '2')
expect(range.include?(Version.new('1.2'))).to eq(true)
end
it 'returns false for instance in range of strings' do
range = Version::Range.new('1', '2')
expect(range.include?(Version.new('2.2'))).to eq(false)
end
it 'returns true for big version in range' do
range = Version::Range.new('1', '2')
expect(range.include?(Version.new('1.20'))).to eq(true)
end
end
describe 'Range#to_a' do
it 'generates the corret array when versions have 3 components' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 2 components' do
range = Version::Range.new(Version.new('1.1'), Version.new('1.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 1 components' do
range = Version::Range.new(Version.new('1'), Version.new('1.2'))
output = [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6', '1.0.7',
'1.0.8', '1.0.9', '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_a).to eq(output)
end
end
end

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

....F.......F...F..

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-5kvuva/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::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-5kvuva/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)>'

  3) 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-5kvuva/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 18.72 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-5kvuva/spec.rb:298 # spec Version checks that initialize can be given no arguments
rspec /tmp/d20161119-19072-5kvuva/spec.rb:471 # spec Version::Range tests constructing ranges with strings
rspec /tmp/d20161119-19072-5kvuva/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Мила обнови решението на 17.11.2016 22:31 (преди почти 2 години)

+RSpec.describe 'Version' do
+ describe '#initialize' do
+ it 'makes instance by given string' do
+ new_version = Version.new('1.2.3')
+
+ expect(new_version).to be_an_instance_of(Version)
+ end
+
+ it 'makes instance by empty string' do
+ new_version = Version.new('')
+
+ expect(new_version).to be_an_instance_of(Version)
+ end
+
+ it 'makes instance with no argument' do
+ new_version = Version.new
+
+ expect(new_version).to be_an_instance_of(Version)
+ end
+
+ it 'makes instance by given another instance' do
+ old_version = Version.new('1.2')
+ new_version = Version.new(old_version)
+
+ expect(new_version).to be_an_instance_of(Version)
+ end
+ context 'when is given invalid argument' do
+ it 'raises ArgumentError when the argument starts with a dot' do
+ expect { Version.new('.2.3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '.2.3'"
+ )
+ end
+
+ it 'raises ArgumentError when argument ends with a dot' do
+ expect { Version.new('1.3.') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.3.'"
+ )
+ end
+
+ it 'raises ArgumentError when argument contains two consecutive dots' do
+ expect { Version.new('1..3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1..3'"
+ )
+ end
+
+ it 'raises ArgumentError when argument has negative number' do
+ expect { Version.new('1.-2.1') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.-2.1'"
+ )
+ end
+ end
+ end
+
+ describe 'comparing two versions' do
+ it 'compres equal versions' do
+ first_version = Version.new('1.2.3')
+ second_version = Version.new('1.2.3')
+
+ expect(first_version > second_version).to eq(false)
+ expect(first_version < second_version).to eq(false)
+ expect(first_version >= second_version).to eq(true)
+ expect(first_version <= second_version).to eq(true)
+ expect(first_version == second_version).to eq(true)
+ expect(first_version <=> second_version).to eq(0)
+ end
+
+ it 'compres versions when one is ending with zeros' do
+ first_version = Version.new('1.2')
+ second_version = Version.new('1.2.0.0')
+
+ expect(first_version > second_version).to eq(false)
+ expect(first_version < second_version).to eq(false)
+ expect(first_version >= second_version).to eq(true)
+ expect(first_version <= second_version).to eq(true)
+ expect(first_version == second_version).to eq(true)
+ expect(first_version <=> second_version).to eq(0)
+ end
+
+ it 'compres from left to right' do
+ first_version = Version.new('1.2.3')
+ second_version = Version.new('1.3.1')
+
+ expect(first_version > second_version).to eq(false)
+ expect(first_version < second_version).to eq(true)
+ expect(first_version >= second_version).to eq(false)
+ expect(first_version <= second_version).to eq(true)
+ expect(first_version == second_version).to eq(false)
+ expect(first_version <=> second_version).to eq(-1)
+ end
+
+ it 'compres versions with different length' do
+ first_version = Version.new('1.2.3.4')
+ second_version = Version.new('1.3')
+
+ expect(first_version > second_version).to eq(false)
+ expect(first_version < second_version).to eq(true)
+ expect(first_version >= second_version).to eq(false)
+ expect(first_version <= second_version).to eq(true)
+ expect(first_version == second_version).to eq(false)
+ expect(first_version <=> second_version).to eq(-1)
+ end
+
+ it 'zero version is the smallest version' do
+ first_version = Version.new('0.0.1')
+ second_version = Version.new('')
+
+ expect(first_version > second_version).to eq(true)
+ expect(first_version < second_version).to eq(false)
+ expect(first_version >= second_version).to eq(true)
+ expect(first_version <= second_version).to eq(false)
+ expect(first_version == second_version).to eq(false)
+ expect(first_version <=> second_version).to eq(1)
+ end
+ end
+
+ describe '#to_s' do
+ it 'converts simple version to string' do
+ expect(Version.new('1.0.1').to_s).to eq('1.0.1')
+ end
+
+ it 'converts version ending with zeros' do
+ expect(Version.new('1.0.0').to_s).to eq('1')
+ end
+
+ it 'converts empty version to empty string' do
+ expect(Version.new('').to_s).to eq('')
+ end
+
+ it 'converts version with zeros to empty string' do
+ expect(Version.new('0.0').to_s).to eq('')
+ end
+ end
+
+ describe '#components' do
+ it 'returns array of the components of version' do
+ expect(Version.new('1.3.15.6').components).to match_array([1, 3, 15, 6])
+ end
+
+ it 'returns array of the components of version ending with zeros' do
+ expect(Version.new('1.3.0.0').components).to match_array([1, 3])
+ end
+
+ it 'returns array of the components of version containing zeros' do
+ expect(Version.new('1.0.1').components).to match_array([1, 0, 1])
+ end
+
+ it 'returns array of the components of version starting with zeros' do
+ expect(Version.new('0.0.1').components).to match_array([0, 0, 1])
+ end
+
+ it 'returns hole array of components when given argument' do
+ expect(Version.new('1.3.5').components(3)).to match_array([1, 3, 5])
+ end
+
+ it 'returns part of the array of components when given argument' do
+ expect(Version.new('1.3.5.6').components(2)).to match_array([1, 3])
+ end
+
+ it 'returns array of components filled with zeros in the blank places' do
+ expect(Version.new('1.3').components(4)).to match_array([1, 3, 0, 0])
+ end
+
+ it 'does not modify the version when getting superversion' do
+ version = Version.new('1.2.3')
+ version.components(2)
+
+ expect(version).to eq(Version.new('1.2.3'))
+ end
+
+ it 'does not modify the version when getting subversion' do
+ new_version = Version.new('1.2.3')
+ new_version.components(5)
+
+ expect(new_version).to eq(Version.new('1.2.3'))
+ end
+ end
+
+ describe 'Range#include?' do
+ it 'returns true for version in range of instances' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect((range.include? Version.new('1.5'))).to eq(true)
+ end
+
+ it 'returns false for version in range of instances' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect((range.include? Version.new('2'))).to eq(false)
+ end
+
+ it 'returns true for string in range of instances' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect((range.include? '1.5')).to eq(true)
+ end
+
+ it 'returns false for string not in range of instances' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect((range.include? '2.5')).to eq(false)
+ end
+
+ it 'returns true for string in range of strings' do
+ expect((Version::Range.new('1', '2').include? '1.2')).to eq(true)
+ end
+
+ it 'returns false for string not in range of strings' do
+ expect((Version::Range.new('1', '2').include? '2.2')).to eq(false)
+ end
+
+ it 'returns true for instance in range of strings' do
+ range = Version::Range.new('1', '2')
+ expect((range.include? Version.new('1.2'))).to eq(true)
+ end
+
+ it 'returns false for instance in range of strings' do
+ range = Version::Range.new('1', '2')
+ expect((range.include? Version.new('2.2'))).to eq(false)
+ end
+
+ it 'returns true for big version in range' do
+ range = Version::Range.new('1', '2')
+ expect((range.include? Version.new('1.20'))).to eq(true)
+ end
+ end
+
+ describe 'Range#to_a' do
+ it 'generates the corret array when versions have 3 components' do
+ range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
+ output = [
+ '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_a).to eq(output)
+ end
+
+ it 'generates the corret array when versions have 2 components' do
+ range = Version::Range.new(Version.new('1.1'), Version.new('1.2'))
+ output = [
+ '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_a).to eq(output)
+ end
+
+ it 'generates the corret array when versions have 1 components' do
+ range = Version::Range.new(Version.new('1'), Version.new('1.2'))
+ output = [
+ '1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6', '1.0.7',
+ '1.0.8', '1.0.9', '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_a).to eq(output)
+ end
+ end
+end

Мила обнови решението на 17.11.2016 23:54 (преди почти 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
- it 'makes instance by given string' do
- new_version = Version.new('1.2.3')
-
- expect(new_version).to be_an_instance_of(Version)
- end
-
- it 'makes instance by empty string' do
- new_version = Version.new('')
-
- expect(new_version).to be_an_instance_of(Version)
- end
-
- it 'makes instance with no argument' do
- new_version = Version.new
-
- expect(new_version).to be_an_instance_of(Version)
- end
-
- it 'makes instance by given another instance' do
- old_version = Version.new('1.2')
- new_version = Version.new(old_version)
-
- expect(new_version).to be_an_instance_of(Version)
- end
context 'when is given invalid argument' do
it 'raises ArgumentError when the argument starts with a dot' do
expect { Version.new('.2.3') }.to raise_error(
ArgumentError,
"Invalid version string '.2.3'"
)
end
it 'raises ArgumentError when argument ends with a dot' do
expect { Version.new('1.3.') }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
it 'raises ArgumentError when argument contains two consecutive dots' do
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
end
it 'raises ArgumentError when argument has negative number' do
expect { Version.new('1.-2.1') }.to raise_error(
ArgumentError,
"Invalid version string '1.-2.1'"
)
end
end
end
describe 'comparing two versions' do
it 'compres equal versions' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.2.3')
- expect(first_version > second_version).to eq(false)
- expect(first_version < second_version).to eq(false)
- expect(first_version >= second_version).to eq(true)
- expect(first_version <= second_version).to eq(true)
- expect(first_version == second_version).to eq(true)
+ expect(first_version).not_to be > second_version
+ expect(first_version).not_to be < second_version
+ expect(first_version).to be >= second_version
+ expect(first_version).to be <= second_version
+ expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres versions when one is ending with zeros' do
first_version = Version.new('1.2')
second_version = Version.new('1.2.0.0')
- expect(first_version > second_version).to eq(false)
- expect(first_version < second_version).to eq(false)
- expect(first_version >= second_version).to eq(true)
- expect(first_version <= second_version).to eq(true)
- expect(first_version == second_version).to eq(true)
+ expect(first_version).not_to be > second_version
+ expect(first_version).not_to be < second_version
+ expect(first_version).to be >= second_version
+ expect(first_version).to be <= second_version
+ expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres from left to right' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.3.1')
- expect(first_version > second_version).to eq(false)
- expect(first_version < second_version).to eq(true)
- expect(first_version >= second_version).to eq(false)
- expect(first_version <= second_version).to eq(true)
- expect(first_version == second_version).to eq(false)
+ expect(first_version).not_to be > second_version
+ expect(first_version).to be < second_version
+ expect(first_version).not_to be >= second_version
+ expect(first_version).to be <= second_version
+ expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'compres versions with different length' do
first_version = Version.new('1.2.3.4')
second_version = Version.new('1.3')
- expect(first_version > second_version).to eq(false)
- expect(first_version < second_version).to eq(true)
- expect(first_version >= second_version).to eq(false)
- expect(first_version <= second_version).to eq(true)
- expect(first_version == second_version).to eq(false)
+ expect(first_version).not_to be > second_version
+ expect(first_version).to be < second_version
+ expect(first_version).not_to be >= second_version
+ expect(first_version).to be <= second_version
+ expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'zero version is the smallest version' do
first_version = Version.new('0.0.1')
second_version = Version.new('')
- expect(first_version > second_version).to eq(true)
- expect(first_version < second_version).to eq(false)
- expect(first_version >= second_version).to eq(true)
- expect(first_version <= second_version).to eq(false)
- expect(first_version == second_version).to eq(false)
+ expect(first_version).to be > second_version
+ expect(first_version).not_to be < second_version
+ expect(first_version).to be >= second_version
+ expect(first_version).not_to be <= second_version
+ expect(first_version).not_to be eq(second_version)
expect(first_version <=> second_version).to eq(1)
end
end
describe '#to_s' do
it 'converts simple version to string' do
expect(Version.new('1.0.1').to_s).to eq('1.0.1')
end
it 'converts version ending with zeros' do
expect(Version.new('1.0.0').to_s).to eq('1')
end
it 'converts empty version to empty string' do
expect(Version.new('').to_s).to eq('')
end
it 'converts version with zeros to empty string' do
expect(Version.new('0.0').to_s).to eq('')
end
end
describe '#components' do
it 'returns array of the components of version' do
- expect(Version.new('1.3.15.6').components).to match_array([1, 3, 15, 6])
+ expect(Version.new('1.3.15.6').components).to eq([1, 3, 15, 6])
end
it 'returns array of the components of version ending with zeros' do
- expect(Version.new('1.3.0.0').components).to match_array([1, 3])
+ expect(Version.new('1.3.0.0').components).to eq([1, 3])
end
it 'returns array of the components of version containing zeros' do
- expect(Version.new('1.0.1').components).to match_array([1, 0, 1])
+ expect(Version.new('1.0.1').components).to eq([1, 0, 1])
end
it 'returns array of the components of version starting with zeros' do
- expect(Version.new('0.0.1').components).to match_array([0, 0, 1])
+ expect(Version.new('0.0.1').components).to eq([0, 0, 1])
end
it 'returns hole array of components when given argument' do
- expect(Version.new('1.3.5').components(3)).to match_array([1, 3, 5])
+ expect(Version.new('1.3.5').components(3)).to eq([1, 3, 5])
end
it 'returns part of the array of components when given argument' do
- expect(Version.new('1.3.5.6').components(2)).to match_array([1, 3])
+ expect(Version.new('1.3.5.6').components(2)).to eq([1, 3])
end
it 'returns array of components filled with zeros in the blank places' do
- expect(Version.new('1.3').components(4)).to match_array([1, 3, 0, 0])
+ expect(Version.new('1.3').components(4)).to eq([1, 3, 0, 0])
end
- it 'does not modify the version when getting superversion' do
+ it 'does not modify the version when changing components' do
version = Version.new('1.2.3')
- version.components(2)
+ version.components.shift
expect(version).to eq(Version.new('1.2.3'))
- end
-
- it 'does not modify the version when getting subversion' do
- new_version = Version.new('1.2.3')
- new_version.components(5)
-
- expect(new_version).to eq(Version.new('1.2.3'))
end
end
describe 'Range#include?' do
it 'returns true for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect((range.include? Version.new('1.5'))).to eq(true)
end
it 'returns false for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect((range.include? Version.new('2'))).to eq(false)
end
it 'returns true for string in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect((range.include? '1.5')).to eq(true)
end
it 'returns false for string not in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
expect((range.include? '2.5')).to eq(false)
end
it 'returns true for string in range of strings' do
expect((Version::Range.new('1', '2').include? '1.2')).to eq(true)
end
it 'returns false for string not in range of strings' do
expect((Version::Range.new('1', '2').include? '2.2')).to eq(false)
end
it 'returns true for instance in range of strings' do
range = Version::Range.new('1', '2')
expect((range.include? Version.new('1.2'))).to eq(true)
end
it 'returns false for instance in range of strings' do
range = Version::Range.new('1', '2')
expect((range.include? Version.new('2.2'))).to eq(false)
end
it 'returns true for big version in range' do
range = Version::Range.new('1', '2')
expect((range.include? Version.new('1.20'))).to eq(true)
end
end
describe 'Range#to_a' do
it 'generates the corret array when versions have 3 components' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 2 components' do
range = Version::Range.new(Version.new('1.1'), Version.new('1.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 1 components' do
range = Version::Range.new(Version.new('1'), Version.new('1.2'))
output = [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6', '1.0.7',
'1.0.8', '1.0.9', '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_a).to eq(output)
end
end
end

Мила обнови решението на 18.11.2016 00:02 (преди почти 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
context 'when is given invalid argument' do
it 'raises ArgumentError when the argument starts with a dot' do
expect { Version.new('.2.3') }.to raise_error(
ArgumentError,
"Invalid version string '.2.3'"
)
end
it 'raises ArgumentError when argument ends with a dot' do
expect { Version.new('1.3.') }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
it 'raises ArgumentError when argument contains two consecutive dots' do
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
end
it 'raises ArgumentError when argument has negative number' do
expect { Version.new('1.-2.1') }.to raise_error(
ArgumentError,
"Invalid version string '1.-2.1'"
)
end
end
end
describe 'comparing two versions' do
it 'compres equal versions' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.2.3')
expect(first_version).not_to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres versions when one is ending with zeros' do
first_version = Version.new('1.2')
second_version = Version.new('1.2.0.0')
expect(first_version).not_to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).to eq(second_version)
expect(first_version <=> second_version).to eq(0)
end
it 'compres from left to right' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.3.1')
expect(first_version).not_to be > second_version
expect(first_version).to be < second_version
expect(first_version).not_to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'compres versions with different length' do
first_version = Version.new('1.2.3.4')
second_version = Version.new('1.3')
expect(first_version).not_to be > second_version
expect(first_version).to be < second_version
expect(first_version).not_to be >= second_version
expect(first_version).to be <= second_version
expect(first_version).not_to eq(second_version)
expect(first_version <=> second_version).to eq(-1)
end
it 'zero version is the smallest version' do
first_version = Version.new('0.0.1')
second_version = Version.new('')
expect(first_version).to be > second_version
expect(first_version).not_to be < second_version
expect(first_version).to be >= second_version
expect(first_version).not_to be <= second_version
expect(first_version).not_to be eq(second_version)
expect(first_version <=> second_version).to eq(1)
end
end
describe '#to_s' do
it 'converts simple version to string' do
expect(Version.new('1.0.1').to_s).to eq('1.0.1')
end
it 'converts version ending with zeros' do
expect(Version.new('1.0.0').to_s).to eq('1')
end
it 'converts empty version to empty string' do
expect(Version.new('').to_s).to eq('')
end
it 'converts version with zeros to empty string' do
expect(Version.new('0.0').to_s).to eq('')
end
end
describe '#components' do
it 'returns array of the components of version' do
expect(Version.new('1.3.15.6').components).to eq([1, 3, 15, 6])
end
it 'returns array of the components of version ending with zeros' do
expect(Version.new('1.3.0.0').components).to eq([1, 3])
end
it 'returns array of the components of version containing zeros' do
expect(Version.new('1.0.1').components).to eq([1, 0, 1])
end
it 'returns array of the components of version starting with zeros' do
expect(Version.new('0.0.1').components).to eq([0, 0, 1])
end
it 'returns hole array of components when given argument' do
expect(Version.new('1.3.5').components(3)).to eq([1, 3, 5])
end
it 'returns part of the array of components when given argument' do
expect(Version.new('1.3.5.6').components(2)).to eq([1, 3])
end
it 'returns array of components filled with zeros in the blank places' do
expect(Version.new('1.3').components(4)).to eq([1, 3, 0, 0])
end
it 'does not modify the version when changing components' do
version = Version.new('1.2.3')
version.components.shift
expect(version).to eq(Version.new('1.2.3'))
end
end
describe 'Range#include?' do
it 'returns true for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
- expect((range.include? Version.new('1.5'))).to eq(true)
+ expect(range.include?(Version.new('1.5'))).to eq(true)
end
it 'returns false for version in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
- expect((range.include? Version.new('2'))).to eq(false)
+ expect(range.include?(Version.new('2'))).to eq(false)
end
it 'returns true for string in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
- expect((range.include? '1.5')).to eq(true)
+ expect(range.include?('1.5')).to eq(true)
end
it 'returns false for string not in range of instances' do
range = Version::Range.new(Version.new('1'), Version.new('2'))
- expect((range.include? '2.5')).to eq(false)
+ expect(range.include?('2.5')).to eq(false)
end
it 'returns true for string in range of strings' do
- expect((Version::Range.new('1', '2').include? '1.2')).to eq(true)
+ expect(Version::Range.new('1', '2').include?('1.2')).to eq(true)
end
it 'returns false for string not in range of strings' do
- expect((Version::Range.new('1', '2').include? '2.2')).to eq(false)
+ expect(Version::Range.new('1', '2').include?('2.2')).to eq(false)
end
it 'returns true for instance in range of strings' do
range = Version::Range.new('1', '2')
- expect((range.include? Version.new('1.2'))).to eq(true)
+ expect(range.include?(Version.new('1.2'))).to eq(true)
end
it 'returns false for instance in range of strings' do
range = Version::Range.new('1', '2')
- expect((range.include? Version.new('2.2'))).to eq(false)
+ expect(range.include?(Version.new('2.2'))).to eq(false)
end
it 'returns true for big version in range' do
range = Version::Range.new('1', '2')
- expect((range.include? Version.new('1.20'))).to eq(true)
+ expect(range.include?(Version.new('1.20'))).to eq(true)
end
end
describe 'Range#to_a' do
it 'generates the corret array when versions have 3 components' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 2 components' do
range = Version::Range.new(Version.new('1.1'), Version.new('1.2'))
output = [
'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_a).to eq(output)
end
it 'generates the corret array when versions have 1 components' do
range = Version::Range.new(Version.new('1'), Version.new('1.2'))
output = [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6', '1.0.7',
'1.0.8', '1.0.9', '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_a).to eq(output)
end
end
end