Решение на Четвърта задача - Unit тестване от Делян Лафчиев

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

Към профила на Делян Лафчиев

Резултати

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

Код

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
describe '#components' do
context 'when the components of a version are requested' do
it 'returns the Integer Array object of the canonical representation' do
version1 = Version.new("1.2.0.0")
version2 = Version.new("3.4.2.1")
expect(version1.components).to eq([1, 2])
expect(version2.components).to eq([3, 4, 2, 1])
end
end
context 'when the components of a version are requested with parameter' do
it 'returns the requested number of components' do
version = Version.new("3.4.2.1")
expect(version.components(3)).to eq([3, 4, 2])
expect(version.components(5)).to eq([3, 4, 2, 1, 0])
end
end
end
end

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

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:
       
         ...FF.....
         
         Failures:
         
           1) Version comparing Versions when a version is constructed from another Version object is equal to the one it is constructed from
              Failure/Error: eq_version_1 = Vesion.new("5.4.7")
              NameError:
                uninitialized constant Vesion
              # ./solution_spec.rb:29:in `block (4 levels) in <top (required)>'
         
           2) Version comparing Versions when a version is logically equal to another is equal to the other version
              Failure/Error: eq_version_1 = Vesion.new("5.4.0")
              NameError:
                uninitialized constant Vesion
              # ./solution_spec.rb:36:in `block (4 levels) in <top (required)>'
         
         Finished in 0.00966 seconds
         10 examples, 2 failures
         
         Failed examples:
         
         rspec ./solution_spec.rb:28 # Version comparing Versions when a version is constructed from another Version object is equal to the one it is constructed from
         rspec ./solution_spec.rb:35 # Version comparing Versions when a version is logically equal to another is equal to the other version
     # /tmp/d20161119-19072-1kq66q4/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.73189 seconds
1 example, 1 failure

Failed examples:

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

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

Делян обнови решението на 18.11.2016 23:00 (преди над 1 година)

+RSpec.describe 'Version' do
+
+end

Делян обнови решението на 18.11.2016 23:00 (преди над 1 година)

RSpec.describe 'Version' do
-
+ p 0
end

Делян обнови решението на 18.11.2016 23:24 (преди над 1 година)

RSpec.describe 'Version' do
- p 0
+ describe 'constructing a Version' do
+ context 'when the version is constructed from the empty string' do
+ end
+ end
end

Делян обнови решението на 18.11.2016 23:27 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed from the empty string' do
+ it 'is equal to 0' do
+ empty_string_version = Version.new("")
+ expect(empty_string_version).to eq Version.new("0")
+ end
+ end
+ context 'when the version is being constructed from an invalid string' do
+ it 'raises an ArgumentError' do
+ msg1 = "Invalid version string '.3.4'"
+ msg2 = "Invalid version string '1..4.7'"
+ msg3 = "Invalid version string '3.7rc'"
+ expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
+ expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
+ expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
+ end
end
end
end

Делян обнови решението на 18.11.2016 23:32 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
+ describe 'comparing Versions' do
+ context 'when a version is constructed from another Version object' do
+ it 'is equal to the one it is constructed from' do
+ eq_version_1 = Vesion.new("5.4.7")
+ eq_version_2 = Vesion.new(eq_version_1)
+ expect(eq_version_1).to eq(eq_version_2)
+ expect(eq_version_1.to_s).to eq(eq_version_2.to_s)
+ end
+ end
+ context '' do
+ end
+ context '' do
+ end
+ end
end

Делян обнови решението на 18.11.2016 23:40 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
+ context 'when the version is constructed without an argument' do
+ it 'is equal to 0' do
+ version_constructed_without_argument = Version.new
+ expect(version_constructed_without_argument).to eq Version.new("0")
+ end
+ end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
- expect(eq_version_1.to_s).to eq(eq_version_2.to_s)
end
end
- context '' do
+ context 'when a version precedes or follows another' do
+ it 'compares less or greater than the other version' do
+ old_version = Version.new("1.1.9")
+ new_version = Version.new("5.2.9")
+ newer_version = Version.new("7.0.3")
+ expect(old_version).to be < new_version
+ expect(newer_version).to be > new_version
+ end
end
context '' do
end
end
end

Делян обнови решението на 18.11.2016 23:48 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
+ context 'when a version is logically equal to another' do
+ it 'is equal to the other version' do
+ eq_version_1 = Vesion.new("5.4.0")
+ eq_version_2 = Vesion.new("5.4")
+ expect(eq_version_1).to eq(eq_version_2)
+ end
+ end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
- old_version = Version.new("1.1.9")
- new_version = Version.new("5.2.9")
+ old_version = Version.new("0.1.9")
+ new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
- context '' do
+ end
+ describe '#to_s' do
+ context 'when a version is represented as a string' do
+ it 'is represented as the string it was constructed from' do
+ version = Version.new("3.12.1")
+ expect(version.to_s).to eq("3.12.1")
+ end
+ end
+ context 'except that when a version is constructed with trailing zeros' do
+ it 'is represented canonically (without them) as a string' do
+ version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
+ expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
+ end
end
end
end

Делян обнови решението на 18.11.2016 23:51 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
+ describe '#components' do
+ context 'when the components of a version are requested' do
+ it 'returns the Integer Array object of the canonical representation' do
+
+ end
+ end
+ end
end

Делян обнови решението на 18.11.2016 23:54 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
describe '#components' do
context 'when the components of a version are requested' do
it 'returns the Integer Array object of the canonical representation' do
-
+ version1 = Version.new("1.2.0.0")
+ version2 = Version.new("3.4.2.1")
+ expect(version1.components).to eq([1, 2])
+ expect(version2.components).to eq([3, 4, 2, 1])
end
end
end
end

Делян обнови решението на 18.11.2016 23:55 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
describe '#components' do
context 'when the components of a version are requested' do
it 'returns the Integer Array object of the canonical representation' do
version1 = Version.new("1.2.0.0")
version2 = Version.new("3.4.2.1")
expect(version1.components).to eq([1, 2])
expect(version2.components).to eq([3, 4, 2, 1])
+ expect(version2.components(3)).to eq([3, 4, 2])
+ expect(version2.components(5)).to eq([3, 4, 2, 1, 0])
end
end
end
end

Делян обнови решението на 18.11.2016 23:58 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
describe '#components' do
context 'when the components of a version are requested' do
it 'returns the Integer Array object of the canonical representation' do
version1 = Version.new("1.2.0.0")
version2 = Version.new("3.4.2.1")
expect(version1.components).to eq([1, 2])
expect(version2.components).to eq([3, 4, 2, 1])
- expect(version2.components(3)).to eq([3, 4, 2])
- expect(version2.components(5)).to eq([3, 4, 2, 1, 0])
+ end
+ end
+ context 'when the components of a version are requested with parameter' do
+ it 'returns the Integer Array object of the canonical representation' do
+ version = Version.new("3.4.2.1")
+ expect(version.components(3)).to eq([3, 4, 2])
+ expect(version.components(5)).to eq([3, 4, 2, 1, 0])
end
end
end
end

Делян обнови решението на 18.11.2016 23:59 (преди над 1 година)

RSpec.describe 'Version' do
describe 'constructing a Version' do
context 'when the version is constructed without an argument' do
it 'is equal to 0' do
version_constructed_without_argument = Version.new
expect(version_constructed_without_argument).to eq Version.new("0")
end
end
context 'when the version is constructed from the empty string' do
it 'is equal to 0' do
empty_string_version = Version.new("")
expect(empty_string_version).to eq Version.new("0")
end
end
context 'when the version is being constructed from an invalid string' do
it 'raises an ArgumentError' do
msg1 = "Invalid version string '.3.4'"
msg2 = "Invalid version string '1..4.7'"
msg3 = "Invalid version string '3.7rc'"
expect { Version.new(".3.4") }.to raise_error(ArgumentError, msg1)
expect { Version.new("1..4.7") }.to raise_error(ArgumentError, msg2)
expect { Version.new("3.7rc") }.to raise_error(ArgumentError, msg3)
end
end
end
describe 'comparing Versions' do
context 'when a version is constructed from another Version object' do
it 'is equal to the one it is constructed from' do
eq_version_1 = Vesion.new("5.4.7")
eq_version_2 = Vesion.new(eq_version_1)
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version is logically equal to another' do
it 'is equal to the other version' do
eq_version_1 = Vesion.new("5.4.0")
eq_version_2 = Vesion.new("5.4")
expect(eq_version_1).to eq(eq_version_2)
end
end
context 'when a version precedes or follows another' do
it 'compares less or greater than the other version' do
old_version = Version.new("0.1.9")
new_version = Version.new("5.2")
newer_version = Version.new("7.0.3")
expect(old_version).to be < new_version
expect(newer_version).to be > new_version
end
end
end
describe '#to_s' do
context 'when a version is represented as a string' do
it 'is represented as the string it was constructed from' do
version = Version.new("3.12.1")
expect(version.to_s).to eq("3.12.1")
end
end
context 'except that when a version is constructed with trailing zeros' do
it 'is represented canonically (without them) as a string' do
version_with_trailing_zeroes = Version.new("1.1.1.0.0.0")
expect(version_with_trailing_zeroes.to_s).to eq("1.1.1")
end
end
end
describe '#components' do
context 'when the components of a version are requested' do
it 'returns the Integer Array object of the canonical representation' do
version1 = Version.new("1.2.0.0")
version2 = Version.new("3.4.2.1")
expect(version1.components).to eq([1, 2])
expect(version2.components).to eq([3, 4, 2, 1])
end
end
context 'when the components of a version are requested with parameter' do
- it 'returns the Integer Array object of the canonical representation' do
+ it 'returns the requested number of components' do
version = Version.new("3.4.2.1")
expect(version.components(3)).to eq([3, 4, 2])
expect(version.components(5)).to eq([3, 4, 2, 1, 0])
end
end
end
end