<commit_before># frozen_string_literal: true

RSpec.describe Dry::Validation::Contract, '#message' do
  subject(:contract) do
    Class.new(Dry::Validation::Contract) do
      config.messages_file = SPEC_ROOT.join('fixtures/messages/errors.en.yml').realpath
    end.new
  end

  it 'returns message text for flat rule' do
    expect(contract.message(:taken, rule: :email, tokens: { email: '<EMAIL>' }))
      .to eql('looks like <EMAIL> is taken')
  end

  it 'returns message text for nested rule' do
    expect(contract.message(:invalid, rule: %i[address street]))
      .to eql("doesn't look good")
  end

  it 'raises error when template was not found' do
    expect { contract.message(:not_here, rule: :email) }
      .to raise_error(Dry::Validation::MissingMessageError, /not_here/)
  end
end
<commit_msg>Test both yaml and i18n in Contract#message spec<commit_after># frozen_string_literal: true

RSpec.describe Dry::Validation::Contract, '#message' do
  shared_context 'resolving' do
    subject(:contract) do
      contract_class.new
    end

    let(:contract_class) do
      Class.new(Dry::Validation::Contract) do
        config.messages_file = SPEC_ROOT.join('fixtures/messages/errors.en.yml').realpath
      end
    end

    it 'returns message text for flat rule' do
      expect(contract.message(:taken, rule: :email, tokens: { email: '<EMAIL>' }))
        .to eql('looks like <EMAIL> is taken')
    end

    it 'returns message text for nested rule' do
      expect(contract.message(:invalid, rule: %i[address street]))
        .to eql("doesn't look good")
    end

    it 'raises error when template was not found' do
      expect { contract.message(:not_here, rule: :email) }
        .to raise_error(Dry::Validation::MissingMessageError, /not_here/)
    end
  end

  context 'using :yaml' do
    before do
      contract_class.config.messages = :yaml
    end

    include_context 'resolving'
  end

  context 'using :i18n' do
    before do
      contract_class.config.messages = :i18n
    end

    include_context 'resolving'
  end
end

{'fraction_non_alphanumeric': 0.11289537712895377, 'fraction_numerical': 0.00291970802919708, 'mean_word_length': 3.4406047516198703, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3421267', 'n_tokens_mistral': 655, 'n_tokens_neox': 629, 'n_words': 181}