Created
September 4, 2009 10:30
-
-
Save manveru/180822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bacon' | |
Bacon.summary_on_exit | |
describe 'keychain splitter' do | |
def split(keychain) | |
return nil, keychain if keychain.first == '0' | |
argument, rest = [], [] | |
digits = true | |
keychain.each do |key| | |
if digits && key =~ /^\d$/ | |
argument << key | |
else | |
digits = false | |
rest << key | |
end | |
end | |
unless argument.empty? | |
return argument.join.to_i, rest | |
else | |
return nil, rest | |
end | |
end | |
should 'split keychain with single numeric prefix' do | |
keychain = %w[1 f o o] | |
split(keychain).should == [1, %w[f]] | |
end | |
should 'split keychain with multiple numeric prefix' do | |
keychain = %w[1 2 3 f o] | |
split(keychain).should == [123, %w[f]] | |
end | |
should 'split keychain without numeric prefix' do | |
keychain = %w[f o o 1 2] | |
split(keychain).should == [nil, %w[f o o 1 2]] | |
end | |
should 'split keychain zero prefix' do | |
keychain = %w[0 f o o 1 2] | |
split(keychain).should == [nil, %w[0 f o o 1 2]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment