Решение на Четвърта задача - Unit тестване от Милен Дончев

Обратно към всички решения

Към профила на Милен Дончев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 15 успешни тест(а)
  • 4 неуспешни тест(а)

Код

RSpec.describe 'Version' do
describe '#initialize' do
context 'when the component is greater than 10' do
it 'succesfully creates an instance' do
expect { Version.new('13.1.5') }.to_not raise_error
end
end
context 'when the parameter is another Version' do
it 'succesfully creates an instance' do
a = Version.new('13.1.5')
expect { Version.new(a) }.to_not raise_error
end
end
context 'when there are no parameters' do
it 'succesfully creates an instance' do
expect { Version.new }.to_not raise_error
end
end
context 'when there is empty parameter' do
it 'succesfully creates an instance' do
expect { Version.new('') }.to_not raise_error
end
end
context 'when negative argument at the beginning of the version' do
it 'raises argument error' do
version = '-5.3.5'
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
end
context 'when negative argument inside the version' do
it 'raises argument error' do
version = '5.-3.5'
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
end
context 'when version begins with "."' do
it 'raises argument error' do
version = '.5'
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
end
context 'when version have multiple dots for delimiter' do
it 'raises argument error' do
version = '2..5'
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
end
context 'when version ends with "."' do
it 'raises argument error' do
version = '4.3.'
expect { Version.new(version) }
.to raise_error(ArgumentError, "Invalid version string '#{version}'")
end
end
end
describe '#==' do
context 'when last parameters are zeros' do
it 'returns true' do
are_equal = Version.new('1.1.4.0.0') == Version.new('1.1.4')
expect(are_equal).to eq true
end
end
context 'when there is zero inside the version parameters' do
it 'doesnt removes the zero' do
are_equal = Version.new('1.1.0.4') == Version.new('1.1.4')
expect(are_equal).to eq false
end
end
context 'when it starts with zero parameter' do
it 'doesnt removes the zero' do
are_equal = Version.new('0.1.1.4') == Version.new('1.1.4')
expect(are_equal).to eq false
end
end
end
describe '#<' do
context 'when equal number of components and second is bigger' do
it 'returns true' do
is_lower = Version.new('1.4.3') < Version.new('1.5.1')
expect(is_lower).to eq true
end
end
context 'when equal number of components and birst is bigger' do
it 'returns false' do
is_lower = Version.new('2.4.3') < Version.new('1.9.3')
expect(is_lower).to eq false
end
end
context 'when equal versions' do
it 'returns false' do
is_lower = Version.new('2.4.3') < Version.new('2.4.3.0')
expect(is_lower).to eq false
end
end
context 'when not equal number of components and birst is bigger' do
it 'returns false' do
is_lower = Version.new('2.4.3') < Version.new('2.3')
expect(is_lower).to eq false
end
end
context 'when not equal number of components and second is bigger' do
it 'returns true' do
is_lower = Version.new('1.2.3') < Version.new('1.3')
expect(is_lower).to eq true
end
end
context 'when not equal number of components and zero as first parameter' do
it 'returns true' do
is_lower = Version.new('0.2.5') < Version.new('2.3')
expect(is_lower).to eq true
end
end
end
describe '#to_s' do
context 'when zeros at the end of the version' do
it 'removes them' do
expect(Version.new('1.1.0.0').to_s).to eq '1.1'
end
end
context 'when zeros at the beginning of the version' do
it 'doesnt remove them' do
expect(Version.new('0.2.1').to_s).to eq '0.2.1'
end
end
context 'when zeros inside of the version' do
it 'doesnt remove them' do
expect(Version.new('5.0.3').to_s).to eq '5.0.3'
end
end
end
describe '#components' do
context 'when all components and zero inside of the version' do
it 'returns all' do
expect(Version.new('1.0.5').components).to eq [1, 0, 5]
end
end
context 'when all components and zero at the beginning of the version' do
it 'returns all' do
expect(Version.new('0.1.0.5').components).to eq [0, 1, 0, 5]
end
end
context 'when all components and zero at the end of the version' do
it 'removes all zeros at the end' do
expect(Version.new('1.0.5.0.0').components).to eq [1, 0, 5]
end
end
context 'when fixes number of components and more components' do
it 'add zeros to the end' do
a = Version.new('0.0.3')
expect(a.components(5)).to eq [0, 0, 3, 0, 0]
expect(a.to_s).to eq '0.0.3'
end
end
context 'when fixes number of components and less components' do
it 'removes the last components' do
a = Version.new('1.3.3.5')
expect(a.components(2)).to eq [1, 3]
expect(a.to_s).to eq '1.3.3.5'
end
end
context 'when fixes number of components and zeros in the middle' do
it 'removes the last components correctly without removing the zero' do
a = Version.new('1.3.0.0.1')
expect(a.components(3)).to eq [1, 3, 0]
expect(a.to_s).to eq '1.3.0.0.1'
end
end
end
describe 'Range#to_a' do
context 'when normal situation' do
it 'returns all versions in range' do
a = Version.new('1.3.0')
b = Version.new('1.3.3')
expected_array = ['1.3', '1.3.1', '1.3.2']
expect(Version::Range.new(a, b).to_a).to eq expected_array
end
end
context 'when overflow some parameter of the version' do
it 'returns all versions in range' do
result = Version::Range.new('1.2.8', '1.3.3').to_a
expected_array = ['1.2.8', '1.2.9', '1.3', '1.3.1', '1.3.2']
expect(result).to eq expected_array
end
end
context 'when first version is bigger than the second' do
it 'returns empty array' do
expect(Version::Range.new('1.3.3', '1.2.9').to_a).to eq []
end
end
context 'when not equal number of components' do
it 'returns all versions in range' do
result = Version::Range.new('2.1.9', '2.3').to_a
expected_array = [
'2.1.9',
'2.2',
'2.2.1',
'2.2.2',
'2.2.3',
'2.2.4',
'2.2.5',
'2.2.6',
'2.2.7',
'2.2.8',
'2.2.9'
]
expect(result).to eq expected_array
end
end
end
describe 'Range#include?' do
context 'when between the first and the second' do
it 'returns true' do
a = Version.new('1')
b = Version.new('2')
result = Version::Range.new(a, b).include? Version.new('1.0.5')
expect(result).to eq true
end
end
context 'when between the first and the second and component >= 10' do
it 'returns true' do
result = Version::Range.new('0.4.4', '0.5.7').include? '0.4.11'
expect(result).to eq true
end
end
context 'when not between the first and the second' do
it 'returns false' do
result = Version::Range.new('1.4', '1.7').include? '1.2'
expect(result).to eq false
end
end
end
end

Лог от изпълнението

.....FF...F....F...

Failures:

  1) spec Version checks for comparison operators positively
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
         def >(other)
           false
         end
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-192gloi/spec.rb:323:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) spec Version checks for comparison operators negatively
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
         def >(other)
           true
         end
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-192gloi/spec.rb:359:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) spec Version tests that #components cannot be used to modify the version
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
           
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-192gloi/spec.rb:456:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) spec Version::Range tests include? with versions greater than the start one
     Failure/Error: expect(@solution).to_not pass_tests
       expected this solution to not pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               @start_version <= version
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
     # /tmp/d20161119-19072-192gloi/spec.rb:512:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 14.35 seconds
19 examples, 4 failures

Failed examples:

rspec /tmp/d20161119-19072-192gloi/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-192gloi/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-192gloi/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-192gloi/spec.rb:505 # spec Version::Range tests include? with versions greater than the start one

История (1 версия и 0 коментара)

Милен обнови решението на 18.11.2016 12:53 (преди около 2 години)

+RSpec.describe 'Version' do
+ describe '#initialize' do
+ context 'when the component is greater than 10' do
+ it 'succesfully creates an instance' do
+ expect { Version.new('13.1.5') }.to_not raise_error
+ end
+ end
+
+ context 'when the parameter is another Version' do
+ it 'succesfully creates an instance' do
+ a = Version.new('13.1.5')
+ expect { Version.new(a) }.to_not raise_error
+ end
+ end
+
+ context 'when there are no parameters' do
+ it 'succesfully creates an instance' do
+ expect { Version.new }.to_not raise_error
+ end
+ end
+
+ context 'when there is empty parameter' do
+ it 'succesfully creates an instance' do
+ expect { Version.new('') }.to_not raise_error
+ end
+ end
+
+ context 'when negative argument at the beginning of the version' do
+ it 'raises argument error' do
+ version = '-5.3.5'
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+ end
+
+ context 'when negative argument inside the version' do
+ it 'raises argument error' do
+ version = '5.-3.5'
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+ end
+
+ context 'when version begins with "."' do
+ it 'raises argument error' do
+ version = '.5'
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+ end
+
+ context 'when version have multiple dots for delimiter' do
+ it 'raises argument error' do
+ version = '2..5'
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+ end
+
+ context 'when version ends with "."' do
+ it 'raises argument error' do
+ version = '4.3.'
+ expect { Version.new(version) }
+ .to raise_error(ArgumentError, "Invalid version string '#{version}'")
+ end
+ end
+ end
+
+ describe '#==' do
+ context 'when last parameters are zeros' do
+ it 'returns true' do
+ are_equal = Version.new('1.1.4.0.0') == Version.new('1.1.4')
+ expect(are_equal).to eq true
+ end
+ end
+
+ context 'when there is zero inside the version parameters' do
+ it 'doesnt removes the zero' do
+ are_equal = Version.new('1.1.0.4') == Version.new('1.1.4')
+ expect(are_equal).to eq false
+ end
+ end
+
+ context 'when it starts with zero parameter' do
+ it 'doesnt removes the zero' do
+ are_equal = Version.new('0.1.1.4') == Version.new('1.1.4')
+ expect(are_equal).to eq false
+ end
+ end
+ end
+
+ describe '#<' do
+ context 'when equal number of components and second is bigger' do
+ it 'returns true' do
+ is_lower = Version.new('1.4.3') < Version.new('1.5.1')
+ expect(is_lower).to eq true
+ end
+ end
+
+ context 'when equal number of components and birst is bigger' do
+ it 'returns false' do
+ is_lower = Version.new('2.4.3') < Version.new('1.9.3')
+ expect(is_lower).to eq false
+ end
+ end
+
+ context 'when equal versions' do
+ it 'returns false' do
+ is_lower = Version.new('2.4.3') < Version.new('2.4.3.0')
+ expect(is_lower).to eq false
+ end
+ end
+
+ context 'when not equal number of components and birst is bigger' do
+ it 'returns false' do
+ is_lower = Version.new('2.4.3') < Version.new('2.3')
+ expect(is_lower).to eq false
+ end
+ end
+
+ context 'when not equal number of components and second is bigger' do
+ it 'returns true' do
+ is_lower = Version.new('1.2.3') < Version.new('1.3')
+ expect(is_lower).to eq true
+ end
+ end
+
+ context 'when not equal number of components and zero as first parameter' do
+ it 'returns true' do
+ is_lower = Version.new('0.2.5') < Version.new('2.3')
+ expect(is_lower).to eq true
+ end
+ end
+ end
+
+ describe '#to_s' do
+ context 'when zeros at the end of the version' do
+ it 'removes them' do
+ expect(Version.new('1.1.0.0').to_s).to eq '1.1'
+ end
+ end
+
+ context 'when zeros at the beginning of the version' do
+ it 'doesnt remove them' do
+ expect(Version.new('0.2.1').to_s).to eq '0.2.1'
+ end
+ end
+
+ context 'when zeros inside of the version' do
+ it 'doesnt remove them' do
+ expect(Version.new('5.0.3').to_s).to eq '5.0.3'
+ end
+ end
+ end
+
+ describe '#components' do
+ context 'when all components and zero inside of the version' do
+ it 'returns all' do
+ expect(Version.new('1.0.5').components).to eq [1, 0, 5]
+ end
+ end
+
+ context 'when all components and zero at the beginning of the version' do
+ it 'returns all' do
+ expect(Version.new('0.1.0.5').components).to eq [0, 1, 0, 5]
+ end
+ end
+
+ context 'when all components and zero at the end of the version' do
+ it 'removes all zeros at the end' do
+ expect(Version.new('1.0.5.0.0').components).to eq [1, 0, 5]
+ end
+ end
+
+ context 'when fixes number of components and more components' do
+ it 'add zeros to the end' do
+ a = Version.new('0.0.3')
+ expect(a.components(5)).to eq [0, 0, 3, 0, 0]
+ expect(a.to_s).to eq '0.0.3'
+ end
+ end
+
+ context 'when fixes number of components and less components' do
+ it 'removes the last components' do
+ a = Version.new('1.3.3.5')
+ expect(a.components(2)).to eq [1, 3]
+ expect(a.to_s).to eq '1.3.3.5'
+ end
+ end
+
+ context 'when fixes number of components and zeros in the middle' do
+ it 'removes the last components correctly without removing the zero' do
+ a = Version.new('1.3.0.0.1')
+ expect(a.components(3)).to eq [1, 3, 0]
+ expect(a.to_s).to eq '1.3.0.0.1'
+ end
+ end
+ end
+
+ describe 'Range#to_a' do
+ context 'when normal situation' do
+ it 'returns all versions in range' do
+ a = Version.new('1.3.0')
+ b = Version.new('1.3.3')
+ expected_array = ['1.3', '1.3.1', '1.3.2']
+ expect(Version::Range.new(a, b).to_a).to eq expected_array
+ end
+ end
+
+ context 'when overflow some parameter of the version' do
+ it 'returns all versions in range' do
+ result = Version::Range.new('1.2.8', '1.3.3').to_a
+ expected_array = ['1.2.8', '1.2.9', '1.3', '1.3.1', '1.3.2']
+ expect(result).to eq expected_array
+ end
+ end
+
+ context 'when first version is bigger than the second' do
+ it 'returns empty array' do
+ expect(Version::Range.new('1.3.3', '1.2.9').to_a).to eq []
+ end
+ end
+
+ context 'when not equal number of components' do
+ it 'returns all versions in range' do
+ result = Version::Range.new('2.1.9', '2.3').to_a
+ expected_array = [
+ '2.1.9',
+ '2.2',
+ '2.2.1',
+ '2.2.2',
+ '2.2.3',
+ '2.2.4',
+ '2.2.5',
+ '2.2.6',
+ '2.2.7',
+ '2.2.8',
+ '2.2.9'
+ ]
+ expect(result).to eq expected_array
+ end
+ end
+ end
+
+ describe 'Range#include?' do
+ context 'when between the first and the second' do
+ it 'returns true' do
+ a = Version.new('1')
+ b = Version.new('2')
+ result = Version::Range.new(a, b).include? Version.new('1.0.5')
+ expect(result).to eq true
+ end
+ end
+
+ context 'when between the first and the second and component >= 10' do
+ it 'returns true' do
+ result = Version::Range.new('0.4.4', '0.5.7').include? '0.4.11'
+ expect(result).to eq true
+ end
+ end
+
+ context 'when not between the first and the second' do
+ it 'returns false' do
+ result = Version::Range.new('1.4', '1.7').include? '1.2'
+ expect(result).to eq false
+ end
+ end
+ end
+end