Решение на Четвърта задача - Unit тестване от Михаил Здравков

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

Към профила на Михаил Здравков

Резултати

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

Код

RSpec.describe 'Version' do
describe "#initialize" do

Относно тестването на инициализации имам въпрос. Принципно инициализацията е нещо, което не връща някакъв резултат и няма външно от обекта действие. Как е редно да се тества? В случая аз ползвам други методи като to_s в случая с Version и to_a в случая с Range за да мога да видя дали инициализацията е коректна. Обаче по тоя начин обвързвам теста за един метод с другия и това не ме кефи много.

it "initializes with another instance of the class" do
expect(Version.new(Version.new('0.42')).to_s).to eq '0.42'
end
it "initializes with no arguments" do
expect(Version.new).to eq Version.new('0')
end
it "initializes with an empty string" do
expect(Version.new('')).to eq Version.new('0')
end
it "fails when version starts with a dot" do
expect { Version.new('.1') }.to(
raise_error(ArgumentError, "Invalid version string '.1'")
)
end
it "fails when version ends with a dot" do
expect { Version.new('1.2.') }.to(
raise_error(ArgumentError, "Invalid version string '1.2.'")
)
end
it "fails when version contains letters" do
expect { Version.new('v1.2') }.to(
raise_error(ArgumentError, "Invalid version string 'v1.2'")
)
end
it "fails when version contains two dots without a number between" do
expect { Version.new('1..2') }.to(
raise_error(ArgumentError, "Invalid version string '1..2'")
)
end
it "fails when version contains negative numbers" do
expect { Version.new('-1.2') }.to(
raise_error(ArgumentError, "Invalid version string '-1.2'")
)
expect { Version.new('1.-2') }.to(
raise_error(ArgumentError, "Invalid version string '1.-2'")
)
end
end
describe "#to_s" do
it "renders the canonical representation of the version" do
expect(Version.new('1.2.0').to_s).to eq '1.2'
expect(Version.new('1.2.0.0').to_s).to eq '1.2'
expect(Version.new('0.0').to_s).to eq ''
end
end
def test_inequality_both_ways(version1, method, version2)
expect(version1.public_send(method, version2)).to be true
expect(version2.public_send(method, version1)).to be false
end
describe "#>" do
it "compares correctly in the basic cases" do
test_inequality_both_ways Version.new('1.2'), '>', Version.new('0.2')
test_inequality_both_ways Version.new('1.2'), '>', Version.new('1.1')
test_inequality_both_ways Version.new('1.3.2'), '>', Version.new('1.2.5')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2.3'), '>', Version.new('1.2')
end
end
describe "#>=" do
it "compares correctly when the versions are differnt" do
test_inequality_both_ways Version.new('1.2'), '>=', Version.new('0.2')
test_inequality_both_ways Version.new('1.2'), '>=', Version.new('1.1')
test_inequality_both_ways Version.new('1.3.2'), '>=', Version.new('1.2.5')
end
it "compares correctly when the versions are the same" do
expect(Version.new('0')).to be >= Version.new('0')
expect(Version.new('1')).to be >= Version.new('1')
expect(Version.new('1.2.3')).to be >= Version.new('1.2.3')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2.3'), '>=', Version.new('1.2')
expect(Version.new('1.2.0')).to be >= Version.new('1.2')
expect(Version.new('1.2')).to be >= Version.new('1.2.0')
end
end
describe "#<" do
it "compares correctly in the basic cases" do
test_inequality_both_ways Version.new('0.2'), '<', Version.new('1.2')
test_inequality_both_ways Version.new('1.1'), '<', Version.new('1.2')
test_inequality_both_ways Version.new('1.2.5'), '<', Version.new('1.3.2')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2'), '<', Version.new('1.2.3')
end
end
describe "#<=" do
it "compares correctly when the versions are differnt" do
test_inequality_both_ways Version.new('0.2'), '<=', Version.new('1.2')
test_inequality_both_ways Version.new('1.1'), '<=', Version.new('1.2')
test_inequality_both_ways Version.new('1.2.5'), '<=', Version.new('1.3.2')
end
it "compares correctly when the versions are the same" do
expect(Version.new('0')).to be <= Version.new('0')
expect(Version.new('1')).to be <= Version.new('1')
expect(Version.new('1.2.3')).to be <= Version.new('1.2.3')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2'), '<=', Version.new('1.2.3')
expect(Version.new('1.2.0')).to be <= Version.new('1.2')
expect(Version.new('1.2')).to be <= Version.new('1.2.0')
end
end
describe "#==" do
it "compares correctly versions of equal length" do
expect(Version.new('0')).to be == Version.new('0')
expect(Version.new('1')).to be == Version.new('1')
expect(Version.new('1.2.3')).to be == Version.new('1.2.3')
expect(Version.new('0')).to_not be == Version.new('1')
expect(Version.new('1.2.3')).to_not be == Version.new('1.3.2')
end
it "compares correctly versions of different lengths" do
expect(Version.new('1.2')).to be == Version.new('1.2.0')
expect(Version.new('0.1')).to_not be == Version.new('1')
expect(Version.new('1.2.0')).to_not be == Version.new('1.2.3')
end
end
describe "#<=>" do
it "compares correctly when the versions are differnt" do
expect(Version.new('0.2') <=> Version.new('1.2')).to be -1
expect(Version.new('1.2') <=> Version.new('0.2')).to be 1
expect(Version.new('1.1') <=> Version.new('1.2')).to be -1
expect(Version.new('1.2') <=> Version.new('1.1')).to be 1
expect(Version.new('1.2.5') <=> Version.new('1.3.2')).to be -1
expect(Version.new('1.3.2') <=> Version.new('1.2.5')).to be 1
end
it "compares correctly when the versions are the same" do
v0 = Version.new('0')
v1 = Version.new('1')
v123 = Version.new('1.2.3')
expect(v0 <=> Version.new('0')).to be 0
expect(v1 <=> Version.new('1')).to be 0
expect(v123 <=> Version.new('1.2.3')).to be 0
end
it "compares correctly when versions have different lengths" do
expect(Version.new('1.2') <=> Version.new('1.2.3')).to be -1
expect(Version.new('1.2.3') <=> Version.new('1.2')).to be 1
expect(Version.new('1.2.0') <=> Version.new('1.2')).to be 0
expect(Version.new('1.2') <=> Version.new('1.2.0')).to be 0
end
end
describe "#components" do
it "returns the components as an array" do
expect(Version.new('1.2.3').components).to eq [1, 2, 3]
end
it "does not skip leading zeros" do
expect(Version.new('0.0.2.3').components).to eq [0, 0, 2, 3]
end
it "skips trailing zeros" do
expect(Version.new('1.2.3.0').components).to eq [1, 2, 3]
expect(Version.new('1.2.3.0.0').components).to eq [1, 2, 3]
end
it "takes only the number of components that was requested" do
expect(Version.new('1.2.3').components(2)).to eq [1, 2]
end
it "fills the remaining positions with zeros if it needs" do
expect(Version.new('1.2.3').components(5)).to eq [1, 2, 3, 0, 0]
end
it "does not allow for changing the object" do
version = Version.new('1.0.42')
version.components.sort!
expect(version.components).to eq [1, 0, 42]
end
end
describe "Version::Range" do
describe "#initialize" do
it "initializes with empty strings" do
range = Version::Range.new('', '')
expect(range.to_a).to eq []
end
it "initializes from strings" do
range = Version::Range.new('1.1', '1.1.5')
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "initializes with two instances of Version" do
range = Version::Range.new(Version.new('1.1'), Version.new('1.1.5'))
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "initializes with combinations of Version instances and strings" do
range1 = Version::Range.new(Version.new('1.1'), '1.1.5')
expect(range1.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
range2 = Version::Range.new('1.1', Version.new('1.1.5'))
expect(range2.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
end
describe "#include?" do
let(:range) { Version::Range.new('1.1', '1.1.5') }
it "accepts a string argument" do
expect(range.include?('1.1.2')).to be true
end
it "includes the starting version" do
expect(range.include?('1.1')).to be true
end
it "excludes the end version" do
expect(range.include?('1.1.5')).to be false
end
it "accepts a Version instance as an argumnt" do
expect(range.include?(Version.new('1.1.2'))).to be true
end
end
describe "#to_a" do
it "returns all version in the interval [start, end)" do
range = Version::Range.new('1.1', '1.1.5')
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "increments all components in order" do
range = Version::Range.new('0.9.8', '1.0.2')
expect(range.to_a).to eq ['0.9.8', '0.9.9', '1', '1.0.1']
end
it "returns an empty array when start and end versions are the same" do
range = Version::Range.new('1.1.5', '1.1.5')
expect(range.to_a).to eq []
end
end
end
end

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

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

Failures:

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

Finished in 19.2 seconds
19 examples, 1 failure

Failed examples:

rspec /tmp/d20161119-19072-xdwuzo/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

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

Михаил обнови решението на 16.11.2016 01:16 (преди над 7 години)

+RSpec.describe 'Version' do
+ before(:all) do
+ @v0 = Version.new('0')

Това не ми харесва много, че ми се наложи да го правя, но смятам, че е хубаво да тествам сравнението за равенство, а ако използвам expect(something == something) Рубокопа се оплаква, че сравнявам нещо със същото нещо и не ми дава да си кача решението. По тоя начин го забаламосвам.

+ @v1 = Version.new('1')
+ @v123 = Version.new('1.2.3')
+ end
+
+ describe "#initialize" do

Относно тестването на инициализации имам въпрос. Принципно инициализацията е нещо, което не връща някакъв резултат и няма външно от обекта действие. Как е редно да се тества? В случая аз ползвам други методи като to_s в случая с Version и to_a в случая с Range за да мога да видя дали инициализацията е коректна. Обаче по тоя начин обвързвам теста за един метод с другия и това не ме кефи много.

+ it "initializes with another instance of the class" do
+ expect(Version.new(Version.new('0.42')).to_s).to eq '0.42'
+ end
+
+ it "initializes with no arguments" do
+ expect(Version.new).to eq Version.new('0')
+ end
+
+ it "initializes with an empty string" do
+ expect(Version.new('')).to eq Version.new('0')
+ end
+
+ it "fails when version starts with a dot" do
+ expect { Version.new('.1') }.to(
+ raise_error(ArgumentError, "Invalid version string '.1'")
+ )
+ end
+
+ it "fails when version ends with a dot" do
+ expect { Version.new('1.2.') }.to(
+ raise_error(ArgumentError, "Invalid version string '1.2.'")
+ )
+ end
+
+ it "fails when version contains letters" do
+ expect { Version.new('v1.2') }.to(
+ raise_error(ArgumentError, "Invalid version string 'v1.2'")
+ )
+ end
+
+ it "fails when version contains two dots without a number between" do
+ expect { Version.new('1..2') }.to(
+ raise_error(ArgumentError, "Invalid version string '1..2'")
+ )
+ end
+
+ it "fails when version contains negative numbers" do
+ expect { Version.new('-1.2') }.to(
+ raise_error(ArgumentError, "Invalid version string '-1.2'")
+ )
+
+ expect { Version.new('1.-2') }.to(
+ raise_error(ArgumentError, "Invalid version string '1.-2'")
+ )
+ end
+ end
+
+ describe "#to_s" do
+ it "renders the canonical representation of the version" do
+ expect(Version.new('1.2.0').to_s).to eq '1.2'
+ expect(Version.new('1.2.0.0').to_s).to eq '1.2'
+ expect(Version.new('0.0').to_s).to eq ''
+ end
+ end
+
+ def test_inequality_both_ways(version1, method, version2)
+ expect(version1.send(method, version2)).to be true
+ expect(version2.send(method, version1)).to be false
+ end
+
+ describe "#>" do
+ it "compares correctly in the basic cases" do
+ test_inequality_both_ways Version.new('1.2'), '>', Version.new('0.2')
+
+ test_inequality_both_ways Version.new('1.2'), '>', Version.new('1.1')
+
+ test_inequality_both_ways Version.new('1.3.2'), '>', Version.new('1.2.5')
+ end
+
+ it "compares correctly when versions have different lengths" do
+ test_inequality_both_ways Version.new('1.2.3'), '>', Version.new('1.2')
+ end
+ end
+
+ describe "#>=" do
+ it "compares correctly when the versions are differnt" do
+ test_inequality_both_ways Version.new('1.2'), '>=', Version.new('0.2')
+
+ test_inequality_both_ways Version.new('1.2'), '>=', Version.new('1.1')
+
+ test_inequality_both_ways Version.new('1.3.2'), '>=', Version.new('1.2.5')
+ end
+
+ it "compares correctly when the versions are the same" do
+ expect(@v0 >= Version.new('0')).to be true
+ expect(@v1 >= Version.new('1')).to be true
+ expect(@v123 >= Version.new('1.2.3')).to be true
+ end
+
+ it "compares correctly when versions have different lengths" do
+ test_inequality_both_ways Version.new('1.2.3'), '>=', Version.new('1.2')
+
+ expect(Version.new('1.2.0') >= Version.new('1.2')).to be true
+ expect(Version.new('1.2') >= Version.new('1.2.0')).to be true
+ end
+ end
+
+ describe "#<" do
+ it "compares correctly in the basic cases" do
+ test_inequality_both_ways Version.new('0.2'), '<', Version.new('1.2')
+
+ test_inequality_both_ways Version.new('1.1'), '<', Version.new('1.2')
+
+ test_inequality_both_ways Version.new('1.2.5'), '<', Version.new('1.3.2')
+ end
+
+ it "compares correctly when versions have different lengths" do
+ test_inequality_both_ways Version.new('1.2'), '<', Version.new('1.2.3')
+ end
+ end
+
+ describe "#<=" do
+ it "compares correctly when the versions are differnt" do
+ test_inequality_both_ways Version.new('0.2'), '<=', Version.new('1.2')
+
+ test_inequality_both_ways Version.new('1.1'), '<=', Version.new('1.2')
+
+ test_inequality_both_ways Version.new('1.2.5'), '<=', Version.new('1.3.2')
+ end
+
+ it "compares correctly when the versions are the same" do
+ expect(@v0 <= Version.new('0')).to be true
+ expect(@v1 <= Version.new('1')).to be true
+ expect(@v123 <= Version.new('1.2.3')).to be true
+ end
+
+ it "compares correctly when versions have different lengths" do
+ test_inequality_both_ways Version.new('1.2'), '<=', Version.new('1.2.3')
+
+ expect(Version.new('1.2.0') <= Version.new('1.2')).to be true
+ expect(Version.new('1.2') <= Version.new('1.2.0')).to be true
+ end
+ end
+
+ describe "#==" do
+ it "compares correctly versions of equal length" do
+ expect(@v0 == Version.new('0')).to be true
+ expect(@v1 == Version.new('1')).to be true
+ expect(@v123 == Version.new('1.2.3')).to be true
+
+ expect(Version.new('0') == Version.new('1')).to be false
+ expect(Version.new('1.2.3') == Version.new('1.3.2')).to be false
+ end
+
+ it "compares correctly versions of different lengths" do
+ expect(Version.new('1.2') == Version.new('1.2.0')).to be true
+
+ expect(Version.new('0.1') == Version.new('1')).to be false
+ expect(Version.new('1.2.0') == Version.new('1.2.3')).to be false
+ end
+ end
+
+ describe "#<=>" do
+ it "compares correctly when the versions are differnt" do
+ expect(Version.new('0.2') <=> Version.new('1.2')).to be -1
+ expect(Version.new('1.2') <=> Version.new('0.2')).to be 1
+
+ expect(Version.new('1.1') <=> Version.new('1.2')).to be -1
+ expect(Version.new('1.2') <=> Version.new('1.1')).to be 1
+
+ expect(Version.new('1.2.5') <=> Version.new('1.3.2')).to be -1
+ expect(Version.new('1.3.2') <=> Version.new('1.2.5')).to be 1
+ end
+
+ it "compares correctly when the versions are the same" do
+ expect(@v0 <=> Version.new('0')).to be 0
+ expect(@v1 <=> Version.new('1')).to be 0
+ expect(@v123 <=> Version.new('1.2.3')).to be 0
+ end
+
+ it "compares correctly when versions have different lengths" do
+ expect(Version.new('1.2') <=> Version.new('1.2.3')).to be -1
+ expect(Version.new('1.2.3') <=> Version.new('1.2')).to be 1
+
+ expect(Version.new('1.2.0') <=> Version.new('1.2')).to be 0
+ expect(Version.new('1.2') <=> Version.new('1.2.0')).to be 0
+ end
+ end
+
+ describe "#components" do
+ it "returns the components as an array" do
+ expect(Version.new('1.2.3').components).to eq [1, 2, 3]
+ end
+
+ it "does not skip leading zeros" do
+ expect(Version.new('0.0.2.3').components).to eq [0, 0, 2, 3]
+ end
+
+ it "skips trailing zeros" do
+ expect(Version.new('1.2.3.0').components).to eq [1, 2, 3]
+ expect(Version.new('1.2.3.0.0').components).to eq [1, 2, 3]
+ end
+
+ it "takes only the number of components that was requested" do
+ expect(Version.new('1.2.3').components(2)).to eq [1, 2]
+ end
+
+ it "fills the remaining positions with zeros if it needs" do
+ expect(Version.new('1.2.3').components(5)).to eq [1, 2, 3, 0, 0]
+ end
+
+ it "does not allow for changing the object" do
+ version = Version.new('1.0.42')
+ version.components.sort!
+ expect(version.components).to eq [1, 0, 42]
+ end
+ end
+
+ describe "Version::Range" do
+ describe "#initialize" do
+ it "initializes with empty strings" do
+ range = Version::Range.new('', '')
+ expect(range.to_a).to eq []
+ end
+
+ it "initializes from strings" do
+ range = Version::Range.new('1.1', '1.1.5')
+ expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
+ end
+
+ it "initializes with two instances of Version" do
+ range = Version::Range.new(Version.new('1.1'), Version.new('1.1.5'))
+ expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
+ end
+
+ it "initializes with combinations of Version instances and strings" do
+ range1 = Version::Range.new(Version.new('1.1'), '1.1.5')
+ expect(range1.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
+
+ range2 = Version::Range.new('1.1', Version.new('1.1.5'))
+ expect(range2.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
+ end
+ end
+
+ describe "#include?" do
+ before(:all) do

Не използвай before(:all), а само before (еквивалентно е на before(:each)). В този случай, ако нещо "счупи" обекта, той ще продължи да бъде счупен и в останалите тестове - тоест ще "протече" някакво състояние, което е лошо.

А още по-добре, направи го с let.

+ @range = Version::Range.new('1.1', '1.1.5')
+ end
+
+ it "accepts a string argument" do
+ expect(@range.include?('1.1.2')).to be true
+ end
+
+ it "includes the starting version" do
+ expect(@range.include?('1.1')).to be true
+ end
+
+ it "excludes the end version" do
+ expect(@range.include?('1.1.5')).to be false
+ end
+
+ it "accepts a Version instance as an argumnt" do
+ expect(@range.include?(Version.new('1.1.2'))).to be true
+ end
+ end
+
+ describe "#to_a" do
+ it "returns all version in the interval [start, end)" do
+ range = Version::Range.new('1.1', '1.1.5')
+ expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
+ end
+
+ it "increments all components in order" do
+ range = Version::Range.new('0.9.8', '1.0.2')
+ expect(range.to_a).to eq ['0.9.8', '0.9.9', '1', '1.0.1']
+ end
+
+ it "returns an empty array when start and end versions are the same" do
+ range = Version::Range.new('1.1.5', '1.1.5')
+ expect(range.to_a).to eq []
+ end
+ end
+ end
+end

Михаил обнови решението на 18.11.2016 08:34 (преди над 7 години)

RSpec.describe 'Version' do
- before(:all) do
- @v0 = Version.new('0')
- @v1 = Version.new('1')
- @v123 = Version.new('1.2.3')
- end
-
describe "#initialize" do
it "initializes with another instance of the class" do
expect(Version.new(Version.new('0.42')).to_s).to eq '0.42'
end
it "initializes with no arguments" do
expect(Version.new).to eq Version.new('0')
end
it "initializes with an empty string" do
expect(Version.new('')).to eq Version.new('0')
end
it "fails when version starts with a dot" do
expect { Version.new('.1') }.to(
raise_error(ArgumentError, "Invalid version string '.1'")
)
end
it "fails when version ends with a dot" do
expect { Version.new('1.2.') }.to(
raise_error(ArgumentError, "Invalid version string '1.2.'")
)
end
it "fails when version contains letters" do
expect { Version.new('v1.2') }.to(
raise_error(ArgumentError, "Invalid version string 'v1.2'")
)
end
it "fails when version contains two dots without a number between" do
expect { Version.new('1..2') }.to(
raise_error(ArgumentError, "Invalid version string '1..2'")
)
end
it "fails when version contains negative numbers" do
expect { Version.new('-1.2') }.to(
raise_error(ArgumentError, "Invalid version string '-1.2'")
)
expect { Version.new('1.-2') }.to(
raise_error(ArgumentError, "Invalid version string '1.-2'")
)
end
end
describe "#to_s" do
it "renders the canonical representation of the version" do
expect(Version.new('1.2.0').to_s).to eq '1.2'
expect(Version.new('1.2.0.0').to_s).to eq '1.2'
expect(Version.new('0.0').to_s).to eq ''
end
end
def test_inequality_both_ways(version1, method, version2)
- expect(version1.send(method, version2)).to be true
- expect(version2.send(method, version1)).to be false
+ expect(version1.public_send(method, version2)).to be true
+ expect(version2.public_send(method, version1)).to be false
end
describe "#>" do
it "compares correctly in the basic cases" do
test_inequality_both_ways Version.new('1.2'), '>', Version.new('0.2')
test_inequality_both_ways Version.new('1.2'), '>', Version.new('1.1')
test_inequality_both_ways Version.new('1.3.2'), '>', Version.new('1.2.5')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2.3'), '>', Version.new('1.2')
end
end
describe "#>=" do
it "compares correctly when the versions are differnt" do
test_inequality_both_ways Version.new('1.2'), '>=', Version.new('0.2')
test_inequality_both_ways Version.new('1.2'), '>=', Version.new('1.1')
test_inequality_both_ways Version.new('1.3.2'), '>=', Version.new('1.2.5')
end
it "compares correctly when the versions are the same" do
- expect(@v0 >= Version.new('0')).to be true
- expect(@v1 >= Version.new('1')).to be true
- expect(@v123 >= Version.new('1.2.3')).to be true
+ expect(Version.new('0')).to be >= Version.new('0')
+ expect(Version.new('1')).to be >= Version.new('1')
+ expect(Version.new('1.2.3')).to be >= Version.new('1.2.3')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2.3'), '>=', Version.new('1.2')
- expect(Version.new('1.2.0') >= Version.new('1.2')).to be true
- expect(Version.new('1.2') >= Version.new('1.2.0')).to be true
+ expect(Version.new('1.2.0')).to be >= Version.new('1.2')
+ expect(Version.new('1.2')).to be >= Version.new('1.2.0')
end
end
describe "#<" do
it "compares correctly in the basic cases" do
test_inequality_both_ways Version.new('0.2'), '<', Version.new('1.2')
test_inequality_both_ways Version.new('1.1'), '<', Version.new('1.2')
test_inequality_both_ways Version.new('1.2.5'), '<', Version.new('1.3.2')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2'), '<', Version.new('1.2.3')
end
end
describe "#<=" do
it "compares correctly when the versions are differnt" do
test_inequality_both_ways Version.new('0.2'), '<=', Version.new('1.2')
test_inequality_both_ways Version.new('1.1'), '<=', Version.new('1.2')
test_inequality_both_ways Version.new('1.2.5'), '<=', Version.new('1.3.2')
end
it "compares correctly when the versions are the same" do
- expect(@v0 <= Version.new('0')).to be true
- expect(@v1 <= Version.new('1')).to be true
- expect(@v123 <= Version.new('1.2.3')).to be true
+ expect(Version.new('0')).to be <= Version.new('0')
+ expect(Version.new('1')).to be <= Version.new('1')
+ expect(Version.new('1.2.3')).to be <= Version.new('1.2.3')
end
it "compares correctly when versions have different lengths" do
test_inequality_both_ways Version.new('1.2'), '<=', Version.new('1.2.3')
- expect(Version.new('1.2.0') <= Version.new('1.2')).to be true
- expect(Version.new('1.2') <= Version.new('1.2.0')).to be true
+ expect(Version.new('1.2.0')).to be <= Version.new('1.2')
+ expect(Version.new('1.2')).to be <= Version.new('1.2.0')
end
end
describe "#==" do
it "compares correctly versions of equal length" do
- expect(@v0 == Version.new('0')).to be true
- expect(@v1 == Version.new('1')).to be true
- expect(@v123 == Version.new('1.2.3')).to be true
+ expect(Version.new('0')).to be == Version.new('0')
+ expect(Version.new('1')).to be == Version.new('1')
+ expect(Version.new('1.2.3')).to be == Version.new('1.2.3')
- expect(Version.new('0') == Version.new('1')).to be false
- expect(Version.new('1.2.3') == Version.new('1.3.2')).to be false
+ expect(Version.new('0')).to_not be == Version.new('1')
+ expect(Version.new('1.2.3')).to_not be == Version.new('1.3.2')
end
it "compares correctly versions of different lengths" do
- expect(Version.new('1.2') == Version.new('1.2.0')).to be true
+ expect(Version.new('1.2')).to be == Version.new('1.2.0')
- expect(Version.new('0.1') == Version.new('1')).to be false
- expect(Version.new('1.2.0') == Version.new('1.2.3')).to be false
+ expect(Version.new('0.1')).to_not be == Version.new('1')
+ expect(Version.new('1.2.0')).to_not be == Version.new('1.2.3')
end
end
describe "#<=>" do
it "compares correctly when the versions are differnt" do
expect(Version.new('0.2') <=> Version.new('1.2')).to be -1
expect(Version.new('1.2') <=> Version.new('0.2')).to be 1
expect(Version.new('1.1') <=> Version.new('1.2')).to be -1
expect(Version.new('1.2') <=> Version.new('1.1')).to be 1
expect(Version.new('1.2.5') <=> Version.new('1.3.2')).to be -1
expect(Version.new('1.3.2') <=> Version.new('1.2.5')).to be 1
end
it "compares correctly when the versions are the same" do
- expect(@v0 <=> Version.new('0')).to be 0
- expect(@v1 <=> Version.new('1')).to be 0
- expect(@v123 <=> Version.new('1.2.3')).to be 0
+ v0 = Version.new('0')
+ v1 = Version.new('1')
+ v123 = Version.new('1.2.3')
+ expect(v0 <=> Version.new('0')).to be 0
+ expect(v1 <=> Version.new('1')).to be 0
+ expect(v123 <=> Version.new('1.2.3')).to be 0
end
it "compares correctly when versions have different lengths" do
expect(Version.new('1.2') <=> Version.new('1.2.3')).to be -1
expect(Version.new('1.2.3') <=> Version.new('1.2')).to be 1
expect(Version.new('1.2.0') <=> Version.new('1.2')).to be 0
expect(Version.new('1.2') <=> Version.new('1.2.0')).to be 0
end
end
describe "#components" do
it "returns the components as an array" do
expect(Version.new('1.2.3').components).to eq [1, 2, 3]
end
it "does not skip leading zeros" do
expect(Version.new('0.0.2.3').components).to eq [0, 0, 2, 3]
end
it "skips trailing zeros" do
expect(Version.new('1.2.3.0').components).to eq [1, 2, 3]
expect(Version.new('1.2.3.0.0').components).to eq [1, 2, 3]
end
it "takes only the number of components that was requested" do
expect(Version.new('1.2.3').components(2)).to eq [1, 2]
end
it "fills the remaining positions with zeros if it needs" do
expect(Version.new('1.2.3').components(5)).to eq [1, 2, 3, 0, 0]
end
it "does not allow for changing the object" do
version = Version.new('1.0.42')
version.components.sort!
expect(version.components).to eq [1, 0, 42]
end
end
describe "Version::Range" do
describe "#initialize" do
it "initializes with empty strings" do
range = Version::Range.new('', '')
expect(range.to_a).to eq []
end
it "initializes from strings" do
range = Version::Range.new('1.1', '1.1.5')
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "initializes with two instances of Version" do
range = Version::Range.new(Version.new('1.1'), Version.new('1.1.5'))
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "initializes with combinations of Version instances and strings" do
range1 = Version::Range.new(Version.new('1.1'), '1.1.5')
expect(range1.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
range2 = Version::Range.new('1.1', Version.new('1.1.5'))
expect(range2.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
end
describe "#include?" do
- before(:all) do
- @range = Version::Range.new('1.1', '1.1.5')
- end
+ let(:range) { Version::Range.new('1.1', '1.1.5') }
it "accepts a string argument" do
- expect(@range.include?('1.1.2')).to be true
+ expect(range.include?('1.1.2')).to be true
end
it "includes the starting version" do
- expect(@range.include?('1.1')).to be true
+ expect(range.include?('1.1')).to be true
end
it "excludes the end version" do
- expect(@range.include?('1.1.5')).to be false
+ expect(range.include?('1.1.5')).to be false
end
it "accepts a Version instance as an argumnt" do
- expect(@range.include?(Version.new('1.1.2'))).to be true
+ expect(range.include?(Version.new('1.1.2'))).to be true
end
end
describe "#to_a" do
it "returns all version in the interval [start, end)" do
range = Version::Range.new('1.1', '1.1.5')
expect(range.to_a).to eq ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4']
end
it "increments all components in order" do
range = Version::Range.new('0.9.8', '1.0.2')
expect(range.to_a).to eq ['0.9.8', '0.9.9', '1', '1.0.1']
end
it "returns an empty array when start and end versions are the same" do
range = Version::Range.new('1.1.5', '1.1.5')
expect(range.to_a).to eq []
end
end
end
end