Даниела обнови решението на 18.11.2016 23:06 (преди над 8 години)
+RSpec.describe 'Version' do
+ describe "initialization" do
+ context "with no argument" do
+ it "initializes correctly with empty string" do
+ expect(Version.new.to_s).to eq ''
+ end
+ end
+
+ context "with valid argument" do
+ it "initializes correctly" do
+ expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
+ expect(Version.new('2.12.7').to_s).to eq '2.12.7'
+ expect(Version.new('3.5.0.0').to_s).to eq '3.5'
+ expect(Version.new('0.8').to_s).to eq '0.8'
+ expect(Version.new('0.7.0').to_s).to eq '0.7'
+ end
+ end
+
+ context "with invalid argument" do
+ it "raises error for invalid version" do
+ expect { Version.new('.2') }
+ .to raise_error(ArgumentError, 'Invalid version string \'.2\'')
+ expect { Version.new('1..2') }
+ .to raise_error(ArgumentError, 'Invalid version string \'1..2\'')
+ expect { Version.new('2.8.7.') }
+ .to raise_error(ArgumentError, 'Invalid version string \'2.8.7.\'')
+ end
+ end
+ end
+
+ describe 'comparison' do
+ it 'can compare two versions' do
+ expect(Version.new('1.1.0') == Version.new('1.1')).to be true
+ expect(Version.new('1.1.0') == Version.new('1.5')).to be false
+ expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
+ expect(Version.new('1.7') <=> Version.new('1.1.5')).to eq 1
+ expect(Version.new('1.1.0') <=> Version.new('1.2')).to eq -1
+ expect(Version.new('1.2.3') < Version.new('1.3')).to be true
+ expect(Version.new('1.8') < Version.new('1.3.1')).to be false
+ expect(Version.new('1.3') > Version.new('1.2.3')).to be true
+ expect(Version.new('1.2.1') > Version.new('1.2.3')).to be false
+ expect(Version.new('2.8.0') <= Version.new('2.8')).to be true
+ expect(Version.new('2.8') <= Version.new('2.8.2')).to be true
+ expect(Version.new('4.1.2') <= Version.new('2.8')).to be false
+ expect(Version.new('1.4.0') >= Version.new('1.4')).to be true
+ expect(Version.new('1.4.7') >= Version.new('1.4')).to be true
+ expect(Version.new('0.1') >= Version.new('1.4')).to be false
+ end
+ end
+
+ describe '#to_string' do
+ it 'ignores the zeros at the end' do
+ expect(Version.new('1.2.4.0').to_s).to eq '1.2.4'
+ expect(Version.new('0.7.0.0').to_s).to eq '0.7'
+ expect(Version.new('1.2.3').to_s).to eq '1.2.3'
+ end
+ end
+
+ describe '#components' do
+ let(:version) { Version.new('5.7.2') }
+
+ it 'doesn\'t mutate the object' do
+ id = version.object_id
+ expect(version.components.object_id).to_not eq id
+ expect(version.components(2).object_id).to_not eq id
+ end
+
+ context 'without argument' do
+ it 'returns a correct array' do
+ expect(Version.new('1.2.4.0').components).to match_array [1, 2, 4]
+ expect(Version.new('0.7.0.0').components).to match_array [0, 7]
+ expect(Version.new('1.2.3').components).to match_array [1, 2, 3]
+ end
+ end
+
+ context 'with argument' do
+ it 'returns a correct array' do
+ expect(Version.new('5.7.2.8.1.8.0').components(4))
+ .to match_array [5, 7, 2, 8]
+ expect(Version.new('5.7').components(4))
+ .to match_array [5, 7, 0, 0]
+ expect(Version.new('1.2.3').components(3))
+ .to match_array [1, 2, 3]
+ end
+ end
+ end
+
+ describe 'Version::Range' do
+ let(:range) do
+ Version::Range.new(Version.new('1.2.0'), Version.new('2.3.1'))
+ end
+
+ describe '#include?' do
+ it 'knows if a given version is within a range' do
+ expect(range).to include Version.new('1.2.0')
+ expect(range).to include Version.new('1.2.1')
+ expect(range).to include Version.new('1.23.1')
+ expect(range).to_not include Version.new('2.3.1')
+ expect(range).to_not include Version.new('7.2.1')
+ end
+ end
+
+ describe '#to_a' do
+ let(:range1) do
+ Version::Range.new(Version.new('1.4.0'), Version.new('1.5.2'))
+ end
+ let(:range2) { Version::Range.new(Version.new('2'), Version.new('2.1')) }
+ let(:range3) do
+ Version::Range.new(Version.new('2.4'), Version.new('2.4'))
+ end
+
+ it 'can generate an array of all version within a range' do
+ expect(range1.to_a).to eq [
+ '1.4', '1.4.1', '1.4.2', '1.4.3', '1.4.4',
+ '1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9', '1.5', '1.5.1'
+ ]
+ expect(range2.to_a).to eq [
+ '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(range3.to_a).to eq []
+ end
+ end
+ end
+end