Skip to content

Commit

Permalink
Use .to eq consistently (#104)
Browse files Browse the repository at this point in the history
Several older tests still had `.to eql("foo")` while most tests used
`.to eq "foo"`. We'll prefer the short, plain version and keep the
parentheses as needed for comparing hashes.
  • Loading branch information
jlw authored Jan 11, 2024
1 parent 15d3cc8 commit 0920644
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
22 changes: 11 additions & 11 deletions spec/rails_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def prepare_params(params)
it "coerces to integer" do
get :index, **prepare_params(page: "666")

expect(controller.params[:page]).to eql(666)
expect(controller.params[:page]).to eq 666
end

it "raises InvalidParameterError if supplied an array instead of other type (prevent TypeError)" do
Expand Down Expand Up @@ -43,9 +43,9 @@ def prepare_params(params)
'price' => '$1,000.00'
}}
get :edit, **prepare_params(params)
expect(controller.params[:book][:author][:age]).to eql 70
expect(controller.params[:book][:author][:age]).to eq 70
expect(controller.params[:book][:author][:age]).to be_kind_of Integer
expect(controller.params[:book][:price]).to eql 1000.0
expect(controller.params[:book][:price]).to eq 1000.0
expect(controller.params[:book][:price]).to be_instance_of BigDecimal
end

Expand All @@ -61,8 +61,8 @@ def prepare_params(params)
}}
expect { get :edit, **prepare_params(params) }.to raise_error { |error|
expect(error).to be_a(RailsParam::InvalidParameterError)
expect(error.param).to eql("book[author][first_name]")
expect(error.options).to eql({:required => true})
expect(error.param).to eq "book[author][first_name]"
expect(error.options).to eq({:required => true})
}
end

Expand All @@ -73,7 +73,7 @@ def prepare_params(params)
'price' => '$1,000.00'
}}
get :edit, **prepare_params(params)
expect(controller.params[:book][:price]).to eql 1000.0
expect(controller.params[:book][:price]).to eq 1000.0
expect(controller.params[:book][:price]).to be_instance_of BigDecimal
end
end
Expand All @@ -82,8 +82,8 @@ def prepare_params(params)
it "raises an exception with params attributes" do
expect { get :index, **prepare_params(sort: "foo") }.to raise_error { |error|
expect(error).to be_a(RailsParam::InvalidParameterError)
expect(error.param).to eql("sort")
expect(error.options).to eql({:in => ["asc", "desc"], :default => "asc", :transform => :downcase})
expect(error.param).to eq "sort"
expect(error.options).to eq({:in => ["asc", "desc"], :default => "asc", :transform => :downcase})
}
end
end
Expand All @@ -92,16 +92,16 @@ def prepare_params(params)
it "applies transformations" do
get :index, **prepare_params(sort: "ASC")

expect(controller.params[:sort]).to eql("asc")
expect(controller.params[:sort]).to eq "asc"
end
end

describe "default values" do
it "applies default values" do
get :index

expect(controller.params[:page]).to eql(1)
expect(controller.params[:sort]).to eql("asc")
expect(controller.params[:page]).to eq 1
expect(controller.params[:sort]).to eq "asc"
end
end

Expand Down
72 changes: 36 additions & 36 deletions spec/rails_param/param_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ def params;
it "transforms the value" do
allow(controller).to receive(:params).and_return({ "word" => "foo" })
controller.param! :word, String, transform: :upcase
expect(controller.params["word"]).to eql("FOO")
expect(controller.params["word"]).to eq "FOO"
end

it "transforms default value" do
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: "foo", transform: :upcase
expect(controller.params["word"]).to eql("FOO")
expect(controller.params["word"]).to eq "FOO"
end
end

context "with a block" do
it "transforms the value" do
allow(controller).to receive(:params).and_return({ "word" => "FOO" })
controller.param! :word, String, transform: lambda { |n| n.downcase }
expect(controller.params["word"]).to eql("foo")
expect(controller.params["word"]).to eq "foo"
end

it "transforms default value" do
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: "foo", transform: lambda { |n| n.upcase }
expect(controller.params["word"]).to eql("FOO")
expect(controller.params["word"]).to eq "FOO"
end

it "transforms falsey value" do
allow(controller).to receive(:params).and_return({ "foo" => "0" })
controller.param! :foo, :boolean, transform: lambda { |n| n ? "bar" : "no bar" }
expect(controller.params["foo"]).to eql("no bar")
expect(controller.params["foo"]).to eq "no bar"
end
end

Expand Down Expand Up @@ -84,27 +84,27 @@ def params;
it "defaults to the value" do
allow(controller).to receive(:params).and_return({})
controller.param! :word, String, default: "foo"
expect(controller.params["word"]).to eql("foo")
expect(controller.params["word"]).to eq "foo"
end

it "does not default to the value if value already provided" do
allow(controller).to receive(:params).and_return({ "word" => "bar" })
controller.param! :word, String, default: "foo"
expect(controller.params["word"]).to eql("bar")
expect(controller.params["word"]).to eq "bar"
end
end

context "with a block" do
it "defaults to the block value" do
allow(controller).to receive(:params).and_return({})
controller.param! :foo, :boolean, default: lambda { false }
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "does not default to the value if value already provided" do
allow(controller).to receive(:params).and_return({ "foo" => "bar" })
controller.param! :foo, String, default: lambda { 'not bar' }
expect(controller.params["foo"]).to eql("bar")
expect(controller.params["foo"]).to eq "bar"
end
end
end
Expand All @@ -114,15 +114,15 @@ def params;
it "will convert to String" do
allow(controller).to receive(:params).and_return({ "foo" => :bar })
controller.param! :foo, String
expect(controller.params["foo"]).to eql("bar")
expect(controller.params["foo"]).to eq "bar"
end
end

describe "Integer" do
it "will convert to Integer if the value is valid" do
allow(controller).to receive(:params).and_return({ "foo" => "42" })
controller.param! :foo, Integer
expect(controller.params["foo"]).to eql(42)
expect(controller.params["foo"]).to eq 42
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -148,7 +148,7 @@ def params;
it "will convert to Float" do
allow(controller).to receive(:params).and_return({ "foo" => "42.22" })
controller.param! :foo, Float
expect(controller.params["foo"]).to eql(42.22)
expect(controller.params["foo"]).to eq 42.22
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -174,7 +174,7 @@ def params;
it "will convert to Array" do
allow(controller).to receive(:params).and_return({ "foo" => "2,3,4,5" })
controller.param! :foo, Array
expect(controller.params["foo"]).to eql(["2", "3", "4", "5"])
expect(controller.params["foo"]).to eq ["2", "3", "4", "5"]
end

it "will raise InvalidParameterError if the value is a boolean" do
Expand All @@ -191,7 +191,7 @@ def params;
it "will convert to Hash" do
allow(controller).to receive(:params).and_return({ "foo" => "key1:foo,key2:bar" })
controller.param! :foo, Hash
expect(controller.params["foo"]).to eql({ "key1" => "foo", "key2" => "bar" })
expect(controller.params["foo"]).to eq({ "key1" => "foo", "key2" => "bar" })
end

it "will raise InvalidParameterError if the value is a boolean" do
Expand All @@ -209,7 +209,7 @@ def params;
it "will convert to DateTime" do
allow(controller).to receive(:params).and_return({ "foo" => "1984-01-10" })
controller.param! :foo, Date
expect(controller.params["foo"]).to eql(Date.new(1984, 1, 10))
expect(controller.params["foo"]).to eq Date.new(1984, 1, 10)
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -226,7 +226,7 @@ def params;
it "will convert to DateTime" do
allow(controller).to receive(:params).and_return({ "foo" => "1984-01-10T12:25:00.000+02:00" })
controller.param! :foo, Date, format: "%F"
expect(controller.params["foo"]).to eql(Date.new(1984, 1, 10))
expect(controller.params["foo"]).to eq Date.new(1984, 1, 10)
end

it "will raise InvalidParameterError if the value is not valid" do
Expand Down Expand Up @@ -254,7 +254,7 @@ def params;
it "will convert to Time" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
controller.param! :foo, Time
expect(controller.params["foo"]).to eql(Time.new(2014, 8, 7, 12, 25, 0, 7200))
expect(controller.params["foo"]).to eq Time.new(2014, 8, 7, 12, 25, 0, 7200)
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -271,7 +271,7 @@ def params;
it "will convert to Time" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
controller.param! :foo, Time, format: "%F"
expect(controller.params["foo"]).to eql(Time.new(2014, 8, 7))
expect(controller.params["foo"]).to eq Time.new(2014, 8, 7)
end

it "will raise InvalidParameterError if the value is not valid" do
Expand Down Expand Up @@ -299,7 +299,7 @@ def params;
it "will convert to DateTime" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
controller.param! :foo, DateTime
expect(controller.params["foo"]).to eql(DateTime.new(2014, 8, 7, 12, 25, 0, '+2'))
expect(controller.params["foo"]).to eq DateTime.new(2014, 8, 7, 12, 25, 0, '+2')
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -316,7 +316,7 @@ def params;
it "will convert to DateTime" do
allow(controller).to receive(:params).and_return({ "foo" => "2014-08-07T12:25:00.000+02:00" })
controller.param! :foo, DateTime, format: "%F"
expect(controller.params["foo"]).to eql(DateTime.new(2014, 8, 7))
expect(controller.params["foo"]).to eq DateTime.new(2014, 8, 7)
end

it "will raise InvalidParameterError if the value is not valid" do
Expand All @@ -343,71 +343,71 @@ def params;
it "converts to BigDecimal using default precision" do
allow(controller).to receive(:params).and_return({ "foo" => 12345.67890123456 })
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 12345.678901235
expect(controller.params["foo"]).to eq 12345.678901235
end

it "converts to BigDecimal using precision option" do
allow(controller).to receive(:params).and_return({ "foo" => 12345.6789 })
controller.param! :foo, BigDecimal, precision: 6
expect(controller.params["foo"]).to eql 12345.7
expect(controller.params["foo"]).to eq 12345.7
end

it "converts formatted currency string to big decimal" do
allow(controller).to receive(:params).and_return({ "foo" => "$100,000" })
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 100000.0
expect(controller.params["foo"]).to eq 100000.0
end
end

describe "booleans" do
it "converts 1/0" do
allow(controller).to receive(:params).and_return({ "foo" => "1" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
expect(controller.params["foo"]).to eq true

allow(controller).to receive(:params).and_return({ "foo" => "0" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "converts true/false" do
allow(controller).to receive(:params).and_return({ "foo" => "true" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
expect(controller.params["foo"]).to eq true

allow(controller).to receive(:params).and_return({ "foo" => "false" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "converts t/f" do
allow(controller).to receive(:params).and_return({ "foo" => "t" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
expect(controller.params["foo"]).to eq true

allow(controller).to receive(:params).and_return({ "foo" => "f" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "converts yes/no" do
allow(controller).to receive(:params).and_return({ "foo" => "yes" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
expect(controller.params["foo"]).to eq true

allow(controller).to receive(:params).and_return({ "foo" => "no" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "converts y/n" do
allow(controller).to receive(:params).and_return({ "foo" => "y" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(true)
expect(controller.params["foo"]).to eq true

allow(controller).to receive(:params).and_return({ "foo" => "n" })
controller.param! :foo, TrueClass
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end

it "return InvalidParameterError if value not boolean" do
Expand All @@ -422,7 +422,7 @@ def params;
it "set default boolean" do
allow(controller).to receive(:params).and_return({})
controller.param! :foo, :boolean, default: false
expect(controller.params["foo"]).to eql(false)
expect(controller.params["foo"]).to eq false
end
end

Expand Down Expand Up @@ -773,7 +773,7 @@ def params;

it "succeeds in the range" do
controller.param! :price, Integer, in: 1..100
expect(controller.params["price"]).to eql(50)
expect(controller.params["price"]).to eq 50
end

it "raises outside the range" do
Expand All @@ -791,7 +791,7 @@ def params;
it "succeeds when valid" do
allow(controller).to receive(:params).and_return({ "number" => "50" })
controller.param! :number, Integer, custom: custom_validation
expect(controller.params["number"]).to eql(50)
expect(controller.params["number"]).to eq 50
end

it "raises when invalid" do
Expand Down

0 comments on commit 0920644

Please sign in to comment.