<commit_before>module HaveStructureMatcher
  module Methods
    def optionally(optional_expected_value)
      ->(actual_value) { actual_value.nil? || HaveStructureMatcher.match?(actual_value, optional_expected_value) }
    end
  end

  def self.match?(actual, expected)
    actual == build_diff(actual, expected)
  end

  def self.build_diff(actual, expected)
    return expected unless actual.is_a?(Hash)

    keys = actual.keys | expected.keys

    keys.each_with_object({}) do |key, memo|
      if actual.key?(key) && expected.key?(key)
        actual_value = actual[key]
        expected_value = expected[key]

        if expected_value.is_a?(Hash)
          memo[key] = build_diff(actual_value, expected_value)
        elsif value_match?(actual_value, expected_value)
          memo[key] = actual_value
        else
          memo[key] = print_expected_value(expected_value)
        end
      elsif expected.key?(key)
        expected_value = expected[key]
        memo[key] = print_expected_value(expected_value)
      end
    end
  end

  def self.print_expected_value(expected_value)
    if expected_value.respond_to?(:call)
      "Proc [#{expected_value.source_location.join(':')}]"
    else
      expected_value
    end
  end

  def self.value_match?(actual_value, expected_value)
    if expected_value.is_a?(Regexp)
      actual_value =~ expected_value
    elsif expected_value.is_a?(Class)
      actual_value.is_a?(expected_value)
    elsif expected_value.respond_to?(:call)
      expected_value.call(actual_value)
    else
      actual_value == expected_value
    end
  end
end

RSpec.configure do |config|
  config.include HaveStructureMatcher::Methods
end
<commit_msg>Support arrays within nested structures<commit_after>module HaveStructureMatcher
  module Methods
    def optionally(optional_expected_value)
      ->(actual_value) { actual_value.nil? || HaveStructureMatcher.match?(actual_value, optional_expected_value) }
    end
  end

  def self.match?(actual, expected)
    actual == build_diff(actual, expected)
  end

  def self.build_diff(actual, expected)
    return expected unless actual.is_a?(Hash)

    keys = actual.keys | expected.keys

    keys.each_with_object({}) do |key, memo|
      if actual.key?(key) && expected.key?(key)
        actual_value = actual[key]
        expected_value = expected[key]

        if expected_value.is_a?(Hash)
          memo[key] = build_diff(actual_value, expected_value)
        elsif expected_value.is_a?(Array)
          memo[key] = expected_value.zip(actual_value).map { |(e,a)| build_diff(a, e) }
        elsif value_match?(actual_value, expected_value)
          memo[key] = actual_value
        else
          memo[key] = print_expected_value(expected_value)
        end
      elsif expected.key?(key)
        expected_value = expected[key]
        memo[key] = print_expected_value(expected_value)
      end
    end
  end

  def self.print_expected_value(expected_value)
    if expected_value.respond_to?(:call)
      "Proc [#{expected_value.source_location.join(':')}]"
    else
      expected_value
    end
  end

  def self.value_match?(actual_value, expected_value)
    if expected_value.is_a?(Regexp)
      actual_value =~ expected_value
    elsif expected_value.is_a?(Class)
      actual_value.is_a?(expected_value)
    elsif expected_value.respond_to?(:call)
      expected_value.call(actual_value)
    else
      actual_value == expected_value
    end
  end
end

RSpec.configure do |config|
  config.include HaveStructureMatcher::Methods
end

{'fraction_non_alphanumeric': 0.09511641113003975, 'fraction_numerical': 0.0, 'mean_word_length': 3.392768079800499, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2605464', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1106, 'n_words': 207}