test: cover I18nBackendTest, test_lookup_returns_nil_for_unknown_key
This commit is contained in:
1 parent
d57b53db31
commit
c5ce7d5ff4
2 files changed
+138
No files matched your search
@@ -0,0 +1,53 @@
|
||||
require 'minitest/autorun'
|
||||
require 'prism'
|
||||
require 'tmpdir'
|
||||
|
||||
class I18nBackendTest < Minitest::Test
|
||||
def setup
|
||||
@backend = Prism::I18n::Backend.new
|
||||
end
|
||||
|
||||
def test_lookup_returns_nil_for_unknown_key
|
||||
assert_nil @backend.lookup(:en, 'missing.key')
|
||||
end
|
||||
|
||||
def test_store_and_lookup_nested_key
|
||||
@backend.store_translations(:en, 'posts' => { 'title' => 'Title', 'body' => 'Body' })
|
||||
assert_equal 'Title', @backend.lookup(:en, 'posts.title')
|
||||
assert_equal 'Body', @backend.lookup(:en, :'posts.body')
|
||||
end
|
||||
|
||||
def test_store_translations_deep_merges_instead_of_replacing
|
||||
@backend.store_translations(:en, 'posts' => { 'title' => 'Title' })
|
||||
@backend.store_translations(:en, 'posts' => { 'body' => 'Body' })
|
||||
assert_equal 'Title', @backend.lookup(:en, 'posts.title')
|
||||
assert_equal 'Body', @backend.lookup(:en, 'posts.body')
|
||||
end
|
||||
|
||||
def test_locales_lists_every_stored_locale
|
||||
@backend.store_translations(:en, 'hello' => 'Hello')
|
||||
@backend.store_translations(:pl, 'hello' => 'Czesc')
|
||||
assert_equal [:en, :pl], @backend.locales.sort
|
||||
end
|
||||
|
||||
def test_lookup_returns_nil_when_key_path_hits_a_non_hash
|
||||
@backend.store_translations(:en, 'hello' => 'Hello')
|
||||
assert_nil @backend.lookup(:en, 'hello.nested')
|
||||
end
|
||||
|
||||
def test_load_file_reads_json_and_uses_basename_as_locale
|
||||
Dir.mktmpdir do |dir|
|
||||
path = File.join(dir, 'en.json')
|
||||
File.write(path, '{"greeting": "Hi %{name}"}')
|
||||
locale = @backend.load_file(path)
|
||||
assert_equal :en, locale
|
||||
assert_equal 'Hi %{name}', @backend.lookup(:en, 'greeting')
|
||||
end
|
||||
end
|
||||
|
||||
def test_clear_removes_all_translations
|
||||
@backend.store_translations(:en, 'hello' => 'Hello')
|
||||
@backend.clear!
|
||||
assert_empty @backend.locales
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,85 @@
|
||||
require 'minitest/autorun'
|
||||
require 'prism'
|
||||
require 'support'
|
||||
|
||||
class GreetingsController < Prism::Controller
|
||||
def show
|
||||
html(t('hello'))
|
||||
end
|
||||
|
||||
def remember
|
||||
session[:locale] = params['locale']
|
||||
html(t('hello'))
|
||||
end
|
||||
|
||||
def stamp
|
||||
html(l(Time.utc(2026, 3, 5, 8, 30, 0)))
|
||||
end
|
||||
end
|
||||
|
||||
class GreetingsApp < Prism::Application
|
||||
enable_sessions secret: 'i18n-controller-test-secret'
|
||||
|
||||
get '/greet', to: 'greetings#show'
|
||||
get '/greet/remember', to: 'greetings#remember'
|
||||
get '/greet/stamp', to: 'greetings#stamp'
|
||||
end
|
||||
|
||||
class I18nControllerTest < Minitest::Test
|
||||
include TestSupport
|
||||
|
||||
def setup
|
||||
Prism::I18n.reset!
|
||||
Prism::I18n.store_translations(:en, 'hello' => 'Hello')
|
||||
Prism::I18n.store_translations(:pl, 'hello' => 'Czesc')
|
||||
end
|
||||
|
||||
def teardown
|
||||
Prism::I18n.reset!
|
||||
end
|
||||
|
||||
def call(method, path, **opts)
|
||||
GreetingsApp.call(env_for(method, path, **opts))
|
||||
end
|
||||
|
||||
def test_defaults_to_default_locale_with_no_hints
|
||||
_, _, body = call('GET', '/greet')
|
||||
assert_equal 'Hello', body.join
|
||||
end
|
||||
|
||||
def test_locale_query_param_overrides_default
|
||||
_, _, body = call('GET', '/greet?locale=pl')
|
||||
assert_equal 'Czesc', body.join
|
||||
end
|
||||
|
||||
def test_unknown_locale_query_param_is_ignored
|
||||
_, _, body = call('GET', '/greet?locale=de')
|
||||
assert_equal 'Hello', body.join
|
||||
end
|
||||
|
||||
def test_accept_language_header_is_used_when_no_param_given
|
||||
_, _, body = call('GET', '/greet', headers: { 'Accept-Language' => 'pl,en;q=0.5' })
|
||||
assert_equal 'Czesc', body.join
|
||||
end
|
||||
|
||||
def test_locale_param_wins_over_accept_language_header
|
||||
_, _, body = call('GET', '/greet?locale=en', headers: { 'Accept-Language' => 'pl' })
|
||||
assert_equal 'Hello', body.join
|
||||
end
|
||||
|
||||
def test_session_locale_is_sticky_across_requests
|
||||
status, headers, body = call('GET', '/greet/remember?locale=pl')
|
||||
assert_equal 200, status
|
||||
assert_equal 'Czesc', body.join
|
||||
line = headers['Set-Cookie'].split("\n").find { |c| c.start_with?('prism.session=') }
|
||||
cookie = line.split(';').first
|
||||
|
||||
_, _, body = call('GET', '/greet', cookies: cookie)
|
||||
assert_equal 'Czesc', body.join
|
||||
end
|
||||
|
||||
def test_localize_helper_formats_time_in_view
|
||||
_, _, body = call('GET', '/greet/stamp')
|
||||
assert_equal '2026-03-05 08:30:00', body.join
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user