Created
January 18, 2011 23:30
-
-
Save wpiekutowski/785376 to your computer and use it in GitHub Desktop.
parse.rb
This file contains 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
def parse(name) | |
pattern = / | |
(?:\[(\w+)\])? | |
(\w+) | |
(?:\[(\w+)\])? | |
/x | |
words = pattern.match(name).captures | |
combinations = [words[1]] | |
if words[0] | |
combinations << words[0] + words[1] | |
end | |
if words[2] | |
combinations << words[1] + words[2] | |
end | |
if words[0] and words[2] | |
combinations << words[0] + words[1] + words[2] | |
end | |
combinations | |
end | |
describe "parse" do | |
it "should list all permutations, text in brackets is optional" do | |
strings = parse("foo[bar]") | |
strings.should include('foo') | |
strings.should include('foobar') | |
strings.should_not include('bar') | |
strings.size.should == 2 | |
end | |
it "should list all permutations, text in brackets is optional" do | |
strings = parse("[foo]bar") | |
strings.should include('bar') | |
strings.should include('foobar') | |
strings.should_not include('foo') | |
strings.size.should == 2 | |
end | |
it "should list all permutations, text in brackets is optional" do | |
strings = parse("[foo]bar[baz]") | |
strings.should include('foobar') | |
strings.should include('bar') | |
strings.should include('barbaz') | |
strings.should include('foobarbaz') | |
strings.should_not include('foo') | |
strings.should_not include('baz') | |
strings.size.should == 4 | |
end | |
it "should return the string, when there are no brackets" do | |
strings = parse("foo") | |
strings.should include('foo') | |
strings.size.should == 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment