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

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

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

Резултати

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

Код

describe Version do
describe '#<=>' do
it 'compares versions when the second is older' do
first_version = Version.new('1.2.3')
second_version = Version.new('1.3.0')
result = first_version <=> second_version
expect(result).to eq(-1)
end
it 'compares versions when the first is older' do
first_version = Version.new('1.4.3')
second_version = Version.new('1.3.0')
result = first_version <=> second_version
expect(result).to eq(1)
end
it 'comapres version when they equals' do
first_version = Version.new('1.1.1.0')
second_version = Version.new('1.1.1')
result = first_version <=> second_version
expect(result).to eq(0)
end
end
describe '#initialize' do
it ' works with no arguments given' do
version = Version.new
expect(version).to eq(Version.new(0))
end
it 'throws an exception with invalid arguments given' do
expect { Version.new('aa') }
.to raise_error(ArgumentError, "Invalid version string 'aa'")
end
it 'throws an exception with invalid arguments given' do
expect { Version.new(-2) }
.to raise_error(ArgumentError, "Invalid version string '-2'")
end
end
describe '#components' do
it 'removes zeros when no arguments are given' do
expect(Version.new('1.2.3.2.0').components).to eq([1, 2, 3, 2])
end
it 'works with less than components count arguments given' do
expect(Version.new('1.2.3.2.4.2.1').components(5)).to eq([1, 2, 3, 2, 4])
end
it 'works with parameter less than the count of the components given' do
expect(Version.new('1.2.3').components(2)).to eq([1, 2])
end
it 'works with parameter more than the count of the components given' do
expect(Version.new('1.2.3').components(5)).to eq([1, 2, 3, 0, 0])
end
it 'works with version 0' do
expect(Version.new('0').components).to eq([])
end
it 'does not change the object' do
version = Version.new('1.2.3')
components = version.components
components << '4'
expect(version).to eq(Version.new('1.2.3'))
end
end
describe '#to_s' do
it 'converts to string correctly' do
expect(Version.new('1.2.3').to_s).to eq('1.2.3')
end
it 'converts to string correctly and removes zeroes from the end' do
expect(Version.new('1.2.3.0.0').to_s).to eq('1.2.3')
end
end
describe Version::Range do
describe '#include?' do
it 'checks if the version is included in range' do
start_version = Version.new('1')
end_version = Version.new('2')
range = Version::Range.new(start_version, end_version)
expect(range.include?(Version.new('1.5'))).to eq(true)
end
it 'checks if the version is included in range' do
start_version = Version.new('1')
end_version = Version.new('2')
range = Version::Range.new(start_version, end_version)
expect(range.include?(Version.new('2.1'))).to eq(false)
end
it 'checks if the version is included in range and is two digit numer' do
start_version = Version.new('1')
end_version = Version.new('2')
range = Version::Range.new(start_version, end_version)
expect(range.include?(Version.new('1.10.1'))).to eq(true)
end
it 'checks if the version is included in range' do
start_version = Version.new('1')
end_version = Version.new('1')
range = Version::Range.new(start_version, end_version)
expect(range.include?(Version.new('1'))).to eq(false)
end
it 'works when strings are passed as arguments' do
expect(Version::Range.new('1', '3').include?(2)).to eq(true)
end
it 'works when strings are passed as arguments' do
expect(Version::Range.new('1', '1.1').include?(1)).to eq(true)
end
it 'works when strings are passed as arguments' do
expect(Version::Range.new('1', '1').include?(1)).to eq(false)
end
end
describe '#to_a' do
it 'converts to array' do
range = Version::Range.new(Version.new('1.1'), Version.new('1.1.4'))
expect(range.to_a).to eq(["1.1", '1.1.1', "1.1.2", "1.1.3"])
end
it 'returns empty array when there is no version between them' do
range = Version::Range.new(Version.new('1'), Version.new('1'))
expect(range.to_a).to eq([])
end
it 'includes the first and does not includes the last version' do
range = Version::Range.new(Version.new('1.9.3'), Version.new('1.9.7'))
expect(range.to_a).to eq ['1.9.3', '1.9.4', '1.9.5', '1.9.6']
end
end
end
end

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

.....FF.........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-1pkgf4p/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-1pkgf4p/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::Range tests include? with versions lower 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)
               @end_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-1pkgf4p/spec.rb:522: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.33 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-1pkgf4p/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-1pkgf4p/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-1pkgf4p/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

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

+describe Version do
+ describe '#<=>' do
+ it 'compares versions when the second is older' do
+ first_version = Version.new('1.2.3')
+ second_version = Version.new('1.3.0')
+ result = first_version <=> second_version
+
+ expect(result).to eq(-1)
+ end
+
+ it 'compares versions when the first is older' do
+ first_version = Version.new('1.4.3')
+ second_version = Version.new('1.3.0')
+ result = first_version <=> second_version
+
+ expect(result).to eq(1)
+ end
+
+ it 'comapres version when they equals' do
+ first_version = Version.new('1.1.1.0')
+ second_version = Version.new('1.1.1')
+ result = first_version <=> second_version
+
+ expect(result).to eq(0)
+ end
+ end
+
+ describe '#initialize' do
+ it ' works with no arguments given' do
+ version = Version.new
+ expect(version).to eq(Version.new(0))
+ end
+
+ it 'throws an exception with invalid arguments given' do
+ expect { Version.new('aa') }
+ .to raise_error(ArgumentError, "Invalid version string 'aa'")
+ end
+
+ it 'throws an exception with invalid arguments given' do
+ expect { Version.new(-2) }
+ .to raise_error(ArgumentError, "Invalid version string '-2'")
+ end
+ end
+
+ describe '#components' do
+ it 'removes zeros when no arguments are given' do
+ expect(Version.new('1.2.3.2.0').components).to eq([1, 2, 3, 2])
+ end
+
+ it 'works with less than components count arguments given' do
+ expect(Version.new('1.2.3.2.4.2.1').components(5)).to eq([1, 2, 3, 2, 4])
+ end
+
+ it 'works with parameter less than the count of the components given' do
+ expect(Version.new('1.2.3').components(2)).to eq([1, 2])
+ end
+
+ it 'works with parameter more than the count of the components given' do
+ expect(Version.new('1.2.3').components(5)).to eq([1, 2, 3, 0, 0])
+ end
+
+ it 'works with version 0' do
+ expect(Version.new('0').components).to eq([])
+ end
+
+ it 'does not change the object' do
+ version = Version.new('1.2.3')
+ components = version.components
+ components << '4'
+
+ expect(version).to eq(Version.new('1.2.3'))
+ end
+ end
+
+ describe '#to_s' do
+ it 'converts to string correctly' do
+ expect(Version.new('1.2.3').to_s).to eq('1.2.3')
+ end
+
+ it 'converts to string correctly and removes zeroes from the end' do
+ expect(Version.new('1.2.3.0.0').to_s).to eq('1.2.3')
+ end
+ end
+
+ describe Version::Range do
+ describe '#include?' do
+ it 'checks if the version is included in range' do
+ start_version = Version.new('1')
+ end_version = Version.new('2')
+ range = Version::Range.new(start_version, end_version)
+ expect(range.include?(Version.new('1.5'))).to eq(true)
+ end
+
+ it 'checks if the version is included in range' do
+ start_version = Version.new('1')
+ end_version = Version.new('2')
+ range = Version::Range.new(start_version, end_version)
+ expect(range.include?(Version.new('2.1'))).to eq(false)
+ end
+
+ it 'checks if the version is included in range and is two digit numer' do
+ start_version = Version.new('1')
+ end_version = Version.new('2')
+ range = Version::Range.new(start_version, end_version)
+ expect(range.include?(Version.new('1.10.1'))).to eq(true)
+ end
+
+ it 'checks if the version is included in range' do
+ start_version = Version.new('1')
+ end_version = Version.new('1')
+ range = Version::Range.new(start_version, end_version)
+ expect(range.include?(Version.new('1'))).to eq(false)
+ end
+
+ it 'works when strings are passed as arguments' do
+ expect(Version::Range.new('1', '3').include?(2)).to eq(true)
+ end
+
+ it 'works when strings are passed as arguments' do
+ expect(Version::Range.new('1', '1.1').include?(1)).to eq(true)
+ end
+
+ it 'works when strings are passed as arguments' do
+ expect(Version::Range.new('1', '1').include?(1)).to eq(false)
+ end
+ end
+
+ describe '#to_a' do
+ it 'converts to array' do
+ range = Version::Range.new(Version.new('1.1'), Version.new('1.1.4'))
+ expect(range.to_a).to eq(["1.1", '1.1.1', "1.1.2", "1.1.3"])
+ end
+
+ it 'returns empty array when there is no version between them' do
+ range = Version::Range.new(Version.new('1'), Version.new('1'))
+ expect(range.to_a).to eq([])
+ end
+
+ it 'includes the first and does not includes the last version' do
+ range = Version::Range.new(Version.new('1.9.3'), Version.new('1.9.7'))
+ expect(range.to_a).to eq ['1.9.3', '1.9.4', '1.9.5', '1.9.6']
+ end
+ end
+ end
+end