Instantly share code, notes, and snippets.

dnagir/cycle_tracks.rbSecret Created Jul 31, 2010

What would you like to do?
 def skip_tracks(playlist, skip) if skip > 0 skip.times { playlist.push playlist.shift } else skip.abs.times { playlist.unshift playlist.pop } end playlist end
 Feature: Track skipping In order to ignore some tracks I want to be able to skip them Scenario Outline: Skipping forward Given the playlist "a, b, c, d" When I skip track forward Then I should have the playlist "" Examples: | skip | result | | 1 | b,c,d,a | | 2 | c,d,a,b | | 3 | d,a,b,c | | 4 | a,b,c,d | | 5 | b,c,d,a | | 6 | c,d,a,b | Scenario Outline: Skipping backward Given the playlist "a, b, c, d" When I skip track backward Then I should have the playlist "" Examples: | skip | result | | 1 | d,a,b,c | | 2 | c,d,a,b | | 3 | b,c,d,a | | 4 | a,b,c,d | | 5 | d,a,b,c | | 6 | c,d,a,b |
 Given /^the playlist "([^"]*)"\$/ do |list| @playlist = list.split(',').map { |x| x.strip } end When /^I skip (\d+) tracks? (\w+)\$/ do |count, direction| skip_count = count.to_i * (direction == 'forward' ? 1 : -1) @playlist = skip_tracks(@playlist, skip_count) end Then /^I should have the playlist "([^"]*)"\$/ do |list| @playlist.should == list.split(',').map { |x| x.strip } end