Решение на Четвърта задача - Unit тестване от Стамен Драгоев

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

Към профила на Стамен Драгоев

Резултати

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

Код

RSpec.describe 'Version' do
describe '#initialize' do
it 'waits for an exception from creation of new instance' do
expect { Version.new('1.1..0') }.to raise_error(
ArgumentError,
"Invalid version string '1.1..0'"
)
expect { Version.new('.3') }.to raise_error(
ArgumentError,
"Invalid version string '.3'"
)
expect { Version.new('2.2.') }.to raise_error(
ArgumentError,
"Invalid version string '2.2.'"
)
expect { Version.new(Version.new('1.3.')) }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
end
describe '#components' do
it 'returns an array properly with same length like the one of version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
end
it 'returns an array properly with length smaller than that of version' do
expect(Version.new('1.3.5').components(2)).to eq [1, 3]
end
it 'returns an extended array properly' do
expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
end
it 'does not allow modification of state of the instance' do
test_object = Version.new('1.3.5')
test_object.components.push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(2).push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(5).push(4)
expect(test_object.components).to eq [1, 3, 5]
end
end
describe '#to_s' do
it 'returns a string properly' do
expect(Version.new('1.02.2').to_s).to eq "1.2.2"
expect(Version.new('0.0.1.0.00').to_s).to eq "0.0.1"
expect(Version.new('').to_s).to eq ""
end
end
describe "#methods for compare" do
it "compares versions" do
expect(Version.new('1.2.3')).to_not be > Version.new('1.3.1')
expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
expect(Version.new('1.20.3.0')).to be >= Version.new('1.2.3')
expect(Version.new('1.2.0.0')).to be >= Version.new('1.2')
expect(Version.new('')).to eq Version.new
expect(Version.new('1.2.3')).to_not eq Version.new('1.2')
expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
expect(Version.new('1.2.3.0')).to_not be < Version.new('1.2.3')
expect(Version.new('1.20.3.0')).to be <= Version.new('10.2.3')
expect(Version.new('1.2.0.0')).to be <= Version.new('1.2')
end
end
end
describe Version::Range do
describe '#include?' do
it 'returns whether a version is in the range' do
test_object = Version::Range.new(Version.new('1'), Version.new('2'))
expect(test_object.include?(Version.new('1.5'))).to be true
expect(test_object.include?(Version.new('1.0'))).to be true
expect(test_object.include?(Version.new('2.0'))).to be false
end
end
describe '#to_a' do
it 'returns array from all versions in current range' do
expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
'1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq [
'1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1', '1.1.0').to_a).to eq [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6',
'1.0.7', '1.0.8', '1.0.9'
]
expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
end
end
end

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

.....FF.........F..

Failures:

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

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

  3) spec Version::Range tests 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-ray2c1/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 15.44 seconds
19 examples, 3 failures

Failed examples:

rspec /tmp/d20161119-19072-ray2c1/spec.rb:317 # spec Version checks for comparison operators positively
rspec /tmp/d20161119-19072-ray2c1/spec.rb:353 # spec Version checks for comparison operators negatively
rspec /tmp/d20161119-19072-ray2c1/spec.rb:515 # spec Version::Range tests include? with versions lower than the start one

История (5 версии и 16 коментара)

Стамен обнови решението на 16.11.2016 23:35 (преди около 2 години)

+RSpec.describe 'Version' do
+ describe Version do
+ describe '#initialize' do
+ it 'make new instance with invalid arguments' do
+ string = "Invalid version string '1.1..0'"
+ expect { Version.new('1.1..0') }.to raise_error(ArgumentError, string)
+ string = "Invalid version string '.3'"
+ expect { Version.new('.3') }.to raise_error(ArgumentError, string)
+ string = "Invalid version string '2.2.'"
+ expect { Version.new('2.2.') }.to raise_error(ArgumentError, string)
+ string = "Invalid version string '1.3.'"
+ ex = ArgumentError
+ expect { Version.new(Version.new('1.3.')) }.to raise_error(ex, string)
+ end
+ end
+ describe '#components' do
+ it 'returns a properly array' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ expect(Version.new('1.3.5').components(2)).to eq [1, 3]
+ expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
+ end
+ it 'do not allow to modify state of instance' do
+ obj = Version.new('1.3.5')
+ arr = obj.components
+ arr.push(4)
+ expect(obj.components).to eq [1, 3, 5]
+
+ obj = Version.new('1.3.5')
+ arr = obj.components(2)
+ arr.push(4)
+ expect(obj.components).to eq [1, 3, 5]
+
+ obj = Version.new('1.3.5')
+ arr = obj.components(5)
+ arr.push(4)
+ expect(obj.components).to eq [1, 3, 5]
+ end
+ end
+ describe '#to_s' do
+ it 'returns a properly string' do
+ expect(Version.new('0.0.1.00.00').to_s).to eq "0.0.1"
+ expect(Version.new('').to_s).to eq ""
+ end
+ end
+ describe "#methods for compare" do
+ it "compare versions with '>'" do
+ expect(Version.new('1.2.3') > Version.new('1.3.1')).to be false
+ expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
+ expect(Version.new('1.20.3.0') >= Version.new('1.2.3')).to be true
+ expect(Version.new('1.2.0.0') >= Version.new('1.2')).to be true
+ expect(Version.new('') == Version.new).to be true
+ expect(Version.new('1.2.3') == Version.new('1.2')).to be false
+ expect(Version.new('1.2.3') < Version.new('1.3.1')).to be true
+ expect(Version.new('1.2.3.0') < Version.new('1.2.3')).to be false
+ expect(Version.new('1.20.3.0') <= Version.new('10.2.3')).to be true
+ expect(Version.new('1.2.0.0') <= Version.new('1.2')).to be true
+ end
+ end
+ end
+ describe Version::Range do
+ describe '#include?' do
+ it 'return whether is in range' do
+ obj = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(obj.include?(Version.new('1.5'))).to be true
+ expect(obj.include?(Version.new('1.0'))).to be true
+ expect(obj.include?(Version.new('2.0'))).to be false
+ end
+ end
+ describe '#to_a' do
+ it 'array from all versions in current range' do
+ arr = ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6']
+ ['1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'].each { |e| arr << e }
+ expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq arr

Нали знаеш, че може да правиш масиви и на повече от един ред? Това с << е абсолютно излишно и объркващо.

expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
  '1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
  '1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
+ arr = ['1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6']
+ ['1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'].each { |e| arr << e }
+ expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq arr
+ expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
+ expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
+ end
+ end
+ end
+end

Това ли е форматът, в който трябва да е решението на задачата , смисъл упоменато е

RSpec.describe 'Version' do # Тук пишете код - describe, context, it и т.н. end

т.е. нормално ли е в момента?

И като цяло някаква препоръка за подобряване на решението , тъй като изобщо нямам опит в тази област (изграждането на тестове).

Стамен обнови решението на 17.11.2016 20:44 (преди около 2 години)

RSpec.describe 'Version' do
- describe Version do
- describe '#initialize' do
- it 'make new instance with invalid arguments' do
- string = "Invalid version string '1.1..0'"
- expect { Version.new('1.1..0') }.to raise_error(ArgumentError, string)
- string = "Invalid version string '.3'"
- expect { Version.new('.3') }.to raise_error(ArgumentError, string)
- string = "Invalid version string '2.2.'"
- expect { Version.new('2.2.') }.to raise_error(ArgumentError, string)
- string = "Invalid version string '1.3.'"
- ex = ArgumentError
- expect { Version.new(Version.new('1.3.')) }.to raise_error(ex, string)
- end
+ describe '#initialize' do
+ it 'wait exception from create new instance with invalid arguments' do
+ expect { Version.new('1.1..0') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.1..0'"
+ )
+ expect { Version.new('.3') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '.3'"
+ )
+ expect { Version.new('2.2.') }.to raise_error(
+ ArgumentError,
+ "Invalid version string '2.2.'"
+ )
+ expect { Version.new(Version.new('1.3.')) }.to raise_error(
+ ArgumentError,
+ "Invalid version string '1.3.'"
+ )
end
- describe '#components' do
- it 'returns a properly array' do
- expect(Version.new('1.3.5').components).to eq [1, 3, 5]
- expect(Version.new('1.3.5').components(2)).to eq [1, 3]
- expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
- end
- it 'do not allow to modify state of instance' do
- obj = Version.new('1.3.5')
- arr = obj.components
- arr.push(4)
- expect(obj.components).to eq [1, 3, 5]
+ end
- obj = Version.new('1.3.5')
- arr = obj.components(2)
- arr.push(4)
- expect(obj.components).to eq [1, 3, 5]
+ describe '#components' do
+ it 'returns properly an array with same length like length of version ' do
+ expect(Version.new('1.3.5').components).to eq [1, 3, 5]
+ end
- obj = Version.new('1.3.5')
- arr = obj.components(5)
- arr.push(4)
- expect(obj.components).to eq [1, 3, 5]
- end
+ it 'returns properly an array with length smaller than length of version' do
+ expect(Version.new('1.3.5').components(2)).to eq [1, 3]
end
- describe '#to_s' do
- it 'returns a properly string' do
- expect(Version.new('0.0.1.00.00').to_s).to eq "0.0.1"
- expect(Version.new('').to_s).to eq ""
- end
+
+ it 'returns an extended array properly' do
+ expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
+ end
+
+ it 'do not allow to modify state of instance' do
+ test_object = Version.new('1.3.5')
+ test_object.components.push(4)
+ expect(test_object.components).to eq [1, 3, 5]
+
+ test_object = Version.new('1.3.5')
+ test_object.components(2).push(4)
+ expect(test_object.components).to eq [1, 3, 5]
+
+ test_object = Version.new('1.3.5')
+ test_object.components(5).push(4)
+ expect(test_object.components).to eq [1, 3, 5]
end
- describe "#methods for compare" do
- it "compare versions with '>'" do
- expect(Version.new('1.2.3') > Version.new('1.3.1')).to be false
- expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
- expect(Version.new('1.20.3.0') >= Version.new('1.2.3')).to be true
- expect(Version.new('1.2.0.0') >= Version.new('1.2')).to be true
- expect(Version.new('') == Version.new).to be true
- expect(Version.new('1.2.3') == Version.new('1.2')).to be false
- expect(Version.new('1.2.3') < Version.new('1.3.1')).to be true
- expect(Version.new('1.2.3.0') < Version.new('1.2.3')).to be false
- expect(Version.new('1.20.3.0') <= Version.new('10.2.3')).to be true
- expect(Version.new('1.2.0.0') <= Version.new('1.2')).to be true
- end
+ end
+
+ describe '#to_s' do
+ it 'returns a string properly' do
+ expect(Version.new('1.02.2').to_s).to eq "1.2.2"
+ expect(Version.new('0.0.1.0.00').to_s).to eq "0.0.1"
+ expect(Version.new('').to_s).to eq ""
end
end
- describe Version::Range do
- describe '#include?' do
- it 'return whether is in range' do
- obj = Version::Range.new(Version.new('1'), Version.new('2'))
- expect(obj.include?(Version.new('1.5'))).to be true
- expect(obj.include?(Version.new('1.0'))).to be true
- expect(obj.include?(Version.new('2.0'))).to be false
- end
+
+ describe "#methods for compare" do
+ it "compare versions" do
+ expect(Version.new('1.2.3')).to_not be > Version.new('1.3.1')
+ expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
+ expect(Version.new('1.20.3.0')).to be >= Version.new('1.2.3')
+ expect(Version.new('1.2.0.0')).to be >= Version.new('1.2')
+ expect(Version.new('')).to eq Version.new
+ expect(Version.new('1.2.3')).to_not eq Version.new('1.2')
+ expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
+ expect(Version.new('1.2.3.0')).to_not be < Version.new('1.2.3')
+ expect(Version.new('1.20.3.0')).to be <= Version.new('10.2.3')
+ expect(Version.new('1.2.0.0')).to be <= Version.new('1.2')
end
- describe '#to_a' do
- it 'array from all versions in current range' do
- arr = ['1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6']
- ['1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'].each { |e| arr << e }
- expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq arr
- arr = ['1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6']
- ['1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'].each { |e| arr << e }
- expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq arr
- expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
- expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
- end
- end
end
+end
+
+describe Version::Range do
+ describe '#include?' do
+ it 'returns whether a version is in the range' do
+ test_object = Version::Range.new(Version.new('1'), Version.new('2'))
+ expect(test_object.include?(Version.new('1.5'))).to be true
+ expect(test_object.include?(Version.new('1.0'))).to be true
+ expect(test_object.include?(Version.new('2.0'))).to be false
+ end
+ end
+
+ describe '#to_a' do
+ it 'returns array from all versions in current range' do
+ expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
+ '1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
+ '1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
+ ]
+ expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq [
+ '1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
+ '1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
+ ]
+ expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
+ expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
+ end
+ end
end

Стамен обнови решението на 17.11.2016 20:49 (преди около 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
it 'wait exception from create new instance with invalid arguments' do
expect { Version.new('1.1..0') }.to raise_error(
ArgumentError,
"Invalid version string '1.1..0'"
)
expect { Version.new('.3') }.to raise_error(
ArgumentError,
"Invalid version string '.3'"
)
expect { Version.new('2.2.') }.to raise_error(
ArgumentError,
"Invalid version string '2.2.'"
)
expect { Version.new(Version.new('1.3.')) }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
end
describe '#components' do
it 'returns properly an array with same length like length of version ' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
end
it 'returns properly an array with length smaller than length of version' do
expect(Version.new('1.3.5').components(2)).to eq [1, 3]
end
it 'returns an extended array properly' do
expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
end
it 'do not allow to modify state of instance' do
test_object = Version.new('1.3.5')
test_object.components.push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(2).push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(5).push(4)
expect(test_object.components).to eq [1, 3, 5]
end
end
describe '#to_s' do
it 'returns a string properly' do
expect(Version.new('1.02.2').to_s).to eq "1.2.2"
expect(Version.new('0.0.1.0.00').to_s).to eq "0.0.1"
expect(Version.new('').to_s).to eq ""
end
end
describe "#methods for compare" do
it "compare versions" do
expect(Version.new('1.2.3')).to_not be > Version.new('1.3.1')
expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
expect(Version.new('1.20.3.0')).to be >= Version.new('1.2.3')
expect(Version.new('1.2.0.0')).to be >= Version.new('1.2')
expect(Version.new('')).to eq Version.new
expect(Version.new('1.2.3')).to_not eq Version.new('1.2')
expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
expect(Version.new('1.2.3.0')).to_not be < Version.new('1.2.3')
expect(Version.new('1.20.3.0')).to be <= Version.new('10.2.3')
expect(Version.new('1.2.0.0')).to be <= Version.new('1.2')
end
end
end
describe Version::Range do
describe '#include?' do
it 'returns whether a version is in the range' do
test_object = Version::Range.new(Version.new('1'), Version.new('2'))
expect(test_object.include?(Version.new('1.5'))).to be true
expect(test_object.include?(Version.new('1.0'))).to be true
expect(test_object.include?(Version.new('2.0'))).to be false
end
end
describe '#to_a' do
it 'returns array from all versions in current range' do
expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
'1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq [
'1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
+ expect(Version::Range.new('1', '1.1.0').to_a).to eq [
+ '1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6',
+ '1.0.7', '1.0.8', '1.0.9'
+ ]
expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
end
end
end

Стамен обнови решението на 18.11.2016 17:44 (преди почти 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
- it 'wait exception from create new instance with invalid arguments' do
+ it 'waits for an exception from creation of new instance' do
expect { Version.new('1.1..0') }.to raise_error(
ArgumentError,
"Invalid version string '1.1..0'"
)
expect { Version.new('.3') }.to raise_error(
ArgumentError,
"Invalid version string '.3'"
)
expect { Version.new('2.2.') }.to raise_error(
ArgumentError,
"Invalid version string '2.2.'"
)
expect { Version.new(Version.new('1.3.')) }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
end
-
describe '#components' do
- it 'returns properly an array with same length like length of version ' do
+ it 'returns an array properly with same length like the one of version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
end
-
- it 'returns properly an array with length smaller than length of version' do
+ it 'returns an array properly with length smaller than that of version' do
expect(Version.new('1.3.5').components(2)).to eq [1, 3]
end
-
it 'returns an extended array properly' do
expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
end
-
- it 'do not allow to modify state of instance' do
+ it 'does not allow modification of state of the instance' do
test_object = Version.new('1.3.5')
test_object.components.push(4)
expect(test_object.components).to eq [1, 3, 5]
-
test_object = Version.new('1.3.5')
test_object.components(2).push(4)
expect(test_object.components).to eq [1, 3, 5]
-
test_object = Version.new('1.3.5')
test_object.components(5).push(4)
expect(test_object.components).to eq [1, 3, 5]
end
end
-
describe '#to_s' do
it 'returns a string properly' do
expect(Version.new('1.02.2').to_s).to eq "1.2.2"
expect(Version.new('0.0.1.0.00').to_s).to eq "0.0.1"
expect(Version.new('').to_s).to eq ""
end
end
-
describe "#methods for compare" do
- it "compare versions" do
+ it "compares versions" do
expect(Version.new('1.2.3')).to_not be > Version.new('1.3.1')
expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
expect(Version.new('1.20.3.0')).to be >= Version.new('1.2.3')
expect(Version.new('1.2.0.0')).to be >= Version.new('1.2')
expect(Version.new('')).to eq Version.new
expect(Version.new('1.2.3')).to_not eq Version.new('1.2')
expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
expect(Version.new('1.2.3.0')).to_not be < Version.new('1.2.3')
expect(Version.new('1.20.3.0')).to be <= Version.new('10.2.3')
expect(Version.new('1.2.0.0')).to be <= Version.new('1.2')
end
end
end
-
describe Version::Range do
describe '#include?' do
it 'returns whether a version is in the range' do
test_object = Version::Range.new(Version.new('1'), Version.new('2'))
expect(test_object.include?(Version.new('1.5'))).to be true
expect(test_object.include?(Version.new('1.0'))).to be true
expect(test_object.include?(Version.new('2.0'))).to be false
end
end
-
describe '#to_a' do
it 'returns array from all versions in current range' do
expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
'1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq [
'1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1', '1.1.0').to_a).to eq [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6',
'1.0.7', '1.0.8', '1.0.9'
]
expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
end
end
end

Стамен обнови решението на 18.11.2016 19:47 (преди почти 2 години)

RSpec.describe 'Version' do
describe '#initialize' do
it 'waits for an exception from creation of new instance' do
expect { Version.new('1.1..0') }.to raise_error(
ArgumentError,
"Invalid version string '1.1..0'"
)
expect { Version.new('.3') }.to raise_error(
ArgumentError,
"Invalid version string '.3'"
)
expect { Version.new('2.2.') }.to raise_error(
ArgumentError,
"Invalid version string '2.2.'"
)
expect { Version.new(Version.new('1.3.')) }.to raise_error(
ArgumentError,
"Invalid version string '1.3.'"
)
end
end
+
describe '#components' do
it 'returns an array properly with same length like the one of version' do
expect(Version.new('1.3.5').components).to eq [1, 3, 5]
end
+
it 'returns an array properly with length smaller than that of version' do
expect(Version.new('1.3.5').components(2)).to eq [1, 3]
end
+
it 'returns an extended array properly' do
expect(Version.new('1.3.5').components(5)).to eq [1, 3, 5, 0, 0]
end
+
it 'does not allow modification of state of the instance' do
test_object = Version.new('1.3.5')
test_object.components.push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(2).push(4)
expect(test_object.components).to eq [1, 3, 5]
test_object = Version.new('1.3.5')
test_object.components(5).push(4)
expect(test_object.components).to eq [1, 3, 5]
end
end
+
describe '#to_s' do
it 'returns a string properly' do
expect(Version.new('1.02.2').to_s).to eq "1.2.2"
expect(Version.new('0.0.1.0.00').to_s).to eq "0.0.1"
expect(Version.new('').to_s).to eq ""
end
end
+
describe "#methods for compare" do
it "compares versions" do
expect(Version.new('1.2.3')).to_not be > Version.new('1.3.1')
expect(Version.new('1.2.3.0') > Version.new('1.2.3')).to be false
expect(Version.new('1.20.3.0')).to be >= Version.new('1.2.3')
expect(Version.new('1.2.0.0')).to be >= Version.new('1.2')
expect(Version.new('')).to eq Version.new
expect(Version.new('1.2.3')).to_not eq Version.new('1.2')
expect(Version.new('1.2.3')).to be < Version.new('1.3.1')
expect(Version.new('1.2.3.0')).to_not be < Version.new('1.2.3')
expect(Version.new('1.20.3.0')).to be <= Version.new('10.2.3')
expect(Version.new('1.2.0.0')).to be <= Version.new('1.2')
end
end
end
+
describe Version::Range do
describe '#include?' do
it 'returns whether a version is in the range' do
test_object = Version::Range.new(Version.new('1'), Version.new('2'))
expect(test_object.include?(Version.new('1.5'))).to be true
expect(test_object.include?(Version.new('1.0'))).to be true
expect(test_object.include?(Version.new('2.0'))).to be false
end
end
+
describe '#to_a' do
it 'returns array from all versions in current range' do
expect(Version::Range.new('1.1.0', '1.2.2').to_a).to eq [
'1.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1.1.0.1', '1.2.2').to_a).to eq [
'1.1.0.1', '1.1.1', '1.1.2', '1.1.3', '1.1.4', '1.1.5', '1.1.6',
'1.1.7', '1.1.8', '1.1.9', '1.2', '1.2.1'
]
expect(Version::Range.new('1', '1.1.0').to_a).to eq [
'1', '1.0.1', '1.0.2', '1.0.3', '1.0.4', '1.0.5', '1.0.6',
'1.0.7', '1.0.8', '1.0.9'
]
expect(Version::Range.new('1.1.0', Version.new('1.1.0')).to_a).to eq []
expect(Version::Range.new(Version.new('1.1.2'), '1.1.0').to_a).to eq []
end
end
end