Skip to content

Commit

Permalink
Merge pull request #20 from tmaier/patch-1
Browse files Browse the repository at this point in the history
Fix .find modifies string which is an issue for frozen strings
  • Loading branch information
scsmith authored Apr 18, 2017
2 parents ffe05d0 + 150de61 commit 6a70cff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/language_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ def self.find_by_iso_639_2t(code)
end

def self.find_by_name(name)
return if name.nil?

LanguageList::BY_NAME[name.downcase]
end

def self.find(code)
code.downcase!
return if code.nil?

code = code.downcase
find_by_iso_639_1(code) ||
find_by_iso_639_3(code) ||
find_by_iso_639_2b(code) ||
Expand Down
70 changes: 70 additions & 0 deletions test/language_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,66 @@ def test_find_by_iso_639_1
assert_equal 'English', english.name
end

def test_find_by_iso_639_1_with_nil
result = LanguageList::LanguageInfo.find_by_iso_639_1(nil)
assert_equal nil, result
end

def test_find_by_iso_639_1_with_empty_string
result = LanguageList::LanguageInfo.find_by_iso_639_1('')
assert_equal nil, result
end

def test_find_by_iso_639_1_with_frozen_string
english = LanguageList::LanguageInfo.find_by_iso_639_1('en'.freeze)
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_by_iso_639_3
english = LanguageList::LanguageInfo.find_by_iso_639_3('eng')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_by_iso_639_3_with_nil
result = LanguageList::LanguageInfo.find_by_iso_639_3(nil)
assert_equal nil, result
end

def test_find_by_iso_639_3_with_empty_string
result = LanguageList::LanguageInfo.find_by_iso_639_3('')
assert_equal nil, result
end

def test_find_by_iso_639_3_with_frozen_string
english = LanguageList::LanguageInfo.find_by_iso_639_3('eng'.freeze)
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_by_name_with_nil
result = LanguageList::LanguageInfo.find_by_name(nil)
assert_equal nil, result
end

def test_find_by_name_with_empty_string
result = LanguageList::LanguageInfo.find_by_name('')
assert_equal nil, result
end

def test_find_by_name_with_frozen_string
english = LanguageList::LanguageInfo.find_by_name('English'.freeze)
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'eng', english.iso_639_2b
assert_equal 'eng', english.iso_639_2t
assert_equal 'English', english.name
end

def test_find_by_name
english = LanguageList::LanguageInfo.find_by_name('English')
assert_equal 'en', english.iso_639_1
Expand All @@ -48,6 +101,23 @@ def test_case_insensitive_find_by_name
assert_equal 'English', english.name
end

def test_find_with_nil
result = LanguageList::LanguageInfo.find(nil)
assert_equal nil, result
end

def test_find_with_empty_string
result = LanguageList::LanguageInfo.find('')
assert_equal nil, result
end

def test_find_with_frozen_string
english = LanguageList::LanguageInfo.find('EN'.freeze)
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_with_iso_639_1_code
english = LanguageList::LanguageInfo.find('en')
assert_equal 'en', english.iso_639_1
Expand Down

0 comments on commit 6a70cff

Please sign in to comment.