Skip to content

Instantly share code, notes, and snippets.

@weilandia
Created November 1, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weilandia/1011fa45721b7c78c88e370e4fa37b0a to your computer and use it in GitHub Desktop.
Save weilandia/1011fa45721b7c78c88e370e4fa37b0a to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "pry"
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activesupport", "5.2.0"
end
require "active_support/core_ext/hash"
require "active_support/hash_with_indifferent_access"
require "minitest/autorun"
class HashWithIndifferentAccessTest < Minitest::Test
def setup
@strings = { "a" => 1, "b" => 2 }
@symbols = { a: 1, b: 2 }
@mixed = { :a => 1, "b" => 2 }
end
def test_symbolize_keys_for_hash_with_indifferent_access
assert_instance_of Hash, @symbols.with_indifferent_access.symbolize_keys
assert_equal @symbols, @symbols.with_indifferent_access.symbolize_keys
assert_equal @symbols, @strings.with_indifferent_access.symbolize_keys
assert_equal @symbols, @mixed.with_indifferent_access.symbolize_keys
end
def test_to_options_for_hash_with_indifferent_access
assert_instance_of Hash, @symbols.with_indifferent_access.to_options
assert_equal @symbols, @symbols.with_indifferent_access.to_options
assert_equal @symbols, @strings.with_indifferent_access.to_options
assert_equal @symbols, @mixed.with_indifferent_access.to_options
end
# Example of how this may cause issues in an upgrade to Rails 5.2 -- If people previously used to_options as an alias for symbolize_keys and passed that result to a method that uses keyword arguments, it will not word.
def test_to_options_does_not_work_with_keyword_args
params = HashWithIndifferentAccess.new(arg: true).to_options
assert_raises ArgumentError do
mock_keyword_method(params)
end
end
def test_symbolize_keys_works_with_keyword_args
params = HashWithIndifferentAccess.new(arg: true).symbolize_keys
assert mock_keyword_method(params)
end
def mock_keyword_method(arg: false)
arg
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment