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

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

Към профила на Дарин Тодоров

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
it 'raise error for non-valid versions with string' do
expect { Version.new('.1.2') }
.to raise_error(ArgumentError, /Invalid version string '.1.2'/)
expect { Version.new('1.2.') }
.to raise_error(ArgumentError, /Invalid version string '1.2.'/)
expect { Version.new('1..2') }
.to raise_error(ArgumentError, /Invalid version string '1..2'/)
end
it 'raise error for non-valid versions with other instance' do
expect { Version.new(Version.new('1.2.')) }
.to raise_error(ArgumentError, /Invalid version string '1.2.'/)
end
it 'makes valid version with string' do
expect { Version.new('1.2') }.to_not raise_error
expect { Version.new('0.0') }.to_not raise_error
expect { Version.new }.to_not raise_error
end
it 'makes valid version with other instance' do
expect { Version.new(Version.new('1.2.0')) }.to_not raise_error
end
end
describe '#<=>' do
it 'can compare versions with > ' do
expect(Version.new('1.2.3') > Version.new('1.3.1')).to eq(false)
expect(Version.new('2.1.2') > Version.new('2.1')).to eq(true)
expect(Version.new > Version.new('1.3.4')).to eq(false)
expect(Version.new(Version.new('2.3.0.1')) >
Version.new(Version.new('2.3'))
).to eq(true)
end
it 'can compare versions with < ' do
expect(Version.new('1.2.3') < Version.new('1.3.1')).to eq(true)
expect(Version.new('2.1.2') < Version.new('2.1')).to eq(false)
expect(Version.new < Version.new('1.3.4')).to eq(true)
expect(Version.new(Version.new('2.3.0.1')) <
Version.new(Version.new('2.3'))
).to eq(false)
end
it 'can compare versions with >=' do
expect(Version.new(Version.new('0')) >=
Version.new(Version.new)
).to eq(true)
expect(Version.new(Version.new('3.22.1')) >=
Version.new('3.22.0')
).to eq(true)
end
it 'can compare versions with <=' do
expect(Version.new(Version.new('0')) <=
Version.new(Version.new)
).to eq(true)
expect(Version.new(Version.new('3.21')) <=
Version.new('2.1')
).to eq(false)
end
it 'can compare versions with ==' do
expect(Version.new('9') == Version.new('9.0.0.0')).to eq(true)
expect(Version.new(Version.new('0')) ==
Version.new(Version.new)
).to eq(true)
expect(Version.new(Version.new('3.21')) ==
Version.new('2.1')
).to eq(false)
end
it 'can compare versions with <=>' do
expect(Version.new('2.2.0.1.0.0') <=> Version.new('2.2.0.1')).to eq(0)
expect(Version.new('1.2.3') <=> Version.new('2.1.3')).to eq(-1)
expect(Version.new(Version.new('3.2.3')) <=>
Version.new('2.1.3')
).to eq(1)
end
end
describe '#to_s' do
it 'makes canonical representation of version' do
expect(Version.new.to_s).to eq ''
expect(Version.new('1.2.0.0.0').to_s).to eq '1.2'
expect(Version.new(Version.new('0.1.0')).to_s).to eq '0.1'
end
end
describe '#components' do
it 'makes an array with all version`s components' do
expect(Version.new('1.2.3').components).to eq [1, 2, 3]
expect(Version.new('1.2.3.0.0').components).to eq [1, 2, 3]
expect(Version.new.components).to eq []
end
it 'makes an array with lesser than version`s components' do
expect(Version.new('3.2.3.1.2').components(4)).to eq [3, 2, 3, 1]
expect(Version.new(Version.new('0.0.1')).components(2)).to eq [0, 0]
end
it 'makes an array with greater than version`s components' do
expect(Version.new('3').components(3)).to eq [3, 0, 0]
expect(Version.new.components(2)).to eq [0, 0]
end
end
describe 'Version::Range' do
describe '#include?' do
it 'checks that the inside of the range contains a version' do
range = Version::Range.new(Version.new('1.9'), Version.new('2.0.2'))
string_range = Version::Range.new('0.1', '5.9')
expect((range.include? Version.new('2'))).to eq(true)
expect((range.include? '1.5')).to eq(false)
expect((string_range.include? Version.new('4.33.0.3'))).to eq(true)
end
it 'checks that endigns of the range contain a version' do
range = Version::Range.new(Version.new('1.9'), Version.new('2.0.2'))
expect((range.include? Version.new('1.9.0'))).to eq(true)
expect((range.include? Version.new('2.0.2.0'))).to eq(false)
end
it 'checks if submitted version matches with range with one argument' do
range = Version::Range.new(Version.new('1'), Version.new('1'))
expect((range.include? Version.new('1'))).to eq(false)
end
end
describe '#to_a' do
it 'makes a range of two different versions' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2'))
.map { |item| item.to_s }
range2 = Version::Range.new(Version.new('3.1.9'), Version.new('3.2.6'))
.map { |item| item.to_s }
expect(range).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'
]
expect(range2).to eq [
'3.1.9', '3.2', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5'
]
end
it 'makes empty array of range of same versions' do
range = Version::Range.new(Version.new('3.1.0'), Version.new('3.1.0'))
expect(range.to_a).to eq []
end
end
end
end

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

......F...F.F....F.

Failures:

  1) 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-135fjy6/spec.rb:386: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 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-135fjy6/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)>'

  3) 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-135fjy6/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)>'

  4) spec Version::Range smoke-tests #to_a
     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
           def to_a
             []
           end
             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-135fjy6/spec.rb:532: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 18.52 seconds
19 examples, 4 failures

Failed examples:

rspec /tmp/d20161119-19072-135fjy6/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-135fjy6/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-135fjy6/spec.rb:471 # spec Version::Range tests constructing ranges with strings
rspec /tmp/d20161119-19072-135fjy6/spec.rb:525 # spec Version::Range smoke-tests #to_a

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

Дарин обнови решението на 18.11.2016 18:22 (преди почти 2 години)

+RSpec.describe 'Version' do
+ describe '#initialize' do
+ it 'raise error for non-valid versions with string' do
+ expect { Version.new('.1.2') }
+ .to raise_error(ArgumentError, /Invalid version string '.1.2'/)
+ expect { Version.new('1.2.') }
+ .to raise_error(ArgumentError, /Invalid version string '1.2.'/)
+ expect { Version.new('1..2') }
+ .to raise_error(ArgumentError, /Invalid version string '1..2'/)
+ end
+
+ it 'raise error for non-valid versions with other instance' do
+ expect { Version.new(Version.new('1.2.')) }
+ .to raise_error(ArgumentError, /Invalid version string '1.2.'/)
+ end
+
+ it 'makes valid version with string' do
+ expect { Version.new('1.2') }.to_not raise_error
+ expect { Version.new('0.0') }.to_not raise_error
+ expect { Version.new }.to_not raise_error
+ end
+
+ it 'makes valid version with other instance' do
+ expect { Version.new(Version.new('1.2.0')) }.to_not raise_error
+ end
+ end
+
+ describe '#<=>' do
+ it 'can compare versions with > ' do
+ expect(Version.new('1.2.3') > Version.new('1.3.1')).to eq(false)
+ expect(Version.new('2.1.2') > Version.new('2.1')).to eq(true)
+ expect(Version.new > Version.new('1.3.4')).to eq(false)
+ expect(Version.new(Version.new('2.3.0.1')) >
+ Version.new(Version.new('2.3'))
+ ).to eq(true)
+ end
+
+ it 'can compare versions with < ' do
+ expect(Version.new('1.2.3') < Version.new('1.3.1')).to eq(true)
+ expect(Version.new('2.1.2') < Version.new('2.1')).to eq(false)
+ expect(Version.new < Version.new('1.3.4')).to eq(true)
+ expect(Version.new(Version.new('2.3.0.1')) <
+ Version.new(Version.new('2.3'))
+ ).to eq(false)
+ end
+
+ it 'can compare versions with >=' do
+ expect(Version.new(Version.new('0')) >=
+ Version.new(Version.new)
+ ).to eq(true)
+ expect(Version.new(Version.new('3.22.1')) >=
+ Version.new('3.22.0')
+ ).to eq(true)
+ end
+
+ it 'can compare versions with <=' do
+ expect(Version.new(Version.new('0')) <=
+ Version.new(Version.new)
+ ).to eq(true)
+ expect(Version.new(Version.new('3.21')) <=
+ Version.new('2.1')
+ ).to eq(false)
+ end
+
+ it 'can compare versions with ==' do
+ expect(Version.new('9') == Version.new('9.0.0.0')).to eq(true)
+ expect(Version.new(Version.new('0')) ==
+ Version.new(Version.new)
+ ).to eq(true)
+ expect(Version.new(Version.new('3.21')) ==
+ Version.new('2.1')
+ ).to eq(false)
+ end
+
+ it 'can compare versions with <=>' do
+ expect(Version.new('2.2.0.1.0.0') <=> Version.new('2.2.0.1')).to eq(0)
+ expect(Version.new('1.2.3') <=> Version.new('2.1.3')).to eq(-1)
+ expect(Version.new(Version.new('3.2.3')) <=>
+ Version.new('2.1.3')
+ ).to eq(1)
+ end
+ end
+
+ describe '#to_s' do
+ it 'makes canonical representation of version' do
+ expect(Version.new.to_s).to eq ''
+ expect(Version.new('1.2.0.0.0').to_s).to eq '1.2'
+ expect(Version.new(Version.new('0.1.0')).to_s).to eq '0.1'
+ end
+ end
+
+ describe '#components' do
+ it 'makes an array with all version`s components' do
+ expect(Version.new('1.2.3').components).to eq [1, 2, 3]
+ expect(Version.new('1.2.3.0.0').components).to eq [1, 2, 3]
+ expect(Version.new.components).to eq []
+ end
+
+ it 'makes an array with lesser than version`s components' do
+ expect(Version.new('3.2.3.1.2').components(4)).to eq [3, 2, 3, 1]
+ expect(Version.new(Version.new('0.0.1')).components(2)).to eq [0, 0]
+ end
+
+ it 'makes an array with greater than version`s components' do
+ expect(Version.new('3').components(3)).to eq [3, 0, 0]
+ expect(Version.new.components(2)).to eq [0, 0]
+ end
+ end
+
+ describe 'Version::Range' do
+ describe '#include?' do
+ it 'checks that the inside of the range contains a version' do
+ range = Version::Range.new(Version.new('1.9'), Version.new('2.0.2'))
+ string_range = Version::Range.new('0.1', '5.9')
+ expect((range.include? Version.new('2'))).to eq(true)
+ expect((range.include? '1.5')).to eq(false)
+ expect((string_range.include? Version.new('4.33.0.3'))).to eq(true)
+ end
+
+ it 'checks that endigns of the range contain a version' do
+ range = Version::Range.new(Version.new('1.9'), Version.new('2.0.2'))
+ expect((range.include? Version.new('1.9.0'))).to eq(true)
+ expect((range.include? Version.new('2.0.2.0'))).to eq(false)
+ end
+
+ it 'checks if submitted version matches with range with one argument' do
+ range = Version::Range.new(Version.new('1'), Version.new('1'))
+ expect((range.include? Version.new('1'))).to eq(false)
+ end
+ end
+
+ describe '#to_a' do
+ it 'makes a range of two different versions' do
+ range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2'))
+ .map { |item| item.to_s }
+
+ range2 = Version::Range.new(Version.new('3.1.9'), Version.new('3.2.6'))
+ .map { |item| item.to_s }
+
+ expect(range).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'
+ ]
+ expect(range2).to eq [
+ '3.1.9', '3.2', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5'
+ ]
+ end
+
+ it 'makes empty array of range of same versions' do
+ range = Version::Range.new(Version.new('3.1.0'), Version.new('3.1.0'))
+ expect(range.to_a).to eq []
+ end
+ end
+ end
+end