forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test cases and rakefile to Active Support
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@310 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
5 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'rake/testtask' | ||
|
||
task :default => :test | ||
Rake::TestTask.new { |t| | ||
t.pattern = 'test/*_test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,32 @@ | ||
def silence_warnings | ||
old_verbose, $VERBOSE = $VERBOSE, nil | ||
result = yield | ||
$VERBOSE = old_verbose | ||
return result | ||
begin | ||
yield | ||
ensure | ||
$VERBOSE = old_verbose | ||
end | ||
end | ||
|
||
class Hash | ||
# Return a new hash with all keys converted to symbols. | ||
def symbolize_keys | ||
inject({}) do |options, (key, value)| | ||
options[key.to_sym] = value | ||
options | ||
end | ||
end | ||
|
||
# Destructively convert all keys to symbols. | ||
def symbolize_keys! | ||
keys.each do |key| | ||
unless key.is_a?(Symbol) | ||
self[key.to_sym] = self[key] | ||
delete(key) | ||
end | ||
end | ||
self | ||
end | ||
|
||
alias_method :to_options, :symbolize_keys | ||
alias_method :to_options!, :symbolize_keys! | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'test/unit' | ||
require File.dirname(__FILE__) + '/../lib/misc' | ||
|
||
class MiscTest < Test::Unit::TestCase | ||
def test_silence_warnings | ||
silence_warnings { assert_nil $VERBOSE } | ||
assert_equal 1234, silence_warnings { 1234 } | ||
end | ||
|
||
def test_silence_warnings_verbose_invariant | ||
old_verbose = $VERBOSE | ||
begin | ||
silence_warnings { raise } | ||
flunk | ||
rescue | ||
assert_equal old_verbose, $VERBOSE | ||
end | ||
end | ||
end | ||
|
||
class HashOptionsTest < Test::Unit::TestCase | ||
def setup | ||
@strings = { 'a' => 1, 'b' => 2 } | ||
@symbols = { :a => 1, :b => 2 } | ||
@mixed = { :a => 1, 'b' => 2 } | ||
end | ||
|
||
def test_methods | ||
h = {} | ||
assert_respond_to h, :symbolize_keys | ||
assert_respond_to h, :symbolize_keys! | ||
assert_respond_to h, :to_options | ||
assert_respond_to h, :to_options! | ||
end | ||
|
||
def test_symbolize_keys | ||
assert_equal @symbols, @symbols.symbolize_keys | ||
assert_equal @symbols, @strings.symbolize_keys | ||
assert_equal @symbols, @mixed.symbolize_keys | ||
|
||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys } | ||
end | ||
|
||
def test_symbolize_keys! | ||
assert_equal @symbols, @symbols.dup.symbolize_keys! | ||
assert_equal @symbols, @strings.dup.symbolize_keys! | ||
assert_equal @symbols, @mixed.dup.symbolize_keys! | ||
|
||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! } | ||
end | ||
end |