Skip to content

Commit

Permalink
Merge branch 'luispcosta-freeze_constants'
Browse files Browse the repository at this point in the history
  • Loading branch information
scsmith committed Mar 15, 2017
2 parents 25fbf11 + dac1155 commit d89d4ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/language_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def self.find(code)
warn "Reverting to hash load: #{e.message}"
yaml_data = YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__),'..', 'data', 'languages.yml')))
yaml_data.map{|e| LanguageInfo.new(e) }
end
ISO_639_1 = ALL_LANGUAGES.select(&:iso_639_1?)
LIVING_LANGUAGES = ALL_LANGUAGES.select(&:living?)
COMMON_LANGUAGES = ALL_LANGUAGES.select(&:common?)
end.freeze
ISO_639_1 = ALL_LANGUAGES.select(&:iso_639_1?).freeze
LIVING_LANGUAGES = ALL_LANGUAGES.select(&:living?).freeze
COMMON_LANGUAGES = ALL_LANGUAGES.select(&:common?).freeze

BY_NAME = {}
BY_ISO_639_1 = {}
Expand All @@ -94,5 +94,9 @@ def self.find(code)
BY_ISO_639_2B[lang.iso_639_2b] = lang if lang.iso_639_2b
BY_ISO_639_2T[lang.iso_639_2t] = lang if lang.iso_639_2t
end

BY_NAME.freeze
BY_ISO_639_1.freeze
BY_ISO_639_3.freeze
BY_ISO_639_2B.freeze
BY_ISO_639_2T.freeze
end
38 changes: 38 additions & 0 deletions test/language_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,42 @@ def test_common_name_when_not_present
english = LanguageList::LanguageInfo.find('en')
assert_equal english.name, english.common_name
end

def test_cannot_change_constants
assert_raises RuntimeError, TypeError do
LanguageList::ALL_LANGUAGES[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::ISO_639_1[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::LIVING_LANGUAGES[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::COMMON_LANGUAGES[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::BY_NAME[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::BY_ISO_639_1[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::BY_ISO_639_3[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::BY_ISO_639_2T[0] = nil
end

assert_raises RuntimeError, TypeError do
LanguageList::BY_ISO_639_2B[0] = nil
end
end
end

0 comments on commit d89d4ce

Please sign in to comment.