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

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

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

Резултати

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

Код

RSpec.describe 'Version' do
let(:version_square) { Version.new('1.4.9.16') }
let(:major_range) { Version::Range.new('1', '2') }
def expect_error(version)
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
describe '#new' do
it 'raises ArgumentError when version contains non-numeric symbols' do
expect_error('1.0-SNAPSHOT')
expect_error('1.-1')
expect_error('1.+1')
expect_error('1.1_000')
end
it 'raises ArgumentError when some number is missing' do
expect_error('.3')
expect_error('0..3')
expect_error('3.2.15.')
end
end
describe '#components' do
it 'can get components with zerolike versions' do
expect(Version.new.components).to eq []
expect(Version.new('').components).to eq []
expect(Version.new('0').components).to eq []
end
it 'can get components when version is created via existing one' do
version = Version.new(version_square)
expect(version.components).to eq [1, 4, 9, 16]
end
it 'can get components when version has trailing zeros' do
version = Version.new('1.2.3.4.0.0')
expect(version.components).to eq [1, 2, 3, 4]
end
it 'can get components when given length is equal to version numbers' do
expect(version_square.components(4)).to eq [1, 4, 9, 16]
end
it 'can get components when given length is less than version numbers' do
expect(version_square.components(3)).to eq [1, 4, 9]
end
context 'when given length is bigger than version numbers' do
it 'can get components ' do
expect(version_square.components(6)).to eq([1, 4, 9, 16, 0, 0])
end
end
it 'can get components' do
expect(version_square.components).to eq [1, 4, 9, 16]
end
it 'does not allow modification of the class internal state' do
version_square.components.pop
expect(version_square.components).to eq [1, 4, 9, 16]
end
it 'does not allow modification of the other class internal state' do
version = Version.new(version_square)
version.components.pop
expect(version.components).to eq [1, 4, 9, 16]
end
end
describe '<=>' do
context 'when lengths are equal' do
it 'knows the bigger version' do
actual = Version.new('1.4.9.17') <=> version_square
expect(actual).to eq 1
end
it 'knows the lower version' do
actual = Version.new('1.4.9.15') <=> version_square
expect(actual).to eq -1
end
it 'knows that versions are equal' do
actual = Version.new('1.4.9.16') <=> version_square
expect(actual).to eq 0
end
end
context 'when lengths are not equal' do
it 'knows the bigger version' do
actual = Version.new('1.4.9.16.8') <=> version_square
expect(actual).to eq 1
end
it 'knows the lower version' do
actual = Version.new('1.4.9.15.2.1') <=> version_square
expect(actual).to eq -1
end
it 'knows that versions are equal' do
actual = Version.new('1.4.9.16.0.0') <=> version_square
expect(actual).to eq 0
end
end
end
describe '#to_s' do
it 'stringifies' 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 'stringifies when major, minor and build numbers are zeros' do
expect(Version.new('0.1.0.2.0').to_s).to eq '0.1.0.2'
end
it 'stringifies' do
expect(version_square.to_s).to eq '1.4.9.16'
end
end
def expect_range_error(start, last)
expect { Version::Range.new(start, last) }
.to raise_error(ArgumentError, "Invalid version string '#{start}'")
end
describe 'Version::Range' do
describe '#new' do
it 'raises ArgumentError when versions contain non-numeric symbols' do
expect_range_error('1.0-SNAPSHOT', '1.0-BETA')
expect_range_error('1.-1', '1.-2')
expect_range_error('1.+1', '1.+2')
expect_range_error('1.1_000', '1.2_000')
end
it 'raises ArgumentError when some number is missing' do
expect_range_error('.3', '.3.0')
expect_range_error('0..3', '0..4')
expect_range_error('3.2.15.', '3.2.16.')
end
it 'can create new instance with zerolike versions' do
Version::Range.new('', '')
Version::Range.new('0', '0')
end
it 'can create new instance with existing versions' do
Version::Range.new(Version.new, Version.new)
Version::Range.new(Version.new('1'), Version.new('1.0.1'))
end
it 'can create new instance with strings' do
Version::Range.new('1', '1.2.3')
end
end
def expect_include_error(range, version)
expect { range.include?(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
describe '#include?' do
it 'raises ArgumentError when versions contain non-numeric symbols' do
expect_include_error(major_range, '1.0-SNAPSHOT')
expect_include_error(major_range, '1.-1')
expect_include_error(major_range, '1.+1')
expect_include_error(major_range, '1.1_000')
end
it 'raises ArgumentError when some number is missing' do
expect_include_error(major_range, '.3')
expect_include_error(major_range, '0..3')
expect_include_error(major_range, '3.2.15.')
end
it 'can determine whether range includes version' do
expect(major_range.include?('1.13.9')).to eq true
end
it 'can determine whether range does not include' do
expect(major_range.include?('0.209.9')).to eq false
end
it 'includes the interval start to the range' do
expect(major_range.include?('1')).to eq true
end
it 'does not include the interval end to the range' do
expect(major_range.include?('2')).to eq false
end
end
def map_to_versions(string_versions)
string_versions.map { |v| Version.new(v) }
end
describe '#to_a' do
it 'can generate versions with starting empty string version' do
range = Version::Range.new('', '0.0.5')
expected = map_to_versions [
'0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
]
expect(range.to_a).to eq expected
end
it 'can generate versions with starting zerolike version' do
range = Version::Range.new(Version.new('0'), Version.new('0.0.5'))
expected = map_to_versions [
'0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
]
expect(range.to_a).to eq expected
end
it 'can generate versions when start equals end' do
range = Version::Range.new('2.1', '2.1')
expect(range.to_a).to eq []
end
it 'can generate versions to build version' do
range = Version::Range.new('0.0.8', '0.1')
expected = map_to_versions ['0.0.8', '0.0.9']
expect(range.to_a).to eq expected
end
it 'can generate versions to minor version' do
range = Version::Range.new('1', '1.1')
expected = map_to_versions [
'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'
]
expect(range.to_a).to eq expected
end
it 'can generate versions to major version' do
range = Version::Range.new(Version.new('1.8.9'), Version.new('2.1'))
expected = map_to_versions [
'1.8.9', '1.9.0', '1.9.1', '1.9.2', '1.9.3', '1.9.4', '1.9.5',
'1.9.6', '1.9.7', '1.9.8', '1.9.9', '2', '2.0.1', '2.0.2',
'2.0.3', '2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
]
expect(range.to_a).to eq expected
end
end
end
end

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

.....FF............

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-7ts1iu/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-7ts1iu/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)>'

Finished in 18.63 seconds
19 examples, 2 failures

Failed examples:

rspec /tmp/d20161119-19072-7ts1iu/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-7ts1iu/spec.rb:353 # spec Version checks for comparison operators negatively

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

Исмаил обнови решението на 17.11.2016 22:05 (преди над 1 година)

+RSpec.describe 'Version' do
+ let(:square_numbers) { [1, 4, 9, 16] }
+ let(:version_square) { Version.new(square_numbers.join('.')) }
+ let(:major_range) { Version::Range.new('1', '2') }
+ let(:invalid_version_msg) { 'Invalid version string' }
+ let(:snapshot_version) { '1.0-SNAPSHOT' }
+ let(:beta_version) { '1.0-BETA' }
+ let(:negative_version) { '1.-1' }
+ let(:positive_version) { '1.+1' }
+ let(:underscore_version) { '1.1_000' }
+ let(:missing_major_version) { '.3' }
+ let(:missing_minor_version) { '0..3' }
+ let(:missing_build_version) { '3.2.15.' }

Браво, че си се постарал с имената! За съжаление, това е доста лошо използване на let. Защо са ти необходими всичките тези променливи тук? Просто си ги напиши долу, където ги използваш.

+
+ def expect_error(version)
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "#{invalid_version_msg} '#{version}'")
+ end
+
+ def expect_range_error(start, last)
+ expect { Version::Range.new(start, last) }
+ .to raise_error(ArgumentError, "#{invalid_version_msg} '#{start}'")
+ end
+
+ def expect_include_error(range, version)
+ expect { range.include?(version) }
+ .to raise_error(ArgumentError, "#{invalid_version_msg} '#{version}'")
+ end
+
+ def map_to_versions(array)
+ array.map { |elem| Version.new(elem) }
+ end
+
+ describe '#new' do
+ context 'when version contains non-numeric symbols' do
+ it 'raises ArgumentError' do
+ expect_error(snapshot_version)
+ expect_error(negative_version)
+ expect_error(positive_version)
+ expect_error(underscore_version)
+ end
+ end
+
+ context 'when some number is missing' do
+ it 'raises ArgumentError' do
+ expect_error(missing_major_version)
+ expect_error(missing_minor_version)
+ expect_error(missing_build_version)
+ end
+ end
+
+ context 'with zerolike versions' do
+ it 'can create new instance' do
+ Version.new
+ Version.new('')
+ Version.new('0')
+ end

М, да, в долните тестове използваш, че могат да се подават такива аргументи. Този тест е безсмислен, тъй като ако той фейлне, значи със сигурност и някой от долните ще фейлне. Тоест, не проверява нищо ново.

+ end
+
+ context 'with existing version' do
+ it 'can create new instance' do
+ Version.new(version_square)
+ end
+ end
+
+ it 'can create new instance' do
+ Version.new('1.15.3126.47')
+ end
+
+ describe '#components' do
+ context 'with zerolike versions' do
+ it 'can get components' do
+ expect(Version.new.components).to match_array []
+ expect(Version.new('').components).to match_array []
+ expect(Version.new('0').components).to match_array []
+ end
+ end
+
+ context 'when version is created via existing one' do
+ it 'can get components' do
+ version = Version.new(version_square)
+ expect(version.components).to eq square_numbers
+ end
+ end
+
+ context 'when version has trailing zeros' do
+ it 'can get components' do
+ version = Version.new('1.2.3.4.0.0')
+ expect(version.components).to eq [1, 2, 3, 4]
+ end
+ end
+
+ context 'when given length is equal to version numbers' do
+ it 'can get components ' do
+ expect(version_square.components(4)).to eq square_numbers
+ end
+ end
+
+ context 'when given length is less than version numbers' do
+ it 'can get components ' do
+ expect(version_square.components(3))
+ .to eq square_numbers.take(3)
+ end
+ end
+
+ context 'when given length is bigger than version numbers' do
+ it 'can get components ' do
+ expect(version_square.components(6))
+ .to eq(square_numbers + [0, 0])
+ end
+ end
+
+ it 'can get components' do
+ expect(version_square.components).to eq square_numbers
+ end
+
+ it 'does not allow modification of the class internal state' do
+ components = version_square.components
+
+ expect(components).to eq square_numbers
+ components.pop
+ expect(version_square.components).to eq square_numbers
+ end
+
+ it 'does not allow modification of the other class internal state' do
+ version = Version.new(version_square)
+ components = version.components
+
+ expect(components).to eq square_numbers
+ components.pop
+ expect(version.components).to eq square_numbers
+ end
+ end
+
+ describe '<=>' do
+ context 'when lengths are equal' do
+ it 'knows the bigger version' do
+ actual = Version.new('1.4.9.17') <=> version_square
+ expect(actual).to eq 1
+ end
+
+ it 'knows the lower version' do
+ actual = Version.new('1.4.9.15') <=> version_square
+ expect(actual).to eq -1
+ end
+
+ it 'knows that versions are equal' do
+ actual = Version.new('1.4.9.16') <=> version_square
+ expect(actual).to eq 0
+ end
+ end
+
+ context 'when lengths are not equal' do
+ it 'knows the bigger version' do
+ actual = Version.new('1.4.9.16.8') <=> version_square
+ expect(actual).to eq 1
+ end
+
+ it 'knows the lower version' do
+ actual = Version.new('1.4.9.15.2.1') <=> version_square
+ expect(actual).to eq -1
+ end
+
+ it 'knows that versions are equal' do
+ actual = Version.new('1.4.9.16.0.0') <=> version_square
+ expect(actual).to eq 0
+ end
+ end
+ end
+
+ describe '#to_s' do
+ context 'with zerolike versions' do
+ it 'stringifies' do
+ expect(Version.new.to_s).to eq ''
+ expect(Version.new('').to_s).to eq ''
+ expect(Version.new('0').to_s).to eq ''
+ end
+ end
+
+ context 'when major, minor and build numbers are zeros' do
+ it 'stringifies' do
+ expect(Version.new('0.1.0.2.0').to_s).to eq '0.1.0.2'
+ end
+ end
+
+ it 'stringifies' do
+ expect(version_square.to_s).to eq square_numbers.join('.')
+ end
+ end
+
+ describe 'Version::Range' do
+ describe '#new' do
+ context 'when versions contain non-numeric symbols' do
+ it 'raises ArgumentError' do
+ expect_range_error(snapshot_version, beta_version)
+ expect_range_error(negative_version, '1.-2')
+ expect_range_error(positive_version, '1.+2')
+ expect_range_error(underscore_version, '1.2_000')
+ end
+ end
+
+ context 'when some number is missing' do
+ it 'raises ArgumentError' do
+ expect_range_error(missing_major_version, '.3.0')
+ expect_range_error(missing_minor_version, '0..4')
+ expect_range_error(missing_build_version, '3.2.16.')
+ end
+ end
+
+ context 'with zerolike versions' do
+ it 'can create new instance' do
+ Version::Range.new('', '')
+ Version::Range.new('0', '0')
+ end
+ end
+
+ context 'with existing versions' do
+ it 'can create new instance' do
+ Version::Range.new(Version.new, Version.new)
+ Version::Range.new(Version.new('1'), Version.new('1.0.1'))
+ end
+ end
+
+ it 'can create new instance' do
+ Version::Range.new('1', '1.2.3')
+ end
+ end
+
+ describe '#include?' do
+ context 'when versions contain non-numeric symbols' do
+ it 'raises ArgumentError' do
+ expect_include_error(major_range, snapshot_version)
+ expect_include_error(major_range, negative_version)
+ expect_include_error(major_range, positive_version)
+ expect_include_error(major_range, underscore_version)
+ end
+ end
+
+ context 'when some number is missing' do
+ it 'raises ArgumentError' do
+ expect_include_error(major_range, missing_major_version)
+ expect_include_error(major_range, missing_minor_version)
+ expect_include_error(major_range, missing_build_version)
+ end
+ end
+
+ it 'can determine whether range includes version' do
+ expect(major_range.include?('1.13.9')).to eq true
+ end
+
+ it 'can determine whether range does not include' do
+ expect(major_range.include?('0.209.9')).to eq false
+ end
+
+ it 'includes the interval start to the range' do
+ expect(major_range.include?('1')).to eq true
+ end
+
+ it 'does not include the interval end to the range' do
+ expect(major_range.include?('2')).to eq false
+ end
+ end
+
+ describe '#to_a' do
+ context 'with zerolike start version' do
+ it 'can generate versions' do
+ range = Version::Range.new('', '0.0.5')
+ expected = map_to_versions [
+ '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
+ ]
+
+ expect(range.to_a).to match_array expected
+ end
+
+ it 'can generate versions' do
+ range = Version::Range.new(Version.new('0'), Version.new('0.0.5'))
+ expected = map_to_versions [
+ '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
+ ]
+
+ expect(range.to_a).to match_array expected
+ end
+ end
+
+ it 'can generate versions' do
+ range = Version::Range.new('0.0.8', '0.1')
+ expected = map_to_versions ['0.0.8', '0.0.9']
+
+ expect(range.to_a).to match_array expected
+ end
+
+ it 'can generate versions' do
+ range = Version::Range.new('2.1', '2.1')
+ expect(range.to_a).to match_array []
+ end
+
+ it 'can generate versions' do
+ range = Version::Range.new('1', '1.1')
+ expected = map_to_versions [
+ '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'
+ ]
+
+ expect(range.to_a).to match_array expected
+ end
+
+ it 'can generate versions' do
+ range = Version::Range.new(Version.new('1.8.9'), Version.new('2.1'))
+ expected = map_to_versions [
+ '1.8.9', '1.9.0', '1.9.1', '1.9.2', '1.9.3', '1.9.4', '1.9.5',
+ '1.9.6', '1.9.7', '1.9.8', '1.9.9', '2', '2.0.1', '2.0.2',
+ '2.0.3', '2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
+ ]
+
+ expect(range.to_a).to match_array expected
+ end
+ end
+ end
+end

Исмаил обнови решението на 18.11.2016 00:44 (преди над 1 година)

RSpec.describe 'Version' do
- let(:square_numbers) { [1, 4, 9, 16] }
- let(:version_square) { Version.new(square_numbers.join('.')) }
+ let(:version_square) { Version.new('1.4.9.16') }
let(:major_range) { Version::Range.new('1', '2') }
- let(:invalid_version_msg) { 'Invalid version string' }
- let(:snapshot_version) { '1.0-SNAPSHOT' }
- let(:beta_version) { '1.0-BETA' }
- let(:negative_version) { '1.-1' }
- let(:positive_version) { '1.+1' }
- let(:underscore_version) { '1.1_000' }
- let(:missing_major_version) { '.3' }
- let(:missing_minor_version) { '0..3' }
- let(:missing_build_version) { '3.2.15.' }
def expect_error(version)
expect { Version.new(version) }
- .to raise_error(ArgumentError, "#{invalid_version_msg} '#{version}'")
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
- def expect_range_error(start, last)
- expect { Version::Range.new(start, last) }
- .to raise_error(ArgumentError, "#{invalid_version_msg} '#{start}'")
- end
-
- def expect_include_error(range, version)
- expect { range.include?(version) }
- .to raise_error(ArgumentError, "#{invalid_version_msg} '#{version}'")
- end
-
- def map_to_versions(array)
- array.map { |elem| Version.new(elem) }
- end
-
describe '#new' do
- context 'when version contains non-numeric symbols' do
- it 'raises ArgumentError' do
- expect_error(snapshot_version)
- expect_error(negative_version)
- expect_error(positive_version)
- expect_error(underscore_version)
- end
+ it 'raises ArgumentError when version contains non-numeric symbols' do
+ expect_error('1.0-SNAPSHOT')
+ expect_error('1.-1')
+ expect_error('1.+1')
+ expect_error('1.1_000')
end
- context 'when some number is missing' do
- it 'raises ArgumentError' do
- expect_error(missing_major_version)
- expect_error(missing_minor_version)
- expect_error(missing_build_version)
- end
+ it 'raises ArgumentError when some number is missing' do
+ expect_error('.3')
+ expect_error('0..3')
+ expect_error('3.2.15.')
end
-
- context 'with zerolike versions' do
- it 'can create new instance' do
- Version.new
- Version.new('')
- Version.new('0')
- end
- end
-
- context 'with existing version' do
- it 'can create new instance' do
- Version.new(version_square)
- end
- end
-
- it 'can create new instance' do
- Version.new('1.15.3126.47')
- end
end
describe '#components' do
- context 'with zerolike versions' do
- it 'can get components' do
- expect(Version.new.components).to match_array []
- expect(Version.new('').components).to match_array []
- expect(Version.new('0').components).to match_array []
- end
+ it 'can get components with zerolike versions' do
+ expect(Version.new.components).to eq []
+ expect(Version.new('').components).to eq []
+ expect(Version.new('0').components).to eq []
end
- context 'when version is created via existing one' do
- it 'can get components' do
- version = Version.new(version_square)
- expect(version.components).to eq square_numbers
- end
+ it 'can get components when version is created via existing one' do
+ version = Version.new(version_square)
+ expect(version.components).to eq [1, 4, 9, 16]
end
- context 'when version has trailing zeros' do
- it 'can get components' do
- version = Version.new('1.2.3.4.0.0')
- expect(version.components).to eq [1, 2, 3, 4]
- end
+ it 'can get components when version has trailing zeros' do
+ version = Version.new('1.2.3.4.0.0')
+ expect(version.components).to eq [1, 2, 3, 4]
end
- context 'when given length is equal to version numbers' do
- it 'can get components ' do
- expect(version_square.components(4)).to eq square_numbers
- end
+ it 'can get components when given length is equal to version numbers' do
+ expect(version_square.components(4)).to eq [1, 4, 9, 16]
end
- context 'when given length is less than version numbers' do
- it 'can get components ' do
- expect(version_square.components(3))
- .to eq square_numbers.take(3)
- end
+ it 'can get components when given length is less than version numbers' do
+ expect(version_square.components(3)).to eq [1, 4, 9]
end
context 'when given length is bigger than version numbers' do
it 'can get components ' do
- expect(version_square.components(6))
- .to eq(square_numbers + [0, 0])
+ expect(version_square.components(6)).to eq([1, 4, 9, 16, 0, 0])
end
end
it 'can get components' do
- expect(version_square.components).to eq square_numbers
+ expect(version_square.components).to eq [1, 4, 9, 16]
end
it 'does not allow modification of the class internal state' do
- components = version_square.components
-
- expect(components).to eq square_numbers
- components.pop
- expect(version_square.components).to eq square_numbers
+ version_square.components.pop
+ expect(version_square.components).to eq [1, 4, 9, 16]
end
it 'does not allow modification of the other class internal state' do
version = Version.new(version_square)
- components = version.components
+ version.components.pop
- expect(components).to eq square_numbers
- components.pop
- expect(version.components).to eq square_numbers
+ expect(version.components).to eq [1, 4, 9, 16]
end
end
describe '<=>' do
context 'when lengths are equal' do
it 'knows the bigger version' do
actual = Version.new('1.4.9.17') <=> version_square
expect(actual).to eq 1
end
it 'knows the lower version' do
actual = Version.new('1.4.9.15') <=> version_square
expect(actual).to eq -1
end
it 'knows that versions are equal' do
actual = Version.new('1.4.9.16') <=> version_square
expect(actual).to eq 0
end
end
context 'when lengths are not equal' do
it 'knows the bigger version' do
actual = Version.new('1.4.9.16.8') <=> version_square
expect(actual).to eq 1
end
it 'knows the lower version' do
actual = Version.new('1.4.9.15.2.1') <=> version_square
expect(actual).to eq -1
end
it 'knows that versions are equal' do
actual = Version.new('1.4.9.16.0.0') <=> version_square
expect(actual).to eq 0
end
end
end
describe '#to_s' do
- context 'with zerolike versions' do
- it 'stringifies' 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 'stringifies' do
+ expect(Version.new.to_s).to eq ''
+ expect(Version.new('').to_s).to eq ''
+ expect(Version.new('0').to_s).to eq ''
end
- context 'when major, minor and build numbers are zeros' do
- it 'stringifies' do
- expect(Version.new('0.1.0.2.0').to_s).to eq '0.1.0.2'
- end
+ it 'stringifies when major, minor and build numbers are zeros' do
+ expect(Version.new('0.1.0.2.0').to_s).to eq '0.1.0.2'
end
it 'stringifies' do
- expect(version_square.to_s).to eq square_numbers.join('.')
+ expect(version_square.to_s).to eq '1.4.9.16'
end
end
+ def expect_range_error(start, last)
+ expect { Version::Range.new(start, last) }
+ .to raise_error(ArgumentError, "Invalid version string '#{start}'")
+ end
+
describe 'Version::Range' do
describe '#new' do
- context 'when versions contain non-numeric symbols' do
- it 'raises ArgumentError' do
- expect_range_error(snapshot_version, beta_version)
- expect_range_error(negative_version, '1.-2')
- expect_range_error(positive_version, '1.+2')
- expect_range_error(underscore_version, '1.2_000')
- end
+ it 'raises ArgumentError when versions contain non-numeric symbols' do
+ expect_range_error('1.0-SNAPSHOT', '1.0-BETA')
+ expect_range_error('1.-1', '1.-2')
+ expect_range_error('1.+1', '1.+2')
+ expect_range_error('1.1_000', '1.2_000')
end
- context 'when some number is missing' do
- it 'raises ArgumentError' do
- expect_range_error(missing_major_version, '.3.0')
- expect_range_error(missing_minor_version, '0..4')
- expect_range_error(missing_build_version, '3.2.16.')
- end
+ it 'raises ArgumentError when some number is missing' do
+ expect_range_error('.3', '.3.0')
+ expect_range_error('0..3', '0..4')
+ expect_range_error('3.2.15.', '3.2.16.')
end
- context 'with zerolike versions' do
- it 'can create new instance' do
- Version::Range.new('', '')
- Version::Range.new('0', '0')
- end
+ it 'can create new instance with zerolike versions' do
+ Version::Range.new('', '')
+ Version::Range.new('0', '0')
end
- context 'with existing versions' do
- it 'can create new instance' do
- Version::Range.new(Version.new, Version.new)
- Version::Range.new(Version.new('1'), Version.new('1.0.1'))
- end
+ it 'can create new instance with existing versions' do
+ Version::Range.new(Version.new, Version.new)
+ Version::Range.new(Version.new('1'), Version.new('1.0.1'))
end
- it 'can create new instance' do
+ it 'can create new instance with strings' do
Version::Range.new('1', '1.2.3')
end
end
+ def expect_include_error(range, version)
+ expect { range.include?(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+
describe '#include?' do
- context 'when versions contain non-numeric symbols' do
- it 'raises ArgumentError' do
- expect_include_error(major_range, snapshot_version)
- expect_include_error(major_range, negative_version)
- expect_include_error(major_range, positive_version)
- expect_include_error(major_range, underscore_version)
- end
+ it 'raises ArgumentError when versions contain non-numeric symbols' do
+ expect_include_error(major_range, '1.0-SNAPSHOT')
+ expect_include_error(major_range, '1.-1')
+ expect_include_error(major_range, '1.+1')
+ expect_include_error(major_range, '1.1_000')
end
- context 'when some number is missing' do
- it 'raises ArgumentError' do
- expect_include_error(major_range, missing_major_version)
- expect_include_error(major_range, missing_minor_version)
- expect_include_error(major_range, missing_build_version)
- end
+ it 'raises ArgumentError when some number is missing' do
+ expect_include_error(major_range, '.3')
+ expect_include_error(major_range, '0..3')
+ expect_include_error(major_range, '3.2.15.')
end
it 'can determine whether range includes version' do
expect(major_range.include?('1.13.9')).to eq true
end
it 'can determine whether range does not include' do
expect(major_range.include?('0.209.9')).to eq false
end
it 'includes the interval start to the range' do
expect(major_range.include?('1')).to eq true
end
it 'does not include the interval end to the range' do
expect(major_range.include?('2')).to eq false
end
end
+ def map_to_versions(string_versions)
+ string_versions.map { |v| Version.new(v) }
+ end
+
describe '#to_a' do
- context 'with zerolike start version' do
- it 'can generate versions' do
- range = Version::Range.new('', '0.0.5')
- expected = map_to_versions [
- '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
- ]
+ it 'can generate versions with starting empty string version' do
+ range = Version::Range.new('', '0.0.5')
+ expected = map_to_versions [
+ '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
+ ]
- expect(range.to_a).to match_array expected
- end
+ expect(range.to_a).to eq expected
+ end
- it 'can generate versions' do
- range = Version::Range.new(Version.new('0'), Version.new('0.0.5'))
- expected = map_to_versions [
- '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
- ]
+ it 'can generate versions with starting zerolike version' do
+ range = Version::Range.new(Version.new('0'), Version.new('0.0.5'))
+ expected = map_to_versions [
+ '0', '0.0.1', '0.0.2', '0.0.3', '0.0.4'
+ ]
- expect(range.to_a).to match_array expected
- end
+ expect(range.to_a).to eq expected
end
- it 'can generate versions' do
+ it 'can generate versions when start equals end' do
+ range = Version::Range.new('2.1', '2.1')
+ expect(range.to_a).to eq []
+ end
+
+ it 'can generate versions to build version' do
range = Version::Range.new('0.0.8', '0.1')
expected = map_to_versions ['0.0.8', '0.0.9']
- expect(range.to_a).to match_array expected
+ expect(range.to_a).to eq expected
end
- it 'can generate versions' do
- range = Version::Range.new('2.1', '2.1')
- expect(range.to_a).to match_array []
- end
-
- it 'can generate versions' do
+ it 'can generate versions to minor version' do
range = Version::Range.new('1', '1.1')
expected = map_to_versions [
'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'
]
- expect(range.to_a).to match_array expected
+ expect(range.to_a).to eq expected
end
- it 'can generate versions' do
+ it 'can generate versions to major version' do
range = Version::Range.new(Version.new('1.8.9'), Version.new('2.1'))
expected = map_to_versions [
'1.8.9', '1.9.0', '1.9.1', '1.9.2', '1.9.3', '1.9.4', '1.9.5',
'1.9.6', '1.9.7', '1.9.8', '1.9.9', '2', '2.0.1', '2.0.2',
'2.0.3', '2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
]
- expect(range.to_a).to match_array expected
+ expect(range.to_a).to eq expected
end
end
end
end