<commit_before>require 'nokogiri'

def xmlspec(path, &block)
  doc = Nokogiri::XML(File.open(path, 'r'))
  Document.new(doc).instance_eval &block
end

class Document
  attr_reader :doc

  def initialize(doc)
    @doc = doc
  end

  def context(path, &block)
    ctx = @doc.xpath(path)
    Context.new(ctx).instance_eval &block
  end

  class Context
    attr_reader :ctx

    def initialize(ctx)
      @ctx = ctx
    end

    def assertExists(path)
      !@ctx.xpath(path).to_a.empty?
    end

    def assertContains(path=".", descriptor={})
      return false unless assertExists(path)
      return true if descriptor.empty?
      resolved_path = "#{path}/#{descriptor_to_expression(descriptor)}"
      puts @ctx.xpath(resolved_path)<commit_msg>Added some permit and require logic to xmlunit and renamed the open method to xmlspec<commit_after>require 'nokogiri'

def xmlspec(path, &block)
  doc = Nokogiri::XML(File.open(path, 'r'))
  Document.new(doc).instance_eval &block
end

def error_function(message)
  puts message
end

class Document
  attr_reader :doc

  def initialize(doc)
    @doc = doc
  end

  def context(path, &block)
    ctx = @doc.xpath(path)
    Context.new(ctx).instance_eval &block
  end

  class Context
    attr_reader :ctx

    def initialize(ctx)
      @ctx = ctx
    end

    def require(path="", paths=[], &block)
      paths << path
      paths.each do |p|
        matches = @ctx.xpath(p)
        return error_function("The following match #{p} is required in the given context") if matches.to_a.empty?
        matches.each { |m| Context.new(m).instance_eval &block } unless block.nil?
      end
    end

    def permit(path="", paths=[], &block)
      present = @ctx.children.map { |child| child.name() }
      paths << path
      return error_function("The following elements: #{present - paths} are not valid") if (present - paths).length > 0
      present.each { |p| Context.new(p).instance_eval &block } unless block.nil?
    end

    def assertExists(path)
      !@ctx.xpath(path).to_a.empty?
    end

    def assertContains(path=".", descriptor={})
      return false unless assertExists(path)
      return true if descriptor.empty?
      resolved_path = "#{path}/#{descriptor_to_expression(descriptor)}"
      puts @ctx.xpath(resolved_path)
{'fraction_non_alphanumeric': 0.11984196663740122, 'fraction_numerical': 0.0004389815627743635, 'mean_word_length': 3.2360594795539033, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1313505', 'n_tokens_mistral': 734, 'n_tokens_neox': 722, 'n_words': 192}