Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly stub constants #650

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,14 @@ def call(exception, locale, key, options); key; end

test "can reserve a key" do
begin
reserved_keys_were = I18n::RESERVED_KEYS.dup
stub_const(I18n, :RESERVED_KEYS, []) do
I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert !I18n::RESERVED_KEYS.include?(:foo)
assert !I18n::RESERVED_KEYS.include?(:bar)

I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
end
ensure
I18n::RESERVED_KEYS = reserved_keys_were
I18n.instance_variable_set(:@reserved_keys_pattern, nil)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def store_translations(locale, data, options = I18n::EMPTY_HASH)
def locales_dir
File.dirname(__FILE__) + '/test_data/locales'
end

def stub_const(klass, constant, new_value)
old_value = klass.const_get(constant)
klass.send(:remove_const, constant)
klass.const_set(constant, new_value)
yield
ensure
klass.send(:remove_const, constant)
klass.const_set(constant, old_value)
end
end

class DummyRackApp
Expand Down