<commit_before>require 'io/grab'

# Custom matcher to test text written to standard output and standard error
#
# @example
#   expect { $stderr.puts "Some random error" }.to write(/Some.* error/).to(:stderr)
#
# @example
#   expect { $stderr.puts "Some specific error" }.to write('Some specific error').to(:stderr)
#
# @note http://greyblake.com/blog/2012/12/14/custom-expectations-with-rspec/
RSpec::Matchers.define :write do |message|
  chain(:to) do |io|
    @io = io
  end

  match do |block|
    stream = case io
    when :stdout
      STDOUT
    when :stderr
      STDERR
    else
      io
    end

    output = stream.grab &block

    case message
    when Hash then output.include?(JSON.pretty_generate message)
    when String then output.include? message<commit_msg>Deprecation warning--<commit_after>require 'io/grab'

# Custom matcher to test text written to standard output and standard error
#
# @example
#   expect { $stderr.puts "Some random error" }.to write(/Some.* error/).to(:stderr)
#
# @example
#   expect { $stderr.puts "Some specific error" }.to write('Some specific error').to(:stderr)
#
# @note http://greyblake.com/blog/2012/12/14/custom-expectations-with-rspec/
RSpec::Matchers.define :write do |message|
  supports_block_expectations

  chain(:to) do |io|
    @io = io
  end

  match do |block|
    stream = case io
    when :stdout
      STDOUT
    when :stderr
      STDERR
    else
      io
    end

    output = stream.grab &block

    case message
    when Hash then output.include?(JSON.pretty_generate message)
    when String then output.include? message
{'fraction_non_alphanumeric': 0.11565053425518541, 'fraction_numerical': 0.01005656819610308, 'mean_word_length': 3.167539267015707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3095946', 'n_tokens_mistral': 528, 'n_tokens_neox': 505, 'n_words': 151}