Решение на Четвърта задача - Unit тестване от Георги Иванов

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

Към профила на Георги Иванов

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
it 'accepts empty string as an argument' do
expect(Version.new('')).to eq Version.new('0')
end
it 'creates instance without taking arguments' do
expect(Version.new).to eq Version.new('0')
end
context 'when argument is not valid version string' do
it 'raises ArgumentError' do
def expect_error(argument)
expect do
Version.new(argument)
end.to raise_error(
ArgumentError,
"Invalid version string '#{argument}'"
)
end
expect_error('1.')
expect_error('.3')
expect_error('0..3')
expect_error('..3')
expect_error('41..')
expect_error('42.')
expect_error('.43')
expect_error('44..45')
expect_error('..46')
expect_error('111..222..333')
end
end
end
describe 'implements comparison' do
it 'does not distinguish by last zero components' do
expect(Version.new('1.1')).to eq Version.new('1.1.0')
expect(Version.new('1.1')).to eq Version.new('1.1.0.0')
expect(Version.new('11')).to eq Version.new('11.0.0.0.0.0.0.0')
end
it 'distinguish by leading zeros' do
expect(Version.new('0.1')).to_not eq Version.new('1')
expect(Version.new('0.0.7')).to_not eq Version.new('7')
end
it 'checks for equality' do
expect(Version.new('1.3.57')).to eq Version.new('1.3.57')
expect(Version.new('1.35.7')).to_not eq Version.new('1.3.57')
expect(Version.new('13.5.7')).to_not eq Version.new('1.3.5.7')
expect(Version.new('255.255.255.255'))
.to eq Version.new('255.255.255.255')
end
it 'checks witch version is older' do
expect(Version.new('1.2.3')).to be < Version.new('1.2.4')
expect(Version.new('1.2.3')).to be > Version.new('1.2.2')
expect(Version.new('1.2')).to be < Version.new('1.2.101')
expect(Version.new('1.2')).to be > Version.new('1.1.101')
expect(Version.new('1.20')).to be < Version.new('2')
expect(Version.new('111.112.113.114'))
.to be < Version.new('111.112.113.115')
end
end
describe '#to_s' do
it 'converts version to string' do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
expect(Version.new('12.34.5').to_s).to eq '12.34.5'
expect(Version.new('123.45678').to_s).to eq '123.45678'
expect(Version.new('1.1.0').to_s).to eq '1.1'
expect(Version.new('100.1.0.0.0.0.0.0').to_s).to eq '100.1'
end
end
describe '#components' do
it 'converts version to array' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.4.6.9.16').components).to eq [1, 4, 6, 9, 16]
expect(Version.new('1.1.2.3.5.8').components).to eq [1, 1, 2, 3, 5, 8]
expect(Version.new('0.0.1.2.3.0').components).to eq [0, 0, 1, 2, 3]
end
it 'returns new array' do
version = Version.new('1.4.6.9.16')
new_array = version.components.map! { |n| Math.sqrt(n) }
expect(version.components).to_not eq new_array
end
end
describe 'Version::Range' do
describe '#initialize' do
it 'can be initialized by versions or strings' do
expect do
Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
end.to_not raise_error
expect do
Version::Range.new(Version.new('1.2.3'), '1.2.4').to_not raise_error
end
expect do
Version::Range.new('1.2.3', Version.new('1.2.4')).to_not raise_error
end
expect do
Version::Range.new('1.2.3', '1.2.4').to_not raise_error
end
end
end
describe 'Range#include?' do
it 'checks if the range includes the argument' do
expect(
Version::Range
.new(
Version.new('1'),
Version.new('2')
).include?(Version.new('1.5'))
).to eq true
expect(
Version::Range
.new(
Version.new('10.0.1.4'),
Version.new('23.54.16')
).include?(Version.new('17.8.9'))
).to eq true
expect(
Version::Range
.new(
Version.new('0'),
Version.new('1.0.0')
).include?(Version.new('0.9.99.9.99.9.99.9'))
).to eq true
end
end
describe 'Range#to_a' do
context 'when all components are explicitly given' do
it 'converts range to array' do
range1 = Version::Range.new(
Version.new('1.1.1'),
Version.new('1.2.2')
)
expected_array1 = [
Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9'),
Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range1.to_a).to match_array expected_array1
range2 = Version::Range.new(
Version.new('1.0.0'),
Version.new('1.2.1')
)
expected_array2 =
[
Version.new('1.0.0'), Version.new('1.0.1'), Version.new('1.0.2'),
Version.new('1.0.3'), Version.new('1.0.4'), Version.new('1.0.5'),
Version.new('1.0.6'), Version.new('1.0.7'), Version.new('1.0.8'),
Version.new('1.0.9'), Version.new('1.1.0'), Version.new('1.1.1'),
Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
Version.new('1.1.8'), Version.new('1.1.9'), Version.new('1.2.0')
]
expect(range2.to_a).to match_array expected_array2
range3 = Version::Range.new('1.9.9', '2.0.2')
expected_array3 =
[Version.new('1.9.9'), Version.new('2'), Version.new('2.0.1')]
expect(range3.to_a).to match_array expected_array3
end
end
context 'when two components are explicitly given for a '
it 'converts range of versions deffined by two components to array' do
range = Version::Range.new(Version.new('1.1'), Version.new('1.2.2'))
expected_array = [
Version.new('1.1.0'), Version.new('1.1.1'), Version.new('1.1.2'),
Version.new('1.1.3'), Version.new('1.1.4'), Version.new('1.1.5'),
Version.new('1.1.6'), Version.new('1.1.7'), Version.new('1.1.8'),
Version.new('1.1.9'), Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range.to_a).to match_array expected_array
end
end
end
end

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

.....FF.FF....FFF..

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-1vzxz37/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-1vzxz37/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 #components with less components than present
     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
               [42, 42, 42, 42, 42]
             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-1vzxz37/spec.rb:420: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 tests #components with more components than present
     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
               [42, 42, 42, 42, 42]
             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-1vzxz37/spec.rb:438: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)>'

  5) spec Version::Range smoke-tests include?
     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)
               true
             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-1vzxz37/spec.rb:502: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)>'

  6) spec Version::Range tests include? with versions greater 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)
               @start_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-1vzxz37/spec.rb:512: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)>'

  7) 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-1vzxz37/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.58 seconds
19 examples, 7 failures

Failed examples:

rspec /tmp/d20161119-19072-1vzxz37/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:405 # spec Version tests #components with less components than present
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:423 # spec Version tests #components with more components than present
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:495 # spec Version::Range smoke-tests include?
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:505 # spec Version::Range tests include? with versions greater than the start one
rspec /tmp/d20161119-19072-1vzxz37/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

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

+RSpec.describe 'Version' do
+ describe '#initialize' do
+ it 'accepts empty string as an argument' do
+ expect(Version.new('')).to eq Version.new('0')
+ end
+
+ it 'creates instance without taking arguments' do
+ expect(Version.new).to eq Version.new('0')
+ end
+
+ context 'when argument is not valid version string' do
+ it 'raises ArgumentError' do
+ def expect_error(argument)
+ expect do
+ Version.new(argument)
+ end.to raise_error(
+ ArgumentError,
+ "Invalid version string '#{argument}'"
+ )
+ end
+
+ expect_error('1.')
+ expect_error('.3')
+ expect_error('0..3')
+ expect_error('..3')
+ expect_error('41..')
+ expect_error('42.')
+ expect_error('.43')
+ expect_error('44..45')
+ expect_error('..46')
+ end
+ end
+ end
+
+ describe 'implements comparison' do
+ it 'does not distinguish by last zero components' do
+ expect(Version.new('1.1')).to eq Version.new('1.1.0')
+ expect(Version.new('1.1')).to eq Version.new('1.1.0.0')
+ end
+ it 'distinguish by leading zeros' do
+ expect(Version.new('0.1')).to_not eq Version.new('1')
+ expect(Version.new('0.0.7')).to_not eq Version.new('7')
+ end
+ it 'checks for equality' do
+ expect(Version.new('1.3.57')).to eq Version.new('1.3.57')
+ expect(Version.new('1.35.7')).to_not eq Version.new('1.3.57')
+ expect(Version.new('13.5.7')).to_not eq Version.new('1.3.5.7')
+ expect(Version.new('255.255.255.255'))
+ .to eq Version.new('255.255.255.255')
+ end
+ it 'knows witch version is older' do
+ expect(Version.new('1.2.3')).to be < Version.new('1.2.4')
+ expect(Version.new('1.2.3')).to be > Version.new('1.2.2')
+ expect(Version.new('1.2')).to be < Version.new('1.2.101')
+ expect(Version.new('1.2')).to be > Version.new('1.1.101')
+ expect(Version.new('1.20')).to be < Version.new('2')
+ expect(Version.new('111.112.113.114'))
+ .to be < Version.new('111.112.113.115')
+ end
+ end
+
+ describe '#to_s' do
+ it 'converts version to string' do
+ expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
+ expect(Version.new('1.1.0').to_s).to eq '1.1'
+ expect(Version.new('100.1.0.0.0.0.0.0').to_s).to eq '100.1'
+ end
+ end
+
+ describe '#components' do
+ it 'converts version to array' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ expect(Version.new('1.4.6.9.16').components).to eq [1, 4, 6, 9, 16]
+ end
+ it 'returns new array' do
+ version = Version.new('1.4.6.9.16')
+ new_array = version.components.map { |n| Math.sqrt(n) }
+ expect(version.components).to_not eq new_array
+ end
+ end
+ describe 'Version::Range' do
+ describe '#initialize' do
+ it 'can be initialized by versions or strings' do
+ expect do
+ Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
+ end.to_not raise_error
+ expect do
+ Version::Range.new(Version.new('1.2.3'), '1.2.4').to_not raise_error
+ end
+ expect do
+ Version::Range.new('1.2.3', Version.new('1.2.4')).to_not raise_error
+ end
+ expect do
+ Version::Range.new('1.2.3', '1.2.4').to_not raise_error
+ end
+ end
+ end
+ describe 'Range#include?' do
+ it 'checks if the range includes the argument' do
+ expect(Version::Range.new(Version.new('1'), Version.new('2'))
+ ).include? Version.new('1.5').to eq true
+ end
+ end
+
+ describe 'Range#to_a' do
+ context 'when all components are explicitly given' do
+ it 'converts range to array' do
+ range1 = Version::Range.new(
+ Version.new('1.1.1'),
+ Version.new('1.2.2')
+ )
+ expected_array1 = [
+ Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
+ Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
+ Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9'),
+ Version.new('1.2.0'), Version.new('1.2.1')
+ ]
+ expect(range1.to_a).to match_array expected_array1
+ range2 = Version::Range.new(
+ Version.new('1.0.0'),
+ Version.new('1.2.1')
+ )
+ expected_array2 =
+ [
+ Version.new('1.0.0'), Version.new('1.0.1'), Version.new('1.0.2'),
+ Version.new('1.0.3'), Version.new('1.0.4'), Version.new('1.0.5'),
+ Version.new('1.0.6'), Version.new('1.0.7'), Version.new('1.0.8'),
+ Version.new('1.0.9'), Version.new('1.1.0'), Version.new('1.1.1'),
+ Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
+ Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
+ Version.new('1.1.8'), Version.new('1.1.9'), Version.new('1.2.0')
+ ]
+ expect(range2.to_a).to match_array expected_array2
+ range3 = Version::Range.new('1.9.9', '2.0.2')
+ expected_array3 =
+ [Version.new('1.9.9'), Version.new('2'), Version.new('2.0.1')]
+ expect(range3.to_a).to match_array expected_array3
+ end
+ end
+ context 'when two components are explicitly given for a '
+ it 'converts range of less than three components to array' do
+ range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
+ expected_array = [
+ Version.new('1.1.0'), Version.new('1.1.1'), Version.new('1.1.2'),
+ Version.new('1.1.3'), Version.new('1.1.4'), Version.new('1.1.5'),
+ Version.new('1.1.6'), Version.new('1.1.7'), Version.new('1.1.8'),
+ Version.new('1.1.9'), Version.new('1.2.0'), Version.new('1.2.1')
+ ]
+ expect(range.to_a).to match_array expected_array
+ end
+ end
+ end
+end

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

RSpec.describe 'Version' do
describe '#initialize' do
it 'accepts empty string as an argument' do
expect(Version.new('')).to eq Version.new('0')
end
it 'creates instance without taking arguments' do
expect(Version.new).to eq Version.new('0')
end
context 'when argument is not valid version string' do
it 'raises ArgumentError' do
def expect_error(argument)
expect do
Version.new(argument)
end.to raise_error(
ArgumentError,
"Invalid version string '#{argument}'"
)
end
expect_error('1.')
expect_error('.3')
expect_error('0..3')
expect_error('..3')
expect_error('41..')
expect_error('42.')
expect_error('.43')
expect_error('44..45')
expect_error('..46')
end
end
end
describe 'implements comparison' do
it 'does not distinguish by last zero components' do
expect(Version.new('1.1')).to eq Version.new('1.1.0')
expect(Version.new('1.1')).to eq Version.new('1.1.0.0')
end
it 'distinguish by leading zeros' do
expect(Version.new('0.1')).to_not eq Version.new('1')
expect(Version.new('0.0.7')).to_not eq Version.new('7')
end
it 'checks for equality' do
expect(Version.new('1.3.57')).to eq Version.new('1.3.57')
expect(Version.new('1.35.7')).to_not eq Version.new('1.3.57')
expect(Version.new('13.5.7')).to_not eq Version.new('1.3.5.7')
expect(Version.new('255.255.255.255'))
.to eq Version.new('255.255.255.255')
end
it 'knows witch version is older' do
expect(Version.new('1.2.3')).to be < Version.new('1.2.4')
expect(Version.new('1.2.3')).to be > Version.new('1.2.2')
expect(Version.new('1.2')).to be < Version.new('1.2.101')
expect(Version.new('1.2')).to be > Version.new('1.1.101')
expect(Version.new('1.20')).to be < Version.new('2')
expect(Version.new('111.112.113.114'))
.to be < Version.new('111.112.113.115')
end
end
describe '#to_s' do
it 'converts version to string' do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
expect(Version.new('1.1.0').to_s).to eq '1.1'
expect(Version.new('100.1.0.0.0.0.0.0').to_s).to eq '100.1'
end
end
describe '#components' do
it 'converts version to array' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.4.6.9.16').components).to eq [1, 4, 6, 9, 16]
end
it 'returns new array' do
version = Version.new('1.4.6.9.16')
new_array = version.components.map { |n| Math.sqrt(n) }
expect(version.components).to_not eq new_array
end
end
describe 'Version::Range' do
describe '#initialize' do
it 'can be initialized by versions or strings' do
expect do
Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
end.to_not raise_error
expect do
Version::Range.new(Version.new('1.2.3'), '1.2.4').to_not raise_error
end
expect do
Version::Range.new('1.2.3', Version.new('1.2.4')).to_not raise_error
end
expect do
Version::Range.new('1.2.3', '1.2.4').to_not raise_error
end
end
end
describe 'Range#include?' do
it 'checks if the range includes the argument' do
- expect(Version::Range.new(Version.new('1'), Version.new('2'))
- ).include? Version.new('1.5').to eq true
+ expect
+ (
+ Version::Range
+ .new(
+ Version.new('1'),
+ Version.new('2')
+ ).include? Version.new('1.5')
+ ).to eq true
end
end
describe 'Range#to_a' do
context 'when all components are explicitly given' do
it 'converts range to array' do
range1 = Version::Range.new(
Version.new('1.1.1'),
Version.new('1.2.2')
)
expected_array1 = [
Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9'),
Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range1.to_a).to match_array expected_array1
range2 = Version::Range.new(
Version.new('1.0.0'),
Version.new('1.2.1')
)
expected_array2 =
[
Version.new('1.0.0'), Version.new('1.0.1'), Version.new('1.0.2'),
Version.new('1.0.3'), Version.new('1.0.4'), Version.new('1.0.5'),
Version.new('1.0.6'), Version.new('1.0.7'), Version.new('1.0.8'),
Version.new('1.0.9'), Version.new('1.1.0'), Version.new('1.1.1'),
Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
Version.new('1.1.8'), Version.new('1.1.9'), Version.new('1.2.0')
]
expect(range2.to_a).to match_array expected_array2
range3 = Version::Range.new('1.9.9', '2.0.2')
expected_array3 =
[Version.new('1.9.9'), Version.new('2'), Version.new('2.0.1')]
expect(range3.to_a).to match_array expected_array3
end
end
context 'when two components are explicitly given for a '
it 'converts range of less than three components to array' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expected_array = [
Version.new('1.1.0'), Version.new('1.1.1'), Version.new('1.1.2'),
Version.new('1.1.3'), Version.new('1.1.4'), Version.new('1.1.5'),
Version.new('1.1.6'), Version.new('1.1.7'), Version.new('1.1.8'),
Version.new('1.1.9'), Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range.to_a).to match_array expected_array
end
end
end
end

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

RSpec.describe 'Version' do
describe '#initialize' do
it 'accepts empty string as an argument' do
expect(Version.new('')).to eq Version.new('0')
end
it 'creates instance without taking arguments' do
expect(Version.new).to eq Version.new('0')
end
context 'when argument is not valid version string' do
it 'raises ArgumentError' do
def expect_error(argument)
expect do
Version.new(argument)
end.to raise_error(
ArgumentError,
"Invalid version string '#{argument}'"
)
end
expect_error('1.')
expect_error('.3')
expect_error('0..3')
expect_error('..3')
expect_error('41..')
expect_error('42.')
expect_error('.43')
expect_error('44..45')
expect_error('..46')
end
end
end
describe 'implements comparison' do
it 'does not distinguish by last zero components' do
expect(Version.new('1.1')).to eq Version.new('1.1.0')
expect(Version.new('1.1')).to eq Version.new('1.1.0.0')
end
it 'distinguish by leading zeros' do
expect(Version.new('0.1')).to_not eq Version.new('1')
expect(Version.new('0.0.7')).to_not eq Version.new('7')
end
it 'checks for equality' do
expect(Version.new('1.3.57')).to eq Version.new('1.3.57')
expect(Version.new('1.35.7')).to_not eq Version.new('1.3.57')
expect(Version.new('13.5.7')).to_not eq Version.new('1.3.5.7')
expect(Version.new('255.255.255.255'))
.to eq Version.new('255.255.255.255')
end
it 'knows witch version is older' do
expect(Version.new('1.2.3')).to be < Version.new('1.2.4')
expect(Version.new('1.2.3')).to be > Version.new('1.2.2')
expect(Version.new('1.2')).to be < Version.new('1.2.101')
expect(Version.new('1.2')).to be > Version.new('1.1.101')
expect(Version.new('1.20')).to be < Version.new('2')
expect(Version.new('111.112.113.114'))
.to be < Version.new('111.112.113.115')
end
end
describe '#to_s' do
it 'converts version to string' do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
expect(Version.new('1.1.0').to_s).to eq '1.1'
expect(Version.new('100.1.0.0.0.0.0.0').to_s).to eq '100.1'
end
end
describe '#components' do
it 'converts version to array' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.4.6.9.16').components).to eq [1, 4, 6, 9, 16]
end
it 'returns new array' do
version = Version.new('1.4.6.9.16')
new_array = version.components.map { |n| Math.sqrt(n) }
expect(version.components).to_not eq new_array
end
end
describe 'Version::Range' do
describe '#initialize' do
it 'can be initialized by versions or strings' do
expect do
Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
end.to_not raise_error
expect do
Version::Range.new(Version.new('1.2.3'), '1.2.4').to_not raise_error
end
expect do
Version::Range.new('1.2.3', Version.new('1.2.4')).to_not raise_error
end
expect do
Version::Range.new('1.2.3', '1.2.4').to_not raise_error
end
end
end
describe 'Range#include?' do
it 'checks if the range includes the argument' do
- expect
- (
+ expect(
Version::Range
- .new(
- Version.new('1'),
- Version.new('2')
- ).include? Version.new('1.5')
+ .new(
+ Version.new('1'),
+ Version.new('2')
+ ).include?(Version.new('1.5'))
).to eq true
end
end
describe 'Range#to_a' do
context 'when all components are explicitly given' do
it 'converts range to array' do
range1 = Version::Range.new(
Version.new('1.1.1'),
Version.new('1.2.2')
)
expected_array1 = [
Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9'),
Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range1.to_a).to match_array expected_array1
range2 = Version::Range.new(
Version.new('1.0.0'),
Version.new('1.2.1')
)
expected_array2 =
[
Version.new('1.0.0'), Version.new('1.0.1'), Version.new('1.0.2'),
Version.new('1.0.3'), Version.new('1.0.4'), Version.new('1.0.5'),
Version.new('1.0.6'), Version.new('1.0.7'), Version.new('1.0.8'),
Version.new('1.0.9'), Version.new('1.1.0'), Version.new('1.1.1'),
Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
Version.new('1.1.8'), Version.new('1.1.9'), Version.new('1.2.0')
]
expect(range2.to_a).to match_array expected_array2
range3 = Version::Range.new('1.9.9', '2.0.2')
expected_array3 =
[Version.new('1.9.9'), Version.new('2'), Version.new('2.0.1')]
expect(range3.to_a).to match_array expected_array3
end
end
context 'when two components are explicitly given for a '
it 'converts range of less than three components to array' do
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expected_array = [
Version.new('1.1.0'), Version.new('1.1.1'), Version.new('1.1.2'),
Version.new('1.1.3'), Version.new('1.1.4'), Version.new('1.1.5'),
Version.new('1.1.6'), Version.new('1.1.7'), Version.new('1.1.8'),
Version.new('1.1.9'), Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range.to_a).to match_array expected_array
end
end
end
end

Георги обнови решението на 18.11.2016 23:06 (преди почти 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
it 'accepts empty string as an argument' do
expect(Version.new('')).to eq Version.new('0')
end
it 'creates instance without taking arguments' do
expect(Version.new).to eq Version.new('0')
end
context 'when argument is not valid version string' do
it 'raises ArgumentError' do
def expect_error(argument)
expect do
Version.new(argument)
end.to raise_error(
ArgumentError,
"Invalid version string '#{argument}'"
)
end
expect_error('1.')
expect_error('.3')
expect_error('0..3')
expect_error('..3')
expect_error('41..')
expect_error('42.')
expect_error('.43')
expect_error('44..45')
expect_error('..46')
+ expect_error('111..222..333')
end
end
end
describe 'implements comparison' do
it 'does not distinguish by last zero components' do
expect(Version.new('1.1')).to eq Version.new('1.1.0')
expect(Version.new('1.1')).to eq Version.new('1.1.0.0')
+ expect(Version.new('11')).to eq Version.new('11.0.0.0.0.0.0.0')
end
it 'distinguish by leading zeros' do
expect(Version.new('0.1')).to_not eq Version.new('1')
expect(Version.new('0.0.7')).to_not eq Version.new('7')
end
it 'checks for equality' do
expect(Version.new('1.3.57')).to eq Version.new('1.3.57')
expect(Version.new('1.35.7')).to_not eq Version.new('1.3.57')
expect(Version.new('13.5.7')).to_not eq Version.new('1.3.5.7')
expect(Version.new('255.255.255.255'))
.to eq Version.new('255.255.255.255')
end
- it 'knows witch version is older' do
+ it 'checks witch version is older' do
expect(Version.new('1.2.3')).to be < Version.new('1.2.4')
expect(Version.new('1.2.3')).to be > Version.new('1.2.2')
expect(Version.new('1.2')).to be < Version.new('1.2.101')
expect(Version.new('1.2')).to be > Version.new('1.1.101')
expect(Version.new('1.20')).to be < Version.new('2')
expect(Version.new('111.112.113.114'))
.to be < Version.new('111.112.113.115')
end
end
describe '#to_s' do
it 'converts version to string' do
expect(Version.new('1.2.3.4').to_s).to eq '1.2.3.4'
+ expect(Version.new('12.34.5').to_s).to eq '12.34.5'
+ expect(Version.new('123.45678').to_s).to eq '123.45678'
expect(Version.new('1.1.0').to_s).to eq '1.1'
expect(Version.new('100.1.0.0.0.0.0.0').to_s).to eq '100.1'
end
end
describe '#components' do
it 'converts version to array' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.4.6.9.16').components).to eq [1, 4, 6, 9, 16]
+ expect(Version.new('1.1.2.3.5.8').components).to eq [1, 1, 2, 3, 5, 8]
+ expect(Version.new('0.0.1.2.3.0').components).to eq [0, 0, 1, 2, 3]
end
it 'returns new array' do
version = Version.new('1.4.6.9.16')
- new_array = version.components.map { |n| Math.sqrt(n) }
+ new_array = version.components.map! { |n| Math.sqrt(n) }
expect(version.components).to_not eq new_array
end
end
describe 'Version::Range' do
describe '#initialize' do
it 'can be initialized by versions or strings' do
expect do
Version::Range.new(Version.new('1.2.3'), Version.new('1.2.4'))
end.to_not raise_error
expect do
Version::Range.new(Version.new('1.2.3'), '1.2.4').to_not raise_error
end
expect do
Version::Range.new('1.2.3', Version.new('1.2.4')).to_not raise_error
end
expect do
Version::Range.new('1.2.3', '1.2.4').to_not raise_error
end
end
end
describe 'Range#include?' do
it 'checks if the range includes the argument' do
expect(
Version::Range
.new(
Version.new('1'),
Version.new('2')
).include?(Version.new('1.5'))
).to eq true
+ expect(
+ Version::Range
+ .new(
+ Version.new('10.0.1.4'),
+ Version.new('23.54.16')
+ ).include?(Version.new('17.8.9'))
+ ).to eq true
+ expect(
+ Version::Range
+ .new(
+ Version.new('0'),
+ Version.new('1.0.0')
+ ).include?(Version.new('0.9.99.9.99.9.99.9'))
+ ).to eq true
end
end
describe 'Range#to_a' do
context 'when all components are explicitly given' do
it 'converts range to array' do
range1 = Version::Range.new(
Version.new('1.1.1'),
Version.new('1.2.2')
)
expected_array1 = [
Version.new('1.1.1'), Version.new('1.1.2'), Version.new('1.1.3'),
Version.new('1.1.4'), Version.new('1.1.5'), Version.new('1.1.6'),
Version.new('1.1.7'), Version.new('1.1.8'), Version.new('1.1.9'),
Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range1.to_a).to match_array expected_array1
range2 = Version::Range.new(
Version.new('1.0.0'),
Version.new('1.2.1')
)
expected_array2 =
[
Version.new('1.0.0'), Version.new('1.0.1'), Version.new('1.0.2'),
Version.new('1.0.3'), Version.new('1.0.4'), Version.new('1.0.5'),
Version.new('1.0.6'), Version.new('1.0.7'), Version.new('1.0.8'),
Version.new('1.0.9'), Version.new('1.1.0'), Version.new('1.1.1'),
Version.new('1.1.2'), Version.new('1.1.3'), Version.new('1.1.4'),
Version.new('1.1.5'), Version.new('1.1.6'), Version.new('1.1.7'),
Version.new('1.1.8'), Version.new('1.1.9'), Version.new('1.2.0')
]
expect(range2.to_a).to match_array expected_array2
range3 = Version::Range.new('1.9.9', '2.0.2')
expected_array3 =
[Version.new('1.9.9'), Version.new('2'), Version.new('2.0.1')]
expect(range3.to_a).to match_array expected_array3
end
end
context 'when two components are explicitly given for a '
- it 'converts range of less than three components to array' do
- range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
+ it 'converts range of versions deffined by two components to array' do
+ range = Version::Range.new(Version.new('1.1'), Version.new('1.2.2'))
expected_array = [
Version.new('1.1.0'), Version.new('1.1.1'), Version.new('1.1.2'),
Version.new('1.1.3'), Version.new('1.1.4'), Version.new('1.1.5'),
Version.new('1.1.6'), Version.new('1.1.7'), Version.new('1.1.8'),
Version.new('1.1.9'), Version.new('1.2.0'), Version.new('1.2.1')
]
expect(range.to_a).to match_array expected_array
end
end
end
end