# frozen_string_literal: true

require 'active_model/cases/test_base'

module ActiveModel
  class FormatValidatorTest < ClientSideValidations::ActiveModelTestBase
    def test_format_client_side_hash
      expected_hash = { message: 'is invalid', with: /.+/ }

      assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/).client_side_hash(@person, :first_name)
    end

    def test_format_client_side_hash_without
      expected_hash = { message: 'is invalid', without: /.+/ }

      assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: /.+/).client_side_hash(@person, :first_name)
    end

    def test_format_client_side_hash_with_custom_message
      expected_hash = { message: 'is wrong format', with: /.+/ }

      assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: /.+/, message: 'is wrong format').client_side_hash(@person, :first_name)
    end

    def test_format_client_side_hash_ignore_proc
      @person.stubs(:matcher).returns(/.+/)

      assert_nil FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name)
    end

    def test_format_client_side_hash_without_ignore_proc
      @person.stubs(:matcher).returns(/.+/)

      assert_nil FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name)
    end

    def test_format_client_side_hash_observe_proc
      @person.stubs(:matcher).returns(/.+/)
      expected_hash = { message: 'is invalid', with: /.+/ }

      assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], with: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true)
    end

    def test_format_client_side_hash_without_observe_proc
      @person.stubs(:matcher).returns(/.+/)
      expected_hash = { message: 'is invalid', without: /.+/ }

      assert_equal expected_hash, FormatValidator.new(attributes: [:first_name], without: proc { |o| o.matcher }).client_side_hash(@person, :first_name, true)
    end
  end
end
