Skip to content

Commit

Permalink
reorganize css tests (part 2) (#3227)
Browse files Browse the repository at this point in the history
**What problem is this PR intended to solve?**

As part of the preparation for overhauling the CSS parser, doing some
cleanup in the CSS tests to make them easier to understand and easier to
instrument.
  • Loading branch information
flavorjones authored Jun 11, 2024
2 parents 409e041 + 0f367e6 commit f0f6233
Show file tree
Hide file tree
Showing 5 changed files with 465 additions and 401 deletions.
2 changes: 1 addition & 1 deletion lib/nokogiri/css/selector_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def []=(key, value)
# Clear the cache
def clear_cache(create_new_object = false)
@mutex.synchronize do
if create_new_object
if create_new_object # used in tests to avoid 'method redefined' warnings when injecting spies
@cache = {}
else
@cache.clear
Expand Down
6 changes: 6 additions & 0 deletions test/css/test_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
assert_raises(TypeError) { Nokogiri::CSS.xpath_for(Object.new) }
assert_raises(TypeError) { Nokogiri::CSS.xpath_for(["foo", "bar"]) }
end

it "raises an exception for pseudo-classes that are not XPath Names" do
# see https://github.com/sparklemotion/nokogiri/issues/3193
assert_raises(Nokogiri::CSS::SyntaxError) { Nokogiri::CSS.xpath_for("div:-moz-drag-over") }
assert_raises(Nokogiri::CSS::SyntaxError) { Nokogiri::CSS.xpath_for("div:-moz-drag-over()") }
end
end
end
end
106 changes: 0 additions & 106 deletions test/css/test_parser_cache.rb

This file was deleted.

97 changes: 97 additions & 0 deletions test/css/test_selector_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Loading

0 comments on commit f0f6233

Please sign in to comment.