<commit_before># from https://gist.github.com/elgalu/5073871
require 'rspec'
require 'stringio'

# 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|
    output =
      case io
      when :stdout then fake_stdout(&block)
      when :stderr  then fake_stderr(&block)
      else fail("Allowed values for `to` are :stdout and :stderr, got `#{io.inspect}`")
      end

    case message
    when String then output.include? message
    when Regexp then output.match message
    when nil then output
    else fail("Allowed types for write `message` are String or Regexp, got `#{message.class}`")<commit_msg>write expectation can be a Hash, in which case it is assumed to be pretty-printed JSON<commit_after># from https://gist.github.com/elgalu/5073871
require 'rspec'
require 'stringio'

# 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|
    output =
      case io
      when :stdout then fake_stdout(&block)
      when :stderr  then fake_stderr(&block)
      else fail("Allowed values for `to` are :stdout and :stderr, got `#{io.inspect}`")
      end

    case message
    when Hash then output.include?(JSON.pretty_generate message)
    when String then output.include? message
    when Regexp then output.match message
    when nil then output
    else fail("Allowed types for write `message` are String or Regexp, got `#{message.class}`")
{'fraction_non_alphanumeric': 0.12083333333333333, 'fraction_numerical': 0.013888888888888888, 'mean_word_length': 3.6175213675213675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '208512', 'n_tokens_mistral': 693, 'n_tokens_neox': 679, 'n_words': 227}