Христо обнови решението на 18.11.2016 21:39 (преди над 8 години)
+RSpec.describe 'Version' do
+ describe "validate version" do
+ def get_msg(version)
+ "Invalid version string '#{version}'"
+ end
+
+ it 'from String' do
+ expect { Version.new }.to_not raise_error
+ expect { Version.new('') }.to_not raise_error
+ expect { Version.new('1') }.to_not raise_error
+ expect { Version.new('1.0') }.to_not raise_error
+ expect { Version.new('1.00.1') }.to_not raise_error
+ expect { Version.new('0.0.9') }.to_not raise_error
+
+ v = '..1'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '.1'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '1.'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '11.0.'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '1..0'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+ end
+
+ it 'from another version' do
+ v = Version.new('')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1.0')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1.00.1')
+ expect(Version.new(v)).to be == v
+ v = Version.new('0.0.9')
+ expect(Version.new(v)).to be == v
+ end
+ end
+
+ describe 'compare ' do
+ it '> correctly' do
+ test_bigger
+ end
+
+ it '>= correctly' do
+ test_bigger
+ test_equal
+ end
+
+ it '< correctly' do
+ test_smaller
+ end
+
+ it '<= correctly' do
+ test_smaller
+ test_equal
+ end
+
+ it '== correctly' do
+ test_equal
+ end
+
+ it '<=> correctly' do
+ test_single_compare Version.new, Version.new('0'), 0
+ test_single_compare Version.new(''), Version.new('0'), 0
+ test_single_compare Version.new('1.5'), Version.new('1.5.0.0.0'), 0
+
+ test_single_compare Version.new('1.1.2'), Version.new('1.1.1'), 1
+ test_single_compare Version.new('1.2.2'), Version.new('1.1.5'), 1
+ test_single_compare Version.new('1.1.2'), Version.new('1.1'), 1
+ test_single_compare Version.new('1.1.2'), Version.new('1.1.0.0'), 1
+
+ test_single_compare Version.new('1.1.1'), Version.new('1.1.2'), -1
+ test_single_compare Version.new('1.1.5'), Version.new('1.2.2'), -1
+ test_single_compare Version.new('1.1'), Version.new('1.1.2'), -1
+ test_single_compare Version.new('1.1.0.0'), Version.new('1.1.2'), -1
+ end
+
+ def test_bigger
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1.1')
+
+ test_single_bigger Version.new('1.2.2'), Version.new('1.1.5')
+
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1')
+
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1.0.0')
+ end
+
+ def test_smaller
+ test_single_smaller Version.new('1.1.1'), Version.new('1.1.2')
+
+ test_single_smaller Version.new('1.1.5'), Version.new('1.2.2')
+
+ test_single_smaller Version.new('1.1'), Version.new('1.1.2')
+
+ test_single_smaller Version.new('1.1.0.0'), Version.new('1.1.2')
+ end
+
+ def test_equal
+ test_single_equal Version.new, Version.new('0')
+
+ test_single_equal Version.new(''), Version.new('0')
+
+ test_single_equal Version.new('1.5'), Version.new('1.5.0.0.0')
+ end
+
+ def test_single_bigger(left_value, right_value)
+ expect(left_value).to be > right_value
+ expect(right_value).to_not be > left_value
+ end
+
+ def test_single_smaller(left_value, right_value)
+ expect(left_value).to be < right_value
+ expect(right_value).to_not be < left_value
+ end
+
+ def test_single_equal(left_value, right_value)
+ expect(left_value).to be == right_value
+ end
+
+ def test_single_compare(left_value, right_value, expected_result)
+ expect(left_value <=> right_value).to be expected_result
+ end
+ end
+
+ describe 'to_s method' do
+ it 'erase zeros' do
+ expect(Version.new('1.0.0.0').to_s).to eq '1'
+
+ expect(Version.new.to_s).to eq ''
+ end
+
+ it 'does not erase zeros in the middle' do
+ expect(Version.new('1.0.5').to_s).to eq '1.0.5'
+ end
+
+ it 'does not erase zeros in the beginning' do
+ expect(Version.new('0.0.0.5').to_s).to eq '0.0.0.5'
+ end
+ end
+
+ describe 'components method' do
+ it 'does not return the last zeros' do
+ expect(Version.new('1.0.0.0.0').components).to eq [1]
+
+ expect(Version.new.components).to eq []
+ end
+
+ it 'does not erase zeros in the middle or beginning' do
+ expect(Version.new('0.0.1.0.0.5').components).to eq [0, 0, 1, 0, 0, 5]
+ end
+
+ it 'does return array with length, same as required' do
+ n = 5
+
+ expect(Version.new('1').components(n).length).to be n
+
+ expect(Version.new('1.1.2.2.3').components(n).length).to be n
+
+ expect(Version.new('1.1.2.2.3.3').components(n).length).to be n
+ end
+
+ it 'does fill the array with zeros if N is bigger than the length' do
+ expect(Version.new('1').components(5)).to eq [1, 0, 0, 0, 0]
+
+ expect(Version.new.components(5)).to eq [0, 0, 0, 0, 0]
+ end
+
+ it 'does remove subversions if N is smaller than the length' do
+ expect(Version.new('1.2.3.4.5.6').components(5)).to eq [1, 2, 3, 4, 5]
+ end
+
+ it 'does return all the components if N is equal to the length' do
+ expect(Version.new('1.2.3').components(3)).to eq [1, 2, 3]
+ end
+
+ it 'does not allow modify the the state of version' do
+ version = Version.new('1.2.3')
+ version.components.reverse!
+
+ expect(version.components). to eq [1, 2, 3]
+ end
+
+ it 'does not allow modify the the state of version with bigger N' do
+ version = Version.new('1.2.3')
+ version.components(5).reverse!
+
+ expect(version.components(5)). to eq [1, 2, 3, 0, 0]
+ end
+
+ it 'does not allow modify the the state of version with smaller N' do
+ version = Version.new('1.2.3')
+ version.components(2).reverse!
+
+ expect(version.components(2)). to eq [1, 2]
+ end
+ end
+
+ describe 'Range' do
+ describe 'does initialize' do
+ it 'with Version instances' do
+ from_v = Version.new
+ to_v = Version.new('1.5')
+ expect { Version::Range.new from_v, to_v }.to_not raise_error
+ end
+ it 'with Strings' do
+ expect { Version::Range.new '', '1.5' }.to_not raise_error
+ end
+ end
+
+ from_version = Version.new('0.5')
+ to_version = Version.new('1.5')
+ range = Version::Range.new from_version, to_version
+
+ describe 'include?' do
+ describe 'works with Version' do
+ it 'return true if the version is in the range' do
+ expect(range.include?(Version.new('1'))).to be true
+ end
+
+ it 'return true if the version is equal the min range' do
+ expect(range.include?(Version.new('0.5'))).to be true
+ end
+
+ it 'return false if the version is equal the max range' do
+ expect(range.include?(Version.new('1.5'))).to be false
+ end
+
+ it 'return false if the version is bigger the max range' do
+ expect(range.include?(Version.new('1.5.0.0.1'))).to be false
+ end
+
+ it 'return false if the version is smaller the min range' do
+ expect(range.include?(Version.new('0.4'))).to be false
+ end
+ end
+
+ describe 'works with String' do
+ it 'return true if the version is in the range' do
+ expect(range.include?('1')).to be true
+ end
+
+ it 'return true if the version is equal the min range' do
+ expect(range.include?('0.5')).to be true
+ end
+
+ it 'return false if the version is equal the max range' do
+ expect(range.include?('1.5')).to be false
+ end
+
+ it 'return false if the version is bigger the max range' do
+ expect(range.include?('1.5.0.0.1')).to be false
+ end
+
+ it 'return false if the version is smaller the min range' do
+ expect(range.include?('0.4')).to be false
+ end
+ end
+ end
+
+ describe 'to_a' do
+ it 'does return the min version' do
+ expect(range.to_a.include?('0.5')).to be true
+ end
+
+ it 'does not return the max version' do
+ expect(range.to_a.include?('1.5')).to be false
+ end
+
+ it 'does not return versions smaller than the min' do
+ smaller = range.to_a.map { |i| Version.new(i) >= from_version }
+ expect(smaller.all?).to be true
+ end
+
+ it 'does not return version bigger or equal then the max' do
+ bigger = range.to_a.map { |i| Version.new(i) < to_version }
+ expect(bigger.all?).to be true
+ end
+
+ it 'does not contain equal versions' do
+ arr = range.to_a
+ expect(arr.uniq).to match_array arr
+ end
+
+ it 'does contain the right number of elements' do
+ expect(range.to_a.count).to be 100
+ end
+ end
+ end
+end