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

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

Към профила на Константин Нецов

Резултати

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

Код

RSpec.describe 'Version' do
def r_error(p)
raise_error("Invalid version string #{p}", ArgumentError)
end
def range(a, b)
Version::Range.new(a, b)
end
def v_range(a, b)
Version::Range.new(Version.new(a), Version.new(b))
end
def test1(a, b)
v_range(a, b).to_a
end
def test2
['1.4.4', '1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9']
end
def v(version)
Version.new(version)
end
RSpec::Expectations.configuration.on_potential_false_positives = :nothing
describe "#initialize" do
context "creates object" do
it "when valid version is passed as parameter" do
expect { Version.new("1.2.3") }.not_to r_error("1.2.3")
end
it "when version contains big number" do
expect { Version.new("1.13.3") }.not_to r_error("1.13.3")
end
it "when valid version object is passed as parameter" do
expect { Version.new(Version.new("2.2.2")) }.not_to r_error("2.2.2")
end
it "when pass empty string as parameter" do
expect { Version.new(Version.new("")) }.not_to r_error("")
end
it "when there is no parameter" do
expect { Version.new }.not_to r_error("")
end
it "when contains leading zeros" do
expect { Version.new("01.03.02") }.not_to r_error("01.03.02")
end
end
context "fail to create object" do
it "when param is range" do
expect { Version.new("0..3") }.to r_error("0..3")
end
it "when format is invalid" do
expect { Version.new(".3") }.to r_error(".3")
end
it "when version contains negative numbers" do
expect { Version.new("-2.4.4") }.to r_error("-2.4.4")
end
it "when more than one version is passed as parameter" do
expect { Version.new("1.2.3", "2.3.4") }.to r_error("1.2.3 2.3.4")
end
it "when version contain word" do
expect { Version.new("one.two.two") }.to r_error("one.two.two")
end
it "when invalid version object is passed as parameter" do
expect { Version.new(Version.new(".3")) }.to r_error(".3")
end
it "when contains invalid separater" do
expect { Version.new(Version.new("1,2,3")) }.to r_error("1,2,3")
end
end
end
describe "#<=>" do
it "return 0 when versions are equal" do
expect(Version.new("1.1") <=> "1.1").to be 0
end
it "return -1 when version1 < version2" do
expect(Version.new("1.1") <=> Version.new("2.1")).to be -1
end
it "return 1 when version1 > version2" do
expect(Version.new("2.1") <=> Version.new("1.1")).to be 1
end
end
describe "==" do
context "works" do
it "when there is zero at the end" do
expect(Version.new("1.2.0") == Version.new("1.2")).to be true
end
it "when two versions are equal" do
expect(Version.new("1.2.3") == "1.2.3").to be true
end
end
context "don't works" do
it "for 0.1 and 1" do
expect(Version.new("0.1") == Version.new("1")).to be true
end
it "when two versions are different" do
expect(Version.new("1.2.1") == Version.new("1.2.3")).to be true
end
end
end
describe "<" do
context "works" do
it "when versions are with different length " do
expect(Version.new("0.0.1") < Version.new("1")).to be true
end
it "when version < version " do
expect(Version.new("1.1.4") < Version.new("1.2.3")).to be true
end
end
context "don't works" do
it "when version == version " do
expect(Version.new("1.2.3") == "1.2.3").to be false
end
it "when version > version " do
expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
end
end
end
describe ">" do
context "works" do
it "when versions are with different length " do
expect(Version.new("1") > Version.new("0.0.1")).to be true
end
it "when version > version " do
expect(Version.new("2.1.4") > Version.new("1.2.3")).to be true
end
end
context "don't works" do
it "when version == version " do
expect(Version.new("1.2.3") == "1.2.3").to be false
end
it "when version < version " do
expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
end
end
end
describe "=>" do
context "works" do
it "when versions are with different length " do
expect(Version.new("1.0.0") > Version.new("0.4.1")).to be true
end
it "when version > version " do
expect(Version.new("2.1.4") > Version.new("1.2.3")).to be true
end
it "when version == version " do
expect(Version.new("1.2.3") == "1.2.3").to be true
end
end
context "don't works" do
it "when version < version " do
expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
end
end
end
describe "<=" do
context "works" do
it "when versions are with different length " do
expect(Version.new("2.4.1.0.0") < Version.new("2.4.2")).to be true
end
it "when version < version " do
expect(Version.new("2.1.4") < Version.new("3.2.3")).to be true
end
it "when version == version " do
expect(Version.new("1.2.3") == "1.2.3").to be true
end
end
context "don't works" do
it "when version > version " do
expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
end
end
end
describe "#to_s" do
it "when version ends with 0" do
v = Version.new("3.7.0")
expect(v.to_s).to eq "3.7"
end
it "when version don't ends with 0" do
v = Version.new("3.3.3")
expect(v.to_s).to eq "3.3.3"
end
it "return empty string when version initialized without parameter" do
v = Version.new("")
expect(v.to_s).to eq ""
end
it "return empty string when version initialized with empty string" do
v = Version.new
expect(v.to_s).to eq ""
end
end
describe "#components" do
it "return array representing version" do
expect(Version.new("1.2.3").components).to eq [1, 2, 3]
end
it "return array when version last component is zero" do
expect(Version.new("1.2.3.0.0").components).to eq [1, 2, 3]
end
it "return [] when Version.new()" do
expect(Version.new("").components).to eq []
end
it "return [] when Version.new(\"\")" do
expect(Version.new.components).to eq []
end
it "return array from first N components" do
expect(Version.new("1.2.3.4").components(3)).to eq [1, 2, 3]
end
it "return array complemented with 0's when N < version's components" do
expect(Version.new("1.2.3.4").components(6)).to eq [1, 2, 3, 4, 0, 0]
end
end
describe "Version::Range" do
describe "initialize" do
context "creates object" do
it "when boundaries are valid strings" do
expect { range("1.2.3", "2.3.4") }.not_to r_error("1.2.3 2.3.4")
end
it "when boundaries are valid version objects" do
expect { v_range("1.1.1", "2.2.2") }.not_to r_error("1.1.1 2.2.2")
end
it "when lower boundary is equal to upper" do
expect { v_range("2.2.2", "2.2.2") }.not_to r_error("2.2.2 2.2.2")
end
end
context "fails to create object" do
it "when lower boundary is greater than upper" do
expect { range("3.2.3", "2.3.4") }.to r_error("3.2.3 2.3.4")
end
it "when range contains negative boundary" do
expect { range("-3.2.3", "2.3.4") }.to r_error("-3.2.3 2.3.4")
end
it "when there is one boundary" do
expect { Version::Range.new("3.2.1") }.to r_error("3.2.1")
end
it "when there is no boundaries" do
expect { Version::Range.new }.to r_error("")
end
end
end
describe "#include?" do
context "with version string" do
it "return true when version string is in the range" do
expect(range("0.0.1", "1.1.1").include?("1.0.0")).to eq true
end
it "return true when version string is equal to lower boundary" do
expect(range("0.0.1", "1.1.1").include?("0.0.1")).to eq true
end
it "return false when version string is equal to upper boundary" do
expect(range("0.0.1", "1.1.1").include?("1.1.1")).to eq false
end
it "return false when version string is ouside the range" do
expect(range("0.0.1", "1.1.1").include?("2.1.1")).to eq false
end
it "fails when version string is invalid" do
expect(range("0.1", "1.1").include?(".1")).to r_error("0.1 1.1")
end
it "fails when version string is negative" do
expect(range("0.1", "1.1").include?("-2.1")).to r_error("0.1 1.1")
end
it "fails when there is no version string" do
expect(range("0.0.1", "1.1.1").include?).to r_error("0.0.1 1.1.1")
end
it "return false when version is empty string string" do
expect(range("0.0.1", "1.1.1").include?("")).to eq false
end
end
context "with version object" do
it "return true when version object is in the range" do
expect(range("0.0.1", "1.1.1").include?(v("1.0.0"))).to eq true
end
it "return true when version object is equal to lower boundary" do
expect(range("0.0.1", "1.1.1").include?(v("0.0.1"))).to eq true
end
it "return false when version object is equal to upper boundary" do
expect(range("0.0.1", "1.1.1").include?(v("1.1.1"))).to eq false
end
it "return false when version object is ouside the range" do
expect(range("0.1", "1.1").include?(v("2.1.1"))).to eq false
end
it "fails when version object is invalid" do
expect(range("0.1", "1.1").include?(v(".1"))).to r_error("0.1 1.1")
end
it "fails when version object is negative" do
expect(range("0.1", "1.1").include?(v("-2.1"))).to r_error("0.1 1.1")
end
it "fails when there is no version object" do
expect(range("0.0.1", "1.1.1").include?).to r_error("0.0.1 1.1.1")
end
it "return false when version object is empty string" do
expect(range("0.0.1", "1.1.1").include?(Version.new(""))).to eq false
end
end
end
describe "#to_a" do
it "return array of ranges when boundaries are valid" do
a = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expect(a.to_a).to eq(test1('1.1.0', '1.2.2'))
end
it "return [] when lower boundary is equal to upper" do
expect(v_range('1.2.2', '1.2.2').to_a).to match_array([])
end
it "fails when there is negative boundary" do
expect(v_range('1.2.2', '-1.2.2').to_a).to r_error("1.2.2 -1.2.2")
end
it "fails when there are more than three components" do
expect(v_range('1.4.4.4', '1.4.9.1').to_a).to eq(test2)
end
end
end
end

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

/tmp/d20161119-10289-1g3fw74/solution_spec.rb:25:in `block in <top (required)>': undefined method `configuration' for RSpec::Expectations:Module (NoMethodError)
	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-10289-1g3fw74/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'
/tmp/d20161119-10289-18r62mh/solution_spec.rb:25:in `block in <top (required)>': undefined method `configuration' for RSpec::Expectations:Module (NoMethodError)
	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-10289-18r62mh/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'
F

Failures:

  1) spec passes for the correct solution
     Failure/Error: expect(SOLUTION).to pass_tests
       expected this solution to pass the tests:
       
         class Version
           VALID_VERSION_REGEXP = /\A\z|\A[0-9]+(\.[0-9]+)*\z/
         
           include Comparable
         
           def initialize(version = '')
             unless VALID_VERSION_REGEXP.match(version.to_s)
               raise ArgumentError, "Invalid version string '#{version}'"
             end
         
             @components = version.to_s
               .split('.')
               .map(&:to_i)
               .reverse
               .drop_while(&:zero?)
               .reverse
           end
         
           def <=>(other)
             @components <=> Version.new(other).internal_components
           end
         
           def internal_components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def components(positions = 0)
             padding_size = positions - @components.size
         
             if padding_size > 0
               @components + [0] * padding_size
             elsif positions != 0
               @components.take(positions)
             else
               @components.dup
             end
           end
         
           def to_s
             @components.join('.')
           end
         
           class Range
             include Enumerable
         
             def initialize(start_version, end_version)
               @start_version = Version.new(start_version)
               @end_version   = Version.new(end_version)
             end
         
             def include?(version)
               (@start_version <=> version) < 1 && (@end_version <=> version) == 1
             end
         
             def each
               current_version = @start_version
         
               while (current_version <=> @end_version) == -1
                 yield current_version
         
                 current_version = increment_version(current_version)
               end
             end
         
             private
         
             def increment_version(version)
               components = version.internal_components(3)
         
               components[2] += 1
         
               components.to_enum.with_index.reverse_each do |_, index|
                 component = components[index]
         
                 if component >= 10 && components[index - 1]
                   components[index]      = 0
                   components[index - 1] += 1
                 end
               end
         
               Version.new(components.join('.'))
             end
           end
         end
       
       
       RSpec log:
     # /tmp/d20161119-19072-1ryahvx/spec.rb:180:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.71478 seconds
1 example, 1 failure

Failed examples:

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

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

Константин обнови решението на 18.11.2016 00:32 (преди почти 2 години)

+RSpec.describe 'Version' do
+ def r_error(p)
+ raise_error("Invalid version string #{p}", ArgumentError)
+ end
+
+ def range(a, b)
+ Version::Range.new(a, b)
+ end
+
+ def v_range(a, b)
+ Version::Range.new(Version.new(a), Version.new(b))
+ end
+
+ def test1(a, b)
+ v_range(a, b).to_a
+ end
+
+ def test2
+ ['1.4.4', '1.4.5', '1.4.6', '1.4.7', '1.4.8', '1.4.9']
+ end
+
+ def v(version)
+ Version.new(version)
+ end
+ RSpec::Expectations.configuration.on_potential_false_positives = :nothing
+ describe "#initialize" do
+ context "creates object" do
+ it "when valid version is passed as parameter" do
+ expect { Version.new("1.2.3") }.not_to r_error("1.2.3")
+ end
+ it "when version contains big number" do
+ expect { Version.new("1.13.3") }.not_to r_error("1.13.3")
+ end
+ it "when valid version object is passed as parameter" do
+ expect { Version.new(Version.new("2.2.2")) }.not_to r_error("2.2.2")
+ end
+ it "when pass empty string as parameter" do
+ expect { Version.new(Version.new("")) }.not_to r_error("")
+ end
+ it "when there is no parameter" do
+ expect { Version.new }.not_to r_error("")
+ end
+ it "when contains leading zeros" do
+ expect { Version.new("01.03.02") }.not_to r_error("01.03.02")
+ end
+ end
+ context "fail to create object" do
+ it "when param is range" do
+ expect { Version.new("0..3") }.to r_error("0..3")
+ end
+ it "when format is invalid" do
+ expect { Version.new(".3") }.to r_error(".3")
+ end
+ it "when version contains negative numbers" do
+ expect { Version.new("-2.4.4") }.to r_error("-2.4.4")
+ end
+ it "when more than one version is passed as parameter" do
+ expect { Version.new("1.2.3", "2.3.4") }.to r_error("1.2.3 2.3.4")
+ end
+ it "when version contain word" do
+ expect { Version.new("one.two.two") }.to r_error("one.two.two")
+ end
+ it "when invalid version object is passed as parameter" do
+ expect { Version.new(Version.new(".3")) }.to r_error(".3")
+ end
+ it "when contains invalid separater" do
+ expect { Version.new(Version.new("1,2,3")) }.to r_error("1,2,3")
+ end
+ end
+ end
+
+ describe "#<=>" do
+ it "return 0 when versions are equal" do
+ expect(Version.new("1.1") <=> "1.1").to be 0
+ end
+ it "return -1 when version1 < version2" do
+ expect(Version.new("1.1") <=> Version.new("2.1")).to be -1
+ end
+ it "return 1 when version1 > version2" do
+ expect(Version.new("2.1") <=> Version.new("1.1")).to be 1
+ end
+ end
+ describe "==" do
+ context "works" do
+ it "when there is zero at the end" do
+ expect(Version.new("1.2.0") == Version.new("1.2")).to be true
+ end
+ it "when two versions are equal" do
+ expect(Version.new("1.2.3") == "1.2.3").to be true
+ end
+ end
+
+ context "don't works" do
+ it "for 0.1 and 1" do
+ expect(Version.new("0.1") == Version.new("1")).to be true
+ end
+ it "when two versions are different" do
+ expect(Version.new("1.2.1") == Version.new("1.2.3")).to be true
+ end
+ end
+ end
+
+ describe "<" do
+ context "works" do
+ it "when versions are with different length " do
+ expect(Version.new("0.0.1") < Version.new("1")).to be true
+ end
+ it "when version < version " do
+ expect(Version.new("1.1.4") < Version.new("1.2.3")).to be true
+ end
+ end
+
+ context "don't works" do
+ it "when version == version " do
+ expect(Version.new("1.2.3") == "1.2.3").to be false
+ end
+ it "when version > version " do
+ expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
+ end
+ end
+ end
+
+ describe ">" do
+ context "works" do
+ it "when versions are with different length " do
+ expect(Version.new("1") > Version.new("0.0.1")).to be true
+ end
+ it "when version > version " do
+ expect(Version.new("2.1.4") > Version.new("1.2.3")).to be true
+ end
+ end
+
+ context "don't works" do
+ it "when version == version " do
+ expect(Version.new("1.2.3") == "1.2.3").to be false
+ end
+ it "when version < version " do
+ expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
+ end
+ end
+ end
+
+ describe "=>" do
+ context "works" do
+ it "when versions are with different length " do
+ expect(Version.new("1.0.0") > Version.new("0.4.1")).to be true
+ end
+ it "when version > version " do
+ expect(Version.new("2.1.4") > Version.new("1.2.3")).to be true
+ end
+ it "when version == version " do
+ expect(Version.new("1.2.3") == "1.2.3").to be true
+ end
+ end
+
+ context "don't works" do
+ it "when version < version " do
+ expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
+ end
+ end
+ end
+ describe "<=" do
+ context "works" do
+ it "when versions are with different length " do
+ expect(Version.new("2.4.1.0.0") < Version.new("2.4.2")).to be true
+ end
+ it "when version < version " do
+ expect(Version.new("2.1.4") < Version.new("3.2.3")).to be true
+ end
+ it "when version == version " do
+ expect(Version.new("1.2.3") == "1.2.3").to be true
+ end
+ end
+
+ context "don't works" do
+ it "when version > version " do
+ expect(Version.new("1.4.3") < Version.new("1.2.3")).to be true
+ end
+ end
+ end
+
+ describe "#to_s" do
+ it "when version ends with 0" do
+ v = Version.new("3.7.0")
+ expect(v.to_s).to eq "3.7"
+ end
+ it "when version don't ends with 0" do
+ v = Version.new("3.3.3")
+ expect(v.to_s).to eq "3.3.3"
+ end
+ it "return empty string when version initialized without parameter" do
+ v = Version.new("")
+ expect(v.to_s).to eq ""
+ end
+
+ it "return empty string when version initialized with empty string" do
+ v = Version.new
+ expect(v.to_s).to eq ""
+ end
+ end
+
+ describe "#components" do
+ it "return array representing version" do
+ expect(Version.new("1.2.3").components).to eq [1, 2, 3]
+ end
+ it "return array when version last component is zero" do
+ expect(Version.new("1.2.3.0.0").components).to eq [1, 2, 3]
+ end
+ it "return [] when Version.new()" do
+ expect(Version.new("").components).to eq []
+ end
+ it "return [] when Version.new(\"\")" do
+ expect(Version.new.components).to eq []
+ end
+ it "return array from first N components" do
+ expect(Version.new("1.2.3.4").components(3)).to eq [1, 2, 3]
+ end
+ it "return array complemented with 0's when N < version's components" do
+ expect(Version.new("1.2.3.4").components(6)).to eq [1, 2, 3, 4, 0, 0]
+ end
+ end
+
+ describe "Version::Range" do
+ describe "initialize" do
+ context "creates object" do
+ it "when boundaries are valid strings" do
+ expect { range("1.2.3", "2.3.4") }.not_to r_error("1.2.3 2.3.4")
+ end
+ it "when boundaries are valid version objects" do
+ expect { v_range("1.1.1", "2.2.2") }.not_to r_error("1.1.1 2.2.2")
+ end
+ it "when lower boundary is equal to upper" do
+ expect { v_range("2.2.2", "2.2.2") }.not_to r_error("2.2.2 2.2.2")
+ end
+ end
+ context "fails to create object" do
+ it "when lower boundary is greater than upper" do
+ expect { range("3.2.3", "2.3.4") }.to r_error("3.2.3 2.3.4")
+ end
+ it "when range contains negative boundary" do
+ expect { range("-3.2.3", "2.3.4") }.to r_error("-3.2.3 2.3.4")
+ end
+ it "when there is one boundary" do
+ expect { Version::Range.new("3.2.1") }.to r_error("3.2.1")
+ end
+ it "when there is no boundaries" do
+ expect { Version::Range.new }.to r_error("")
+ end
+ end
+ end
+ describe "#include?" do
+ context "with version string" do
+ it "return true when version string is in the range" do
+ expect(range("0.0.1", "1.1.1").include?("1.0.0")).to eq true
+ end
+ it "return true when version string is equal to lower boundary" do
+ expect(range("0.0.1", "1.1.1").include?("0.0.1")).to eq true
+ end
+ it "return false when version string is equal to upper boundary" do
+ expect(range("0.0.1", "1.1.1").include?("1.1.1")).to eq false
+ end
+ it "return false when version string is ouside the range" do
+ expect(range("0.0.1", "1.1.1").include?("2.1.1")).to eq false
+ end
+ it "fails when version string is invalid" do
+ expect(range("0.1", "1.1").include?(".1")).to r_error("0.1 1.1")
+ end
+ it "fails when version string is negative" do
+ expect(range("0.1", "1.1").include?("-2.1")).to r_error("0.1 1.1")
+ end
+ it "fails when there is no version string" do
+ expect(range("0.0.1", "1.1.1").include?).to r_error("0.0.1 1.1.1")
+ end
+ it "return false when version is empty string string" do
+ expect(range("0.0.1", "1.1.1").include?("")).to eq false
+ end
+ end
+ context "with version object" do
+ it "return true when version object is in the range" do
+ expect(range("0.0.1", "1.1.1").include?(v("1.0.0"))).to eq true
+ end
+ it "return true when version object is equal to lower boundary" do
+ expect(range("0.0.1", "1.1.1").include?(v("0.0.1"))).to eq true
+ end
+ it "return false when version object is equal to upper boundary" do
+ expect(range("0.0.1", "1.1.1").include?(v("1.1.1"))).to eq false
+ end
+ it "return false when version object is ouside the range" do
+ expect(range("0.1", "1.1").include?(v("2.1.1"))).to eq false
+ end
+ it "fails when version object is invalid" do
+ expect(range("0.1", "1.1").include?(v(".1"))).to r_error("0.1 1.1")
+ end
+ it "fails when version object is negative" do
+ expect(range("0.1", "1.1").include?(v("-2.1"))).to r_error("0.1 1.1")
+ end
+ it "fails when there is no version object" do
+ expect(range("0.0.1", "1.1.1").include?).to r_error("0.0.1 1.1.1")
+ end
+ it "return false when version object is empty string" do
+ expect(range("0.0.1", "1.1.1").include?(Version.new(""))).to eq false
+ end
+ end
+ end
+
+ describe "#to_a" do
+ it "return array of ranges when boundaries are valid" do
+ a = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
+ expect(a.to_a).to eq(test1('1.1.0', '1.2.2'))
+ end
+ it "return [] when lower boundary is equal to upper" do
+ expect(v_range('1.2.2', '1.2.2').to_a).to match_array([])
+ end
+ it "fails when there is negative boundary" do
+ expect(v_range('1.2.2', '-1.2.2').to_a).to r_error("1.2.2 -1.2.2")
+ end
+ it "fails when there are more than three components" do
+ expect(v_range('1.4.4.4', '1.4.9.1').to_a).to eq(test2)
+ end
+ end
+ end
+end