Никола обнови решението на 17.11.2016 15:17 (преди над 8 години)
+require 'rspec'
+require_relative 'version'
+
+RSpec.describe Version do
+ it 'creates valid versions' do
+ expect { Version.new }.not_to raise_error
+ expect { Version.new('1.2.3.4.5.6.7.8') }.not_to raise_error
+ expect { Version.new('1.1.5') }.not_to raise_error
+ expect { Version.new('1.13.3') }.not_to raise_error
+ end
+
+ it 'doesnt create valid versions' do
+ expect { Version.new('.3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '.3'"
+ )
+ expect { Version.new('0..3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '0..3'"
+ )
+ expect { Version.new('123213213..') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '123213213..'"
+ )
+ end
+
+ it "parses components" do
+ v = Version.new("1.1.1")
+
+ expect(v.components).to eq([1, 1, 1])
+ end
+
+ it "parse components with argument" do
+ v = Version.new('1.2.3')
+
+ expect(v.components(1)).to eq([1])
+ expect(v.components(2)).to eq([1, 2])
+ expect(v.components(4)).to eq([1, 2, 3, 0])
+ end
+
+ context 'comparing versions' do
+ let(:v1) { Version.new('1.2.3.0.1') }
+ let(:v2) { Version.new('1.3.1') }
+ let(:v3) { Version.new('1.3.1.0') }
+ let(:v4) { Version.new('1.3.1.1.2') }
+
+ it "can compare with #<=>" do
+ expect( v1 <=> v2 ).to eq -1
+ expect( v2 <=> v3 ).to eq 0
+ expect( v4 <=> v2 ).to eq 1
+ end
+
+ it "can compare with #==" do
+ expect( v2 == v3 ).to be true
+ expect( v2 == v4 ).to be false
+ end
+
+ it 'can compare with #>' do
+ expect(v2 > v1).to be true
+ expect(v3 > v4).to be false
+ end
+
+ it 'can compare with #<' do
+ expect(v1 < v2).to be true
+ expect(v2 < v3).to be false
+ end
+
+ it 'can compare with #>=' do
+ expect(v2 >= v3).to be true
+ expect(v2 >= v4).to be false
+ end
+
+ it 'can compare with #<=' do
+ expect(v2 <= v3).to be true
+ expect(v4 <= v2).to be false
+ end
+ end
+
+ describe "#to_s" do
+ it "returns the version as string" do
+ v = Version.new('1.1.1.0')
+
+ expect(v.to_s).to eq '1.1.1'
+ end
+ end
+end
+
+describe Version::Range do
+ describe '#include?' do
+ it 'checks if a range includes a version' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect(range).to include(Version.new('1.5.4.3.2.3.4.5.1.0'))
+ expect(range).to include('1.2.3.4.1')
+ expect(range).to_not include('0.9.9.9.9.9.9.9')
+ expect(range).to_not include(Version.new('2.0.0.1'))
+ end
+ end
+
+ describe '#to_a' do
+ it 'returns all versions in a range' do
+ 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'
+ ]
+ end
+
+ it 'works only with 3 components' do
+ range = Version::Range.new(Version.new('1.1'), Version.new('1.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'
+ ]
+ end
+
+ it 'includes first version' do
+ range = Version::Range.new(Version.new('0.0.1'), Version.new('3')).to_a
+
+ expect(range.to_a).to include('0.0.1')
+ end
+
+ it 'doesnt include last version' do
+ range = Version::Range.new(Version.new('0.1'), Version.new('3')).to_a
+
+ expect(range.to_a).not_to include('3')
+ end
+ end
+end