Решение на Четвърта задача - Unit тестване от Калина Бухлева

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

Към профила на Калина Бухлева

Резултати

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

Код

RSpec.describe 'Version' do
describe '#version' do
it "is valid" do
expect(Version.new('1')).not_to be nil
expect(Version.new('1.13.3.4')).not_to be nil
expect(Version.new('100.13.3')).not_to be nil
expect(Version.new(Version.new('100.13.3'))).not_to be nil
expect(Version.new).not_to be nil
expect(Version.new('')).not_to be nil
end
it "is invalid" do
expect do
Version.new('-1.3')
end.to raise_error(ArgumentError, "Invalid version string '-1.3'")
expect do
Version.new('1.-3')
end.to raise_error(ArgumentError, "Invalid version string '1.-3'")
expect do
Version.new('1.0.-3')
end.to raise_error(ArgumentError, "Invalid version string '1.0.-3'")
expect do
Version.new('1.0.3.')
end.to raise_error(ArgumentError, "Invalid version string '1.0.3.'")
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('.')
end.to raise_error(ArgumentError, "Invalid version string '.'")
expect do
Version.new('..')
end.to raise_error(ArgumentError, "Invalid version string '..'")
expect do
Version.new('0..3')
end.to raise_error(ArgumentError, "Invalid version string '0..3'")
expect do
Version.new('v:1.13.3')
end.to raise_error(ArgumentError, "Invalid version string 'v:1.13.3'")
end
it "can be equal" do
version1 = Version.new('1')
version2 = Version.new('1.0')
version3 = Version.new('1.0.0')
version4 = Version.new("1.0.0.9")
expect(version1 == version2).to eq(true)
expect(version1 == version3).to eq(true)
expect(version2 == version3).to eq(true)
expect(version3 <= version4).to eq(true)
expect(version3 <=> version4).to eq(-1)
expect(version3 <=> version1).to eq(0)
end
it "can be compared" do
expect(Version.new('1') < Version.new('1.0.1')).to eq(true)
expect(Version.new('1.0.1') < Version.new('1.1')).to eq(true)
expect(Version.new('1.1') < Version.new('1.1.1')).to eq(true)
expect(Version.new('1.1.1') < Version.new('1.2')).to eq(true)
expect(Version.new('1.2') < Version.new('2')).to eq(true)
expect(Version.new('2') < Version.new('10')).to eq(true)
end
it "can remove zeros in end" do
expect(Version.new('1.1.0').to_s).to eq('1.1')
expect(Version.new('1.0.0.0').to_s).to eq('1')
expect(Version.new('1.0.0').to_s).to eq('1')
expect(Version.new('1.0').to_s).to eq('1')
expect(Version.new('1').to_s).to eq('1')
expect(Version.new('1.0.1').to_s).to eq('1.0.1')
expect(Version.new('0.0.0').to_s).to eq('')
end
it "can get components" do
version = Version.new('1.1.0')
expect(version.components).to eq([1, 1])
expect(version.components(1)).to eq([1])
expect(version.components(2)).to eq([1, 1])
expect(version.components(3)).to eq([1, 1, 0])
expect(version.components(4)).to eq([1, 1, 0, 0])
expect(version).to eq(Version.new('1.1'))
end
end
describe '#range' do
it "is version included" do
range1 = Version::Range.new(Version.new('1'), Version.new('2'))
range2 = Version::Range.new('1', '1.0.0')
range3 = Version::Range.new('1', '10')
expect(range1.include?(Version.new('1.5'))).to eq(true)
expect(range1.include?(Version.new('3.5'))).to eq(false)
expect(range1.include?(Version.new('0.9'))).to eq(false)
expect(range2.include?(Version.new('1'))).to eq(false)
expect(range3.include?(Version.new('0.9.1'))).to eq(false)
end
it "can find all versions" do
range1 = Version::Range.new(Version.new('1'), Version.new('1.0.2'))
range2 = Version::Range.new('1.0.0', '1.0.0')
expect(range1.to_a).not_to include('1.2')
expect(range1.to_a).not_to include('0.9')
expect(range2.to_a).to eq([])
expect(range1.to_a).to eq(['1', '1.0.1'])
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-2hta82/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-2hta82/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-2hta82/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-2hta82/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)>'

Finished in 14.68 seconds
19 examples, 4 failures

Failed examples:

rspec /tmp/d20161119-19072-2hta82/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-2hta82/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-2hta82/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-2hta82/spec.rb:471 # spec Version::Range tests constructing ranges with strings

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

Калина обнови решението на 18.11.2016 22:03 (преди над 1 година)

+RSpec.describe 'Version' do
+ describe '#version' do
+ it "is valid" do
+ expect(Version.new('1')).not_to be nil
+ expect(Version.new('1.13.3.4')).not_to be nil
+ expect(Version.new('100.13.3')).not_to be nil
+ expect(Version.new(Version.new('100.13.3'))).not_to be nil
+ expect(Version.new).not_to be nil
+ expect(Version.new('')).not_to be nil
+ end
+
+ it "is invalid" do
+ expect do
+ Version.new('-1.3')
+ end.to raise_error(ArgumentError, "Invalid version string '-1.3'")
+ expect do
+ Version.new('1.-3')
+ end.to raise_error(ArgumentError, "Invalid version string '1.-3'")
+ expect do
+ Version.new('1.0.-3')
+ end.to raise_error(ArgumentError, "Invalid version string '1.0.-3'")
+ expect do
+ Version.new('1.0.3.')
+ end.to raise_error(ArgumentError, "Invalid version string '1.0.3.'")
+ expect do
+ Version.new('.3')
+ end.to raise_error(ArgumentError, "Invalid version string '.3'")
+ expect do
+ Version.new('.')
+ end.to raise_error(ArgumentError, "Invalid version string '.'")
+ expect do
+ Version.new('..')
+ end.to raise_error(ArgumentError, "Invalid version string '..'")
+ expect do
+ Version.new('0..3')
+ end.to raise_error(ArgumentError, "Invalid version string '0..3'")
+ expect do
+ Version.new('v:1.13.3')
+ end.to raise_error(ArgumentError, "Invalid version string 'v:1.13.3'")
+ end
+
+ it "can be equal" do
+ version1 = Version.new('1')
+ version2 = Version.new('1.0')
+ version3 = Version.new('1.0.0')
+ version4 = Version.new("1.0.0.9")
+ expect(version1 == version2).to eq(true)
+ expect(version1 == version3).to eq(true)
+ expect(version2 == version3).to eq(true)
+ expect(version3 <= version4).to eq(true)
+ expect(version3 <=> version4).to eq(-1)
+ expect(version3 <=> version1).to eq(0)
+ end
+
+ it "can be compared" do
+ expect(Version.new('1') < Version.new('1.0.1')).to eq(true)
+ expect(Version.new('1.0.1') < Version.new('1.1')).to eq(true)
+ expect(Version.new('1.1') < Version.new('1.1.1')).to eq(true)
+ expect(Version.new('1.1.1') < Version.new('1.2')).to eq(true)
+ expect(Version.new('1.2') < Version.new('2')).to eq(true)
+ expect(Version.new('2') < Version.new('10')).to eq(true)
+ end
+
+ it "can remove zeros in end" do
+ expect(Version.new('1.1.0').to_s).to eq('1.1')
+ expect(Version.new('1.0.0.0').to_s).to eq('1')
+ expect(Version.new('1.0.0').to_s).to eq('1')
+ expect(Version.new('1.0').to_s).to eq('1')
+ expect(Version.new('1').to_s).to eq('1')
+ expect(Version.new('1.0.1').to_s).to eq('1.0.1')
+ expect(Version.new('0.0.0').to_s).to eq('')
+ end
+
+ it "can get components" do
+ version = Version.new('1.1.0')
+ expect(version.components).to eq([1, 1])
+ expect(version.components(1)).to eq([1])
+ expect(version.components(2)).to eq([1, 1])
+ expect(version.components(3)).to eq([1, 1, 0])
+ expect(version.components(4)).to eq([1, 1, 0, 0])
+ expect(version).to eq(Version.new('1.1'))
+ end
+ end
+
+ describe '#range' do
+ it "is version included" do
+ range1 = Version::Range.new(Version.new('1'), Version.new('2'))
+ range2 = Version::Range.new('1', '1.0.0')
+ range3 = Version::Range.new('1', '10')
+ expect(range1.include?(Version.new('1.5'))).to eq(true)
+ expect(range1.include?(Version.new('3.5'))).to eq(false)
+ expect(range1.include?(Version.new('0.9'))).to eq(false)
+ expect(range2.include?(Version.new('1'))).to eq(false)
+ expect(range3.include?(Version.new('0.9.1'))).to eq(false)
+ end
+
+ it "can find all versions" do
+ range1 = Version::Range.new(Version.new('1'), Version.new('1.0.2'))
+ range2 = Version::Range.new('1.0.0', '1.0.0')
+ expect(range1.to_a).not_to include('1.2')
+ expect(range1.to_a).not_to include('0.9')
+ expect(range2.to_a).to eq([])
+ expect(range1.to_a).to eq(['1', '1.0.1'])
+ end
+ end
+end
+