File: test_selector_cache.rb

package info (click to toggle)
ruby-nokogiri 1.18.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,076 kB
  • sloc: ansic: 38,893; xml: 27,665; ruby: 27,285; java: 15,348; cpp: 7,107; yacc: 244; sh: 208; makefile: 154; sed: 14
file content (97 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

require "helper"

describe Nokogiri::CSS::SelectorCache do
  before do
    Nokogiri::CSS::SelectorCache.clear_cache
    Nokogiri::CSS::SelectorCache.class_eval do
      class << @cache
        alias_method :old_bracket, :[]

        def access_count
          @access_count ||= 0
        end

        def [](key)
          @access_count ||= 0
          @access_count += 1
          old_bracket(key)
        end
      end
    end
  end

  after do
    Nokogiri::CSS::SelectorCache.clear_cache(true)
  end

  let(:selector_list) { "a1 > b2 > c3" }

  it "uses the cache by default" do
    Nokogiri::CSS.xpath_for(selector_list)
    Nokogiri::CSS.xpath_for(selector_list)

    assert_equal(1, Nokogiri::CSS::SelectorCache.class_eval { @cache.count })
    assert_equal(2, Nokogiri::CSS::SelectorCache.class_eval { @cache.access_count })
  end

  it "uses the cache when explicitly requested" do
    Nokogiri::CSS.xpath_for(selector_list, cache: true)
    Nokogiri::CSS.xpath_for(selector_list, cache: true)

    assert_equal(1, Nokogiri::CSS::SelectorCache.class_eval { @cache.count })
    assert_equal(2, Nokogiri::CSS::SelectorCache.class_eval { @cache.access_count })
  end

  it "does not use the cache when explicitly requested" do
    Nokogiri::CSS.xpath_for(selector_list, cache: false)
    Nokogiri::CSS.xpath_for(selector_list, cache: false)

    assert_equal(0, Nokogiri::CSS::SelectorCache.class_eval { @cache.count })
    assert_equal(0, Nokogiri::CSS::SelectorCache.class_eval { @cache.access_count })
  end

  it "uses the cached expressions" do
    Nokogiri::CSS::SelectorCache.clear_cache

    cache = Nokogiri::CSS::SelectorCache.instance_variable_get(:@cache)

    assert_empty(cache)
    Nokogiri::CSS.xpath_for(selector_list)
    refute_empty(cache)
    key = cache.keys.first

    cache[key] = ["this is an injected value"]
    assert_equal(["this is an injected value"], Nokogiri::CSS.xpath_for(selector_list))
  end

  it "test_cache_key_on_ns_prefix_and_visitor_config" do
    Nokogiri::CSS::SelectorCache.clear_cache

    cache = Nokogiri::CSS::SelectorCache.instance_variable_get(:@cache)
    assert_empty(cache)

    Nokogiri::CSS.xpath_for(selector_list)
    Nokogiri::CSS.xpath_for(selector_list, prefix: ".//")
    Nokogiri::CSS.xpath_for(selector_list, prefix: ".//", ns: { "example" => "http://example.com/" })
    Nokogiri::CSS.xpath_for(
      selector_list,
      visitor: Nokogiri::CSS::XPathVisitor.new(
        builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::ALWAYS,
        prefix: ".//",
        namespaces: { "example" => "http://example.com/" },
      ),
    )
    Nokogiri::CSS.xpath_for(
      selector_list,
      visitor: Nokogiri::CSS::XPathVisitor.new(
        builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::ALWAYS,
        doctype: Nokogiri::CSS::XPathVisitor::DoctypeConfig::HTML5,
        prefix: ".//",
        namespaces: { "example" => "http://example.com/" },
      ),
    )
    assert_equal(5, cache.length)
  end
end