Skip to content

Commit

Permalink
style(rubocop): safe autocorrects
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jul 5, 2023
1 parent 268aa60 commit 67512ed
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 403 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in rails-dom-testing.gemspec
gemspec
Expand Down
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "bundler/gem_tasks"
require "rake/testtask"

task :default => :test
task default: :test
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.libs << "test"
t.pattern = "test/**/*_test.rb"
t.warning = true
t.verbose = true
end
end
2 changes: 1 addition & 1 deletion lib/rails-dom-testing.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'rails/dom/testing/assertions'
require "rails/dom/testing/assertions"
6 changes: 3 additions & 3 deletions lib/rails/dom/testing/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'nokogiri'
require 'rails/dom/testing/assertions/dom_assertions'
require 'rails/dom/testing/assertions/selector_assertions'
require "nokogiri"
require "rails/dom/testing/assertions/dom_assertions"
require "rails/dom/testing/assertions/selector_assertions"

module Rails
module Dom
Expand Down
4 changes: 1 addition & 3 deletions lib/rails/dom/testing/assertions/dom_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def assert_dom_not_equal(expected, actual, message = nil, strict: false)
end

protected

def compare_doms(expected, actual, strict)
expected_children = extract_children(expected, strict)
actual_children = extract_children(actual, strict)
Expand All @@ -41,7 +40,7 @@ def extract_children(node, strict)
if strict
node.children
else
node.children.reject{|n| n.text? && n.text.blank?}
node.children.reject { |n| n.text? && n.text.blank? }
end
end

Expand Down Expand Up @@ -83,7 +82,6 @@ def equal_attribute?(attr, other_attr)
end

private

def fragment(text)
Nokogiri::HTML::DocumentFragment.parse(text)
end
Expand Down
459 changes: 229 additions & 230 deletions lib/rails/dom/testing/assertions/selector_assertions.rb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Assertions
module SelectorAssertions
module CountDescribable
private
def count_description(min, max, count) #:nodoc:
def count_description(min, max, count) # :nodoc:
if min && max && (max != min)
"between #{min} and #{max} elements"
elsif min && max && max == min && count
Expand All @@ -18,7 +18,7 @@ def count_description(min, max, count) #:nodoc:
end

def pluralize_element(quantity)
quantity == 1 ? 'element' : 'elements'
quantity == 1 ? "element" : "elements"
end
end
end
Expand Down
149 changes: 74 additions & 75 deletions lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'minitest'
require 'active_support'
require 'active_support/core_ext/module/attribute_accessors'
require_relative 'substitution_context'
require "minitest"
require "active_support"
require "active_support/core_ext/module/attribute_accessors"
require_relative "substitution_context"

class HTMLSelector #:nodoc:
class HTMLSelector # :nodoc:
attr_reader :css_selector, :tests, :message

include Minitest::Assertions
Expand All @@ -24,99 +24,98 @@ def initialize(values, previous_selection = nil, &root_fallback)
end
end

def selecting_no_body? #:nodoc:
def selecting_no_body? # :nodoc:
# Nokogiri gives the document a body element. Which means we can't
# run an assertion expecting there to not be a body.
@selector == 'body' && @tests[:count] == 0
@selector == "body" && @tests[:count] == 0
end

def select
filter @root.css(@selector, context)
end

private
NO_STRIP = %w{pre script style textarea}

NO_STRIP = %w{pre script style textarea}
mattr_reader(:context) { SubstitutionContext.new }

mattr_reader(:context) { SubstitutionContext.new }
def filter(matches)
match_with = tests[:text] || tests[:html]
return matches if matches.empty? || !match_with

def filter(matches)
match_with = tests[:text] || tests[:html]
return matches if matches.empty? || !match_with
content_mismatch = nil
text_matches = tests.has_key?(:text)
regex_matching = match_with.is_a?(Regexp)

content_mismatch = nil
text_matches = tests.has_key?(:text)
regex_matching = match_with.is_a?(Regexp)
remaining = matches.reject do |match|
# Preserve markup with to_s for html elements
content = text_matches ? match.text : match.children.to_s

remaining = matches.reject do |match|
# Preserve markup with to_s for html elements
content = text_matches ? match.text : match.children.to_s
content.strip! unless NO_STRIP.include?(match.name)
content.sub!(/\A\n/, "") if text_matches && match.name == "textarea"

content.strip! unless NO_STRIP.include?(match.name)
content.sub!(/\A\n/, '') if text_matches && match.name == "textarea"
next if regex_matching ? (content =~ match_with) : (content == match_with)
content_mismatch ||= diff(match_with, content)
true
end

next if regex_matching ? (content =~ match_with) : (content == match_with)
content_mismatch ||= diff(match_with, content)
true
@message ||= content_mismatch if remaining.empty?
Nokogiri::XML::NodeSet.new(matches.document, remaining)
end

@message ||= content_mismatch if remaining.empty?
Nokogiri::XML::NodeSet.new(matches.document, remaining)
end

def extract_root(previous_selection, root_fallback)
possible_root = @values.first

if possible_root == nil
raise ArgumentError, 'First argument is either selector or element ' \
'to select, but nil found. Perhaps you called assert_dom with ' \
'an element that does not exist?'
elsif possible_root.respond_to?(:css)
@values.shift # remove the root, so selector is the first argument
possible_root
elsif previous_selection
previous_selection
else
root_fallback.call
def extract_root(previous_selection, root_fallback)
possible_root = @values.first

if possible_root == nil
raise ArgumentError, "First argument is either selector or element " \
"to select, but nil found. Perhaps you called assert_dom with " \
"an element that does not exist?"
elsif possible_root.respond_to?(:css)
@values.shift # remove the root, so selector is the first argument
possible_root
elsif previous_selection
previous_selection
else
root_fallback.call
end
end
end

def extract_selectors
selector = @values.shift
def extract_selectors
selector = @values.shift

unless selector.is_a? String
raise ArgumentError, "Expecting a selector as the first argument"
end
unless selector.is_a? String
raise ArgumentError, "Expecting a selector as the first argument"
end

@css_selector = context.substitute!(selector, @values.dup, true)
@selector = context.substitute!(selector, @values)
end

def extract_equality_tests
comparisons = {}
case comparator = @values.shift
when Hash
comparisons = comparator
when String, Regexp
comparisons[:text] = comparator
when Integer
comparisons[:count] = comparator
when Range
comparisons[:minimum] = comparator.begin
comparisons[:maximum] = comparator.end
when FalseClass
comparisons[:count] = 0
when NilClass, TrueClass
comparisons[:minimum] = 1
else raise ArgumentError, "I don't understand what you're trying to match"
@css_selector = context.substitute!(selector, @values.dup, true)
@selector = context.substitute!(selector, @values)
end

# By default we're looking for at least one match.
if comparisons[:count]
comparisons[:minimum] = comparisons[:maximum] = comparisons[:count]
else
comparisons[:minimum] ||= 1
def extract_equality_tests
comparisons = {}
case comparator = @values.shift
when Hash
comparisons = comparator
when String, Regexp
comparisons[:text] = comparator
when Integer
comparisons[:count] = comparator
when Range
comparisons[:minimum] = comparator.begin
comparisons[:maximum] = comparator.end
when FalseClass
comparisons[:count] = 0
when NilClass, TrueClass
comparisons[:minimum] = 1
else raise ArgumentError, "I don't understand what you're trying to match"
end

# By default we're looking for at least one match.
if comparisons[:count]
comparisons[:minimum] = comparisons[:maximum] = comparisons[:count]
else
comparisons[:minimum] ||= 1
end
comparisons
end
comparisons
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class SubstitutionContext
def initialize
@substitute = '?'
@substitute = "?"
end

def substitute!(selector, values, format_for_presentation = false)
Expand Down
9 changes: 5 additions & 4 deletions rails-dom-testing.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rails/dom/testing/version'
require "rails/dom/testing/version"

Gem::Specification.new do |spec|
spec.name = "rails-dom-testing"
spec.version = Rails::Dom::Testing::VERSION
spec.authors = ["Rafael Mendonça França", "Kasper Timm Hansen"]
spec.email = ["rafaelmfranca@gmail.com", "kaspth@gmail.com"]
spec.summary = %q{ Dom and Selector assertions for Rails applications }
spec.description = %q{ This gem can compare doms and assert certain elements exists in doms using Nokogiri. }
spec.summary = "Dom and Selector assertions for Rails applications"
spec.description = "This gem can compare doms and assert certain elements exists in doms using Nokogiri."
spec.homepage = "https://github.com/rails/rails-dom-testing"
spec.license = "MIT"

Expand Down
10 changes: 5 additions & 5 deletions test/dom_assertions_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'test_helper'
require 'rails/dom/testing/assertions/dom_assertions'
require "test_helper"
require "rails/dom/testing/assertions/dom_assertions"

class DomAssertionsTest < ActiveSupport::TestCase
Assertion = Minitest::Assertion
Expand All @@ -11,7 +11,7 @@ def test_responds_to_assert_dom_equal
end

def test_dom_equal
html = '<a></a>'
html = "<a></a>"
assert_dom_equal(html, html.dup)
end

Expand All @@ -22,7 +22,7 @@ def test_equal_doms_with_different_order_attributes
end

def test_dom_not_equal
assert_dom_not_equal('<a></a>', '<b></b>')
assert_dom_not_equal("<a></a>", "<b></b>")
end

def test_unequal_doms_attributes_with_different_order_and_values
Expand All @@ -35,7 +35,7 @@ def test_custom_message_is_used_in_failures
message = "This is my message."

e = assert_raises(Assertion) do
assert_dom_equal('<a></a>', '<b></b>', message)
assert_dom_equal("<a></a>", "<b></b>", message)
end

assert_equal e.message, message
Expand Down
Loading

0 comments on commit 67512ed

Please sign in to comment.