Решение на Четвърта задача - Unit тестване от Богомила Пенева

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

Към профила на Богомила Пенева

Резултати

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

Код

RSpec.describe 'Version' do
describe Version do
it 'is greater than' do
expect(Version.new('1.3.1') > Version.new('1.2.3')).to be true
expect(Version.new('1.3') > Version.new('1.2.3')).to be true
end
it 'is less than' do
expect(Version.new('1.2.2') < Version.new('1.3.1')).to be true
expect(Version.new('1.2.3') < Version.new('1.3')).to be true
end
it 'are equal' do
expect(Version.new('1.1.0') == Version.new('1.1')).to be true
expect(Version.new('0.1') == Version.new('1')).to be false
end
it 'less than or equal ' do
expect(Version.new('2.0.10') <= Version.new('2.0.11')).to be true
expect(Version.new('1.2.2') <= Version.new('1.3.1')).to be true
expect(Version.new('1.1.0') <= Version.new('1.1')).to be true
expect(Version.new('0.1') <= Version.new('1')).to be false
end
it 'greater than or equa' do
expect(Version.new('1.3.2') >= Version.new('1.2.3')).to be true
expect(Version.new('1.3') >= Version.new('1.2.2')).to be true
expect(Version.new('1.2.0') >= Version.new('1.2')).to be true
expect(Version.new('0.1') >= Version.new('1')).to be false
end
it 'greater, less or equal' do
expect(Version.new('1.3.1') <=> Version.new('1.2.3')).to eq 1
expect(Version.new('1.3') <=> Version.new('1.2.3')).to eq 1
expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
expect(Version.new('1.2.2') <=> Version.new('1.3.1')).to eq - 1
expect(Version.new('1.2.3') <=> Version.new('1.3')).to eq - 1
end
describe '#to_s ' do
it ' back to string' do
expect(Version.new('1.1.2').to_s).to eq '1.1.2'
expect(Version.new('1.1.0').to_s).to eq '1.1'
end
end
describe '#components' do
it 'version components' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.0').components).to eq [1, 3]
end
end
end
end

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

F

Failures:

  1) spec passes for the correct solution
     Failure/Error: expect(SOLUTION).to pass_tests
       expected this solution to 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) < 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
       
       
       RSpec log:
       
         ...F.F..
         
         Failures:
         
           1) Version Version less than or equal 
              Failure/Error: expect(Version.new('0.1') <= Version.new('1')).to be false
                
                expected #<FalseClass:0> => false
                     got #<TrueClass:20> => true
                
                Compared using equal?, which compares object identity,
                but expected and actual are not the same object. Use
                `expect(actual).to eq(expected)` if you don't care about
                object identity in this example.
              # ./solution_spec.rb:19:in `block (3 levels) in <top (required)>'
         
           2) Version Version greater, less or equal
              Failure/Error: expect(Version.new('1.2.2') <=> Version.new('1.3.1')).to eq - 1
              ArgumentError:
                wrong number of arguments (given 0, expected 1)
              # ./solution_spec.rb:31:in `block (3 levels) in <top (required)>'
         
         Finished in 0.00524 seconds
         8 examples, 2 failures
         
         Failed examples:
         
         rspec ./solution_spec.rb:15 # Version Version less than or equal 
         rspec ./solution_spec.rb:27 # Version Version greater, less or equal
     # /tmp/d20161119-19072-hulc0k/spec.rb:180:in `block (2 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 0.73292 seconds
1 example, 1 failure

Failed examples:

rspec /tmp/d20161119-19072-hulc0k/spec.rb:179 # spec passes for the correct solution

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

Богомила обнови решението на 18.11.2016 21:36 (преди над 1 година)

+RSpec.describe 'Version' do
+ describe Version do
+ it 'is greater than' do
+ expect(Version.new('1.3.1') > Version.new('1.2.3')).to be true
+ expect(Version.new('1.3') > Version.new('1.2.3')).to be true
+ end
+ it 'is less than' do
+ expect(Version.new('1.2.2') < Version.new('1.3.1')).to be true
+ expect(Version.new('1.2.3') < Version.new('1.3')).to be true
+ end
+ it 'are equal' do
+ expect(Version.new('1.1.0') == Version.new('1.1')).to be true
+ expect(Version.new('0.1') == Version.new('1')).to be false
+ end
+ it 'less than or equal ' do
+ expect(Version.new('2.0.10') <= Version.new('2.0.11')).to be true
+ expect(Version.new('1.2.2') <= Version.new('1.3.1')).to be true
+ expect(Version.new('1.1.0') <= Version.new('1.1')).to be true
+ expect(Version.new('0.1') <= Version.new('1')).to be false
+ end
+ it 'greater than or equa' do
+ expect(Version.new('1.3.2') >= Version.new('1.2.3')).to be true
+ expect(Version.new('1.3') >= Version.new('1.2.2')).to be true
+ expect(Version.new('1.2.0') >= Version.new('1.2')).to be true
+ expect(Version.new('0.1') >= Version.new('1')).to be false
+ end
+ it 'greater, less or equal' do
+ expect(Version.new('1.3.1') <=> Version.new('1.2.3')).to eq 1
+ expect(Version.new('1.3') <=> Version.new('1.2.3')).to eq 1
+ expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
+ expect(Version.new('1.2.2') <=> Version.new('1.3.1')).to eq - 1
+ expect(Version.new('1.2.3') <=> Version.new('1.3')).to eq - 1
+ end
+ describe '#to_s ' do
+ it ' back to string' do
+ expect(Version.new('1.1.2').to_s).to eq '1.1.2'
+ expect(Version.new('1.1.0').to_s).to eq '1.1'
+ end
+ end
+ describe '#components' do
+ it 'version components' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.0').components).to eq [1, 3]
+ end
+ end
+ end
+end
+