Skip to content

Commit

Permalink
Merge pull request #601 from Shopify/pm/conditionally-test-json-symbo…
Browse files Browse the repository at this point in the history
…lize

Conditionally assert load_json returns symbolized data
  • Loading branch information
radar committed Jan 26, 2022
2 parents be5a8e0 + 8ac1724 commit b1de892
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/backend/base.rb
Expand Up @@ -255,7 +255,7 @@ def load_yml(filename)
def load_json(filename)
begin
# Use #load_file as a proxy for a version of JSON where symbolize_names and freeze are supported.
if JSON.respond_to?(:load_file)
if ::JSON.respond_to?(:load_file)
[::JSON.load_file(filename, symbolize_names: true, freeze: true), true]
else
[::JSON.parse(File.read(filename)), false]
Expand Down
4 changes: 3 additions & 1 deletion test/backend/simple_test.rb
Expand Up @@ -97,10 +97,12 @@ def setup

test "simple load_json: loads data from a JSON file" do
data, _ = I18n.backend.send(:load_json, "#{locales_dir}/en.json")
assert_equal({ :en => { :foo => { :bar => 'baz' } } }, data)

if JSON.respond_to?(:load_file)
assert_equal({ :en => { :foo => { :bar => 'baz' } } }, data)
assert_predicate data.dig(:en, :foo, :bar), :frozen?
else
assert_equal({ 'en' => { 'foo' => { 'bar' => 'baz' } } }, data)
end
end

Expand Down

0 comments on commit b1de892

Please sign in to comment.