<commit_before>def find_attribute(name, value = nil)
  attribute = find("[data-field='#{name}']")

  yield attribute, @inputs[name] if block_given?

  attribute.text.should == value.to_s if value

  attribute
end

def dont_find_attribute(name, value = nil, &block)
  dont_find_wrap("attribute #{name}") do
    find_attribute(name, value, &block)
  end
end

def has_attribute?(name, value)
  attribute = find_attribute(name)

  attribute.text == value
end

def find_input(name)
  find("[name$='[#{name}]']")
end

def set_input(name, value)
  value = value.id if value.respond_to?(:id)

  @inputs ||= {}
  @inputs[name] = value

  input = find_input(name)

  case input.tag_name.downcase
  when 'select'
    begin
      input.find("option[value='#{value}']").select_option
    rescue Capybara::ElementNotFound
      input.find("option[text()='#{value}']").select_option
    end
  else
    input.set(value)
  end
end

def find_attributes(*attrs)
  attrs.each do |attr|
    find_attribute(attr).text.should == @inputs[attr].to_s
  end
end

def find_submit
  find('[type=submit]')
end

def find_action(action)
  find("[data-action='#{action}']")
end

def find_object_action(object, action)
  find("[data-type='#{object}'][data-action='#{action}']")
end

module Capybara
  class ElementFound < StandardError
    def initialize(search)
      @search = search
    end

    def message
      @search
    end
  end
end

def dont_find_wrap(search)
  yield

  raise Capybara::ElementFound.new(search)
rescue Capybara::ElementNotFound
  true
end

def dont_find(search)
  dont_find_wrap(search) do
    find(search)
  end
end

def dont_find_object(object)
  case object
  when Class
    dont_find("[data-type='#{object}']")
  else
    dont_find("[data-id='#{object.id}']")
  end
end

def find_object(object)
  find(object_matcher(object))
end

def within_object(object, &block)
  within(object_matcher(object), &block)
end

def object_matcher(object)
  "[data-id='#{object.id}'][data-type='#{object.class}']"
end

def within_object_of_type(klass, &block)
  within("[data-type='#{klass}']", &block)
end

def within_any(search, &block)
  case search
  when Class
    search = "[data-type='#{search}']"
  end

  nodes = all(search)

  raise Capybara::ElementNotFound if nodes.empty?

  nodes.each_with_index do |node, index|
    begin
      within("#{search}:eq(#{index + 1})", &block)
      return true
    rescue RSpec::Expectations::ExpectationNotMetError, Capybara::ElementNotFound
    end
  end

  false
end

def refind_object(object)
  object.class.find(object.id)
end<commit_msg>refine some finders<commit_after>def find_attribute(name, value = nil)
  attribute = find("[data-field='#{name}']")

  yield attribute, @inputs[name] if block_given?

  attribute.text.should == value.to_s if value

  attribute
end

def dont_find_attribute(name, value = nil, &block)
  dont_find_wrap("attribute #{name}") do
    find_attribute(name, value, &block)
  end
end

def has_attribute?(name, value)
  attribute = find_attribute(name)

  attribute.text == value
end

def find_input(name)
  find("[name$='[#{name}]']")
end

def set_input(name, value)
  value = value.id if value.respond_to?(:id)

  @inputs ||= {}
  @inputs[name] = value

  input = find_input(name)

  case input.tag_name.downcase
  when 'select'
    begin
      input.find("option[value='#{value}']").select_option
    rescue Capybara::ElementNotFound
      input.find("option[text()='#{value}']").select_option
    end
  else
    input.set(value)
  end
end

def find_attributes(*attrs)
  attrs.each do |attr|
    find_attribute(attr).text.should == @inputs[attr].to_s
  end
end

def find_submit
  find('[type=submit]')
end

def find_action(action)
  find("[data-action='#{action}']")
end

def find_object_action(object, action)
  find("[data-type='#{object}'][data-action='#{action}']")
end

module Capybara
  class ElementFound < StandardError
    def initialize(search)
      @search = search
    end

    def message
      @search
    end
  end
end

def dont_find_wrap(search)
  yield

  raise Capybara::ElementFound.new(search)
rescue Capybara::ElementNotFound
  true
end

def dont_find(search)
  dont_find_wrap(search) do
    find(search)
  end
end

def dont_find_object(object)
  case object
  when Class
    dont_find("[data-type='#{object}']")
  else
    dont_find("[data-id='#{object.id}']")
  end
end

def find_object(object)
  find(object_matcher(object))
end

def within_object(object, &block)
  within(object_matcher(object), &block)
end

def object_matcher(object)
  if object.respond_to?(:id)
    match = "[data-type='#{object.class}'][data-id='#{object.id}']"
  elsif object.kind_of?(::Class)
    match = "[data-type='#{object}']"
  end

  match
end

def within_object_of_type(klass, &block)
  within_object(klass, &block)
end

def within_any(search, &block)
  case search
  when Class
    search = "[data-type='#{search}']"
  end

  nodes = all(search)

  raise Capybara::ElementNotFound if nodes.empty?

  nodes.each_with_index do |node, index|
    begin
      within("#{search}:eq(#{index + 1})", &block)
      return true
    rescue RSpec::Expectations::ExpectationNotMetError, Capybara::ElementNotFound
    end
  end

  false
end

def refind_object(object)
  object.class.find(object.id)
end
{'fraction_non_alphanumeric': 0.14215033301617508, 'fraction_numerical': 0.00038058991436726926, 'mean_word_length': 4.424148606811145, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2968659', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1880, 'n_words': 390}