Skip to content

Commit

Permalink
Freeze public available constants. Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luispcosta committed Mar 13, 2017
1 parent 07288b2 commit 8a7f791
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/language_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,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 @@ -76,5 +76,7 @@ def self.find(code)
BY_ISO_639_1[lang.iso_639_1] = lang if lang.iso_639_1
BY_ISO_639_3[lang.iso_639_3] = lang if lang.iso_639_3
end

BY_NAME.freeze
BY_ISO_639_1.freeze
BY_ISO_639_3.freeze
end
30 changes: 30 additions & 0 deletions test/language_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,34 @@ 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, /frozen Array/i do
LanguageList::ALL_LANGUAGES[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::ISO_639_1[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::LIVING_LANGUAGES[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::COMMON_LANGUAGES[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::BY_NAME[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::BY_ISO_639_1[0] = nil
end

assert_raises RuntimeError, /frozen Array/i do
LanguageList::BY_ISO_639_3[0] = nil
end
end
end

0 comments on commit 8a7f791

Please sign in to comment.