Решение на Четвърта задача - Unit тестване от Ралица Дарджонова

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

Към профила на Ралица Дарджонова

Резултати

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

Код

RSpec.describe Version do
describe 'creating' do
it 'handles invalid input' do
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
expect { Version.new('1.3..6') }.to raise_error(
ArgumentError,
"Invalid version string '1.3..6'"
)
expect { Version.new('1.3.а.6') }.to raise_error(
ArgumentError,
"Invalid version string '1.3.а.6'"
)
expect { Version.new('.3') }.to raise_error(
ArgumentError,
"Invalid version string '.3'"
)
expect { Version.new('3.') }.to raise_error(
ArgumentError,
"Invalid version string '3.'"
)
end
end
context 'comparing versions' do
it 'uses < properly' do
expect(Version.new('1.2.3')).to be < Version.new('1.3')
expect(Version.new('1.3.3')).not_to be < Version.new('1.3')
expect(Version.new('1.3.3')).not_to be < Version.new('1.3.3')
expect(Version.new('2.3.3')).not_to be < Version.new('1.4.0.6')
expect(Version.new('0.3.3')).to be < Version.new('1.0.0')
expect(Version.new('2.3.1')).to be < Version.new('2.3.4')
expect(Version.new('')).to be < Version.new('0.0.4')
end
it 'uses > properly' do
expect(Version.new('1.2.3')).not_to be > Version.new('1.3')
expect(Version.new('1.3.3')).to be > Version.new('1.3')
expect(Version.new('1.3')).not_to be > Version.new('1.3')
expect(Version.new('1.4')).to be > Version.new('1.3.9')
expect(Version.new('0.9.7')).not_to be > Version.new('1.0.1')
expect(Version.new('2.3.3')).to be > Version.new('2.3.1')
expect(Version.new('0.0')).not_to be > Version.new('0.0.4')
expect(Version.new('0.1')).to be > Version.new('')
end
it 'uses >= properly' do
expect(Version.new('1.3.3')).to be >= Version.new('1.3')
expect(Version.new('1.3.3')).to be >= Version.new('1.3.3')
expect(Version.new('1.3.3')).to be >= Version.new('1.3.2')
expect(Version.new('1.3.3')).to be >= Version.new('1.3.3.0')
expect(Version.new('1.2.9')).not_to be >= Version.new('1.3.3')
expect(Version.new('1.2.9')).not_to be >= Version.new('1.2.9.1')
expect(Version.new('1.2.9')).not_to be >= Version.new('1.3')
expect(Version.new('')).not_to be >= Version.new('0.0.4')
expect(Version.new('0.1')).to be >= Version.new('')
end
it 'uses <= properly' do
expect(Version.new('1.3.3')).not_to be <= Version.new('1.3')
expect(Version.new('1.3.3')).to be <= Version.new('1.3.3')
expect(Version.new('1.3.3')).not_to be <= Version.new('1.3.2')
expect(Version.new('1.3.3')).to be <= Version.new('1.3.3.0')
expect(Version.new('1.2.9')).to be <= Version.new('1.3.3')
expect(Version.new('1.2.9')).to be <= Version.new('1.2.9.1')
expect(Version.new('1.2.9')).to be <= Version.new('1.3')
expect(Version.new('1.4')).not_to be <= Version.new('1.3.9')
expect(Version.new('')).to be <= Version.new('0.0.4')
expect(Version.new('0.1')).not_to be <= Version.new('')
end
it 'uses == properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(version1 == version2).to be true
expect(Version.new('1.3.3.0.0') == Version.new('1.3.3')).to be true
expect(Version.new('1.3.3') == Version.new('1.3.3.0')).to be true
expect(Version.new('1.3.3') == Version.new('1.3')).to be false
expect(Version.new('1.4') == Version.new('1.4.1')).to be false
expect(Version.new('1.3.3.2') == Version.new('1.3.3.1')).to be false
expect(Version.new('') == Version.new('0.0')).to be true
end
it 'uses <=> properly' do
expect(Version.new('1.3.3') <=> Version.new('1.3')).to be 1
expect(Version.new <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('1.3')).to be -1
expect(Version.new('0.1') <=> Version.new).to be 1
expect(Version.new('') <=> Version.new('1.3')).to be -1
expect(Version.new('1.3.3.0.0') <=> Version.new('1.3.3')).to be 0
expect(Version.new('1.3.3.9') <=> Version.new('1.3.4')).to be -1
end
end
describe '#to_s' do
it 'converts into string when initialize with string' do
expect(Version.new('').to_s).to eq ''
expect(Version.new.to_s).to eq ''
expect(Version.new('0.0').to_s).to eq ''
expect(Version.new('1.1').to_s).to eq '1.1'
expect(Version.new('1.2.0').to_s).to eq '1.2'
end
it 'converts into string when initialized with Version' do
expect(Version.new(Version.new('')).to_s).to eq ''
expect(Version.new(Version.new).to_s).to eq ''
expect(Version.new(Version.new('0.0.0')).to_s).to eq ''
expect(Version.new(Version.new('3.4.0.0.0.0')).to_s).to eq '3.4'
expect(Version.new(Version.new('0.2')).to_s).to eq '0.2'
end
end
describe '#components' do
it 'works correctly without argument' do
expect(Version.new('').components).to eq []
expect(Version.new('0').components).to eq []
expect(Version.new.components).to eq []
expect(Version.new('1.3.4').components).to eq [1, 3, 4]
expect(Version.new('1.2.0.0').components).to eq [1, 2]
end
it 'works correctly with argument' do
expect(Version.new('').components(2)).to eq [0, 0]
expect(Version.new('1.3').components(2)).to eq [1, 3]
expect(Version.new('1.3').components(4)).to eq [1, 3, 0, 0]
expect(Version.new('1.4.4.3').components(3)).to eq [1, 4, 4]
end
end
describe 'Version::Range' do
describe '#include?' do
it 'checks if a version is in range when initialized with strings' do
expect(Version::Range.new('1.2', '1.3')).to include('1.2.5')
expect(Version::Range.new('1.2', '1.3')).not_to include('1.1.9')
expect(Version::Range.new('1.2', '1.3')).not_to include('1.3.1')
end
it 'checks if a version is in range when initialized with Versions' do
version = Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
expect(version).to include( Version.new('1.2.3.8'))
version2 = Version::Range.new(Version.new('1'), Version.new('2'))
expect(version2).to include(Version.new('1.5'))
version3 = Version::Range.new('0.1.2.3', '0.1.2.4')
expect(version3).to include('0.1.2.3.0.1')
end
end
describe '#to_a' do
it 'generates all versions in a range' do
version = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expect(version.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('1.2', '1.3').to_a).to eq [
'1.2',
'1.2.1',
'1.2.2',
'1.2.3',
'1.2.4',
'1.2.5',
'1.2.6',
'1.2.7',
'1.2.8',
'1.2.9'
]
expect(Version::Range.new('1.4', '1.4.2').to_a).to eq ['1.4', '1.4.1']
expect(Version::Range.new('1.4', '1.3.9').to_a).to eq []
expect(Version::Range.new('1.4', '1.4.0').to_a).to eq []
end
end
end
end

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

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

Failures:

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

Finished in 19 seconds
19 examples, 1 failure

Failed examples:

rspec /tmp/d20161119-19072-alyaen/spec.rb:441 # spec Version tests that #components cannot be used to modify the version

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

Ралица обнови решението на 17.11.2016 20:55 (преди над 1 година)

+RSpec.describe Version do
+ context 'creating' do
+ it 'handles invalid input when initialized with string' do
+ expect { Version.new('1..3') }
+ .to raise_error(ArgumentError, "Invalid version string '1..3'")
+ expect { Version.new('1.3..6') }
+ .to raise_error(ArgumentError, "Invalid version string '1.3..6'")
+ expect { Version.new('1.3.а.6') }
+ .to raise_error(ArgumentError, "Invalid version string '1.3.а.6'")
+ expect { Version.new('.3') }
+ .to raise_error(ArgumentError, "Invalid version string '.3'")
+ expect { Version.new('3.') }
+ .to raise_error(ArgumentError, "Invalid version string '3.'")
+ end
+
+ it 'handles invalid input when initialized with Version' do
+ expect { Version.new(Version.new('1..3')) }
+ .to raise_error(ArgumentError, "Invalid version string '1..3'")
+ expect { Version.new(Version.new('.3')) }
+ .to raise_error(ArgumentError, "Invalid version string '.3'")
+ expect { Version.new(Version.new('3.')) }
+ .to raise_error(ArgumentError, "Invalid version string '3.'")
+ end
+
+ it 'handles valid input when initialized with string' do
+ expect { Version.new('0.3') }.not_to raise_error
+ expect { Version.new('1.3.4') }.not_to raise_error
+ expect { Version.new('1.3.0') }.not_to raise_error
+ expect { Version.new('1') }.not_to raise_error
+ expect { Version.new('0.0') }.not_to raise_error
+ expect { Version.new('') }.not_to raise_error
+ expect { Version.new }.not_to raise_error
+ end
+
+ it 'handles valid input when initialized with Version' do
+ expect { Version.new(Version.new('0.3')) }.not_to raise_error
+ expect { Version.new(Version.new('1.3.4')) }.not_to raise_error
+ expect { Version.new(Version.new('1.3.0')) }.not_to raise_error
+ expect { Version.new(Version.new('0.0')) }.not_to raise_error
+ expect { Version.new(Version.new('')) }.not_to raise_error
+ expect { Version.new(Version.new) }.not_to raise_error
+ end
+ end
+
+ context 'comparing versions' do
+ it 'uses < properly' do
+ version1 = Version.new('1.3.3')
+ version2 = Version.new('1.3.3')
+
+ expect(Version.new('1.2.3') < Version.new('1.3')).to be true
+ expect(Version.new('1.3.3') < Version.new('1.3')).to be false
+ expect(version1 < version2).to be false
+ expect(Version.new('2.3.3') < Version.new('1.4.0.6')).to be false
+ expect(Version.new('0.3.3') < Version.new('1.0.0')).to be true
+ expect(Version.new('2.3.1') < Version.new('2.3.4')).to be true
+ expect(Version.new('') < Version.new('0.0.4')).to be true
+ end
+
+ it 'uses > properly' do
+ version1 = Version.new('1.3')
+ version2 = Version.new('1.3')
+
+ expect(Version.new('1.2.3') > Version.new('1.3')).to be false
+ expect(Version.new('1.3.3') > Version.new('1.3')).to be true
+ expect( version1 > version2).to be false
+ expect(Version.new('1.4') > Version.new('1.3.9')).to be true
+ expect(Version.new('0.9.7') > Version.new('1.0.1')).to be false
+ expect(Version.new('2.3.3') > Version.new('2.3.1')).to be true
+ expect(Version.new('0.0') > Version.new('0.0.4')).to be false
+ expect(Version.new('0.1') > Version.new('')).to be true
+ end
+ it 'uses >= properly' do
+ version1 = Version.new('1.3.3')
+ version2 = Version.new('1.3.3')
+
+ expect(Version.new('1.3.3') >= Version.new('1.3')).to be true
+ expect( version1 >= version2).to be true
+ expect(Version.new('1.3.3') >= Version.new('1.3.2')).to be true
+ expect(Version.new('1.3.3') >= Version.new('1.3.3.0')).to be true
+ expect(Version.new('1.2.9') >= Version.new('1.3.3')).to be false
+ expect(Version.new('1.2.9') >= Version.new('1.2.9.1')).to be false
+ expect(Version.new('1.2.9') >= Version.new('1.3')).to be false
+ expect(Version.new('') >= Version.new('0.0.4')).to be false
+ expect(Version.new('0.1') >= Version.new('')).to be true
+ end
+
+ it 'uses <= properly' do
+ version1 = Version.new('1.3.3')
+ version2 = Version.new('1.3.3')
+
+ expect(Version.new('1.3.3') <= Version.new('1.3')).to be false
+ expect( version1 <= version2 ).to be true
+ expect(Version.new('1.3.3') <= Version.new('1.3.2')).to be false
+ expect(Version.new('1.3.3') <= Version.new('1.3.3.0')).to be true
+ expect(Version.new('1.2.9') <= Version.new('1.3.3')).to be true
+ expect(Version.new('1.2.9') <= Version.new('1.2.9.1')).to be true
+ expect(Version.new('1.2.9') <= Version.new('1.3')).to be true
+ expect(Version.new('1.4') <= Version.new('1.3.9')).to be false
+ expect(Version.new('') <= Version.new('0.0.4')).to be true
+ expect(Version.new('0.1') <= Version.new('')).to be false
+ end
+
+ it 'uses == properly' do
+ version1 = Version.new('1.3.3')
+ version2 = Version.new('1.3.3')
+
+ expect(version1 == version2).to be true
+ expect(Version.new('1.3.3.0.0') == Version.new('1.3.3')).to be true
+ expect(Version.new('1.3.3') == Version.new('1.3.3.0')).to be true
+ expect(Version.new('1.3.3') == Version.new('1.3')).to be false
+ expect(Version.new('1.4') == Version.new('1.4.1')).to be false
+ expect(Version.new('1.3.3.2') == Version.new('1.3.3.1')).to be false
+ expect(Version.new('') == Version.new('0.0')).to be true
+ end
+
+ it 'uses <=> properly' do
+ expect(Version.new('1.3.3') <=> Version.new('1.3')).to be 1
+ expect(Version.new <=> Version.new('')).to be 0
+ expect(Version.new('0') <=> Version.new('')).to be 0
+ expect(Version.new('0') <=> Version.new('1.3')).to be -1
+ expect(Version.new('0.1') <=> Version.new).to be 1
+ expect(Version.new('') <=> Version.new('1.3')).to be -1
+ expect(Version.new('1.3.3.0.0') <=> Version.new('1.3.3')).to be 0
+ expect(Version.new('1.3.3.9') <=> Version.new('1.3.4')).to be -1
+ end
+ end
+
+ describe '#to_s' do
+ it 'converts into string when initialize with string' do
+ expect(Version.new('').to_s).to eq ''
+ expect(Version.new.to_s).to eq ''
+ expect(Version.new('0.0').to_s).to eq ''
+ expect(Version.new('1.1').to_s).to eq '1.1'
+ expect(Version.new('1.2.0').to_s).to eq '1.2'
+ end
+ it 'converts into string when initialized with Version' do
+ expect(Version.new(Version.new('')).to_s).to eq ''
+ expect(Version.new(Version.new).to_s).to eq ''
+ expect(Version.new(Version.new('0.0.0')).to_s).to eq ''
+ expect(Version.new(Version.new('3.4.0.0.0.0')).to_s).to eq '3.4'
+ expect(Version.new(Version.new('0.2')).to_s).to eq '0.2'
+ end
+ end
+
+ describe '#components' do
+ it 'works correctly without argument when initialized with string' do
+ expect(Version.new('').components).to eq []
+ expect(Version.new('0').components).to eq []
+ expect(Version.new.components).to eq []
+ expect(Version.new('1.3.4').components).to eq [1, 3, 4]
+ expect(Version.new('1.2.0.0').components).to eq [1, 2]
+ end
+ it 'works correctly with argument when initialized with string' do
+ expect(Version.new('').components(2)).to eq [0, 0]
+ expect(Version.new('1.3').components(2)).to eq [1, 3]
+ expect(Version.new('1.3').components(4)).to eq [1, 3, 0, 0]
+ expect(Version.new('1.4.4.3').components(3)).to eq [1, 4, 4]
+ end
+ it 'works correctly without argument when initialized with Version' do
+ expect(Version.new(Version.new('')).components).to eq []
+ expect(Version.new(Version.new('0')).components).to eq []
+ expect(Version.new(Version.new).components).to eq []
+ expect(Version.new(Version.new('1.3.4')).components).to eq [1, 3, 4]
+ expect(Version.new(Version.new('1.2.0.0')).components).to eq [1, 2]
+ end
+ it 'works correctly with argument when initialized with Version' do
+ expect(Version.new(Version.new('')).components(2)).to eq [0, 0]
+ expect(Version.new(Version.new('1.3')).components(2)).to eq [1, 3]
+ expect(Version.new(Version.new('1.3')).components(4)).to eq [1, 3, 0, 0]
+ expect(Version.new(Version.new('1.4.4.3')).components(3)).to eq [1, 4, 4]
+ end
+ end
+
+ describe 'Version::Range' do
+ describe '#include?' do
+ it 'checks correctly if a version is in range' do
+ expect(Version::Range.new('1.2', '1.3').include?('1.2.5')).to be true
+
+ version1 = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(version1.include?(Version.new('1.5'))).to be true
+ expect(Version::Range.new('1.2', '1.3').include?('1.1.9')).to be false
+ expect(Version::Range.new('1.2', '1.3').include?('1.3.1')).to be false
+
+ version = Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
+ expect(version.include?( Version.new('1.2.3.8'))).to be true
+
+ version3 = Version::Range.new('0.1.2.3.4.5', '0.1.2.3.4.7')
+ expect(version3.include?('0.1.2.3.4.6.0.0.1')).to eq true
+ end
+ end
+
+ describe '#to_a' do
+ it 'generates all versions in a range' do
+ 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('1.4', '1.4.2').to_a). to eq ['1.4', '1.4.1']
+ expect(Version::Range.new('1.4', '1.3.9').to_a). to eq []
+ expect(Version::Range.new('1.4', '1.4.0').to_a). to eq []
+ end
+ end
+ end
+end

Ралица обнови решението на 17.11.2016 21:27 (преди над 1 година)

RSpec.describe Version do
context 'creating' do
it 'handles invalid input when initialized with string' do
expect { Version.new('1..3') }
.to raise_error(ArgumentError, "Invalid version string '1..3'")
expect { Version.new('1.3..6') }
.to raise_error(ArgumentError, "Invalid version string '1.3..6'")
expect { Version.new('1.3.а.6') }
.to raise_error(ArgumentError, "Invalid version string '1.3.а.6'")
expect { Version.new('.3') }
.to raise_error(ArgumentError, "Invalid version string '.3'")
expect { Version.new('3.') }
.to raise_error(ArgumentError, "Invalid version string '3.'")
end
it 'handles invalid input when initialized with Version' do
expect { Version.new(Version.new('1..3')) }
.to raise_error(ArgumentError, "Invalid version string '1..3'")
expect { Version.new(Version.new('.3')) }
.to raise_error(ArgumentError, "Invalid version string '.3'")
expect { Version.new(Version.new('3.')) }
.to raise_error(ArgumentError, "Invalid version string '3.'")
end
it 'handles valid input when initialized with string' do
expect { Version.new('0.3') }.not_to raise_error
expect { Version.new('1.3.4') }.not_to raise_error
expect { Version.new('1.3.0') }.not_to raise_error
expect { Version.new('1') }.not_to raise_error
expect { Version.new('0.0') }.not_to raise_error
expect { Version.new('') }.not_to raise_error
expect { Version.new }.not_to raise_error
end
it 'handles valid input when initialized with Version' do
expect { Version.new(Version.new('0.3')) }.not_to raise_error
expect { Version.new(Version.new('1.3.4')) }.not_to raise_error
expect { Version.new(Version.new('1.3.0')) }.not_to raise_error
expect { Version.new(Version.new('0.0')) }.not_to raise_error
expect { Version.new(Version.new('')) }.not_to raise_error
expect { Version.new(Version.new) }.not_to raise_error
end
end
context 'comparing versions' do
it 'uses < properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(Version.new('1.2.3') < Version.new('1.3')).to be true
expect(Version.new('1.3.3') < Version.new('1.3')).to be false
expect(version1 < version2).to be false
expect(Version.new('2.3.3') < Version.new('1.4.0.6')).to be false
expect(Version.new('0.3.3') < Version.new('1.0.0')).to be true
expect(Version.new('2.3.1') < Version.new('2.3.4')).to be true
expect(Version.new('') < Version.new('0.0.4')).to be true
end
it 'uses > properly' do
version1 = Version.new('1.3')
version2 = Version.new('1.3')
expect(Version.new('1.2.3') > Version.new('1.3')).to be false
expect(Version.new('1.3.3') > Version.new('1.3')).to be true
expect( version1 > version2).to be false
expect(Version.new('1.4') > Version.new('1.3.9')).to be true
expect(Version.new('0.9.7') > Version.new('1.0.1')).to be false
expect(Version.new('2.3.3') > Version.new('2.3.1')).to be true
expect(Version.new('0.0') > Version.new('0.0.4')).to be false
expect(Version.new('0.1') > Version.new('')).to be true
end
it 'uses >= properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(Version.new('1.3.3') >= Version.new('1.3')).to be true
expect( version1 >= version2).to be true
expect(Version.new('1.3.3') >= Version.new('1.3.2')).to be true
expect(Version.new('1.3.3') >= Version.new('1.3.3.0')).to be true
expect(Version.new('1.2.9') >= Version.new('1.3.3')).to be false
expect(Version.new('1.2.9') >= Version.new('1.2.9.1')).to be false
expect(Version.new('1.2.9') >= Version.new('1.3')).to be false
expect(Version.new('') >= Version.new('0.0.4')).to be false
expect(Version.new('0.1') >= Version.new('')).to be true
end
it 'uses <= properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(Version.new('1.3.3') <= Version.new('1.3')).to be false
expect( version1 <= version2 ).to be true
expect(Version.new('1.3.3') <= Version.new('1.3.2')).to be false
expect(Version.new('1.3.3') <= Version.new('1.3.3.0')).to be true
expect(Version.new('1.2.9') <= Version.new('1.3.3')).to be true
expect(Version.new('1.2.9') <= Version.new('1.2.9.1')).to be true
expect(Version.new('1.2.9') <= Version.new('1.3')).to be true
expect(Version.new('1.4') <= Version.new('1.3.9')).to be false
expect(Version.new('') <= Version.new('0.0.4')).to be true
expect(Version.new('0.1') <= Version.new('')).to be false
end
it 'uses == properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(version1 == version2).to be true
expect(Version.new('1.3.3.0.0') == Version.new('1.3.3')).to be true
expect(Version.new('1.3.3') == Version.new('1.3.3.0')).to be true
expect(Version.new('1.3.3') == Version.new('1.3')).to be false
expect(Version.new('1.4') == Version.new('1.4.1')).to be false
expect(Version.new('1.3.3.2') == Version.new('1.3.3.1')).to be false
expect(Version.new('') == Version.new('0.0')).to be true
end
it 'uses <=> properly' do
expect(Version.new('1.3.3') <=> Version.new('1.3')).to be 1
expect(Version.new <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('1.3')).to be -1
expect(Version.new('0.1') <=> Version.new).to be 1
expect(Version.new('') <=> Version.new('1.3')).to be -1
expect(Version.new('1.3.3.0.0') <=> Version.new('1.3.3')).to be 0
expect(Version.new('1.3.3.9') <=> Version.new('1.3.4')).to be -1
end
end
describe '#to_s' do
it 'converts into string when initialize with string' do
expect(Version.new('').to_s).to eq ''
expect(Version.new.to_s).to eq ''
expect(Version.new('0.0').to_s).to eq ''
expect(Version.new('1.1').to_s).to eq '1.1'
expect(Version.new('1.2.0').to_s).to eq '1.2'
end
it 'converts into string when initialized with Version' do
expect(Version.new(Version.new('')).to_s).to eq ''
expect(Version.new(Version.new).to_s).to eq ''
expect(Version.new(Version.new('0.0.0')).to_s).to eq ''
expect(Version.new(Version.new('3.4.0.0.0.0')).to_s).to eq '3.4'
expect(Version.new(Version.new('0.2')).to_s).to eq '0.2'
end
end
describe '#components' do
it 'works correctly without argument when initialized with string' do
expect(Version.new('').components).to eq []
expect(Version.new('0').components).to eq []
expect(Version.new.components).to eq []
expect(Version.new('1.3.4').components).to eq [1, 3, 4]
expect(Version.new('1.2.0.0').components).to eq [1, 2]
end
it 'works correctly with argument when initialized with string' do
expect(Version.new('').components(2)).to eq [0, 0]
expect(Version.new('1.3').components(2)).to eq [1, 3]
expect(Version.new('1.3').components(4)).to eq [1, 3, 0, 0]
expect(Version.new('1.4.4.3').components(3)).to eq [1, 4, 4]
end
it 'works correctly without argument when initialized with Version' do
expect(Version.new(Version.new('')).components).to eq []
expect(Version.new(Version.new('0')).components).to eq []
expect(Version.new(Version.new).components).to eq []
expect(Version.new(Version.new('1.3.4')).components).to eq [1, 3, 4]
expect(Version.new(Version.new('1.2.0.0')).components).to eq [1, 2]
end
it 'works correctly with argument when initialized with Version' do
expect(Version.new(Version.new('')).components(2)).to eq [0, 0]
expect(Version.new(Version.new('1.3')).components(2)).to eq [1, 3]
expect(Version.new(Version.new('1.3')).components(4)).to eq [1, 3, 0, 0]
expect(Version.new(Version.new('1.4.4.3')).components(3)).to eq [1, 4, 4]
end
end
describe 'Version::Range' do
describe '#include?' do
- it 'checks correctly if a version is in range' do
+ it 'checks if a version is in range when initialized with strings' do
expect(Version::Range.new('1.2', '1.3').include?('1.2.5')).to be true
-
- version1 = Version::Range.new(Version.new('1'), Version.new('2'))
- expect(version1.include?(Version.new('1.5'))).to be true
expect(Version::Range.new('1.2', '1.3').include?('1.1.9')).to be false
expect(Version::Range.new('1.2', '1.3').include?('1.3.1')).to be false
-
+ end
+ it 'checks if a version is in range when initialized with Versions' do
version = Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
expect(version.include?( Version.new('1.2.3.8'))).to be true
-
- version3 = Version::Range.new('0.1.2.3.4.5', '0.1.2.3.4.7')
- expect(version3.include?('0.1.2.3.4.6.0.0.1')).to eq true
+ version2 = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(version2.include?(Version.new('1.5'))).to be true
+ version3 = Version::Range.new('0.1.2.3', '0.1.2.4')
+ expect(version3.include?('0.1.2.3.0.1')).to eq true
end
end
describe '#to_a' do
it 'generates all versions in a range' do
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'
]

Моля те, направи това подравняване като хората. Супер грозно е в момента.

range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expect(range.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('1.4', '1.4.2').to_a). to eq ['1.4', '1.4.1']
- expect(Version::Range.new('1.4', '1.3.9').to_a). to eq []
- expect(Version::Range.new('1.4', '1.4.0').to_a). to eq []
+ expect(Version::Range.new('1.2', '1.3').to_a)
+ .to eq [
+ '1.2', '1.2.1', '1.2.2', '1.2.3', '1.2.4', '1.2.5',
+ '1.2.6', '1.2.7', '1.2.8', '1.2.9'
+ ]
+ expect(Version::Range.new('1.4', '1.4.2').to_a).to eq ['1.4', '1.4.1']
+ expect(Version::Range.new('1.4', '1.3.9').to_a).to eq []
+ expect(Version::Range.new('1.4', '1.4.0').to_a).to eq []
end
end
end
end

Ралица обнови решението на 18.11.2016 03:04 (преди над 1 година)

RSpec.describe Version do
- context 'creating' do
- it 'handles invalid input when initialized with string' do
- expect { Version.new('1..3') }
- .to raise_error(ArgumentError, "Invalid version string '1..3'")
- expect { Version.new('1.3..6') }
- .to raise_error(ArgumentError, "Invalid version string '1.3..6'")
- expect { Version.new('1.3.а.6') }
- .to raise_error(ArgumentError, "Invalid version string '1.3.а.6'")
- expect { Version.new('.3') }
- .to raise_error(ArgumentError, "Invalid version string '.3'")
- expect { Version.new('3.') }
- .to raise_error(ArgumentError, "Invalid version string '3.'")
+ describe 'creating' do
+ it 'handles invalid input' do
+ expect { Version.new('1..3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1..3'"
+ )
+ expect { Version.new('1.3..6') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.3..6'"
+ )
+ expect { Version.new('1.3.а.6') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.3.а.6'"
+ )
+ expect { Version.new('.3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '.3'"
+ )
+ expect { Version.new('3.') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '3.'"
+ )
end
-
- it 'handles invalid input when initialized with Version' do
- expect { Version.new(Version.new('1..3')) }
- .to raise_error(ArgumentError, "Invalid version string '1..3'")
- expect { Version.new(Version.new('.3')) }
- .to raise_error(ArgumentError, "Invalid version string '.3'")
- expect { Version.new(Version.new('3.')) }
- .to raise_error(ArgumentError, "Invalid version string '3.'")
- end
-
- it 'handles valid input when initialized with string' do
- expect { Version.new('0.3') }.not_to raise_error
- expect { Version.new('1.3.4') }.not_to raise_error
- expect { Version.new('1.3.0') }.not_to raise_error
- expect { Version.new('1') }.not_to raise_error
- expect { Version.new('0.0') }.not_to raise_error
- expect { Version.new('') }.not_to raise_error
- expect { Version.new }.not_to raise_error
- end
-
- it 'handles valid input when initialized with Version' do
- expect { Version.new(Version.new('0.3')) }.not_to raise_error
- expect { Version.new(Version.new('1.3.4')) }.not_to raise_error
- expect { Version.new(Version.new('1.3.0')) }.not_to raise_error
- expect { Version.new(Version.new('0.0')) }.not_to raise_error
- expect { Version.new(Version.new('')) }.not_to raise_error
- expect { Version.new(Version.new) }.not_to raise_error
- end
end
context 'comparing versions' do
it 'uses < properly' do
- version1 = Version.new('1.3.3')
- version2 = Version.new('1.3.3')
-
- expect(Version.new('1.2.3') < Version.new('1.3')).to be true
- expect(Version.new('1.3.3') < Version.new('1.3')).to be false
- expect(version1 < version2).to be false
- expect(Version.new('2.3.3') < Version.new('1.4.0.6')).to be false
- expect(Version.new('0.3.3') < Version.new('1.0.0')).to be true
- expect(Version.new('2.3.1') < Version.new('2.3.4')).to be true
- expect(Version.new('') < Version.new('0.0.4')).to be true
+ expect(Version.new('1.2.3')).to be < Version.new('1.3')
+ expect(Version.new('1.3.3')).not_to be < Version.new('1.3')
+ expect(Version.new('1.3.3')).not_to be < Version.new('1.3.3')
+ expect(Version.new('2.3.3')).not_to be < Version.new('1.4.0.6')
+ expect(Version.new('0.3.3')).to be < Version.new('1.0.0')
+ expect(Version.new('2.3.1')).to be < Version.new('2.3.4')
+ expect(Version.new('')).to be < Version.new('0.0.4')
end
it 'uses > properly' do
- version1 = Version.new('1.3')
- version2 = Version.new('1.3')
-
- expect(Version.new('1.2.3') > Version.new('1.3')).to be false
- expect(Version.new('1.3.3') > Version.new('1.3')).to be true
- expect( version1 > version2).to be false
- expect(Version.new('1.4') > Version.new('1.3.9')).to be true
- expect(Version.new('0.9.7') > Version.new('1.0.1')).to be false
- expect(Version.new('2.3.3') > Version.new('2.3.1')).to be true
- expect(Version.new('0.0') > Version.new('0.0.4')).to be false
- expect(Version.new('0.1') > Version.new('')).to be true
+ expect(Version.new('1.2.3')).not_to be > Version.new('1.3')
+ expect(Version.new('1.3.3')).to be > Version.new('1.3')
+ expect(Version.new('1.3')).not_to be > Version.new('1.3')
+ expect(Version.new('1.4')).to be > Version.new('1.3.9')
+ expect(Version.new('0.9.7')).not_to be > Version.new('1.0.1')
+ expect(Version.new('2.3.3')).to be > Version.new('2.3.1')
+ expect(Version.new('0.0')).not_to be > Version.new('0.0.4')
+ expect(Version.new('0.1')).to be > Version.new('')
end
- it 'uses >= properly' do
- version1 = Version.new('1.3.3')
- version2 = Version.new('1.3.3')
- expect(Version.new('1.3.3') >= Version.new('1.3')).to be true
- expect( version1 >= version2).to be true
- expect(Version.new('1.3.3') >= Version.new('1.3.2')).to be true
- expect(Version.new('1.3.3') >= Version.new('1.3.3.0')).to be true
- expect(Version.new('1.2.9') >= Version.new('1.3.3')).to be false
- expect(Version.new('1.2.9') >= Version.new('1.2.9.1')).to be false
- expect(Version.new('1.2.9') >= Version.new('1.3')).to be false
- expect(Version.new('') >= Version.new('0.0.4')).to be false
- expect(Version.new('0.1') >= Version.new('')).to be true
+ it 'uses >= properly' do
+ expect(Version.new('1.3.3')).to be >= Version.new('1.3')
+ expect(Version.new('1.3.3')).to be >= Version.new('1.3.3')
+ expect(Version.new('1.3.3')).to be >= Version.new('1.3.2')
+ expect(Version.new('1.3.3')).to be >= Version.new('1.3.3.0')
+ expect(Version.new('1.2.9')).not_to be >= Version.new('1.3.3')
+ expect(Version.new('1.2.9')).not_to be >= Version.new('1.2.9.1')
+ expect(Version.new('1.2.9')).not_to be >= Version.new('1.3')
+ expect(Version.new('')).not_to be >= Version.new('0.0.4')
+ expect(Version.new('0.1')).to be >= Version.new('')
end
it 'uses <= properly' do
- version1 = Version.new('1.3.3')
- version2 = Version.new('1.3.3')
-
- expect(Version.new('1.3.3') <= Version.new('1.3')).to be false
- expect( version1 <= version2 ).to be true
- expect(Version.new('1.3.3') <= Version.new('1.3.2')).to be false
- expect(Version.new('1.3.3') <= Version.new('1.3.3.0')).to be true
- expect(Version.new('1.2.9') <= Version.new('1.3.3')).to be true
- expect(Version.new('1.2.9') <= Version.new('1.2.9.1')).to be true
- expect(Version.new('1.2.9') <= Version.new('1.3')).to be true
- expect(Version.new('1.4') <= Version.new('1.3.9')).to be false
- expect(Version.new('') <= Version.new('0.0.4')).to be true
- expect(Version.new('0.1') <= Version.new('')).to be false
+ expect(Version.new('1.3.3')).not_to be <= Version.new('1.3')
+ expect(Version.new('1.3.3')).to be <= Version.new('1.3.3')
+ expect(Version.new('1.3.3')).not_to be <= Version.new('1.3.2')
+ expect(Version.new('1.3.3')).to be <= Version.new('1.3.3.0')
+ expect(Version.new('1.2.9')).to be <= Version.new('1.3.3')
+ expect(Version.new('1.2.9')).to be <= Version.new('1.2.9.1')
+ expect(Version.new('1.2.9')).to be <= Version.new('1.3')
+ expect(Version.new('1.4')).not_to be <= Version.new('1.3.9')
+ expect(Version.new('')).to be <= Version.new('0.0.4')
+ expect(Version.new('0.1')).not_to be <= Version.new('')
end
it 'uses == properly' do
version1 = Version.new('1.3.3')
version2 = Version.new('1.3.3')
expect(version1 == version2).to be true
expect(Version.new('1.3.3.0.0') == Version.new('1.3.3')).to be true
expect(Version.new('1.3.3') == Version.new('1.3.3.0')).to be true
expect(Version.new('1.3.3') == Version.new('1.3')).to be false
expect(Version.new('1.4') == Version.new('1.4.1')).to be false
expect(Version.new('1.3.3.2') == Version.new('1.3.3.1')).to be false
expect(Version.new('') == Version.new('0.0')).to be true
end
it 'uses <=> properly' do
expect(Version.new('1.3.3') <=> Version.new('1.3')).to be 1
expect(Version.new <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('')).to be 0
expect(Version.new('0') <=> Version.new('1.3')).to be -1
expect(Version.new('0.1') <=> Version.new).to be 1
expect(Version.new('') <=> Version.new('1.3')).to be -1
expect(Version.new('1.3.3.0.0') <=> Version.new('1.3.3')).to be 0
expect(Version.new('1.3.3.9') <=> Version.new('1.3.4')).to be -1
end
end
describe '#to_s' do
it 'converts into string when initialize with string' do
expect(Version.new('').to_s).to eq ''
expect(Version.new.to_s).to eq ''
expect(Version.new('0.0').to_s).to eq ''
expect(Version.new('1.1').to_s).to eq '1.1'
expect(Version.new('1.2.0').to_s).to eq '1.2'
end
+
it 'converts into string when initialized with Version' do
expect(Version.new(Version.new('')).to_s).to eq ''
expect(Version.new(Version.new).to_s).to eq ''
expect(Version.new(Version.new('0.0.0')).to_s).to eq ''
expect(Version.new(Version.new('3.4.0.0.0.0')).to_s).to eq '3.4'
expect(Version.new(Version.new('0.2')).to_s).to eq '0.2'
end
end
describe '#components' do
- it 'works correctly without argument when initialized with string' do
+ it 'works correctly without argument' do
expect(Version.new('').components).to eq []
expect(Version.new('0').components).to eq []
expect(Version.new.components).to eq []
expect(Version.new('1.3.4').components).to eq [1, 3, 4]
expect(Version.new('1.2.0.0').components).to eq [1, 2]
end
- it 'works correctly with argument when initialized with string' do
+
+ it 'works correctly with argument' do
expect(Version.new('').components(2)).to eq [0, 0]
expect(Version.new('1.3').components(2)).to eq [1, 3]
expect(Version.new('1.3').components(4)).to eq [1, 3, 0, 0]
expect(Version.new('1.4.4.3').components(3)).to eq [1, 4, 4]
end
- it 'works correctly without argument when initialized with Version' do
- expect(Version.new(Version.new('')).components).to eq []
- expect(Version.new(Version.new('0')).components).to eq []
- expect(Version.new(Version.new).components).to eq []
- expect(Version.new(Version.new('1.3.4')).components).to eq [1, 3, 4]
- expect(Version.new(Version.new('1.2.0.0')).components).to eq [1, 2]
- end
- it 'works correctly with argument when initialized with Version' do
- expect(Version.new(Version.new('')).components(2)).to eq [0, 0]
- expect(Version.new(Version.new('1.3')).components(2)).to eq [1, 3]
- expect(Version.new(Version.new('1.3')).components(4)).to eq [1, 3, 0, 0]
- expect(Version.new(Version.new('1.4.4.3')).components(3)).to eq [1, 4, 4]
- end
end
describe 'Version::Range' do
describe '#include?' do
it 'checks if a version is in range when initialized with strings' do
- expect(Version::Range.new('1.2', '1.3').include?('1.2.5')).to be true
- expect(Version::Range.new('1.2', '1.3').include?('1.1.9')).to be false
- expect(Version::Range.new('1.2', '1.3').include?('1.3.1')).to be false
+ expect(Version::Range.new('1.2', '1.3')).to include('1.2.5')
+ expect(Version::Range.new('1.2', '1.3')).not_to include('1.1.9')
+ expect(Version::Range.new('1.2', '1.3')).not_to include('1.3.1')
end
+
it 'checks if a version is in range when initialized with Versions' do
version = Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
- expect(version.include?( Version.new('1.2.3.8'))).to be true
+ expect(version).to include( Version.new('1.2.3.8'))
version2 = Version::Range.new(Version.new('1'), Version.new('2'))
- expect(version2.include?(Version.new('1.5'))).to be true
+ expect(version2).to include(Version.new('1.5'))
version3 = Version::Range.new('0.1.2.3', '0.1.2.4')
- expect(version3.include?('0.1.2.3.0.1')).to eq true
+ expect(version3).to include('0.1.2.3.0.1')
end
end
describe '#to_a' do
it 'generates all versions in a range' do
- 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('1.2', '1.3').to_a)
- .to eq [
- '1.2', '1.2.1', '1.2.2', '1.2.3', '1.2.4', '1.2.5',
- '1.2.6', '1.2.7', '1.2.8', '1.2.9'
+ version = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
+ expect(version.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('1.2', '1.3').to_a).to eq [
+ '1.2',
+ '1.2.1',
+ '1.2.2',
+ '1.2.3',
+ '1.2.4',
+ '1.2.5',
+ '1.2.6',
+ '1.2.7',
+ '1.2.8',
+ '1.2.9'
]
expect(Version::Range.new('1.4', '1.4.2').to_a).to eq ['1.4', '1.4.1']
expect(Version::Range.new('1.4', '1.3.9').to_a).to eq []
expect(Version::Range.new('1.4', '1.4.0').to_a).to eq []
end
end
end
end