spec/currency_spec.rb in currencies-0.2.1 vs spec/currency_spec.rb in currencies-0.3.0
- old
+ new
@@ -61,6 +61,280 @@
it 'should have an exchange rate of 1.0 for the base currency' do
Currency.from_code(Currency.base_currency).exchange_rate.should == 1.0
end
end
-end
\ No newline at end of file
+
+ describe ".major_currencies_selection" do
+ let(:usd) { [ "USD", mock ] }
+ let(:aud) { [ "AUD", mock ] }
+ let(:currencies) { [ usd, aud ] }
+
+ context "with default settings" do
+ subject do
+ Currency.major_currencies_selection(currencies)
+ end
+
+ it { should == usd }
+ end
+
+ context "with changed major currency to AUD" do
+ subject do
+ Currency.major_codes = [ "AUD" ]
+ Currency.major_currencies_selection(currencies)
+ end
+
+ it { should == aud }
+ end
+ end
+
+ describe ".best_from_currencies" do
+ let(:usd) { mock }
+ let(:pln) { mock }
+ let(:aud) { mock }
+
+ context "when given currencies is nil" do
+ subject { Currency.best_from_currencies(nil) }
+
+ it { should be_nil }
+ end
+
+ context "when given currencies is empty array" do
+ subject { Currency.best_from_currencies([]) }
+
+ it { should be_nil }
+ end
+
+ context "when major currency exist within currencies" do
+ let(:valid_currencies) { mock(:nil? => false, :empty? => false) }
+ subject do
+ Currency.stub!(:major_currencies_selection => [mock, usd])
+ Currency.best_from_currencies(valid_currencies)
+ end
+
+ it { should == usd }
+ end
+
+ context "when major currency does not exist within currencies" do
+ let(:valid_currencies) { [ [mock, pln], [mock, aud] ] }
+ subject do
+ Currency.stub!(:major_currencies_selection => nil)
+ Currency.best_from_currencies(valid_currencies)
+ end
+
+ it { should == pln }
+ end
+ end
+
+ describe ".list_from_name" do
+ let(:euro_currency) { mock(:name => "Euro") }
+ let(:euro) { [ [ "EUR", euro_currency ] ] }
+ let(:dollar_currency) { mock(:name => "Dollars") }
+ let(:dollars) { [
+ [ "USD", dollar_currency ],
+ [ "AUD", dollar_currency ]
+ ] }
+ let(:currencies) { euro + dollars }
+
+ subject do
+ Currency.stub!(:currencies => currencies)
+ Currency.list_from_name("Dollars")
+ end
+
+ it { should == dollars }
+ end
+
+ describe ".list_from_symbol" do
+ let(:euro_currency) { mock(:symbol => "€") }
+ let(:euro) { [ [ "EUR", euro_currency ] ] }
+ let(:dollar_currency) { mock(:symbol => "$") }
+ let(:dollars) { [
+ [ "USD", dollar_currency ],
+ [ "AUD", dollar_currency ]
+ ] }
+ let(:currencies) { euro + dollars }
+
+ subject do
+ Currency.stub!(:currencies => currencies)
+ Currency.list_from_symbol("$")
+ end
+
+ it { should == dollars }
+ end
+
+ describe ".best_from_name" do
+ let(:name) { mock }
+ let(:list_from_name) { mock }
+ describe "behavior" do
+ before do
+ Currency.stub!(
+ :best_from_currencies => nil,
+ :list_from_name => list_from_name
+ )
+ end
+ after { Currency.best_from_name(name) }
+
+ it "should select best from list of currencies with given name" do
+ Currency.should_receive(:best_from_currencies).with(list_from_name)
+ end
+
+ it "should select list of currencies with given name" do
+ Currency.should_receive(:list_from_name).with(name).and_return(list_from_name)
+ end
+ end
+
+ describe "returns" do
+ let(:best_from_currencies) { mock }
+ subject do
+ Currency.stub!(
+ :best_from_currencies => best_from_currencies,
+ :list_from_name => list_from_name
+ )
+ Currency.best_from_name(name)
+ end
+
+ it { should == best_from_currencies }
+ end
+ end
+
+ describe ".best_from_symbol" do
+ let(:symbol) { mock }
+ let(:list_from_symbol) { mock }
+ describe "behavior" do
+ before do
+ Currency.stub!(
+ :best_from_currencies => nil,
+ :list_from_symbol => list_from_symbol
+ )
+ end
+ after { Currency.best_from_symbol(symbol) }
+
+ it "should select best from list of currencies with given symbol" do
+ Currency.should_receive(:best_from_currencies).with(list_from_symbol)
+ end
+
+ it "should select list of currencies with given symbol" do
+ Currency.should_receive(:list_from_symbol).with(symbol).and_return(list_from_symbol)
+ end
+ end
+
+ describe "returns" do
+ let(:best_from_currencies) { mock }
+ subject do
+ Currency.stub!(
+ :best_from_currencies => best_from_currencies,
+ :list_from_symbol => list_from_symbol
+ )
+ Currency.best_from_symbol(symbol)
+ end
+
+ it { should == best_from_currencies }
+ end
+ end
+
+ describe ".best_guess" do
+ let(:eur) { mock }
+ let(:string) { mock(:nil? => false, :empty? => false) }
+
+ context "when string not given" do
+ subject { Currency.best_guess(nil) }
+
+ it { should be_nil }
+ end
+
+ context "when given empty string" do
+ subject { Currency.best_guess("") }
+
+ it { should be_nil }
+ end
+
+ context "when code equal to string exist" do
+ subject do
+ Currency.stub!(:from_code).with(string).and_return(eur)
+ Currency.best_guess(string)
+ end
+
+ it { should == eur }
+ end
+
+ context "when best symbol equal to string exist" do
+ subject do
+ Currency.stub!(:from_code).with(string).and_return(nil)
+ Currency.stub!(:best_from_symbol).with(string).and_return(eur)
+ Currency.best_guess(string)
+ end
+
+ it { should == eur }
+ end
+
+ context "when best name equal to string exist" do
+ subject do
+ Currency.stub!(:from_code).with(string).and_return(nil)
+ Currency.stub!(:best_from_symbol).with(string).and_return(nil)
+ Currency.stub!(:best_from_name).with(string).and_return(eur)
+ Currency.best_guess(string)
+ end
+
+ it { should == eur }
+ end
+
+ context "when string not exist in any form" do
+ subject do
+ Currency.stub!(:from_code).with(string).and_return(nil)
+ Currency.stub!(:best_from_symbol).with(string).and_return(nil)
+ Currency.stub!(:best_from_name).with(string).and_return(nil)
+ Currency.best_guess(string)
+ end
+
+ it { should be_nil }
+ end
+
+ describe "behavior" do
+ it "should run methods in proper order" do
+ Currency.should_receive(:from_code).ordered.with("string").and_return(nil)
+ Currency.should_receive(:best_from_symbol).ordered.with("string").and_return(nil)
+ Currency.should_receive(:best_from_name).ordered.with("string").and_return(nil)
+ Currency.best_guess("string")
+ end
+ end
+ end
+
+ describe ".code_from_best_guess" do
+ let(:string) { mock }
+ let(:code) { mock }
+ let(:best_guess) { mock(:try => code) }
+
+ context "when there is best guess" do
+ subject do
+ Currency.stub!(:best_guess).with(string).and_return(best_guess)
+ Currency.code_from_best_guess(string)
+ end
+
+ it { should == code }
+ end
+
+ context "when there is no best guess" do
+ let(:best_guess) { mock(:try => nil) }
+
+ subject do
+ Currency.stub!(:best_guess).with(string).and_return(best_guess)
+ Currency.code_from_best_guess(string)
+ end
+
+ it { should be_nil }
+ end
+
+ describe "behavior" do
+ before { Currency.stub!(:best_guess).with(string).and_return(best_guess) }
+ after { Currency.code_from_best_guess(string) }
+
+ it "should call .best_guess" do
+ Currency.should_receive(:best_guess).with(string).and_return(best_guess)
+ end
+
+ it "should call #code on best guessed" do
+ best_guess.should_receive(:try).with(:code)
+ end
+ end
+ end
+
+end