<commit_before>module MatchNodes
  class Matcher
    def css_select root, selector
      HTML::Selector.new(selector).select(root)
    end

    def initialize expected_nodes
      @expected_nodes = expected_nodes
    end

    def matches? text
      @text = text
      root_node = HTML::Document.new(text, false, false).root
      matches_nodes? [root_node], @expected_nodes
    end

    def failure_attribute_message
      return nil if @failure_attribute_name.nil?
      "

expected attribute #{@failure_attribute_name.inspect}
             to be #{@failure_attribute_expected.inspect}
               got #{@failure_attribute_value.inspect}
"
    end
    def failure_message
      "selector #{@failure_selector.split(/ +/).to_yaml}
didn't match expected nodes
#{@failure_expected.to_yaml}

got #{@failure_nodes.size} nodes :
#{@failure_nodes.join "\n"}#{failure_attribute_message}"
    end

    def description
      "contain html nodes #{@expected_nodes}"
    end

    def matches_attribute? nodes, name, expected, path
      @failure_attribute_name = name
      @failure_attribute_expected = expected
      if name == :innerHTML
        return matches_nodes?(nodes, expected, path)
      elsif name == :innerText
        @failure_attribute_value = inner_text(nodes[0])
        return @failure_attribute_value == expected.to_s
      elsif expected == false
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
        return @failure_attribute_value.blank?
      elsif expected == true
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
        return @failure_attribute_value.present?
      else
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s<commit_msg>attribute matcher: match on regexp<commit_after>module MatchNodes
  class Matcher
    def css_select root, selector
      HTML::Selector.new(selector).select(root)
    end

    def initialize expected_nodes
      @expected_nodes = expected_nodes
    end

    def matches? text
      @text = text
      root_node = HTML::Document.new(text, false, false).root
      matches_nodes? [root_node], @expected_nodes
    end

    def failure_attribute_message
      return nil if @failure_attribute_name.nil?
      "

expected attribute #{@failure_attribute_name.inspect}
             to be #{@failure_attribute_expected.inspect}
               got #{@failure_attribute_value.inspect}
"
    end
    def failure_message
      "selector #{@failure_selector.split(/ +/).to_yaml}
didn't match expected nodes
#{@failure_expected.to_yaml}

got #{@failure_nodes.size} nodes :
#{@failure_nodes.join "\n"}#{failure_attribute_message}"
    end

    def description
      "contain html nodes #{@expected_nodes}"
    end

    def matches_attribute? nodes, name, expected, path
      @failure_attribute_name = name
      @failure_attribute_expected = expected
      if name == :innerHTML
        return matches_nodes?(nodes, expected, path)
      elsif name == :innerText
        @failure_attribute_value = inner_text(nodes[0])
        return @failure_attribute_value == expected.to_s
      elsif expected == false
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
        return @failure_attribute_value.blank?
      elsif expected == true
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
        return @failure_attribute_value.present?
      elsif expected.is_a? Regexp
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
        return @failure_attribute_value.match(expected)
      else
        @failure_attribute_value = nodes[0].attributes[name.to_s].to_s
{'fraction_non_alphanumeric': 0.09221424627277747, 'fraction_numerical': 0.002484815019326339, 'mean_word_length': 3.292654028436019, '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': '2102794', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1044, 'n_words': 228}