Решение на Четвърта задача - Unit тестване от Николина Гюрова

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

Към профила на Николина Гюрова

Резултати

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

Код

RSpec.describe 'Version' do
it 'is 0 for empty arguments' do
v = Version.new('')
expect(v).to eq '0'
v = Version.new
expect(v).to eq '0'
end
it 'trows exception on wrong version' 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\''
)
end
it 'initializes with string' do
expect(Version.new('1')).to eq '1'
expect(Version.new('1.1')).to eq '1.1'
expect(Version.new('1.1.1')).to eq '1.1.1'
expect(Version.new('1.1.1.10')).to eq '1.1.1.10'
end
it 'initializes with version instance' do
v = Version.new('1')
expect(Version.new(v)).to eq '1'
v = Version.new('1.1')
expect(Version.new(v)).to eq '1.1'
v = Version.new('1.1.1')
expect(Version.new(v)).to eq '1.1.1'
v = Version.new('1.1.1.10')
expect(Version.new(v)).to eq '1.1.1.10'
end
it 'handles comparasions with 0' do
expect(Version.new('1.1.0') == Version.new('1.1')).to eq true
expect(Version.new('0.1') == Version.new('1')).to eq false
end
it 'can compare' do
expect(Version.new('1.2.3') < Version.new('1.3.1')).to eq true
expect(Version.new('5') > Version.new('2')).to eq true
expect(Version.new('3.4.1') >= Version.new('3.4')).to eq true
expect(Version.new('3.4.1') <= Version.new('3.4')).to eq false
expect(Version.new('3.4.1') == Version.new('3.4')).to eq false
expect(Version.new('3.4.1') <=> Version.new('3.4')).to eq 1
end
it 'can compare with different number of components' do
expect(Version.new('1.2.3') < Version.new('1.3')).to eq true
expect(Version.new('7') > Version.new('7.0.8')).to eq false
end
it 'converts to string' do
expect(Version.new('1.1.0').to_s).to eq '1.1'
expect(Version.new('11').to_s).to eq '11'
expect(Version.new('15.0.4.0.3').to_s).to eq '15.0.4.0.3'
expect(Version.new('15.0.4.0').to_s).to eq '15.0.4'
end
it 'returns its components' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.0').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.0').components(1)).to eq [1]
expect(Version.new('1.3.5.0.0').components(2)).to eq [1, 3]
expect(Version.new('1.3.5.0.0').components(3)).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.0').components(7)).to eq [1, 3, 5, 0, 0, 0, 0]
end
it 'compponents doesn\'t change the instance' do
v = Version.new('1.3.5.0.0')
expect(v.components(7)).to eq [1, 3, 5, 0, 0, 0, 0]
expect(v.to_s).to eq '1.3.5'
end
it 'checks if a range contains element' do
expect(Version::Range.new(Version.new('1'), Version.new('2'))
.include?(Version.new('1.5'))
).to eq true
expect(Version::Range.new('1.3', '2.5.4')
.include?(Version.new('1.2'))
).to eq false
end
it 'generates all versions in a range' do
expect(Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2')).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'
]
expect(Version::Range.new(Version.new('1.1'), Version.new('1.2')).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
end

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

.....FF...F.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-18xuss5/spec.rb:341: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-18xuss5/spec.rb:368: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-18xuss5/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 constructing ranges with strings
     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 = start_version
               @end_version   = 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-18xuss5/spec.rb:479: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)>'

  5) 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-18xuss5/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 16.58 seconds
19 examples, 5 failures

Failed examples:

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

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

Николина обнови решението на 18.11.2016 23:42 (преди над 7 години)

+RSpec.describe 'Version' do
+ it 'is 0 for empty arguments' do
+ v = Version.new('')
+ expect(v).to eq '0'
+
+ v = Version.new
+ expect(v).to eq '0'
+ end
+
+ it 'trows exception on wrong version' 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\''
+ )
+ end
+
+ it 'initializes with string' do
+ expect(Version.new('1')).to eq '1'
+ expect(Version.new('1.1')).to eq '1.1'
+ expect(Version.new('1.1.1')).to eq '1.1.1'
+ expect(Version.new('1.1.1.10')).to eq '1.1.1.10'
+ end
+
+ it 'initializes with version instance' do
+ v = Version.new('1')
+ expect(Version.new(v)).to eq '1'
+
+ v = Version.new('1.1')
+ expect(Version.new(v)).to eq '1.1'
+
+ v = Version.new('1.1.1')
+ expect(Version.new(v)).to eq '1.1.1'
+
+ v = Version.new('1.1.1.10')
+ expect(Version.new(v)).to eq '1.1.1.10'
+ end
+
+ it 'handles comparasions with 0' do
+ expect(Version.new('1.1.0') == Version.new('1.1')).to eq true
+ expect(Version.new('0.1') == Version.new('1')).to eq false
+ end
+
+ it 'can compare' do
+ expect(Version.new('1.2.3') < Version.new('1.3.1')).to eq true
+ expect(Version.new('5') > Version.new('2')).to eq true
+ expect(Version.new('3.4.1') >= Version.new('3.4')).to eq true
+ expect(Version.new('3.4.1') <= Version.new('3.4')).to eq false
+ expect(Version.new('3.4.1') == Version.new('3.4')).to eq false
+
+ expect(Version.new('3.4.1') <=> Version.new('3.4')).to eq 1
+ end
+
+ it 'can compare with different number of components' do
+ expect(Version.new('1.2.3') < Version.new('1.3')).to eq true
+ expect(Version.new('7') > Version.new('7.0.8')).to eq false
+ end
+
+ it 'converts to string' do
+ expect(Version.new('1.1.0').to_s).to eq '1.1'
+ expect(Version.new('11').to_s).to eq '11'
+ expect(Version.new('15.0.4.0.3').to_s).to eq '15.0.4.0.3'
+ expect(Version.new('15.0.4.0').to_s).to eq '15.0.4'
+ end
+
+ it 'returns its components' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.5.0.0').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.5.0.0').components(1)).to eq [1]
+ expect(Version.new('1.3.5.0.0').components(2)).to eq [1, 3]
+ expect(Version.new('1.3.5.0.0').components(3)).to eq [1, 3, 5]
+ expect(Version.new('1.3.5.0.0').components(7)).to eq [1, 3, 5, 0, 0, 0, 0]
+ end
+
+ it 'compponents doesn\'t change the instance' do
+ v = Version.new('1.3.5.0.0')
+ expect(v.components(7)).to eq [1, 3, 5, 0, 0, 0, 0]
+ expect(v.to_s).to eq '1.3.5'
+ end
+
+ it 'checks if a range contains element' do
+ expect(Version::Range.new(Version.new('1'), Version.new('2'))
+ .include?(Version.new('1.5'))
+ ).to eq true
+
+ expect(Version::Range.new('1.3', '2.5.4')
+ .include?(Version.new('1.2'))
+ ).to eq false
+ end
+
+ it 'generates all versions in a range' do
+ expect(Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2')).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'
+ ]
+
+ expect(Version::Range.new(Version.new('1.1'), Version.new('1.2')).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
+end