10BC0 ActiveModel::Type::Boolean Cast Unexpected Result for Float Zero · Issue #56602 · rails/rails · GitHub
[go: up one dir, main page]

Skip to content

ActiveModel::Type::Boolean Cast Unexpected Result for Float Zero #56602

@crespire

Description

@crespire

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"

  gem "sqlite3"
end

require "active_record/railtie"
require "minitest/autorun"

# This connection will do for database-independent bug reports.
ENV["DATABASE_URL"] = "sqlite3::memory:"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f
  config.eager_load = false
  config.logger = Logger.new($stdout)
  config.secret_key_base = "secret_key_base"

  config.active_record.encryption.primary_key = "primary_key"
  config.active_record.encryption.deterministic_key = "deterministic_key"
  config.active_record.encryption.key_derivation_salt = "key_derivation_salt"
end
Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end

  create_table :comments, force: true do |t|
    t.integer :post_id
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class BugTest < ActiveSupport::TestCase
  def test_integer_and_float_zero_cast_to_boolean
    @integer_cast = ActiveModel::Type::Boolean.new.cast(0)
    @float_cast = ActiveModel::Type::Boolean.new.cast(0.0)

    assert_equal @integer_cast, @float_cast
  end

  def test_integer_zero_cast_to_boolean
    @integer_cast = ActiveModel::Type::Boolean.new.cast(0)
    assert_equal false, @integer_cast
  end

  def test_float_zero_cast_to_boolean
    @float_cast = ActiveModel::Type::Boolean.new.cast(0.0)
    assert_equal false, @float_cast
  end
end

Expected behavior

I would expect float zero (0.0) and integer zero (0) to both cast to the same boolean false

Actual behavior

Float zero (0.0) returns true

System configuration

Rails version: 8.1.2

Ruby version: 3.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0