Теодора обнови решението на 18.11.2016 23:20 (преди над 8 години)
+describe Version do
+ describe '#initialize' do
+ def get_message(invalid_version)
+ 'Invalid version string \'%s\'' % invalid_version
+ end
+
+ it 'initializes with a string' do
+ expect(Version.new('1.1.5')).to eq('1.1.5')
+ end
+
+ it 'initializes with an empty string as 0' do
+ expect(Version.new('')).to eq('0')
+ end
+
+ it 'initializes with white spaces \' \'' do
+ expect { Version.new(' ') }
+ .to raise_error(ArgumentError, get_message(' '))
+ end
+
+ it 'initializes with no parameter as 0' do
+ expect(Version.new).to eq('0')
+ end
+
+ it 'raises an error with an incorrect string \'a.b.c\'' do
+ expect { Version.new('a.b.c') }
+ .to raise_error(ArgumentError, get_message('a.b.c'))
+ end
+
+ it 'raises an error with an incorrect string \'.6\'' do
+ expect { Version.new('.6') }
+ .to raise_error(ArgumentError, get_message('.6'))
+ end
+
+ it 'raises an error with an incorrect string \'0..6\'' do
+ expect { Version.new('0..6') }
+ .to raise_error(ArgumentError, get_message('0..6'))
+ end
+ end
+
+ let(:versions_first_bigger_than_second) do
+ [
+ [Version.new('1.3.1'), Version.new('1.2.3')],
+ [Version.new('1.0.2'), Version.new('1.0.1')],
+ [Version.new('1.3'), Version.new('1.2.3')],
+ [Version.new('1'), Version.new('0.1')]
+ ]
+ end
+
+ let(:equal_versions) do
+ [
+ [Version.new('1.1.1'), Version.new('1.1.1')],
+ [Version.new('1.1.0'), Version.new('1.1')],
+ [Version.new('0.1'), Version.new('0.1.0')],
+ ]
+ end
+
+ describe '#>' do
+ it 'a > b -> true' do
+ are_all_bigger = versions_first_bigger_than_second
+ .map { |v| v[0] > v[1] }.all?
+ expect(are_all_bigger).to eq(true)
+ end
+ it 'a > b -> false' do
+ are_all_not_bigger = versions_first_bigger_than_second
+ .map { |v| v[1] > v[0] }.all?
+ expect(are_all_not_bigger).to eq(false)
+ end
+ end
+
+ describe '#<' do
+ it 'a < b -> true' do
+ are_all_smaller = versions_first_bigger_than_second
+ .map { |v| v[1] < v[0] }.all?
+ expect(are_all_smaller).to eq(true)
+ end
+ it 'a < b -> false' do
+ are_all_not_smaller = versions_first_bigger_than_second
+ .map { |v| v[0] < v[1] }.all?
+ expect(are_all_not_smaller).to eq(false)
+ end
+ end
+
+ describe '#==' do
+ it 'a == b -> true' do
+ are_all_equal = equal_versions.map { |v| v[0] == v[1] }.all?
+ expect(are_all_equal).to eq(true)
+ end
+ it 'a == b -> false' do
+ are_all_not_equal = versions_first_bigger_than_second
+ .map { |v| v[0] == v[1] }.all?
+ expect(are_all_not_equal).to eq(false)
+ end
+ end
+
+ describe '#>=' do
+ it 'a >= b -> true' do
+ are_all_bigger_or_equal = versions_first_bigger_than_second
+ .concat(equal_versions).map { |v| v[0] >= v[1] }.all?
+ expect(are_all_bigger_or_equal).to eq(true)
+ end
+ it 'a >= b -> false' do
+ are_all_not_bigger_or_equal = versions_first_bigger_than_second
+ .map { |v| v[1] >= v[0] }.all?
+ expect(are_all_not_bigger_or_equal).to eq(false)
+ end
+ end
+
+ describe '#<=' do
+ it 'a <= b -> true' do
+ are_all_smaller_or_equal = versions_first_bigger_than_second
+ .concat(equal_versions).map { |v| v[1] <= v[0] }.all?
+ expect(are_all_smaller_or_equal).to eq(true)
+ end
+ it 'a <= b -> false' do
+ are_all_not_smaller_or_equal = versions_first_bigger_than_second
+ .map { |v| v[0] <= v[1] }.all?
+ expect(are_all_not_smaller_or_equal).to eq(false)
+ end
+ end
+
+ describe '#<=>' do
+ it 'a <=> b -> 0' do
+ are_all_equal = equal_versions
+ .map { |v| v[1] <=> v[0] }.all? { |r| r == 0 }
+ expect(are_all_equal).to eq(true)
+ end
+ it 'a <=> b -> 1' do
+ are_all_greater = versions_first_bigger_than_second
+ .map { |v| v[0] <=> v[1] }.all? { |r| r == 1 }
+ expect(are_all_greater).to eq(true)
+ end
+ it 'a <=> b -> -1' do
+ are_all_smaller = versions_first_bigger_than_second
+ .map { |v| v[1] <=> v[0] }.all? { |r| r == -1 }
+ expect(are_all_smaller).to eq(true)
+ end
+ end
+
+ describe '#to_s' do
+ it 'returns \'\' for new version' do
+ expect(Version.new.to_s).to eq('')
+ end
+ it 'returns \'\' for empty version' do
+ expect(Version.new('').to_s).to eq('')
+ end
+ it 'returns the version as a string' do
+ expect(Version.new('1.1.1.2.3.4').to_s).to eq('1.1.1.2.3.4')
+ end
+ it 'returns the version as a string without trailing zeros' do
+ expect(Version.new('1.1.0.0.0').to_s).to eq('1.1')
+ end
+ it 'returns the version as a string with inner zeros' do
+ expect(Version.new('0.0.1.0.0').to_s).to eq('0.0.1')
+ end
+ end
+
+ describe '#components' do
+ it 'returns all the components of the version' do
+ expect(Version.new('1.3.5').components).to eq([1, 3, 5])
+ end
+ it 'returns the components without trailing zeros' do
+ expect(Version.new('1.3.5.0').components).to eq([1, 3, 5])
+ end
+ it 'returns the components with inner zeros' do
+ expect(Version.new('0.3.0.5').components).to eq([0, 3, 0, 5])
+ end
+ # undefined behaviour
+ # it 'returns the components with a given number 0' do
+ # expect(Version.new('1.3.0.5').components(0)).to eq([1, 3, 0, 5])
+ # end
+ it 'returns all components equal to a given number' do
+ expect(Version.new('1.3.0.5').components(4)).to eq([1, 3, 0, 5])
+ end
+ it 'returns only components less than a given number' do
+ expect(Version.new('1.3.0.5').components(3)).to eq([1, 3, 0])
+ end
+ it 'returns the components with trailing zeros for a bigger number' do
+ expect(Version.new('1.0.5').components(6)).to eq([1, 0, 5, 0, 0, 0])
+ end
+ it 'does not mutate the version' do
+ version = Version.new('1.3.5')
+ expect(version.components).to eq([1, 3, 5])
+ expect(version.components.reverse!).to eq([5, 3, 1])
+ expect(version.components).to eq([1, 3, 5])
+ end
+ end
+
+ describe Range do
+ describe '#include?' do
+ context 'versions as objects' do
+ it 'a version is inside a given range' do
+ version1 = Version.new('1.1.1')
+ version2 = Version.new('2.2.1')
+ version3 = Version.new('1.5.1')
+ range = Version::Range.new(version1, version2)
+ expect(range.include?(version3)).to eq(true)
+ end
+ it 'a version equal to start is inside a given range' do
+ version1 = Version.new('1.1.1')
+ version2 = Version.new('2.2.1')
+ range = Version::Range.new(version1, version2)
+ expect(range.include?(version1)).to eq(true)
+ end
+ it 'a version is not inside a given range' do
+ version1 = Version.new('1.1.1')
+ version2 = Version.new('2.2.1')
+ version3 = Version.new('2.2.2')
+ range = Version::Range.new(version1, version2)
+ expect(range.include?(version3)).to eq(false)
+ end
+ it 'a version equal to end is not inside a given range' do
+ version1 = Version.new('1.1.1')
+ version2 = Version.new('2.2.1')
+ range = Version::Range.new(version1, version2)
+ expect(range.include?(version2)).to eq(false)
+ end
+ end
+ context 'versions as strings' do
+ it 'a version is inside a given range' do
+ range = Version::Range.new('1.1.1', '2.2.2')
+ expect(range.include?('1.5.1')).to eq(true)
+ end
+ it 'a version equal to start is inside a given range' do
+ range = Version::Range.new('1.1.1', '2.2.2')
+ expect(range.include?('1.1.1')).to eq(true)
+ end
+ it 'a version is not inside a given range' do
+ range = Version::Range.new('1.1.1', '2.2.2')
+ expect(range.include?('3.5.1')).to eq(false)
+ end
+ it 'a version equal to end is not inside a given range' do
+ range = Version::Range.new('1.1.1', '2.2.2')
+ expect(range.include?('2.2.2')).to eq(false)
+ end
+ end
+ end
+
+ describe '#to_a' do
+ it 'the array has the start version from the range' do
+ version1 = Version.new('1.1')
+ version2 = Version.new('1.2')
+ range = Version::Range.new(version1, version2)
+ expect(range.to_a.first).to eq(version1)
+ end
+ it 'the array does not have the end version from the range' do
+ version1 = Version.new('1.1')
+ version2 = Version.new('1.2')
+ range = Version::Range.new(version1, version2)
+ expect(range.to_a.last).not_to eq(version2)
+ end
+ it 'there is a difference of 1 between the versions in the range' do
+ version1 = Version.new('1.1.0')
+ version2 = Version.new('1.2.0')
+ range = Version::Range.new(version1, version2)
+ expected_range =
+ [
+ '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'
+ ]
+ # result = range.to_a.each_cons(2).all? do |first, second|
+ # first.components(3).last + 1 == second.components.last
+ # end
+ # expect(result).to eq(true)
+ expect(range.to_a).to eq(expected_range)
+ end
+ it 'the next final number turns to 0 when the previous one is 9' do
+ version1 = Version.new('1.1.9')
+ version2 = Version.new('1.2.1')
+ range = Version::Range.new(version1, version2)
+ # expect(range.to_a.last.components(3).last).to eq(0)
+ expect(range.to_a.last).to eq('1.2.0')
+ end
+ it 'the first version after the start is with 3 components' do
+ version1 = Version.new('0')
+ version2 = Version.new('1')
+ range = Version::Range.new(version1, version2)
+ # expect(range.to_a[1].components.size).to eq(3)
+ expect(range.to_a[1]).to eq('0.0.1')
+ end
+ it 'returns an empty array in a range with the same start and end' do
+ version1 = Version.new('1.1')
+ version2 = Version.new('1.1')
+ range = Version::Range.new(version1, version2)
+ expected_range = []
+ expect(range.to_a).to eq(expected_range)
+ end
+ end
+ end
+end