Елеонора обнови решението на 17.11.2016 18:10 (преди над 8 години)
+RSpec.describe 'Version' do
+ describe 'Valid_Version' do
Това може да го кръстиш describe 'validations'
:)
+ it 'can convert to 0' do
+ expect(Version.new("")).to eq 0
+ expect(Version.new).to eq 0
+ end
+
+ it 'can raise ArgumentError for Invalid Input' do
+ expect { Version.new('1..3') }
+ .to raise_error ArgumentError, "Invalid version string '1..3'"
Аз бих го подредил така:
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
+ 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 'can create version' do
Този тест ти е излишен, защото проверяваш същото долу при ==
. Освен това разчиташ на имплементационен детайл като сравняваш версия със стринг.
+ expect(Version.new('1.22.3')).to eq '1.22.3'
+ expect(Version.new('1.2.0')).to eq '1.2.0'
+ end
+ end
+
+ describe 'compare versions' do
+ it 'can check for <' do
+ expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
+ expect(Version.new('1.2.3')).to be < Version.new('1.3')
+ expect(Version.new('1.3.1')).not_to be < Version.new('1.3')
+ end
+
+ it 'can check for >' do
+ expect(Version.new('1.2.3')).not_to be > Version.new('1.3.1')
+ expect(Version.new('1.2.3')).not_to be > Version.new('1.3')
+ expect(Version.new('1.3.1')).to be > Version.new('1.3')
+ end
+
+ it 'can check for <=' do
+ expect(Version.new('1.2.3')).to be <= Version.new('1.3.1')
+ expect(Version.new('1.2.3')).to be <= Version.new('1.3')
+ expect(Version.new('1.3.1')).not_to be <= Version.new('1.3')
+ expect(Version.new('1.3')).to be <= Version.new('1.3.0')
+ expect(Version.new('1.22')).not_to be <= Version.new('1.2.2')
+ end
+
+ it 'can check for >=' do
+ expect(Version.new('1.2.3')).not_to be >= Version.new('1.3.1')
+ expect(Version.new('1.2.3')).not_to be >= Version.new('1.3')
+ expect(Version.new('1.3.1')).to be >= Version.new('1.3')
+ expect(Version.new('1.3')).to be >= Version.new('1.3.0')
+ expect(Version.new('1.22')).to be >= Version.new('1.2.2')
+ end
+
+ it 'can check for ==' do
+ expect(Version.new('1.3.1')).not_to eq Version.new('1.3')
+ expect(Version.new('1.3.0')).to eq Version.new('1.3')
+ expect(Version.new('1.3')).to eq Version.new('1.3.0.0')
+ expect(Version.new('1.22')).not_to eq Version.new('1.2.2')
+ expect(Version.new('0.1')).not_to eq Version.new('1')
+ end
+
+ it 'can check for <=>' do
+ expect(Version.new('1.3.1') <=> Version.new('1.3')).to eq 1
+ expect(Version.new('1.3.0') <=> Version.new('1.3')).to eq 0
+ expect(Version.new('1.3') <=> Version.new('1.3.0.0')).to eq 0
+ # expect(Version.new('1.3') <=> Version.new('1.3')).to eq 0
+ expect(Version.new('1.22') <=> Version.new('1.2.2')).to eq 1
+ expect(Version.new('1.2') <=> Version.new('1.2.2')).to eq -1
+ end
+ end
+
+ describe '#to_s' do
+ it 'can convert to string' do
+ expect(Version.new('1.2.3').to_s).to eq '1.2.3'
+ end
+
+ it 'can remove the zeroes' do
+ expect(Version.new('1.1.0.0').to_s).to eq '1.1'
+ expect(Version.new('1.1.0.1.0').to_s).to eq '1.1.0.1'
+ # expect(Version.new('0').to_s).to eq ''
+ # expect(Version.new.to_s).to eq ''
+ end
+ end
+
+ describe '#components' do
+ it 'can convert to an array simple stirngs' do
+ expect(Version.new('1.3.5').components).to match_array [1, 3, 5]
Каква е разликата между match_array
и eq
?
+ expect(Version.new('1.3.0.5').components).to match_array [1, 3, 5, 0]
+ expect(Version.new('1').components).to match_array [1]
+ expect(Version.new('1.22.2').components).to match_array [1, 2, 22]
+ end
+
+ it 'can convert and add zeroes' do
+ expect(Version.new('1.3.5').components(4)).to match_array [1, 3, 5, 0]
+ expect(Version.new('1.3.5').components(5)).to match_array [1, 3, 5, 0, 0]
+ end
+
+ it 'can convert exactly N components' do
+ expect(Version.new('1.3.5').components(1)).to match_array [1]
+ expect(Version.new('1.3.5').components(2)).to match_array [1, 3]
+ expect(Version.new('1.3.5').components(3)).to match_array [1, 3, 5]
+ end
+
+ it 'does not change the version' do
+ a = Version.new('1.3.5')
+
+ expect(a).not_to be a.components(3)
Този тест никога няма да фейлне. Сравняваш версия с масив и очакваш да са различни. Да, очевидно - винаги ще са различни.
Махни .dup
от имплементацията на components
и помисли как може чрез него да се промени вътрешното състояние на версията.
+ end
+ end
+
+ describe '#Range' do
+ it 'can create range from both version and string' do
+ range = Version::Range.new('1', Version.new('1.6'))
+ range_2 = Version::Range.new(Version.new('1'), Version.new('1.6'))
+ range_3 = Version::Range.new('1', '1.6')
+
+ expect(range).to match_array range_2
+ expect(range).to match_array range_3
+ expect(range_2).to match_array range_3
expect(<RANGE>).to match_array <RANGE>
Какъв е смисълът на този код? Как работи той?
+ end
+
+ describe 'Range#include?' do
+ it 'can check for versions with versions' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect(range).to include(Version.new('1.5'))
+ expect(range).not_to include(Version.new('2.5'))
+
+ range = Version::Range.new(Version.new('1'), Version.new('1.6'))
+ expect(range).to include(Version.new('1.5'))
+ expect(range).not_to include(Version.new('1.6'))
+ end
+
+ it 'can check for strings with strings' do
+ expect(Version::Range.new('1', '2')).to include('1.5')
+ expect(Version::Range.new('1', '2')).not_to include('2.5')
+ expect(Version::Range.new('1', '1.6')).to include('1.5')
+ expect(Version::Range.new('1', '1.5')).not_to include('1.5')
+ end
+
+ it 'can check for strings with versions' do
+ expect(Version::Range.new('1', '2')).to include(Version.new('1.5'))
+ expect(Version::Range.new('1', '2')).not_to include(Version.new('2.5'))
+ expect(Version::Range.new('1', '1.6')).to include(Version.new('1.5'))
+ expect(Version::Range.new('1', '1.5')).not_to include Version.new('1.5')
+ end
+
+ it 'can check for versions with strings' do
+ range = Version::Range.new(Version.new('1'), Version.new('2'))
+
+ expect(range).to include('1.5')
+ expect(range).not_to include('2.5')
+
+ range = Version::Range.new(Version.new('1'), Version.new('1.6'))
+
+ expect(range).to include('1.5')
+ expect(range).not_to include('1.6')
+ end
+ end
+
+ describe 'Range#to_a' do
+ it 'can generate range' do
+ expect(Version::Range.new('1.1', '1.1').to_a).to match_array []
+
+ range = Version::Range.new(Version.new('1.1.0'), Version.new('1.1.2'))
+ expect(range.to_a)
+ .to match_array [Version.new('1.1'), Version.new('1.1.1')]
+
+ expect(Version::Range.new(Version.new('1.1'), Version.new('1.2')).to_a)
+ .to match_array [
+ Version.new('1.1'), Version.new('1.1.1'),
+ Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
+ Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
+ Version.new('1.1.8'), Version.new('1.1.9')
+ ]
+
+ expect(Version::Range.new('1', '1.2'))
+ .to match_array [
+ Version.new('1'), Version.new('1.0.1'),
+ Version.new('1.0.2'), Version.new('1.0.3'), Version.new('1.0.4'),
+ Version.new('1.0.5'), Version.new('1.0.6'), Version.new('1.0.7'),
+ Version.new('1.0.8'), Version.new('1.0.9'), Version.new('1.1'),
+ Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
+ Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
+ Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9')
+ ]
+ end
+ end
+ end
+end