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

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

Към профила на Христо Кирилов

Резултати

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

Код

RSpec.describe 'Version' do
describe "validate version" do
def get_msg(version)
"Invalid version string '#{version}'"
end
it 'from String' do
expect { Version.new }.to_not raise_error
expect { Version.new('') }.to_not raise_error
expect { Version.new('1') }.to_not raise_error
expect { Version.new('1.0') }.to_not raise_error
expect { Version.new('1.00.1') }.to_not raise_error
expect { Version.new('0.0.9') }.to_not raise_error
v = '..1'
expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
v = '.1'
expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
v = '1.'
expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
v = '11.0.'
expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
v = '1..0'
expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
end
it 'from another version' do
v = Version.new('')
expect(Version.new(v)).to be == v
v = Version.new('1')
expect(Version.new(v)).to be == v
v = Version.new('1.0')
expect(Version.new(v)).to be == v
v = Version.new('1.00.1')
expect(Version.new(v)).to be == v
v = Version.new('0.0.9')
expect(Version.new(v)).to be == v
end
end
describe 'compare ' do
it '> correctly' do
test_bigger
end
it '>= correctly' do
test_bigger
test_equal
end
it '< correctly' do
test_smaller
end
it '<= correctly' do
test_smaller
test_equal
end
it '== correctly' do
test_equal
end
it '<=> correctly' do
test_single_compare Version.new, Version.new('0'), 0
test_single_compare Version.new(''), Version.new('0'), 0
test_single_compare Version.new('1.5'), Version.new('1.5.0.0.0'), 0
test_single_compare Version.new('1.1.2'), Version.new('1.1.1'), 1
test_single_compare Version.new('1.2.2'), Version.new('1.1.5'), 1
test_single_compare Version.new('1.1.2'), Version.new('1.1'), 1
test_single_compare Version.new('1.1.2'), Version.new('1.1.0.0'), 1
test_single_compare Version.new('1.1.1'), Version.new('1.1.2'), -1
test_single_compare Version.new('1.1.5'), Version.new('1.2.2'), -1
test_single_compare Version.new('1.1'), Version.new('1.1.2'), -1
test_single_compare Version.new('1.1.0.0'), Version.new('1.1.2'), -1
end
def test_bigger
test_single_bigger Version.new('1.1.2'), Version.new('1.1.1')
test_single_bigger Version.new('1.2.2'), Version.new('1.1.5')
test_single_bigger Version.new('1.1.2'), Version.new('1.1')
test_single_bigger Version.new('1.1.2'), Version.new('1.1.0.0')
end
def test_smaller
test_single_smaller Version.new('1.1.1'), Version.new('1.1.2')
test_single_smaller Version.new('1.1.5'), Version.new('1.2.2')
test_single_smaller Version.new('1.1'), Version.new('1.1.2')
test_single_smaller Version.new('1.1.0.0'), Version.new('1.1.2')
end
def test_equal
test_single_equal Version.new, Version.new('0')
test_single_equal Version.new(''), Version.new('0')
test_single_equal Version.new('1.5'), Version.new('1.5.0.0.0')
end
def test_single_bigger(left_value, right_value)
expect(left_value).to be > right_value
expect(right_value).to_not be > left_value
end
def test_single_smaller(left_value, right_value)
expect(left_value).to be < right_value
expect(right_value).to_not be < left_value
end
def test_single_equal(left_value, right_value)
expect(left_value).to be == right_value
end
def test_single_compare(left_value, right_value, expected_result)
expect(left_value <=> right_value).to be expected_result
end
end
describe 'to_s method' do
it 'erase zeros' do
expect(Version.new('1.0.0.0').to_s).to eq '1'
expect(Version.new.to_s).to eq ''
end
it 'does not erase zeros in the middle' do
expect(Version.new('1.0.5').to_s).to eq '1.0.5'
end
it 'does not erase zeros in the beginning' do
expect(Version.new('0.0.0.5').to_s).to eq '0.0.0.5'
end
end
describe 'components method' do
it 'does not return the last zeros' do
expect(Version.new('1.0.0.0.0').components).to eq [1]
expect(Version.new.components).to eq []
end
it 'does not erase zeros in the middle or beginning' do
expect(Version.new('0.0.1.0.0.5').components).to eq [0, 0, 1, 0, 0, 5]
end
it 'does return array with length, same as required' do
n = 5
expect(Version.new('1').components(n).length).to be n
expect(Version.new('1.1.2.2.3').components(n).length).to be n
expect(Version.new('1.1.2.2.3.3').components(n).length).to be n
end
it 'does fill the array with zeros if N is bigger than the length' do
expect(Version.new('1').components(5)).to eq [1, 0, 0, 0, 0]
expect(Version.new.components(5)).to eq [0, 0, 0, 0, 0]
end
it 'does remove subversions if N is smaller than the length' do
expect(Version.new('1.2.3.4.5.6').components(5)).to eq [1, 2, 3, 4, 5]
end
it 'does return all the components if N is equal to the length' do
expect(Version.new('1.2.3').components(3)).to eq [1, 2, 3]
end
it 'does not allow modify the the state of version' do
version = Version.new('1.2.3')
version.components.reverse!
expect(version.components). to eq [1, 2, 3]
end
it 'does not allow modify the the state of version with bigger N' do
version = Version.new('1.2.3')
version.components(5).reverse!
expect(version.components(5)). to eq [1, 2, 3, 0, 0]
end
it 'does not allow modify the the state of version with smaller N' do
version = Version.new('1.2.3')
version.components(2).reverse!
expect(version.components(2)). to eq [1, 2]
end
end
describe 'Range' do
describe 'does initialize' do
it 'with Version instances' do
from_v = Version.new
to_v = Version.new('1.5')
expect { Version::Range.new from_v, to_v }.to_not raise_error
end
it 'with Strings' do
expect { Version::Range.new '', '1.5' }.to_not raise_error
end
end
from_version = Version.new('0.5')
to_version = Version.new('1.5')
range = Version::Range.new from_version, to_version
describe 'include?' do
describe 'works with Version' do
it 'return true if the version is in the range' do
expect(range.include?(Version.new('1'))).to be true
end
it 'return true if the version is equal the min range' do
expect(range.include?(Version.new('0.5'))).to be true
end
it 'return false if the version is equal the max range' do
expect(range.include?(Version.new('1.5'))).to be false
end
it 'return false if the version is bigger the max range' do
expect(range.include?(Version.new('1.5.0.0.1'))).to be false
end
it 'return false if the version is smaller the min range' do
expect(range.include?(Version.new('0.4'))).to be false
end
end
describe 'works with String' do
it 'return true if the version is in the range' do
expect(range.include?('1')).to be true
end
it 'return true if the version is equal the min range' do
expect(range.include?('0.5')).to be true
end
it 'return false if the version is equal the max range' do
expect(range.include?('1.5')).to be false
end
it 'return false if the version is bigger the max range' do
expect(range.include?('1.5.0.0.1')).to be false
end
it 'return false if the version is smaller the min range' do
expect(range.include?('0.4')).to be false
end
end
end
describe 'to_a' do
it 'does return the min version' do
expect(range.to_a.include?('0.5')).to be true
end
it 'does not return the max version' do
expect(range.to_a.include?('1.5')).to be false
end
it 'does not return versions smaller than the min' do
smaller = range.to_a.map { |i| Version.new(i) >= from_version }
expect(smaller.all?).to be true
end
it 'does not return version bigger or equal then the max' do
bigger = range.to_a.map { |i| Version.new(i) < to_version }
expect(bigger.all?).to be true
end
it 'does not contain equal versions' do
arr = range.to_a
expect(arr.uniq).to match_array arr
end
it 'does contain the right number of elements' do
expect(range.to_a.count).to be 100
end
end
end
end

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

.....FF.....F/tmp/d20161119-10050-10ughn6/solution.rb:55:in `initialize': error (RuntimeError)
	from /tmp/d20161119-10050-10ughn6/solution_spec.rb:218:in `new'
	from /tmp/d20161119-10050-10ughn6/solution_spec.rb:218:in `block (2 levels) in <top (required)>'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `module_eval'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `subclass'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:342:in `describe'
	from /tmp/d20161119-10050-10ughn6/solution_spec.rb:204:in `block in <top (required)>'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `module_eval'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `subclass'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:342:in `describe'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/dsl.rb:18:in `describe'
	from /tmp/d20161119-10050-10ughn6/solution_spec.rb:1:in `<top (required)>'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `load'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `block in load_spec_files'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `each'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `load_spec_files'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:18:in `run'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in `run'
	from /data/rails/evans-2016/shared/bundle/ruby/2.3.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'
......

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-1xf9h2b/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-1xf9h2b/spec.rb:377: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 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-1xf9h2b/spec.rb:479:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 17.98 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-1xf9h2b/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-1xf9h2b/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-1xf9h2b/spec.rb:471 # spec Version::Range tests constructing ranges with strings

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

Христо обнови решението на 18.11.2016 21:39 (преди над 7 години)

+RSpec.describe 'Version' do
+ describe "validate version" do
+ def get_msg(version)
+ "Invalid version string '#{version}'"
+ end
+
+ it 'from String' do
+ expect { Version.new }.to_not raise_error
+ expect { Version.new('') }.to_not raise_error
+ expect { Version.new('1') }.to_not raise_error
+ expect { Version.new('1.0') }.to_not raise_error
+ expect { Version.new('1.00.1') }.to_not raise_error
+ expect { Version.new('0.0.9') }.to_not raise_error
+
+ v = '..1'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '.1'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '1.'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '11.0.'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+
+ v = '1..0'
+ expect { Version.new(v) }.to raise_error(ArgumentError, get_msg(v))
+ end
+
+ it 'from another version' do
+ v = Version.new('')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1.0')
+ expect(Version.new(v)).to be == v
+ v = Version.new('1.00.1')
+ expect(Version.new(v)).to be == v
+ v = Version.new('0.0.9')
+ expect(Version.new(v)).to be == v
+ end
+ end
+
+ describe 'compare ' do
+ it '> correctly' do
+ test_bigger
+ end
+
+ it '>= correctly' do
+ test_bigger
+ test_equal
+ end
+
+ it '< correctly' do
+ test_smaller
+ end
+
+ it '<= correctly' do
+ test_smaller
+ test_equal
+ end
+
+ it '== correctly' do
+ test_equal
+ end
+
+ it '<=> correctly' do
+ test_single_compare Version.new, Version.new('0'), 0
+ test_single_compare Version.new(''), Version.new('0'), 0
+ test_single_compare Version.new('1.5'), Version.new('1.5.0.0.0'), 0
+
+ test_single_compare Version.new('1.1.2'), Version.new('1.1.1'), 1
+ test_single_compare Version.new('1.2.2'), Version.new('1.1.5'), 1
+ test_single_compare Version.new('1.1.2'), Version.new('1.1'), 1
+ test_single_compare Version.new('1.1.2'), Version.new('1.1.0.0'), 1
+
+ test_single_compare Version.new('1.1.1'), Version.new('1.1.2'), -1
+ test_single_compare Version.new('1.1.5'), Version.new('1.2.2'), -1
+ test_single_compare Version.new('1.1'), Version.new('1.1.2'), -1
+ test_single_compare Version.new('1.1.0.0'), Version.new('1.1.2'), -1
+ end
+
+ def test_bigger
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1.1')
+
+ test_single_bigger Version.new('1.2.2'), Version.new('1.1.5')
+
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1')
+
+ test_single_bigger Version.new('1.1.2'), Version.new('1.1.0.0')
+ end
+
+ def test_smaller
+ test_single_smaller Version.new('1.1.1'), Version.new('1.1.2')
+
+ test_single_smaller Version.new('1.1.5'), Version.new('1.2.2')
+
+ test_single_smaller Version.new('1.1'), Version.new('1.1.2')
+
+ test_single_smaller Version.new('1.1.0.0'), Version.new('1.1.2')
+ end
+
+ def test_equal
+ test_single_equal Version.new, Version.new('0')
+
+ test_single_equal Version.new(''), Version.new('0')
+
+ test_single_equal Version.new('1.5'), Version.new('1.5.0.0.0')
+ end
+
+ def test_single_bigger(left_value, right_value)
+ expect(left_value).to be > right_value
+ expect(right_value).to_not be > left_value
+ end
+
+ def test_single_smaller(left_value, right_value)
+ expect(left_value).to be < right_value
+ expect(right_value).to_not be < left_value
+ end
+
+ def test_single_equal(left_value, right_value)
+ expect(left_value).to be == right_value
+ end
+
+ def test_single_compare(left_value, right_value, expected_result)
+ expect(left_value <=> right_value).to be expected_result
+ end
+ end
+
+ describe 'to_s method' do
+ it 'erase zeros' do
+ expect(Version.new('1.0.0.0').to_s).to eq '1'
+
+ expect(Version.new.to_s).to eq ''
+ end
+
+ it 'does not erase zeros in the middle' do
+ expect(Version.new('1.0.5').to_s).to eq '1.0.5'
+ end
+
+ it 'does not erase zeros in the beginning' do
+ expect(Version.new('0.0.0.5').to_s).to eq '0.0.0.5'
+ end
+ end
+
+ describe 'components method' do
+ it 'does not return the last zeros' do
+ expect(Version.new('1.0.0.0.0').components).to eq [1]
+
+ expect(Version.new.components).to eq []
+ end
+
+ it 'does not erase zeros in the middle or beginning' do
+ expect(Version.new('0.0.1.0.0.5').components).to eq [0, 0, 1, 0, 0, 5]
+ end
+
+ it 'does return array with length, same as required' do
+ n = 5
+
+ expect(Version.new('1').components(n).length).to be n
+
+ expect(Version.new('1.1.2.2.3').components(n).length).to be n
+
+ expect(Version.new('1.1.2.2.3.3').components(n).length).to be n
+ end
+
+ it 'does fill the array with zeros if N is bigger than the length' do
+ expect(Version.new('1').components(5)).to eq [1, 0, 0, 0, 0]
+
+ expect(Version.new.components(5)).to eq [0, 0, 0, 0, 0]
+ end
+
+ it 'does remove subversions if N is smaller than the length' do
+ expect(Version.new('1.2.3.4.5.6').components(5)).to eq [1, 2, 3, 4, 5]
+ end
+
+ it 'does return all the components if N is equal to the length' do
+ expect(Version.new('1.2.3').components(3)).to eq [1, 2, 3]
+ end
+
+ it 'does not allow modify the the state of version' do
+ version = Version.new('1.2.3')
+ version.components.reverse!
+
+ expect(version.components). to eq [1, 2, 3]
+ end
+
+ it 'does not allow modify the the state of version with bigger N' do
+ version = Version.new('1.2.3')
+ version.components(5).reverse!
+
+ expect(version.components(5)). to eq [1, 2, 3, 0, 0]
+ end
+
+ it 'does not allow modify the the state of version with smaller N' do
+ version = Version.new('1.2.3')
+ version.components(2).reverse!
+
+ expect(version.components(2)). to eq [1, 2]
+ end
+ end
+
+ describe 'Range' do
+ describe 'does initialize' do
+ it 'with Version instances' do
+ from_v = Version.new
+ to_v = Version.new('1.5')
+ expect { Version::Range.new from_v, to_v }.to_not raise_error
+ end
+ it 'with Strings' do
+ expect { Version::Range.new '', '1.5' }.to_not raise_error
+ end
+ end
+
+ from_version = Version.new('0.5')
+ to_version = Version.new('1.5')
+ range = Version::Range.new from_version, to_version
+
+ describe 'include?' do
+ describe 'works with Version' do
+ it 'return true if the version is in the range' do
+ expect(range.include?(Version.new('1'))).to be true
+ end
+
+ it 'return true if the version is equal the min range' do
+ expect(range.include?(Version.new('0.5'))).to be true
+ end
+
+ it 'return false if the version is equal the max range' do
+ expect(range.include?(Version.new('1.5'))).to be false
+ end
+
+ it 'return false if the version is bigger the max range' do
+ expect(range.include?(Version.new('1.5.0.0.1'))).to be false
+ end
+
+ it 'return false if the version is smaller the min range' do
+ expect(range.include?(Version.new('0.4'))).to be false
+ end
+ end
+
+ describe 'works with String' do
+ it 'return true if the version is in the range' do
+ expect(range.include?('1')).to be true
+ end
+
+ it 'return true if the version is equal the min range' do
+ expect(range.include?('0.5')).to be true
+ end
+
+ it 'return false if the version is equal the max range' do
+ expect(range.include?('1.5')).to be false
+ end
+
+ it 'return false if the version is bigger the max range' do
+ expect(range.include?('1.5.0.0.1')).to be false
+ end
+
+ it 'return false if the version is smaller the min range' do
+ expect(range.include?('0.4')).to be false
+ end
+ end
+ end
+
+ describe 'to_a' do
+ it 'does return the min version' do
+ expect(range.to_a.include?('0.5')).to be true
+ end
+
+ it 'does not return the max version' do
+ expect(range.to_a.include?('1.5')).to be false
+ end
+
+ it 'does not return versions smaller than the min' do
+ smaller = range.to_a.map { |i| Version.new(i) >= from_version }
+ expect(smaller.all?).to be true
+ end
+
+ it 'does not return version bigger or equal then the max' do
+ bigger = range.to_a.map { |i| Version.new(i) < to_version }
+ expect(bigger.all?).to be true
+ end
+
+ it 'does not contain equal versions' do
+ arr = range.to_a
+ expect(arr.uniq).to match_array arr
+ end
+
+ it 'does contain the right number of elements' do
+ expect(range.to_a.count).to be 100
+ end
+ end
+ end
+end