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

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

Към профила на Иван Станков

Резултати

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

Код

describe Version do
context "Validation" do
it "raises an ArgumentError if version is not in valid format" do
expect { Version.new('1.42-21.0') }.to raise_error(ArgumentError)
expect { Version.new('1.42b21.0') }.to raise_error(ArgumentError)
expect { Version.new('.04') }.to raise_error(ArgumentError)
expect { Version.new('0.4..0') }.to raise_error(ArgumentError)
end
context "invalid version string error" do
it "prints the correct message format" do
expect { Version.new('30wrong.f.0.rmat') }.to raise_error(
ArgumentError, "Invalid version string '30wrong.f.0.rmat'"
)
expect { Version.new '1.3.5.' }.to raise_error(
ArgumentError, "Invalid version string '1.3.5.'"
)
end
end
end
context "#to_s" do
it "returns version string" do
expect(Version.new('1.1.1').to_s).to eq '1.1.1'
end
end
context "initialization" do
it "can create new empty version" do
expect(Version.new).to eq 0
end
it "can create simple version(e.g. 1.1.5)" do
expect(Version.new('1.1.5').to_s).to eq '1.1.5'
expect(Version.new('0.11.53.55555555565432.12').to_s).to eq(
'0.11.53.55555555565432.12'
)
end
it 'can remove the unnecessary zeroes' do
expect(Version.new('1.4.5.0').to_s).to eq '1.4.5'
expect(Version.new('01.040.5.10.000').to_s).to eq '1.40.5.10'
end
end
context '#components' do
it 'can find the components of a version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.00.0').components).to eq [1, 3, 5]
expect(Version.new('01.03.005.000.07.0').components).to eq [1, 3, 5, 0, 7]
end
it 'can return N components of a version' do
version = Version.new('1.4.66.320.00.3.1')
expect(version.components(3)).to eq [1, 4, 66]
end
it 'can return N components when N is bigger than # of components' do
version = Version.new('4.5')
expect(version.components(5)).to eq [4, 5, 0, 0, 0]
end
end
context 'Comparing of versions' do
it 'can check if a version is lower than other version' do
expect(Version.new('1.0')).to be < Version.new('1.02.4')
expect(Version.new('1.02.4')).to be < Version.new('1.2.5.1')
expect(Version.new('0.2.5.1')).to be < Version.new('1.02.4')
end
it 'can check if a version is bigger than other version' do
expect(Version.new('1.2.5.1')).to be > Version.new('1.02.4')
expect(Version.new('1.0')).to be > Version.new('0.2.5.1')
expect(Version.new('1.2.5.1')).to be > Version.new('0.2.5.1')
end
it 'can check if versions are equal' do
expect(Version.new('1.02.4')).to be == Version.new('1.2.4')
end
it 'can compare two versions with <=>, <=, >=' do
expect(Version.new('1.02.4') <=> Version.new('1.2.5.1')).to eq -1
expect(Version.new('1.02.4') <=> Version.new('1.0')).to eq 1
expect(Version.new('1.02.4') <=> Version.new('1.2.4')).to eq 0
expect(Version.new('1.0')).to be <= Version.new('1.2.5.1')
expect(Version.new('1.0')).to be >= '1.0'
end
end
end
describe Version::Range do
context '#to_a' do
it 'can return an array of versions' do
expect(Version::Range.new('1.2.1', '1.2.3').to_a).to eq ['1.2.1', '1.2.2']
expect(
Version::Range.new(Version.new('1.0.1'), Version.new('1.0.3')).to_a
).to eq ['1.0.1', '1.0.2']
end
end
context '#include?' do
it 'can check if simple range includes version' do
expect(
Version::Range.new('1.2.1', '1.2.3').include?(Version.new('1.2.2'))
).to be true
end
it 'can check if wider range includes version' do
test_range = Version::Range.new('1.1', '2.5')
expect(test_range.include?(Version.new('1.2.8'))).to be true
expect(test_range.include?(Version.new('2.4.9'))).to be true
expect(test_range.include?(Version.new('2.0.1'))).to be true
expect(test_range.include?(Version.new('1.2.3'))).to be true
expect(test_range.include?(Version.new('2.5'))).to be false
expect(test_range.include?(Version.new('1.0.4'))).to be false
end
end
end

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

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

Failures:

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

  2) spec Version tests that #components cannot be used to modify the version
     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
             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-cllt4r/spec.rb:456: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 each #to_a element
     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
           def to_a
             versions = enum_for(:each).to_a
             versions[1..-2] = [42] * (versions.size - 2)
             versions
           end
             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-cllt4r/spec.rb:544: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 16.22 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-cllt4r/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-cllt4r/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
rspec /tmp/d20161119-19072-cllt4r/spec.rb:535 # spec Version::Range tests each #to_a element

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

Иван обнови решението на 12.11.2016 12:42 (преди над 1 година)

+describe Version do
+ context "Validation" do
+ it "raises an ArgumentError if version is not in valid format" do
+ expect { Version.new('1.42-21.0') }.to raise_error(ArgumentError)
+ expect { Version.new('1.42b21.0') }.to raise_error(ArgumentError)
+ expect { Version.new('.04') }.to raise_error(ArgumentError)
+ expect { Version.new('0.4..0') }.to raise_error(ArgumentError)
+ end
+
+ context "invalid version string error" do
+ it "prints the correct message format" do
+ expect { Version.new('30wrong.f.0.rmat') }.to raise_error(
+ ArgumentError, "Invalid version string '30wrong.f.0.rmat'"
+ )
+ expect { Version.new '1.3.5.' }.to raise_error(
+ ArgumentError, "Invalid version string '1.3.5.'"
+ )
+ end
+ end
+ end
+
+ context "initialization" do
+ it "can create new empty version" do
+ expect(Version.new).to eq 0
+ end
+
+ it "can create simple version(e.g. 1.1.5)" do
+ expect(Version.new('1.1.5')).to eq '1.1.5'
+ expect(Version.new('0.11.53.55555555565432.12')).to eq(
+ '0.11.53.55555555565432.12'
+ )
+ end
+
+ it 'can remove the unnecessary zeroes' do
+ expect(Version.new('1.4.5.0')).to eq '1.4.5'
+ expect(Version.new('01.040.5.10.000')).to eq '1.40.5.10'
+ end
+ end
+
+ context '#components' do
+ it 'can find the components of a version' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.5.0').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.5.0.00.0').components).to eq [1, 3, 5]
+ expect(Version.new('01.03.005.000.07.0').components).to eq [1, 3, 5, 0, 7]
+ end
+
+ it 'can return N components of a version' do
+ version = Version.new('1.4.66.320.00.3.1')
+ expect(version.components(5)).to eq [1, 4, 66]
+ end
+
+ it 'can return N components when N is bigger than # of components' do
+ version = Version.new('4.5')
+ expect(version.components(5)).to eq [4, 5, 0, 0, 0]
+ end
+ end
+end

Иван обнови решението на 12.11.2016 14:17 (преди над 1 година)

describe Version do
context "Validation" do
it "raises an ArgumentError if version is not in valid format" do
expect { Version.new('1.42-21.0') }.to raise_error(ArgumentError)
expect { Version.new('1.42b21.0') }.to raise_error(ArgumentError)
expect { Version.new('.04') }.to raise_error(ArgumentError)
expect { Version.new('0.4..0') }.to raise_error(ArgumentError)
end
context "invalid version string error" do
it "prints the correct message format" do
expect { Version.new('30wrong.f.0.rmat') }.to raise_error(
ArgumentError, "Invalid version string '30wrong.f.0.rmat'"
)
expect { Version.new '1.3.5.' }.to raise_error(
ArgumentError, "Invalid version string '1.3.5.'"
)
end
end
end
+ context "#to_s" do
+ it "returns version string" do
+ expect(Version.new('1.1.1').to_s).to eq '1.1.1'
+ end
+ end
+
context "initialization" do
it "can create new empty version" do
expect(Version.new).to eq 0
end
it "can create simple version(e.g. 1.1.5)" do
- expect(Version.new('1.1.5')).to eq '1.1.5'
- expect(Version.new('0.11.53.55555555565432.12')).to eq(
+ expect(Version.new('1.1.5').to_s).to eq '1.1.5'
+ expect(Version.new('0.11.53.55555555565432.12').to_s).to eq(
'0.11.53.55555555565432.12'
)
end
it 'can remove the unnecessary zeroes' do
- expect(Version.new('1.4.5.0')).to eq '1.4.5'
- expect(Version.new('01.040.5.10.000')).to eq '1.40.5.10'
+ expect(Version.new('1.4.5.0').to_s).to eq '1.4.5'
+ expect(Version.new('01.040.5.10.000').to_s).to eq '1.40.5.10'
end
end
context '#components' do
it 'can find the components of a version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.00.0').components).to eq [1, 3, 5]
expect(Version.new('01.03.005.000.07.0').components).to eq [1, 3, 5, 0, 7]
end
it 'can return N components of a version' do
version = Version.new('1.4.66.320.00.3.1')
- expect(version.components(5)).to eq [1, 4, 66]
+ expect(version.components(3)).to eq [1, 4, 66]
end
it 'can return N components when N is bigger than # of components' do
version = Version.new('4.5')
expect(version.components(5)).to eq [4, 5, 0, 0, 0]
+ end
+ end
+
+ context 'Comparing of versions' do
+ v1 = Version.new('1.0')
+ v2 = Version.new('1.02.4')
+ v3 = Version.new('1.2.5.1')
+ v4 = Version.new('0.2.5.1')

Не е добра идея да ги преизползваш по този начин, защото се запазват между различните тестове. Ако първия тест счупи някой от обектите, следващия тест ще фейлне.

Виж let. let ще инстанцира тези обекти за всеки тест поотделно и няма как да leak-не състоянието от единия тест в другия.

И друго - v1, v2, v3, v4 по принцип не са добри имена, защото при четенето на теста ме караш постоянно да скачам до тези редове, за да проверя какво означават.

Имаш три варианта: - Да им измислиш по-добри имена. Не съм сигурен колко лесно е в този случай. - Дублиране на код в тестовете не трябва да те притеснява особено, ако ще ги направи по-прости. Тоест можеш и да си ги copy-paste-неш долу. - Можеш да си направиш някакъв помощен метод, който да ти спести писането на Version.new

+
+ it 'can check if a version is lower than other version' do
+ expect(v1 < v2).to eq true
+ expect(v3 < v2).to eq false
+ expect(v4 < v2).to eq true
+ end
+
+ it 'can check if a version is bigger than other version' do
+ expect(v2 > v3).to eq false
+ expect(v1 > v4).to eq true
+ expect(v3 > v4).to eq true
+ end
+
+ it 'can check if versions are equal' do
+ expect(v2 == Version.new('1.2.4')).to eq true
+ end
+
+ it 'can compare two versions with <=>, <=, >=' do
+ expect(v2 <=> v3).to eq -1
+ expect(v2 <=> v1).to eq 1
+ expect(v2 <=> Version.new('1.2.4')).to eq 0
+ expect(v1 >= v3).to eq false
+ expect(v1 <= '1.0').to eq true
+ end
+ end
+end
+describe Version::Range do
+ v1 = Version.new('1.0.1')
+ v2 = Version.new('1.0.2')
+
+ # context '#include?' do
+ # it 'can check if simple range includes version' do
+ #
+ # end
+ # end
+
+ context '#to_a' do
+ it 'can return an array of versions' do
+ expect(Range.new('1.2.1', '1.2.2').to_a).to eq ['1.2.1', '1.2.2']
+ expect(Range.new(v1, v2).to_a).to eq ['1.0.1', '1.0.2']
end
end
end

Иван обнови решението на 13.11.2016 10:41 (преди над 1 година)

describe Version do
context "Validation" do
it "raises an ArgumentError if version is not in valid format" do
expect { Version.new('1.42-21.0') }.to raise_error(ArgumentError)
expect { Version.new('1.42b21.0') }.to raise_error(ArgumentError)
expect { Version.new('.04') }.to raise_error(ArgumentError)
expect { Version.new('0.4..0') }.to raise_error(ArgumentError)
end
context "invalid version string error" do
it "prints the correct message format" do
expect { Version.new('30wrong.f.0.rmat') }.to raise_error(
ArgumentError, "Invalid version string '30wrong.f.0.rmat'"
)
expect { Version.new '1.3.5.' }.to raise_error(
ArgumentError, "Invalid version string '1.3.5.'"
)
end
end
end
context "#to_s" do
it "returns version string" do
expect(Version.new('1.1.1').to_s).to eq '1.1.1'
end
end
context "initialization" do
it "can create new empty version" do
expect(Version.new).to eq 0
end
it "can create simple version(e.g. 1.1.5)" do
expect(Version.new('1.1.5').to_s).to eq '1.1.5'
expect(Version.new('0.11.53.55555555565432.12').to_s).to eq(
'0.11.53.55555555565432.12'
)
end
it 'can remove the unnecessary zeroes' do
expect(Version.new('1.4.5.0').to_s).to eq '1.4.5'
expect(Version.new('01.040.5.10.000').to_s).to eq '1.40.5.10'
end
end
context '#components' do
it 'can find the components of a version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.00.0').components).to eq [1, 3, 5]
expect(Version.new('01.03.005.000.07.0').components).to eq [1, 3, 5, 0, 7]
end
it 'can return N components of a version' do
version = Version.new('1.4.66.320.00.3.1')
expect(version.components(3)).to eq [1, 4, 66]
end
it 'can return N components when N is bigger than # of components' do
version = Version.new('4.5')
expect(version.components(5)).to eq [4, 5, 0, 0, 0]
end
end
context 'Comparing of versions' do
- v1 = Version.new('1.0')
- v2 = Version.new('1.02.4')
- v3 = Version.new('1.2.5.1')
- v4 = Version.new('0.2.5.1')
-
it 'can check if a version is lower than other version' do
- expect(v1 < v2).to eq true
- expect(v3 < v2).to eq false
- expect(v4 < v2).to eq true
+ expect(Version.new('1.0')).to be < Version.new('1.02.4')
+ expect(Version.new('1.02.4')).to be < Version.new('1.2.5.1')
+ expect(Version.new('0.2.5.1')).to be < Version.new('1.02.4')
end
it 'can check if a version is bigger than other version' do
- expect(v2 > v3).to eq false
- expect(v1 > v4).to eq true
- expect(v3 > v4).to eq true
+ expect(Version.new('1.2.5.1')).to be > Version.new('1.02.4')
+ expect(Version.new('1.0')).to be > Version.new('0.2.5.1')
+ expect(Version.new('1.2.5.1')).to be > Version.new('0.2.5.1')
end
it 'can check if versions are equal' do
- expect(v2 == Version.new('1.2.4')).to eq true
+ expect(Version.new('1.02.4')).to be == Version.new('1.2.4')
end
it 'can compare two versions with <=>, <=, >=' do
- expect(v2 <=> v3).to eq -1
- expect(v2 <=> v1).to eq 1
- expect(v2 <=> Version.new('1.2.4')).to eq 0
- expect(v1 >= v3).to eq false
- expect(v1 <= '1.0').to eq true
+ expect(Version.new('1.02.4') <=> Version.new('1.2.5.1')).to eq -1
+ expect(Version.new('1.02.4') <=> Version.new('1.0')).to eq 1
+ expect(Version.new('1.02.4') <=> Version.new('1.2.4')).to eq 0
+ expect(Version.new('1.0')).to be <= Version.new('1.2.5.1')
+ expect(Version.new('1.0')).to be >= '1.0'
end
end
end
-describe Version::Range do
- v1 = Version.new('1.0.1')
- v2 = Version.new('1.0.2')
+describe Version::Range do
# context '#include?' do
# it 'can check if simple range includes version' do
#
# end
# end
context '#to_a' do
it 'can return an array of versions' do
expect(Range.new('1.2.1', '1.2.2').to_a).to eq ['1.2.1', '1.2.2']
- expect(Range.new(v1, v2).to_a).to eq ['1.0.1', '1.0.2']
+ expect(Range.new(Version.new('1.0.1'), Version.new('1.0.2')).to_a).to eq(
Failure/Error:
   expect(Range.new(Version.new('1.0.1'), Version.new('1.0.2')).to_a).to eq(
     ['1.0.1', '1.0.2']
   )

 TypeError:
   can't iterate from Version

Тук все още получавам грешка, дори и при последната версия на version.rb. При мен ли е проблема?

П.П. Благодаря за предишните коментари! Използвах .to be на местата къде мога. #let и другите помощни методи ще ми бъдат от полза, но реших да заменя v1, v2... със Vector.new в тестовете, тъй като наистина е по-четливо.

+ ['1.0.1', '1.0.2']
+ )
end
end
end

Иван обнови решението на 13.11.2016 11:20 (преди над 1 година)

describe Version do
context "Validation" do
it "raises an ArgumentError if version is not in valid format" do
expect { Version.new('1.42-21.0') }.to raise_error(ArgumentError)
expect { Version.new('1.42b21.0') }.to raise_error(ArgumentError)
expect { Version.new('.04') }.to raise_error(ArgumentError)
expect { Version.new('0.4..0') }.to raise_error(ArgumentError)
end
context "invalid version string error" do
it "prints the correct message format" do
expect { Version.new('30wrong.f.0.rmat') }.to raise_error(
ArgumentError, "Invalid version string '30wrong.f.0.rmat'"
)
expect { Version.new '1.3.5.' }.to raise_error(
ArgumentError, "Invalid version string '1.3.5.'"
)
end
end
end
context "#to_s" do
it "returns version string" do
expect(Version.new('1.1.1').to_s).to eq '1.1.1'
end
end
context "initialization" do
it "can create new empty version" do
expect(Version.new).to eq 0
end
it "can create simple version(e.g. 1.1.5)" do
expect(Version.new('1.1.5').to_s).to eq '1.1.5'
expect(Version.new('0.11.53.55555555565432.12').to_s).to eq(
'0.11.53.55555555565432.12'
)
end
it 'can remove the unnecessary zeroes' do
expect(Version.new('1.4.5.0').to_s).to eq '1.4.5'
expect(Version.new('01.040.5.10.000').to_s).to eq '1.40.5.10'
end
end
context '#components' do
it 'can find the components of a version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0').components).to eq [1, 3, 5]
expect(Version.new('1.3.5.0.00.0').components).to eq [1, 3, 5]
expect(Version.new('01.03.005.000.07.0').components).to eq [1, 3, 5, 0, 7]
end
it 'can return N components of a version' do
version = Version.new('1.4.66.320.00.3.1')
expect(version.components(3)).to eq [1, 4, 66]
end
it 'can return N components when N is bigger than # of components' do
version = Version.new('4.5')
expect(version.components(5)).to eq [4, 5, 0, 0, 0]
end
end
context 'Comparing of versions' do
it 'can check if a version is lower than other version' do
expect(Version.new('1.0')).to be < Version.new('1.02.4')
expect(Version.new('1.02.4')).to be < Version.new('1.2.5.1')
expect(Version.new('0.2.5.1')).to be < Version.new('1.02.4')
end
it 'can check if a version is bigger than other version' do
expect(Version.new('1.2.5.1')).to be > Version.new('1.02.4')
expect(Version.new('1.0')).to be > Version.new('0.2.5.1')
expect(Version.new('1.2.5.1')).to be > Version.new('0.2.5.1')
end
it 'can check if versions are equal' do
expect(Version.new('1.02.4')).to be == Version.new('1.2.4')
end
it 'can compare two versions with <=>, <=, >=' do
expect(Version.new('1.02.4') <=> Version.new('1.2.5.1')).to eq -1
expect(Version.new('1.02.4') <=> Version.new('1.0')).to eq 1
expect(Version.new('1.02.4') <=> Version.new('1.2.4')).to eq 0
expect(Version.new('1.0')).to be <= Version.new('1.2.5.1')
expect(Version.new('1.0')).to be >= '1.0'
end
end
end
describe Version::Range do
- # context '#include?' do
- # it 'can check if simple range includes version' do
- #
- # end
- # end
-
context '#to_a' do
it 'can return an array of versions' do
- expect(Range.new('1.2.1', '1.2.2').to_a).to eq ['1.2.1', '1.2.2']
- expect(Range.new(Version.new('1.0.1'), Version.new('1.0.2')).to_a).to eq(
- ['1.0.1', '1.0.2']
- )
+ expect(Version::Range.new('1.2.1', '1.2.3').to_a).to eq ['1.2.1', '1.2.2']
+ expect(
+ Version::Range.new(Version.new('1.0.1'), Version.new('1.0.3')).to_a
+ ).to eq ['1.0.1', '1.0.2']
+ end
+ end
+
+ context '#include?' do
+ it 'can check if simple range includes version' do
+ expect(
+ Version::Range.new('1.2.1', '1.2.3').include?(Version.new('1.2.2'))
+ ).to be true
+ end
+
+ it 'can check if wider range includes version' do
+ test_range = Version::Range.new('1.1', '2.5')
+ expect(test_range.include?(Version.new('1.2.8'))).to be true
+ expect(test_range.include?(Version.new('2.4.9'))).to be true
+ expect(test_range.include?(Version.new('2.0.1'))).to be true
+ expect(test_range.include?(Version.new('1.2.3'))).to be true
+ expect(test_range.include?(Version.new('2.5'))).to be false
+ expect(test_range.include?(Version.new('1.0.4'))).to be false
end
end
end