<commit_before>require 'spec_helper'

describe Oga::XPath::Compiler do
  describe 'descendant-or-self axis' do
    before do
      @document = parse('<a><b><b><c class="x"></c></b></b></a>')

      @a1 = @document.children[0]
      @b1 = @a1.children[0]
      @b2 = @b1.children[0]
      @c1 = @b2.children[0]
    end

    it 'returns a node set containing a direct descendant' do
      evaluate_xpath(@document, 'descendant-or-self::b')
        .should == node_set(@b1, @b2)
    end

    it 'returns a node set containing a nested descendant' do
      evaluate_xpath(@document, 'descendant-or-self::c').should == node_set(@c1)
    end

    it 'returns a node set using multiple descendant expressions' do
      evaluate_xpath(@document, 'descendant-or-self::a/descendant-or-self::*')
        .should == node_set(@a1, @b1, @b2, @c1)
    end

    it 'returns a node set containing a descendant with an attribute' do
      evaluate_xpath(@document, 'descendant-or-self::c[@class="x"]')
        .should == node_set(@c1)
    end

    it 'returns a node set containing a descendant relative to a node' do
      evaluate_xpath(@document, 'a/descendant-or-self::b')
        .should == node_set(@b1, @b2)
    end

    it 'returns a node set containing the context node' do
      evaluate_xpath(@document, 'descendant-or-self::a')
        .should == node_set(@a1)
    end

    it 'returns a node set containing the context node relative to a node' do
      evaluate_xpath(@document, 'a/b/b/c/descendant-or-self::c')
        .should == node_set(@c1)
    end

    it 'returns a node set containing the first descendant' do
      evaluate_xpath(@document, 'descendant-or-self::b[1]')
        .should == node_set(@b1)
    end

    it 'returns an empty node set for a non existing descendant' do
      evaluate_xpath(@document, 'descendant-or-self::foobar').should == node_set
    end

    it 'returns a node set containing a descendant using the short form' do
      evaluate_xpath(@document, '//b').should == node_set(@b1, @b2)
    end

    it 'returns a node set containing a nested descendant using the short form' do
      evaluate_xpath(@document, '//a//*').should == node_set(@b1, @b2, @c1)
    end

    it 'returns a node set containing children of a descendant' do
      evaluate_xpath(@document, '//a/b').should == node_set(@b1)
    end
  end
end<commit_msg>Cleaned up descendant-or-self compiler specs<commit_after>require 'spec_helper'

describe Oga::XPath::Compiler do
  describe 'descendant-or-self axis' do
    before do
      @document = parse('<a><b><b><c class="x"></c></b></b></a>')

      @a1 = @document.children[0]
      @b1 = @a1.children[0]
      @b2 = @b1.children[0]
      @c1 = @b2.children[0]
    end

    describe 'using the full syntax' do
      it 'returns a NodeSet containing a direct descendant' do
        evaluate_xpath(@document, 'descendant-or-self::b')
          .should == node_set(@b1, @b2)
      end

      it 'returns a NodeSet containing a nested descendant' do
        evaluate_xpath(@document, 'descendant-or-self::c').should == node_set(@c1)
      end

      it 'returns a NodeSet using multiple descendant expressions' do
        evaluate_xpath(@document, 'descendant-or-self::a/descendant-or-self::*')
          .should == node_set(@a1, @b1, @b2, @c1)
      end

      it 'returns a NodeSet containing a descendant with an attribute' do
        evaluate_xpath(@document, 'descendant-or-self::c[@class="x"]')
          .should == node_set(@c1)
      end

      it 'returns a NodeSet containing a descendant relative to a node' do
        evaluate_xpath(@document, 'a/descendant-or-self::b')
          .should == node_set(@b1, @b2)
      end

      it 'returns a NodeSet containing the context node' do
        evaluate_xpath(@document, 'descendant-or-self::a')
          .should == node_set(@a1)
      end

      it 'returns a NodeSet containing the context node relative to a node' do
        evaluate_xpath(@document, 'a/b/b/c/descendant-or-self::c')
          .should == node_set(@c1)
      end

      it 'returns a NodeSet containing the first descendant' do
        evaluate_xpath(@document, 'descendant-or-self::b[1]')
          .should == node_set(@b1)
      end

      it 'returns an empty NodeSet for a non existing descendant' do
        evaluate_xpath(@document, 'descendant-or-self::foobar')
          .should == node_set
      end

      describe 'relative to a node' do
        it 'returns a NodeSet containing a node and its descendants' do
          evaluate_xpath(@b1, 'descendant-or-self::b')
            .should == node_set(@b1, @b2)
        end

        it 'returns a NodeSet containing only the node itself' do
          evaluate_xpath(@c1, 'descendant-or-self::c').should == node_set(@c1)
        end
      end
    end

    describe 'using the shorthand syntax' do
      it 'returns a NodeSet containing all <b> nodes' do
        evaluate_xpath(@document, '//b').should == node_set(@b1, @b2)
      end

      it 'returns a NodeSet containing all the descendants of <a>' do
        evaluate_xpath(@document, '//a//*').should == node_set(@b1, @b2, @c1)
      end

      it 'returns a NodeSet containing all <b> nodes in an <a> node' do
        evaluate_xpath(@document, '//a/b').should == node_set(@b1)
      end

      describe 'relative to a node' do
        it 'returns a NodeSet containing all <b> nodes' do
          evaluate_xpath(@c1, '//b').should == node_set(@b1, @b2)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12794735880095046, 'fraction_numerical': 0.012794735880095047, 'mean_word_length': 2.7275204359673024, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '2648725', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1744, 'n_words': 517}