Решение на Четвърта задача - Unit тестване от Стефан Якимов

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

Към профила на Стефан Якимов

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
context 'with valid input' do
it 'creates a version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
it 'creates a version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
it 'creates a version when zero is passed' do
version = Version.new('0.0')
expect(version).to eq Version.new('0')
end
it 'creates a version when passed empty string' do
version = Version.new('')
expect(version).to eq Version.new('0')
end
it 'creates a version when nothing is passed' do
version = Version.new
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version).to eq parameter
end
end
context 'with invalid input' do
it 'throws an error when invalid symbol is passed' do
invalid_symbols = "!\"#$%&'()*+,-/:;<=>?@[\\]^_`".split('')
invalid_symbols.each do |sym|
expect do
Version.new("1#{sym}1")
end.to raise_error(ArgumentError, "Invalid version string '1#{sym}1'")
end
end
it 'throws an error when missing numbers are passed' do
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('3..0')
end.to raise_error(ArgumentError, "Invalid version string '3..0'")
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 '.'")
end
it 'throws an error when non integer is passed' do
expect do
Version.new('a.a')
end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
end
end
end
describe '#<=>' do
context 'equal versions' do
it 'compares equal version' do
expect(Version.new('1.1')).to eq Version.new('1.1')
end
it 'returns true when same versions with diff length are compared' do
expect(Version.new('1.1.0')).to eq Version.new('1.1')
end
it 'returns true when zero versions are compared' do
expect(Version.new('0')).to eq Version.new('0')
end
end
context 'non equal versions' do
it 'returns false when versions with zeros upfront are compared' do
expect(Version.new('0.1.1')).not_to eq Version.new('1.1')
end
it 'returns false when symetric versions are compared' do
expect(Version.new('1.2.3')).not_to eq Version.new('3.2.1')
end
end
context 'compare version' do
it 'compares correctly versions with diff length' do
expect(Version.new('1.3') > Version.new('1.2.4')).to be_truthy
end
it 'compares correctly versions with zeros at the end' do
expect(Version.new('1.3.0.0') > Version.new('1.2')).to be_truthy
end
it 'compares correctly versions with zeros at the beginning' do
expect(Version.new('0.1.3') < Version.new('0.1.4')).to be_truthy
end
end
end
describe '#to_s' do
it 'does not remove zeros from the beginning' do
expect(Version.new('0.1').to_s).to eq '0.1'
end
it 'removes zeros from the end' do
expect(Version.new('1.0').to_s).to eq '1'
end
it 'returns empty string from zero version' do
expect(Version.new.to_s).to eq ''
end
end
describe '#components' do
context 'without option parameter' do
it 'should not raise error since the param is optional' do
version = Version.new('')
# this should not raise error
version.components
end
end
context 'with optional parameter' do
it 'when components length < N adds zeros' do
version = Version.new('1')
expect(version.components(3)).to eq [1, 0, 0]
end
it 'when components length > N returns first N' do
version = Version.new('1.2.3')
expect(version.components(2)).to eq [1, 2]
end
end
context 'when a change on the return object is attempted' do
it 'does not allow mutation' do
version = Version.new('1.2')
version.components(2).pop
expect(version.components(2)).to eq [1, 2]
end
end
end
describe 'Range' do
describe '#initialize' do
it 'creates a range from strings' do
expect(Range.new('1.1', '1.9')).to be_truthy
end
it 'creates a range from version instances' do
expect(Range.new(Version.new('1.1'), Version.new('1.9'))).to be_truthy
end
it 'creates a range from a version and string' do
expect(Range.new(Version.new('1.1'), '1.9')).to be_truthy
end
it 'creates a range from a string and version' do
expect(Range.new('1.1', Version.new('1.9'))).to be_truthy
end
end
describe '#include?' do
context 'with a version in the range' do
it 'returns true when passed string' do
within_range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(within_range.include?('1.5')).to be_truthy
end
it 'returns true when passed version instance' do
within_range = Version::Range.new(Version.new('1'), Version.new('2'))
expect(within_range.include?(Version.new('1.5'))).to be_truthy
end
it 'returns true when lower border is passed' do
within_range = Version::Range.new('1', '2')
expect(within_range.include?('1')).to be_truthy
end
end
context 'with a version outside the range' do
it 'returns false' do
excluding_range = Version::Range.new('1', '2')
expect(excluding_range.include?('3')).to be_falsey
end
it 'returns false when upper bound is passed' do
excluding_range = Version::Range.new('1', '2')
expect(excluding_range.include?('2')).to be_falsey
end
end
end
describe '#to_a' do
it 'returnes array from a range' do
returned = Version::Range.new('1.1', '1.2').to_a
expected = [
'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(returned).to eq expected
end
it 'returnes first border from two consecutive version range' do
returned = Version::Range.new('1.1.1', '1.1.2').to_a
expect(returned).to eq ['1.1.1']
end
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-129nheg/spec.rb:341: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-129nheg/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-129nheg/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 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-129nheg/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 15.73 seconds
19 examples, 4 failures

Failed examples:

rspec /tmp/d20161119-19072-129nheg/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-129nheg/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-129nheg/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-129nheg/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Стефан обнови решението на 15.11.2016 14:55 (преди над 7 години)

+ describe '#initialize' do
+ context 'with valid input' do
+ it 'creates an version' do
+ expect(Version.new('1.1.1').to_s).to eq '1.1.1'
+ end
+ it 'creates an instance when zero is passed' do
+ version = Version.new('0')
+ expect(version.to_s).to eq ''
+ end
+ it 'creates an instance when passed empty string' do
+ version = Version.new('')
+ expect(version.to_s).to eq ''
+ end
+
+ it 'creates an instance when nothing is passed' do
+ version = Version.new
+ expect(version.to_s).to eq ''
+ end
+
+ it 'creates an instance from another version' do
+ parameter = Version.new('1.1.1')
+ version = Version.new(parameter)
+ expect(version.to_s).to eq '1.1.1'
+ end
+ end
+ context 'with invalid input' do
+ end
+ end

Стефан обнови решението на 15.11.2016 15:02 (преди над 7 години)

describe '#initialize' do
context 'with valid input' do
- it 'creates an version' do
- expect(Version.new('1.1.1').to_s).to eq '1.1.1'
+ it 'creates an version beginning with zeros' do
+ expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
+
+ it 'creates an version eding with zeros' do
+ expect(Version.new('1.0.0').to_s).to eq '1'
+ end
+
it 'creates an instance when zero is passed' do
- version = Version.new('0')
+ version = Version.new('0.0')
expect(version.to_s).to eq ''
+ expect(version).to eq Version.new('0')
end
+
it 'creates an instance when passed empty string' do
version = Version.new('')
expect(version.to_s).to eq ''
+ expect(version).to eq Version.new('0')
end
it 'creates an instance when nothing is passed' do
version = Version.new
expect(version.to_s).to eq ''
+ expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version.to_s).to eq '1.1.1'
end
end
context 'with invalid input' do
end
- end
+ end

Стефан обнови решението на 15.11.2016 15:25 (преди над 7 години)

describe '#initialize' do
context 'with valid input' do
it 'creates an version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
it 'creates an version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
it 'creates an instance when zero is passed' do
version = Version.new('0.0')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance when passed empty string' do
version = Version.new('')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance when nothing is passed' do
version = Version.new
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version.to_s).to eq '1.1.1'
end
end
+
context 'with invalid input' do
+ it 'throws an error when invalid characters are passed' do
+ expect do
+ # TODO: randomize characters
+ Version.new('1*1')
+ end.to raise_error(ArgumentError, "Invalid version string '1*1'")
+ end
+
+ it 'throws an error when missing numbers are passed' do
+ expect do
+ Version.new('.3')
+ end.to raise_error(ArgumentError, "Invalid version string '.3'")
+
+ expect do
+ Version.new('3..0')
+ end.to raise_error(ArgumentError, "Invalid version string '3..0'")
+
+ 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 '.'")
+ end
+
+ it 'throws an error when non integer is passed' do
+ expect do
+ Version.new('a.a')
+ end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
+ end
end
end

Стефан обнови решението на 15.11.2016 15:26 (преди над 7 години)

+RSpec.describe 'Version' do
+ # по този начин ли трябва да предам тестовете?
describe '#initialize' do
context 'with valid input' do
it 'creates an version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
it 'creates an version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
it 'creates an instance when zero is passed' do
version = Version.new('0.0')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance when passed empty string' do
version = Version.new('')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance when nothing is passed' do
version = Version.new
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version.to_s).to eq '1.1.1'
end
end
context 'with invalid input' do
it 'throws an error when invalid characters are passed' do
expect do
# TODO: randomize characters
Version.new('1*1')
end.to raise_error(ArgumentError, "Invalid version string '1*1'")
end
it 'throws an error when missing numbers are passed' do
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('3..0')
end.to raise_error(ArgumentError, "Invalid version string '3..0'")
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 '.'")
end
it 'throws an error when non integer is passed' do
expect do
Version.new('a.a')
end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
end
end
end
+end

Стефан обнови решението на 15.11.2016 18:12 (преди над 7 години)

RSpec.describe 'Version' do
- # по този начин ли трябва да предам тестовете?
describe '#initialize' do
context 'with valid input' do
- it 'creates an version beginning with zeros' do
+ it 'creates a version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
- it 'creates an version eding with zeros' do
+ it 'creates a version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
- it 'creates an instance when zero is passed' do
+ it 'creates a version when zero is passed' do
version = Version.new('0.0')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
- it 'creates an instance when passed empty string' do
+ it 'creates a version when passed empty string' do
version = Version.new('')
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
- it 'creates an instance when nothing is passed' do
+ it 'creates a version when nothing is passed' do
version = Version.new
expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version.to_s).to eq '1.1.1'
end
end
-
+
context 'with invalid input' do
- it 'throws an error when invalid characters are passed' do
- expect do
- # TODO: randomize characters
- Version.new('1*1')
- end.to raise_error(ArgumentError, "Invalid version string '1*1'")
+ it 'throws an error when invalid symbol is passed' do
+ invalid_symbols = "!\"#$%&'()*+,-/:;<=>?@[\\]^_`".split('')
+ invalid_symbols.each do |sym|
+ expect do
+ Version.new("1#{sym}1")
+ end.to raise_error(ArgumentError, "Invalid version string '1#{sym}1'")
+ end
end
it 'throws an error when missing numbers are passed' do
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('3..0')
end.to raise_error(ArgumentError, "Invalid version string '3..0'")
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 '.'")
end
it 'throws an error when non integer is passed' do
expect do
Version.new('a.a')
end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
end
end
end
-end
+end

Стефан обнови решението на 16.11.2016 15:35 (преди над 7 години)

RSpec.describe 'Version' do
describe '#initialize' do
context 'with valid input' do
it 'creates a version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
it 'creates a version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
it 'creates a version when zero is passed' do
version = Version.new('0.0')
- expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates a version when passed empty string' do
version = Version.new('')
- expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates a version when nothing is passed' do
version = Version.new
- expect(version.to_s).to eq ''
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
- expect(version.to_s).to eq '1.1.1'
+ expect(version).to eq parameter
end
end
context 'with invalid input' do
it 'throws an error when invalid symbol is passed' do
invalid_symbols = "!\"#$%&'()*+,-/:;<=>?@[\\]^_`".split('')
invalid_symbols.each do |sym|
expect do
Version.new("1#{sym}1")
end.to raise_error(ArgumentError, "Invalid version string '1#{sym}1'")
end
end
it 'throws an error when missing numbers are passed' do
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('3..0')
end.to raise_error(ArgumentError, "Invalid version string '3..0'")
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 '.'")
end
it 'throws an error when non integer is passed' do
expect do
Version.new('a.a')
end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
+ end
+ end
+ end
+ describe '#<=>' do
+ context 'equal versions' do
+ it 'compares equal version' do
+ expect(Version.new('1.1')).to eq Version.new('1.1')
+ end
+
+ it 'returns true when same versions with diff length are compared' do
+ expect(Version.new('1.1.0')).to eq Version.new('1.1')
+ end
+
+ it 'returns true when zero versions are compared' do
+ expect(Version.new('0')).to eq Version.new('0')
+ end
+ end
+
+ context 'non equal versions' do
+ it 'returns false when versions with zeros upfront are compared' do
+ expect(Version.new('0.1.1')).not_to eq Version.new('1.1')
+ end
+
+ it 'returns false when symetric versions are compared' do
+ expect(Version.new('1.2.3')).not_to eq Version.new('3.2.1')
+ end
+ end
+
+ context 'compare version' do
+ it 'compares correctly versions with diff length' do
+ expect(Version.new('1.3') > Version.new('1.2.4')).to be_truthy
+ end
+
+ it 'compares correctly versions with zeros at the end' do
+ expect(Version.new('1.3.0.0') > Version.new('1.2')).to be_truthy
+ end
+
+ it 'compares correctly versions with zeros at the beginning' do
+ expect(Version.new('0.1.3') < Version.new('0.1.4')).to be_truthy
+ end
+ end
+ end
+ describe '#to_s' do
+ it 'does not remove zeros from the beginning' do
+ expect(Version.new('0.1').to_s).to eq '0.1'
+ end
+
+ it 'removes zeros from the end' do
+ expect(Version.new('1.0').to_s).to eq '1'
+ end
+
+ it 'returns empty string from zero version' do
+ expect(Version.new.to_s).to eq ''
+ end
+ end
+ describe '#components' do
+ context 'without option parameter' do
+ it 'should not raise error since the param is optional' do
+ version = Version.new('')
+ # this should not raise error
+ version.components
+ end
+ end
+
+ context 'with optional parameter' do
+ it 'when components length < N adds zeros' do
+ version = Version.new('1')
+ expect(version.components(3)).to eq [1, 0, 0]
+ end
+
+ it 'when components length > N returns first N' do
+ version = Version.new('1.2.3')
+ expect(version.components(2)).to eq [1, 2]
end
end
end
end

Стефан обнови решението на 17.11.2016 18:48 (преди над 7 години)

RSpec.describe 'Version' do
describe '#initialize' do
context 'with valid input' do
it 'creates a version beginning with zeros' do
expect(Version.new('0.1.1').to_s).to eq '0.1.1'
end
it 'creates a version eding with zeros' do
expect(Version.new('1.0.0').to_s).to eq '1'
end
it 'creates a version when zero is passed' do
version = Version.new('0.0')
expect(version).to eq Version.new('0')
end
it 'creates a version when passed empty string' do
version = Version.new('')
expect(version).to eq Version.new('0')
end
it 'creates a version when nothing is passed' do
version = Version.new
expect(version).to eq Version.new('0')
end
it 'creates an instance from another version' do
parameter = Version.new('1.1.1')
version = Version.new(parameter)
expect(version).to eq parameter
end
end
context 'with invalid input' do
it 'throws an error when invalid symbol is passed' do
invalid_symbols = "!\"#$%&'()*+,-/:;<=>?@[\\]^_`".split('')
invalid_symbols.each do |sym|
expect do
Version.new("1#{sym}1")
end.to raise_error(ArgumentError, "Invalid version string '1#{sym}1'")
end
end
it 'throws an error when missing numbers are passed' do
expect do
Version.new('.3')
end.to raise_error(ArgumentError, "Invalid version string '.3'")
expect do
Version.new('3..0')
end.to raise_error(ArgumentError, "Invalid version string '3..0'")
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 '.'")
end
it 'throws an error when non integer is passed' do
expect do
Version.new('a.a')
end.to raise_error(ArgumentError, "Invalid version string 'a.a'")
end
end
end
describe '#<=>' do
context 'equal versions' do
it 'compares equal version' do
expect(Version.new('1.1')).to eq Version.new('1.1')
end
it 'returns true when same versions with diff length are compared' do
expect(Version.new('1.1.0')).to eq Version.new('1.1')
end
it 'returns true when zero versions are compared' do
expect(Version.new('0')).to eq Version.new('0')
end
end
context 'non equal versions' do
it 'returns false when versions with zeros upfront are compared' do
expect(Version.new('0.1.1')).not_to eq Version.new('1.1')
end
it 'returns false when symetric versions are compared' do
expect(Version.new('1.2.3')).not_to eq Version.new('3.2.1')
end
end
context 'compare version' do
it 'compares correctly versions with diff length' do
expect(Version.new('1.3') > Version.new('1.2.4')).to be_truthy
end
it 'compares correctly versions with zeros at the end' do
expect(Version.new('1.3.0.0') > Version.new('1.2')).to be_truthy
end
it 'compares correctly versions with zeros at the beginning' do
expect(Version.new('0.1.3') < Version.new('0.1.4')).to be_truthy
end
end
end
describe '#to_s' do
it 'does not remove zeros from the beginning' do
expect(Version.new('0.1').to_s).to eq '0.1'
end
it 'removes zeros from the end' do
expect(Version.new('1.0').to_s).to eq '1'
end
it 'returns empty string from zero version' do
expect(Version.new.to_s).to eq ''
end
end
describe '#components' do
context 'without option parameter' do
it 'should not raise error since the param is optional' do
version = Version.new('')
# this should not raise error
version.components
end
end
context 'with optional parameter' do
it 'when components length < N adds zeros' do
version = Version.new('1')
expect(version.components(3)).to eq [1, 0, 0]
end
it 'when components length > N returns first N' do
version = Version.new('1.2.3')
expect(version.components(2)).to eq [1, 2]
end
end
+
+ context 'when a change on the return object is attempted' do
+ it 'does not allow mutation' do
+ version = Version.new('1.2')
+ version.components(2).pop
+ expect(version.components(2)).to eq [1, 2]
+ end
+ end
+ end
+ describe 'Range' do
+ describe '#initialize' do
+ it 'creates a range from strings' do
+ expect(Range.new('1.1', '1.9')).to be_truthy
+ end
+
+ it 'creates a range from version instances' do
+ expect(Range.new(Version.new('1.1'), Version.new('1.9'))).to be_truthy
+ end
+
+ it 'creates a range from a version and string' do
+ expect(Range.new(Version.new('1.1'), '1.9')).to be_truthy
+ end
+
+ it 'creates a range from a string and version' do
+ expect(Range.new('1.1', Version.new('1.9'))).to be_truthy
+ end
+ end
+ describe '#include?' do
+ context 'with a version in the range' do
+ it 'returns true when passed string' do
+ within_range = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(within_range.include?('1.5')).to be_truthy
+ end
+
+ it 'returns true when passed version instance' do
+ within_range = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(within_range.include?(Version.new('1.5'))).to be_truthy
+ end
+
+ it 'returns true when lower border is passed' do
+ within_range = Version::Range.new('1', '2')
+ expect(within_range.include?('1')).to be_truthy
+ end
+ end
+
+ context 'with a version outside the range' do
+ it 'returns false' do
+ excluding_range = Version::Range.new('1', '2')
+ expect(excluding_range.include?('3')).to be_falsey
+ end
+
+ it 'returns false when upper bound is passed' do
+ excluding_range = Version::Range.new('1', '2')
+ expect(excluding_range.include?('2')).to be_falsey
+ end
+ end
+ end
+ describe '#to_a' do
+ it 'returnes array from a range' do
+ returned = Version::Range.new('1.1', '1.2').to_a
+ expected = [
+ '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(returned).to eq expected
+ end
+
+ it 'returnes first border from two consecutive version range' do
+ returned = Version::Range.new('1.1.1', '1.1.2').to_a
+ expect(returned).to eq ['1.1.1']
+ end
+ end
end
end