Решение на Четвърта задача - Unit тестване от Ралица Дарджонова
Към профила на Ралица Дарджонова
Резултати
- 6 точки от тестове
- 0 бонус точки
- 6 точки общо
- 18 успешни тест(а)
- 1 неуспешни тест(а)
Код
Лог от изпълнението
..........F........ Failures: 1) 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-alyaen/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)>' Finished in 19 seconds 19 examples, 1 failure Failed examples: rspec /tmp/d20161119-19072-alyaen/spec.rb:441 # spec Version tests that #components cannot be used to modify the version
История (3 версии и 9 коментара)
Ралица обнови решението на 17.11.2016 21:27 (преди над 8 години)
Това трябва да е describe
, не context
. describe казва какво тестваш, а context казва в какъв случай го тестваш.
Може да го подредиш и така:
expect { Version.new('1..3') }.to raise_error(
ArgumentError,
"Invalid version string '1..3'"
)
Налага ли се да тестваш този случай? Изобщо, ще се стигне ли до извикването на първото Version.new
някога?
Този тест е излишен, защото в долните тестове го проверяваш имплицитно. Долните тестове ще се счупят ако тези хвърлят грешка.
Виж лекцията от понеделник за по-удобна конструкция. Това ще ти реши и проблема с rubocop-а тук.
Тези инициализации с версии вече си ги тествала горе при стринга. Няма нужда да проверяваш всяко едно нещо с тях постоянно.
Слагай по един празен ред между it
блоковете. Така е по-лесно да се "сканират" от хора, четящи кода (какъвто съм аз в момента).
И тук има по-удобен matcher
Моля те, направи това подравняване като хората. Супер грозно е в момента.
range = Version::Range.new(Version.new('1.1.0'), Version.new('1.2.2'))
expect(range.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'
]