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

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

Към профила на Даниела Русева

Резултати

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

Код

RSpec.describe 'Version' do
describe "initialization" do
context "with no argument" do
it "initializes correctly with empty string" do
expect(Version.new.to_s).to eq ''
end
end
context "with valid argument" do
it "initializes correctly" do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
expect(Version.new('2.12.7').to_s).to eq '2.12.7'
expect(Version.new('3.5.0.0').to_s).to eq '3.5'
expect(Version.new('0.8').to_s).to eq '0.8'
expect(Version.new('0.7.0').to_s).to eq '0.7'
end
end
context "with invalid argument" do
it "raises error for invalid version" do
expect { Version.new('.2') }
.to raise_error(ArgumentError, 'Invalid version string \'.2\'')
expect { Version.new('1..2') }
.to raise_error(ArgumentError, 'Invalid version string \'1..2\'')
expect { Version.new('2.8.7.') }
.to raise_error(ArgumentError, 'Invalid version string \'2.8.7.\'')
end
end
end
describe 'comparison' do
it 'can compare two versions' do
expect(Version.new('1.1.0') == Version.new('1.1')).to be true
expect(Version.new('1.1.0') == Version.new('1.5')).to be false
expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
expect(Version.new('1.7') <=> Version.new('1.1.5')).to eq 1
expect(Version.new('1.1.0') <=> Version.new('1.2')).to eq -1
expect(Version.new('1.2.3') < Version.new('1.3')).to be true
expect(Version.new('1.8') < Version.new('1.3.1')).to be false
expect(Version.new('1.3') > Version.new('1.2.3')).to be true
expect(Version.new('1.2.1') > Version.new('1.2.3')).to be false
expect(Version.new('2.8.0') <= Version.new('2.8')).to be true
expect(Version.new('2.8') <= Version.new('2.8.2')).to be true
expect(Version.new('4.1.2') <= Version.new('2.8')).to be false
expect(Version.new('1.4.0') >= Version.new('1.4')).to be true
expect(Version.new('1.4.7') >= Version.new('1.4')).to be true
expect(Version.new('0.1') >= Version.new('1.4')).to be false
end
end
describe '#to_string' do
it 'ignores the zeros at the end' do
expect(Version.new('1.2.4.0').to_s).to eq '1.2.4'
expect(Version.new('0.7.0.0').to_s).to eq '0.7'
expect(Version.new('1.2.3').to_s).to eq '1.2.3'
end
end
describe '#components' do
let(:version) { Version.new('5.7.2') }
it 'doesn\'t mutate the object' do
id = version.object_id
expect(version.components.object_id).to_not eq id
expect(version.components(2).object_id).to_not eq id
end
context 'without argument' do
it 'returns a correct array' do
expect(Version.new('1.2.4.0').components).to match_array [1, 2, 4]
expect(Version.new('0.7.0.0').components).to match_array [0, 7]
expect(Version.new('1.2.3').components).to match_array [1, 2, 3]
end
end
context 'with argument' do
it 'returns a correct array' do
expect(Version.new('5.7.2.8.1.8.0').components(4))
.to match_array [5, 7, 2, 8]
expect(Version.new('5.7').components(4))
.to match_array [5, 7, 0, 0]
expect(Version.new('1.2.3').components(3))
.to match_array [1, 2, 3]
end
end
end
describe 'Version::Range' do
let(:range) do
Version::Range.new(Version.new('1.2.0'), Version.new('2.3.1'))
end
describe '#include?' do
it 'knows if a given version is within a range' do
expect(range).to include Version.new('1.2')
expect(range).to include Version.new('1.2.1')
expect(range).to include Version.new('1.23.1')
expect(range).to_not include Version.new('2.3.1')
expect(range).to_not include Version.new('7.2.1')
end
end
describe '#to_a' do
let(:range1) do
Version::Range.new(Version.new('1.4.0'), Version.new('1.5.2'))
end
let(:range2) { Version::Range.new(Version.new('2'), Version.new('2.1')) }
let(:range3) do
Version::Range.new(Version.new('2.4'), Version.new('2.4'))
end
it 'can generate an array of all version within a range' do
expect(range1.to_a).to eq [
'1.4', '1.4.1', '1.4.2', '1.4.3', '1.4.4',
'1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9', '1.5', '1.5.1'
]
expect(range2.to_a).to eq [
'2', '2.0.1', '2.0.2', '2.0.3',
'2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
]
expect(range3.to_a).to eq []
end
end
end
end

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

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

Failures:

  1) 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-15gm4ux/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)>'

  2) 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-15gm4ux/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)>'

  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-15gm4ux/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 19.38 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-15gm4ux/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-15gm4ux/spec.rb:471 # spec Version::Range tests constructing ranges with strings
rspec /tmp/d20161119-19072-15gm4ux/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

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

+RSpec.describe 'Version' do
+ describe "initialization" do
+ context "with no argument" do
+ it "initializes correctly with empty string" do
+ expect(Version.new.to_s).to eq ''
+ end
+ end
+
+ context "with valid argument" do
+ it "initializes correctly" do
+ expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
+ expect(Version.new('2.12.7').to_s).to eq '2.12.7'
+ expect(Version.new('3.5.0.0').to_s).to eq '3.5'
+ expect(Version.new('0.8').to_s).to eq '0.8'
+ expect(Version.new('0.7.0').to_s).to eq '0.7'
+ end
+ end
+
+ context "with invalid argument" do
+ it "raises error for invalid version" do
+ expect { Version.new('.2') }
+ .to raise_error(ArgumentError, 'Invalid version string \'.2\'')
+ expect { Version.new('1..2') }
+ .to raise_error(ArgumentError, 'Invalid version string \'1..2\'')
+ expect { Version.new('2.8.7.') }
+ .to raise_error(ArgumentError, 'Invalid version string \'2.8.7.\'')
+ end
+ end
+ end
+
+ describe 'comparison' do
+ it 'can compare two versions' do
+ expect(Version.new('1.1.0') == Version.new('1.1')).to be true
+ expect(Version.new('1.1.0') == Version.new('1.5')).to be false
+ expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
+ expect(Version.new('1.7') <=> Version.new('1.1.5')).to eq 1
+ expect(Version.new('1.1.0') <=> Version.new('1.2')).to eq -1
+ expect(Version.new('1.2.3') < Version.new('1.3')).to be true
+ expect(Version.new('1.8') < Version.new('1.3.1')).to be false
+ expect(Version.new('1.3') > Version.new('1.2.3')).to be true
+ expect(Version.new('1.2.1') > Version.new('1.2.3')).to be false
+ expect(Version.new('2.8.0') <= Version.new('2.8')).to be true
+ expect(Version.new('2.8') <= Version.new('2.8.2')).to be true
+ expect(Version.new('4.1.2') <= Version.new('2.8')).to be false
+ expect(Version.new('1.4.0') >= Version.new('1.4')).to be true
+ expect(Version.new('1.4.7') >= Version.new('1.4')).to be true
+ expect(Version.new('0.1') >= Version.new('1.4')).to be false
+ end
+ end
+
+ describe '#to_string' do
+ it 'ignores the zeros at the end' do
+ expect(Version.new('1.2.4.0').to_s).to eq '1.2.4'
+ expect(Version.new('0.7.0.0').to_s).to eq '0.7'
+ expect(Version.new('1.2.3').to_s).to eq '1.2.3'
+ end
+ end
+
+ describe '#components' do
+ let(:version) { Version.new('5.7.2') }
+
+ it 'doesn\'t mutate the object' do
+ id = version.object_id
+ expect(version.components.object_id).to_not eq id
+ expect(version.components(2).object_id).to_not eq id
+ end
+
+ context 'without argument' do
+ it 'returns a correct array' do
+ expect(Version.new('1.2.4.0').components).to match_array [1, 2, 4]
+ expect(Version.new('0.7.0.0').components).to match_array [0, 7]
+ expect(Version.new('1.2.3').components).to match_array [1, 2, 3]
+ end
+ end
+
+ context 'with argument' do
+ it 'returns a correct array' do
+ expect(Version.new('5.7.2.8.1.8.0').components(4))
+ .to match_array [5, 7, 2, 8]
+ expect(Version.new('5.7').components(4))
+ .to match_array [5, 7, 0, 0]
+ expect(Version.new('1.2.3').components(3))
+ .to match_array [1, 2, 3]
+ end
+ end
+ end
+
+ describe 'Version::Range' do
+ let(:range) do
+ Version::Range.new(Version.new('1.2.0'), Version.new('2.3.1'))
+ end
+
+ describe '#include?' do
+ it 'knows if a given version is within a range' do
+ expect(range).to include Version.new('1.2.0')
+ expect(range).to include Version.new('1.2.1')
+ expect(range).to include Version.new('1.23.1')
+ expect(range).to_not include Version.new('2.3.1')
+ expect(range).to_not include Version.new('7.2.1')
+ end
+ end
+
+ describe '#to_a' do
+ let(:range1) do
+ Version::Range.new(Version.new('1.4.0'), Version.new('1.5.2'))
+ end
+ let(:range2) { Version::Range.new(Version.new('2'), Version.new('2.1')) }
+ let(:range3) do
+ Version::Range.new(Version.new('2.4'), Version.new('2.4'))
+ end
+
+ it 'can generate an array of all version within a range' do
+ expect(range1.to_a).to eq [
+ '1.4', '1.4.1', '1.4.2', '1.4.3', '1.4.4',
+ '1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9', '1.5', '1.5.1'
+ ]
+ expect(range2.to_a).to eq [
+ '2', '2.0.1', '2.0.2', '2.0.3',
+ '2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
+ ]
+ expect(range3.to_a).to eq []
+ end
+ end
+ end
+end

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

RSpec.describe 'Version' do
describe "initialization" do
context "with no argument" do
it "initializes correctly with empty string" do
expect(Version.new.to_s).to eq ''
end
end
context "with valid argument" do
it "initializes correctly" do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
expect(Version.new('2.12.7').to_s).to eq '2.12.7'
expect(Version.new('3.5.0.0').to_s).to eq '3.5'
expect(Version.new('0.8').to_s).to eq '0.8'
expect(Version.new('0.7.0').to_s).to eq '0.7'
end
end
context "with invalid argument" do
it "raises error for invalid version" do
expect { Version.new('.2') }
.to raise_error(ArgumentError, 'Invalid version string \'.2\'')
expect { Version.new('1..2') }
.to raise_error(ArgumentError, 'Invalid version string \'1..2\'')
expect { Version.new('2.8.7.') }
.to raise_error(ArgumentError, 'Invalid version string \'2.8.7.\'')
end
end
end
describe 'comparison' do
it 'can compare two versions' do
expect(Version.new('1.1.0') == Version.new('1.1')).to be true
expect(Version.new('1.1.0') == Version.new('1.5')).to be false
expect(Version.new('1.1.0') <=> Version.new('1.1')).to eq 0
expect(Version.new('1.7') <=> Version.new('1.1.5')).to eq 1
expect(Version.new('1.1.0') <=> Version.new('1.2')).to eq -1
expect(Version.new('1.2.3') < Version.new('1.3')).to be true
expect(Version.new('1.8') < Version.new('1.3.1')).to be false
expect(Version.new('1.3') > Version.new('1.2.3')).to be true
expect(Version.new('1.2.1') > Version.new('1.2.3')).to be false
expect(Version.new('2.8.0') <= Version.new('2.8')).to be true
expect(Version.new('2.8') <= Version.new('2.8.2')).to be true
expect(Version.new('4.1.2') <= Version.new('2.8')).to be false
expect(Version.new('1.4.0') >= Version.new('1.4')).to be true
expect(Version.new('1.4.7') >= Version.new('1.4')).to be true
expect(Version.new('0.1') >= Version.new('1.4')).to be false
end
end
describe '#to_string' do
it 'ignores the zeros at the end' do
expect(Version.new('1.2.4.0').to_s).to eq '1.2.4'
expect(Version.new('0.7.0.0').to_s).to eq '0.7'
expect(Version.new('1.2.3').to_s).to eq '1.2.3'
end
end
describe '#components' do
let(:version) { Version.new('5.7.2') }
it 'doesn\'t mutate the object' do
id = version.object_id
expect(version.components.object_id).to_not eq id
expect(version.components(2).object_id).to_not eq id
end
context 'without argument' do
it 'returns a correct array' do
expect(Version.new('1.2.4.0').components).to match_array [1, 2, 4]
expect(Version.new('0.7.0.0').components).to match_array [0, 7]
expect(Version.new('1.2.3').components).to match_array [1, 2, 3]
end
end
context 'with argument' do
it 'returns a correct array' do
expect(Version.new('5.7.2.8.1.8.0').components(4))
.to match_array [5, 7, 2, 8]
expect(Version.new('5.7').components(4))
.to match_array [5, 7, 0, 0]
expect(Version.new('1.2.3').components(3))
.to match_array [1, 2, 3]
end
end
end
describe 'Version::Range' do
let(:range) do
Version::Range.new(Version.new('1.2.0'), Version.new('2.3.1'))
end
describe '#include?' do
it 'knows if a given version is within a range' do
- expect(range).to include Version.new('1.2.0')
+ expect(range).to include Version.new('1.2')
expect(range).to include Version.new('1.2.1')
expect(range).to include Version.new('1.23.1')
expect(range).to_not include Version.new('2.3.1')
expect(range).to_not include Version.new('7.2.1')
end
end
describe '#to_a' do
let(:range1) do
Version::Range.new(Version.new('1.4.0'), Version.new('1.5.2'))
end
let(:range2) { Version::Range.new(Version.new('2'), Version.new('2.1')) }
let(:range3) do
Version::Range.new(Version.new('2.4'), Version.new('2.4'))
end
it 'can generate an array of all version within a range' do
expect(range1.to_a).to eq [
'1.4', '1.4.1', '1.4.2', '1.4.3', '1.4.4',
'1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9', '1.5', '1.5.1'
]
expect(range2.to_a).to eq [
'2', '2.0.1', '2.0.2', '2.0.3',
'2.0.4', '2.0.5', '2.0.6', '2.0.7', '2.0.8', '2.0.9'
]
expect(range3.to_a).to eq []
end
end
end
end