提交 08fd9f4f 编写于 作者: B Benjamin Quorning

Fix AC::Parameters#== with other AC::Parameters

Creating a protected getter method for `@parameters`.
上级 9d2ea7f6
......@@ -144,11 +144,10 @@ def initialize(parameters = {})
end
# Returns true if another +Parameters+ object contains the same content and
# permitted flag, or other Hash-like object contains the same content. This
# override is in place so you can perform a comparison with `Hash`.
# permitted flag, or other Hash-like object contains the same content.
def ==(other_hash)
if other_hash.respond_to?(:permitted?)
super
self.permitted? == other_hash.permitted? && self.parameters == other_hash.parameters
else
if other_hash.is_a?(Hash)
@parameters == other_hash.with_indifferent_access
......@@ -597,6 +596,8 @@ def method_missing(method_sym, *args, &block)
end
protected
attr_reader :parameters
def permitted=(new_permitted)
@permitted = new_permitted
end
......
......@@ -135,6 +135,39 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
assert(params1 == hash1)
end
test "is equal to Parameters instance with same params" do
params1 = ActionController::Parameters.new(a: 1, b: 2)
params2 = ActionController::Parameters.new(a: 1, b: 2)
assert(params1 == params2)
end
test "is equal to Parameters instance with same permitted params" do
params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a)
params2 = ActionController::Parameters.new(a: 1, b: 2).permit(:a)
assert(params1 == params2)
end
test "is equal to Parameters instance with same different source params, but same permitted params" do
params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a)
params2 = ActionController::Parameters.new(a: 1, c: 3).permit(:a)
assert(params1 == params2)
assert(params2 == params1)
end
test 'is not equal to an unpermitted Parameters instance with same params' do
params1 = ActionController::Parameters.new(a: 1).permit(:a)
params2 = ActionController::Parameters.new(a: 1)
assert(params1 != params2)
assert(params2 != params1)
end
test "is not equal to Parameters instance with different permitted params" do
params1 = ActionController::Parameters.new(a: 1, b: 2).permit(:a, :b)
params2 = ActionController::Parameters.new(a: 1, b: 2).permit(:a)
assert(params1 != params2)
assert(params2 != params1)
end
test "equality with simple types works" do
assert(@params != 'Hello')
assert(@params != 42)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册