Skip to content

Commit

Permalink
refactored + added spec to test for optional pattern parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
luccastera committed Jul 1, 2009
1 parent 08a1465 commit d9144ec
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions library/abbrev/abbrev_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'abbrev'

#test both Abbrev::abbrev and Array#abbrev in
#the same manner, as they're more or less aliases
#of one another

[["Abbrev::abbrev", lambda {|a| Abbrev::abbrev(a)}],
["Array#abbrev", lambda {|a| a.abbrev}]
].each do |(name, func)|
describe "Abbrev::abbrev" do
it "returns a hash of all unambiguous abbreviations of the array of strings passed in" do
Abbrev::abbrev(['ruby', 'rules']).should == {"rub" => "ruby",
"ruby" => "ruby",
"rul" => "rules",
"rule" => "rules",
"rules" => "rules"}

Abbrev::abbrev(["car", "cone"]).should == {"ca" => "car",
"car" => "car",
"co" => "cone",
"con" => "cone",
"cone" => "cone"}
end

describe name do
it "returns a hash of all unambiguous abbreviations of the array of strings passed in" do
func.call(['ruby', 'rules']).should == {"rub" => "ruby",
"ruby" => "ruby",
"rul" => "rules",
"rule" => "rules",
"rules" => "rules"}

func.call(["car", "cone"]).should == {"ca" => "car",
"car" => "car",
"co" => "cone",
"con" => "cone",
"cone" => "cone"}
end
it "passing in a pattern as an optional parameter should filter the abbreviations returned in the hash" do
Abbrev::abbrev(['ruby','rules'],'rub').should == {"ruby"=>"ruby", "rub"=>"ruby"}
end

it "returns an empty hash when called on an empty array" do
Abbrev::abbrev([]).should == {}
end
end

describe "Array#abbrev" do
it "returns a hash of all unambiguous abbreviations of the array of strings passed in" do
['ruby', 'rules'].abbrev.should == {"rub" => "ruby",
"ruby" => "ruby",
"rul" => "rules",
"rule" => "rules",
"rules" => "rules"}

it "returns an empty hash when called on an empty array" do
func.call([]).should == {}
end
["car", "cone"].abbrev.should == {"ca" => "car",
"car" => "car",
"co" => "cone",
"con" => "cone",
"cone" => "cone"}
end

it "passing in a pattern as an optional parameter should filter the abbreviations returned in the hash" do
['ruby','rules'].abbrev('rub').should == {"ruby"=>"ruby", "rub"=>"ruby"}
end

it "returns an empty hash when called on an empty array" do
[].abbrev.should == {}
end
end

0 comments on commit d9144ec

Please sign in to comment.