rails-dom-testing-2.3.0/0000755000004100000410000000000015024427055015112 5ustar www-datawww-datarails-dom-testing-2.3.0/rails-dom-testing.gemspec0000644000004100000410000000476115024427055022031 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: rails-dom-testing 2.3.0 ruby lib Gem::Specification.new do |s| s.name = "rails-dom-testing".freeze s.version = "2.3.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Rafael Mendon\u00E7a Fran\u00E7a".freeze, "Kasper Timm Hansen".freeze] s.date = "1980-01-02" s.description = "This gem can compare doms and assert certain elements exists in doms using Nokogiri.".freeze s.email = ["rafaelmfranca@gmail.com".freeze, "kaspth@gmail.com".freeze] s.files = ["MIT-LICENSE".freeze, "README.md".freeze, "lib/rails-dom-testing.rb".freeze, "lib/rails/dom/testing.rb".freeze, "lib/rails/dom/testing/assertions.rb".freeze, "lib/rails/dom/testing/assertions/dom_assertions.rb".freeze, "lib/rails/dom/testing/assertions/selector_assertions.rb".freeze, "lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb".freeze, "lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb".freeze, "lib/rails/dom/testing/railtie.rb".freeze, "lib/rails/dom/testing/version.rb".freeze, "test/dom_assertions_test.rb".freeze, "test/parser_selection_test.rb".freeze, "test/selector_assertions_test.rb".freeze, "test/test_helper.rb".freeze] s.homepage = "https://github.com/rails/rails-dom-testing".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.5.0".freeze) s.rubygems_version = "3.3.15".freeze s.summary = "Dom and Selector assertions for Rails applications".freeze s.test_files = ["test/dom_assertions_test.rb".freeze, "test/parser_selection_test.rb".freeze, "test/selector_assertions_test.rb".freeze, "test/test_helper.rb".freeze] if s.respond_to? :specification_version then s.specification_version = 4 end if s.respond_to? :add_runtime_dependency then s.add_runtime_dependency(%q.freeze, [">= 5.0.0"]) s.add_runtime_dependency(%q.freeze, [">= 0"]) s.add_runtime_dependency(%q.freeze, [">= 1.6"]) s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 5.0.0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 1.6"]) s.add_dependency(%q.freeze, [">= 0"]) end end rails-dom-testing-2.3.0/lib/0000755000004100000410000000000015024427055015660 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails-dom-testing.rb0000644000004100000410000000017315024427055021550 0ustar www-datawww-data# frozen_string_literal: true require "rails/dom/testing" require "rails/dom/testing/railtie" if defined?(Rails::Railtie) rails-dom-testing-2.3.0/lib/rails/0000755000004100000410000000000015024427055016772 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails/dom/0000755000004100000410000000000015024427055017551 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails/dom/testing/0000755000004100000410000000000015024427055021226 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails/dom/testing/assertions.rb0000644000004100000410000000044115024427055023744 0ustar www-datawww-data# frozen_string_literal: true require_relative "assertions/dom_assertions" require_relative "assertions/selector_assertions" module Rails module Dom module Testing module Assertions include DomAssertions include SelectorAssertions end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/0000755000004100000410000000000015024427055023420 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/dom_assertions.rb0000644000004100000410000001376315024427055027010 0ustar www-datawww-data# frozen_string_literal: true module Rails module Dom module Testing module Assertions module DomAssertions # \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order) # # # assert that the referenced method generates the appropriate HTML string # assert_dom_equal( # 'Apples', # link_to("Apples", "http://www.example.com"), # ) # # By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces # and newlines). If you want stricter matching with exact matching for whitespace, pass # strict: true: # # # these assertions will both pass # assert_dom_equal "
\nfoo\n\
", "
foo
", strict: false # assert_dom_not_equal "
\nfoo\n\
", "
foo
", strict: true # # The DOMs are created using an HTML parser specified by # Rails::Dom::Testing.default_html_version (either :html4 or :html5). # # When testing in a Rails application, the parser default can also be set by setting # +Rails.application.config.dom_testing_default_html_version+. # # If you want to specify the HTML parser just for a particular assertion, pass # html_version: :html4 or html_version: :html5 keyword arguments: # # assert_dom_equal expected, actual, html_version: :html5 # def assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil) expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version) message ||= "Expected: #{expected}\nActual: #{actual}" assert compare_doms(expected_dom, actual_dom, strict), message end # The negated form of +assert_dom_equal+. # # # assert that the referenced method does not generate the specified HTML string # assert_dom_not_equal( # 'Apples', # link_to("Oranges", "http://www.example.com"), # ) # # By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces # and newlines). If you want stricter matching with exact matching for whitespace, pass # strict: true: # # # these assertions will both pass # assert_dom_equal "
\nfoo\n\
", "
foo
", strict: false # assert_dom_not_equal "
\nfoo\n\
", "
foo
", strict: true # # The DOMs are created using an HTML parser specified by # Rails::Dom::Testing.default_html_version (either :html4 or :html5). # # When testing in a Rails application, the parser default can also be set by setting # +Rails.application.config.dom_testing_default_html_version+. # # If you want to specify the HTML parser just for a particular assertion, pass # html_version: :html4 or html_version: :html5 keyword arguments: # # assert_dom_not_equal expected, actual, html_version: :html5 # def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil) expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version) message ||= "Expected: #{expected}\nActual: #{actual}" assert_not compare_doms(expected_dom, actual_dom, strict), message end alias_method :refute_dom_equal, :assert_dom_not_equal protected def compare_doms(expected, actual, strict) expected_children = extract_children(expected, strict) actual_children = extract_children(actual, strict) return false unless expected_children.size == actual_children.size expected_children.each_with_index do |child, i| return false unless equal_children?(child, actual_children[i], strict) end true end def extract_children(node, strict) if strict node.children else node.children.reject { |n| n.text? && n.text.blank? } end end def equal_children?(child, other_child, strict) return false unless child.type == other_child.type if child.element? child.name == other_child.name && equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) && compare_doms(child, other_child, strict) else equal_child?(child, other_child, strict) end end def equal_child?(child, other_child, strict) if strict child.to_s == other_child.to_s else child.to_s.split == other_child.to_s.split end end def equal_attribute_nodes?(nodes, other_nodes) return false unless nodes.size == other_nodes.size nodes = nodes.sort_by(&:name) other_nodes = other_nodes.sort_by(&:name) nodes.each_with_index do |attr, i| return false unless equal_attribute?(attr, other_nodes[i]) end true end def equal_attribute?(attr, other_attr) attr.name == other_attr.name && attr.value == other_attr.value end private def fragment(text, html_version: nil) Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(text) end end end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/selector_assertions.rb0000644000004100000410000004027415024427055030046 0ustar www-datawww-data# frozen_string_literal: true require_relative "selector_assertions/html_selector" module Rails module Dom module Testing module Assertions # Adds the +assert_dom+ method for use in Rails functional # test cases, which can be used to make assertions on the response HTML of a controller # action. You can also call +assert_dom+ within another +assert_dom+ to # make assertions on elements selected by the enclosing assertion. # # Use +css_select+ to select elements without making an assertions, either # from the response HTML or elements selected by the enclosing assertion. # # In addition to HTML responses, you can make the following assertions: # # * +assert_dom_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions. # * +assert_dom_email+ - Assertions on the HTML body of an e-mail. module SelectorAssertions # Select and return all matching elements. # # If called with a single argument, uses that argument as a selector. # Called without an element +css_select+ selects from # the element returned in +document_root_element+ # # The default implementation of +document_root_element+ raises an exception explaining this. # # Returns an empty Nokogiri::XML::NodeSet if no match is found. # # If called with two arguments, uses the first argument as the root # element and the second argument as the selector. Attempts to match the # root element and any of its children. # Returns an empty Nokogiri::XML::NodeSet if no match is found. # # The selector may be a CSS selector expression (String). # css_select returns nil if called with an invalid css selector. # # # Selects all div tags # divs = css_select("div") # # # Selects all paragraph tags and does something interesting # pars = css_select("p") # pars.each do |par| # # Do something fun with paragraphs here... # end # # # Selects all list items in unordered lists # items = css_select("ul>li") # # # Selects all form tags and then all inputs inside the form # forms = css_select("form") # forms.each do |form| # inputs = css_select(form, "input") # ... # end def css_select(*args) raise ArgumentError, "you at least need a selector argument" if args.empty? root = args.size == 1 ? document_root_element : args.shift nodeset(root).css(args.first) end # An assertion that selects elements and makes one or more equality tests. # # If the first argument is an element, selects all matching elements # starting from (and including) that element and all its children in # depth-first order. # # If no element is specified +assert_dom+ selects from # the element returned in +document_root_element+ # unless +assert_dom+ is called from within an +assert_dom+ block. # Override +document_root_element+ to tell +assert_dom+ what to select from. # The default implementation raises an exception explaining this. # # When called with a block +assert_dom+ passes an array of selected elements # to the block. Calling +assert_dom+ from the block, with no element specified, # runs the assertion on the complete set of elements selected by the enclosing assertion. # Alternatively the array may be iterated through so that +assert_dom+ can be called # separately for each element. # # # ==== Example # If the response contains two ordered lists, each with four list elements then: # assert_dom "ol" do |elements| # elements.each do |element| # assert_dom element, "li", 4 # end # end # # will pass, as will: # assert_dom "ol" do # assert_dom "li", 8 # end # # The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression # with substitution values (Array). # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution. # assert_dom returns nil if called with an invalid css selector. # # assert_dom "div:match('id', ?)", "id_string" # assert_dom "div:match('id', ?)", :id_string # assert_dom "div:match('id', ?)", 1 # assert_dom "div:match('id', ?)", /\d+/ # # === Equality Tests # # The equality test may be one of the following: # * true - Assertion is true if at least one element selected. # * false - Assertion is true if no element selected. # * String/Regexp - Assertion is true if the text value of at least # one element matches the string or regular expression. # * Integer - Assertion is true if exactly that number of # elements are selected. # * Range - Assertion is true if the number of selected # elements fit the range. # If no equality test specified, the assertion is true if at least one # element selected. # # To perform more than one equality tests, use a hash with the following keys: # * :text - Narrow the selection to elements that have this text # value (string or regexp). # * :html - Narrow the selection to elements that have this HTML # content (string or regexp). # * :count - Assertion is true if the number of selected elements # is equal to this value. # * :minimum - Assertion is true if the number of selected # elements is at least this value. # * :maximum - Assertion is true if the number of selected # elements is at most this value. # # If the method is called with a block, once all equality tests are # evaluated the block is called with an array of all matched elements. # # # At least one form element # assert_dom "form" # # # Form element includes four input fields # assert_dom "form input", 4 # # # Page title is "Welcome" # assert_dom "title", "Welcome" # # # Page title is "Welcome" and there is only one title element # assert_dom "title", {count: 1, text: "Welcome"}, # "Wrong title or more than one title element" # # # Page contains no forms # assert_dom "form", false, "This page must contain no forms" # # # Test the content and style # assert_dom "body div.header ul.menu" # # # Use substitution values # assert_dom "ol>li:match('id', ?)", /item-\d+/ # # # All input fields in the form have a name # assert_dom "form input" do # assert_dom ":match('name', ?)", /.+/ # Not empty # end def assert_dom(*args, &block) @selected ||= nil selector = HTMLSelector.new(args, @selected) { nodeset document_root_element } dom_assertions(selector, &block) end alias_method :assert_select, :assert_dom # The negated form of +assert_dom+. # # === Equality Tests # # Supports the same equality tests as +assert_dom+ except for: # * true # * false # * Integer # * Range # * :count # * :minimum # * :maximum def assert_not_dom(*args, &block) @selected ||= nil selector = HTMLSelector.new(args, @selected, refute: true) { nodeset document_root_element } dom_assertions(selector, &block) end alias_method :refute_dom, :assert_not_dom alias_method :assert_not_select, :assert_not_dom alias_method :refute_select, :assert_not_dom private def dom_assertions(selector, &block) if selector.selecting_no_body? assert true return end count, max = selector.tests.slice(:count, :maximum).values selector.select.tap do |matches| assert_size_match!(matches.size, selector.tests, selector.css_selector, selector.message) if block_given? if count&.zero? || max&.zero? raise ArgumentError, "Cannot be called with a block when asserting that a selector does not match" end nest_selection(matches, &block) unless matches.empty? end end end # Extracts the content of an element, treats it as encoded HTML and runs # nested assertion on it. # # You typically call this method within another assertion to operate on # all currently selected elements. You can also pass an element or array # of elements. # # The content of each element is un-encoded, and wrapped in the root # element +encoded+. It then calls the block with all un-encoded elements. # # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix) # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do # # Select each entry item and then the title item # assert_dom "entry>title" do # # Run assertions on the encoded title elements # assert_dom_encoded do # assert_dom "b" # end # end # end # # # # Selects all paragraph tags from within the description of an RSS feed # assert_dom "rss[version=2.0]" do # # Select description element of each feed item. # assert_dom "channel>item>description" do # # Run assertions on the encoded elements. # assert_dom_encoded do # assert_dom "p" # end # end # end # # The DOM is created using an HTML parser specified by # Rails::Dom::Testing.default_html_version (either :html4 or :html5). # # When testing in a Rails application, the parser default can also be set by setting # +Rails.application.config.dom_testing_default_html_version+. # # If you want to specify the HTML parser just for a particular assertion, pass # html_version: :html4 or html_version: :html5 keyword arguments: # # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do # assert_dom "entry>title" do # assert_dom_encoded(html_version: :html5) do # assert_dom "b" # end # end # end # def assert_dom_encoded(element = nil, html_version: nil, &block) if !element && !@selected raise ArgumentError, "Element is required when called from a nonnested assert_dom" end content = nodeset(element || @selected).map do |elem| elem.children.select do |child| child.cdata? || (child.text? && !child.blank?) end.map(&:content) end.join selected = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(content) nest_selection(selected) do if content.empty? yield selected else assert_dom ":root", &block end end end alias_method :assert_select_encoded, :assert_dom_encoded # Extracts the body of an email and runs nested assertions on it. # # You must enable deliveries for this assertion to work, use: # ActionMailer::Base.perform_deliveries = true # # Example usage: # # assert_dom_email do # assert_dom "h1", "Email alert" # end # # assert_dom_email do # items = assert_dom "ol>li" # items.each do # # Work with items here... # end # end # # The DOM is created using an HTML parser specified by # Rails::Dom::Testing.default_html_version (either :html4 or :html5). # # When testing in a Rails application, the parser default can also be set by setting # +Rails.application.config.dom_testing_default_html_version+. # # If you want to specify the HTML parser just for a particular assertion, pass # html_version: :html4 or html_version: :html5 keyword arguments: # # assert_dom_email(html_version: :html5) do # assert_dom "h1", "Email alert" # end # def assert_dom_email(html_version: nil, &block) deliveries = ActionMailer::Base.deliveries assert !deliveries.empty?, "No e-mail in delivery list" deliveries.each do |delivery| (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part| if /^text\/html\W/.match?(part["Content-Type"].to_s) root = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(part.body.to_s) assert_dom root, ":root", &block end end end end alias_method :assert_select_email, :assert_dom_email private def document_root_element raise NotImplementedError, "Implementing document_root_element makes " \ "assert_dom work without needing to specify an element to select from." end # +equals+ must contain :minimum, :maximum and :count keys def assert_size_match!(size, equals, css_selector, message = nil) min, max, count = equals[:minimum], equals[:maximum], equals[:count] message ||= %(Expected #{count_description(min, max, count)} matching #{css_selector.inspect}, found #{size}) if count assert_equal count, size, message else assert_operator size, :>=, min, message if min assert_operator size, :<=, max, message if max end end def count_description(min, max, count) if min && max && (max != min) "between #{min} and #{max} elements" elsif min && max && max == min && count "exactly #{count} #{pluralize_element(min)}" elsif min && !(min == 1 && max == 1) "at least #{min} #{pluralize_element(min)}" elsif max "at most #{max} #{pluralize_element(max)}" end end def pluralize_element(quantity) quantity == 1 ? "element" : "elements" end def nest_selection(selection) # Set @selected to allow nested assert_dom. # Can be nested several levels deep. old_selected, @selected = @selected, selection yield @selected ensure @selected = old_selected end def nodeset(node) if node.is_a?(Nokogiri::XML::NodeSet) node else node ||= Nokogiri::HTML::Document.new Nokogiri::XML::NodeSet.new(node.document, [node]) end end end end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/selector_assertions/0000755000004100000410000000000015024427055027512 5ustar www-datawww-datarails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb0000644000004100000410000000254115024427055034361 0ustar www-datawww-data# frozen_string_literal: true module Rails module Dom module Testing module Assertions module SelectorAssertions class SubstitutionContext # :nodoc: def initialize @substitute = "?" end def substitute!(selector, values, format_for_presentation = false) selector.gsub @substitute do |match| next match[0] if values.empty? || !substitutable?(values.first) matcher_for(values.shift, format_for_presentation) end end def match(matches, attribute, matcher) matches.find_all { |node| node[attribute] =~ Regexp.new(matcher) } end private def matcher_for(value, format_for_presentation) # Nokogiri doesn't like arbitrary values without quotes, hence inspect. if format_for_presentation value.inspect # Avoid to_s so Regexps aren't put in quotes. elsif value.is_a?(Regexp) "\"#{value}\"" else value.to_s.inspect end end def substitutable?(value) [ Symbol, Numeric, String, Regexp ].any? { |type| value.is_a? type } end end end end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb0000644000004100000410000001336015024427055032706 0ustar www-datawww-data# frozen_string_literal: true require "minitest" require_relative "substitution_context" module Rails module Dom module Testing module Assertions module SelectorAssertions class HTMLSelector # :nodoc: attr_reader :css_selector, :tests, :message include Minitest::Assertions def initialize(values, previous_selection = nil, refute: false, &root_fallback) @values = values @root = extract_root(previous_selection, root_fallback) extract_selectors @tests = extract_equality_tests(refute) @message = @values.shift if @message.is_a?(Hash) raise ArgumentError, "Last argument was a Hash, which would be used for the assertion message. You probably want this to be a String, or you have the wrong type of arguments." end if @values.shift raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type" end end 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 end def select filter @root.css(@selector, context) end private NO_STRIP = %w{pre script style textarea} mattr_reader(:context) { SubstitutionContext.new } 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) html_matches = tests.has_key?(:html) 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.inner_html content.strip! unless NO_STRIP.include?(match.name) content.delete_prefix!("\n") if text_matches && match.name == "textarea" collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches next if regex_matching ? (content =~ match_with) : (content == match_with) content_mismatch ||= diff(match_with, content) true 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 end end def extract_selectors selector = @values.shift 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(refute) 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 if refute if comparisons[:count] || (comparisons[:minimum] && !comparator.nil?) || comparisons[:maximum] raise ArgumentError, "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match" end comparisons[:count] = 0 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 if comparisons[:minimum] && comparisons[:maximum] && comparisons[:minimum] > comparisons[:maximum] raise ArgumentError, "Range begin or :minimum cannot be greater than Range end or :maximum" end @strict = comparisons[:strict] comparisons end def collapse_html_whitespace!(text) text.gsub!(/\s+/, " ") end end end end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/version.rb0000644000004100000410000000016615024427055023243 0ustar www-datawww-data# frozen_string_literal: true module Rails module Dom module Testing VERSION = "2.3.0" end end end rails-dom-testing-2.3.0/lib/rails/dom/testing/railtie.rb0000644000004100000410000000054715024427055023212 0ustar www-datawww-data# frozen_string_literal: true module Rails module Dom module Testing class Railtie < Rails::Railtie # :nodoc: config.after_initialize do |app| version = app.config.try(:dom_testing_default_html_version) # Rails 7.1+ Rails::Dom::Testing.default_html_version = version if version end end end end end rails-dom-testing-2.3.0/lib/rails/dom/testing.rb0000644000004100000410000000311315024427055021551 0ustar www-datawww-data# frozen_string_literal: true require "nokogiri" require "active_support" require "active_support/core_ext/module/attribute_accessors" require "rails/dom/testing/assertions" module Rails module Dom module Testing mattr_accessor :default_html_version, default: :html4 class << self def html5_support? defined?(Nokogiri::HTML5) end def html_document(html_version: nil) parser_classes = { html4: Nokogiri::HTML4::Document } parser_classes[:html5] = Nokogiri::HTML5::Document if html5_support? choose_html_parser(parser_classes, html_version: html_version) end def html_document_fragment(html_version: nil) parser_classes = { html4: Nokogiri::HTML4::DocumentFragment } parser_classes[:html5] = Nokogiri::HTML5::DocumentFragment if html5_support? choose_html_parser(parser_classes, html_version: html_version) end private def choose_html_parser(parser_classes, html_version: nil) html_version ||= Rails::Dom::Testing.default_html_version case html_version when :html4 parser_classes[:html4] when :html5 unless Rails::Dom::Testing.html5_support? raise NotImplementedError, "html5 parser is not supported on this platform" end parser_classes[:html5] else raise ArgumentError, "html_version must be :html4 or :html5, received #{html_version.inspect}" end end end end end end rails-dom-testing-2.3.0/test/0000755000004100000410000000000015024427055016071 5ustar www-datawww-datarails-dom-testing-2.3.0/test/parser_selection_test.rb0000644000004100000410000000757015024427055023027 0ustar www-datawww-data# frozen_string_literal: true require "test_helper" class DomTestingParserSelectionTest < ActiveSupport::TestCase include DomTestingHelpers test "with default html4" do with_default_html_version(:html4) do assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document) assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment) assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4)) assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4)) if Rails::Dom::Testing.html5_support? assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5)) assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5)) else assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) } assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) } end assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) } assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) } end end test "with default html5" do with_default_html_version(:html5) do if Rails::Dom::Testing.html5_support? assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document) assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment) else assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document } assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment } end assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4)) assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4)) if Rails::Dom::Testing.html5_support? assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5)) assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5)) else assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) } assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) } end assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) } assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) } end end test "with invalid default" do with_default_html_version(:html8) do assert_raises(ArgumentError) { Rails::Dom::Testing.html_document } assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment } assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4)) assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4)) if Rails::Dom::Testing.html5_support? assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5)) assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5)) else assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) } assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) } end assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) } assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) } end end end rails-dom-testing-2.3.0/test/test_helper.rb0000644000004100000410000000077115024427055020741 0ustar www-datawww-data# frozen_string_literal: true require "rails-dom-testing" require "active_support/test_case" require "minitest/autorun" ActiveSupport::TestCase.test_order = :random module DomTestingHelpers def jruby? !! Nokogiri.jruby? end def with_default_html_version(version) old_version = Rails::Dom::Testing.default_html_version begin Rails::Dom::Testing.default_html_version = version yield ensure Rails::Dom::Testing.default_html_version = old_version end end end rails-dom-testing-2.3.0/test/selector_assertions_test.rb0000644000004100000410000004442415024427055023557 0ustar www-datawww-data# encoding: utf-8 # frozen_string_literal: true require "test_helper" class AssertSelectTest < ActiveSupport::TestCase Assertion = Minitest::Assertion include DomTestingHelpers include Rails::Dom::Testing::Assertions::SelectorAssertions def assert_failure(message, &block) e = assert_raises(Assertion, &block) assert_match(message, e.message) if Regexp === message assert_equal(message, e.message) if String === message end # # Test assert_select. # def test_assert_select render_html '
' assert_select "div", 2 assert_failure(/Expected at least 1 element matching "p", found 0/) { assert_select "p" } end def test_equality_integer render_html '
' assert_failure(/Expected exactly 3 elements matching "div", found 2/) { assert_select "div", 3 } assert_failure(/Expected exactly 0 elements matching "div", found 2/) { assert_select "div", 0 } end def test_equality_true_false render_html '
' assert_nothing_raised { assert_select "div" } assert_raise(Assertion) { assert_select "p" } assert_nothing_raised { assert_select "div", true } assert_raise(Assertion) { assert_select "p", true } assert_raise(Assertion) { assert_select "div", false } assert_nothing_raised { assert_select "p", false } end def test_equality_false_with_substitution render_html %{} assert_nothing_raised do assert_select %{a[href="http://example.org?query=value"]}, false end end def test_equality_false_message render_html '
' assert_failure(/Expected exactly 0 elements matching "div", found 2/) { assert_select "div", false } end def test_equality_string_and_regexp render_html '
foo
foo
' assert_nothing_raised { assert_select "div", "foo" } assert_raise(Assertion) { assert_select "div", "bar" } assert_nothing_raised { assert_select "div", text: "foo" } assert_raise(Assertion) { assert_select "div", text: "bar" } assert_nothing_raised { assert_select "div", /(foo|bar)/ } assert_raise(Assertion) { assert_select "div", /foobar/ } assert_nothing_raised { assert_select "div", text: /(foo|bar)/ } assert_raise(Assertion) { assert_select "div", text: /foobar/ } assert_raise(Assertion) { assert_select "p", text: /foobar/ } end def test_equality_of_html render_html %Q{

\n"This is not a big problem," he said.\n

} text = "\"This is not a big problem,\" he said." html = "\"This is not a big problem,\" he said." assert_nothing_raised { assert_select "p", text } assert_raise(Assertion) { assert_select "p", html } assert_nothing_raised { assert_select "p", html: html } assert_raise(Assertion) { assert_select "p", html: text } # No stripping for pre. render_html %Q{
\n"This is not a big problem," he said.\n
} text = "\n\"This is not a big problem,\" he said.\n" html = "\n\"This is not a big problem,\" he said.\n" assert_nothing_raised { assert_select "pre", text } assert_raise(Assertion) { assert_select "pre", html } assert_nothing_raised { assert_select "pre", html: html } assert_raise(Assertion) { assert_select "pre", html: text } end def test_strip_textarea render_html "" assert_select "textarea", "\nfoo\n" render_html "" assert_select "textarea", "foo" end def test_counts render_html '
foo
foo
' assert_nothing_raised { assert_select "div", 2 } assert_failure(/Expected exactly 3 elements matching "div", found 2/) do assert_select "div", 3 end assert_nothing_raised { assert_select "div", 1..2 } assert_failure(/Expected between 3 and 4 elements matching "div", found 2/) do assert_select "div", 3..4 end assert_nothing_raised { assert_select "div", count: 2 } assert_failure(/Expected exactly 3 elements matching "div", found 2/) do assert_select "div", count: 3 end assert_nothing_raised { assert_select "div", minimum: 1 } assert_nothing_raised { assert_select "div", minimum: 2 } assert_failure(/Expected at least 3 elements matching "div", found 2/) do assert_select "div", minimum: 3 end assert_nothing_raised { assert_select "div", maximum: 2 } assert_nothing_raised { assert_select "div", maximum: 3 } assert_failure(/Expected at most 1 element matching "div", found 2/) do assert_select "div", maximum: 1 end assert_nothing_raised { assert_select "div", minimum: 1, maximum: 2 } assert_failure(/Expected between 3 and 4 elements matching "div", found 2/) do assert_select "div", minimum: 3, maximum: 4 end end def test_zero_counts_with_block render_html "
foo
" errors = [ assert_raises(ArgumentError) { assert_select("p", false) { nil } }, assert_raises(ArgumentError) { assert_select("p", 0) { nil } }, assert_raises(ArgumentError) { assert_select("p", count: 0) { nil } }, assert_raises(ArgumentError) { assert_select("p", 0..0) { nil } }, assert_raises(ArgumentError) { assert_select("p", minimum: 0, maximum: 0) { nil } } ] assert_equal ["Cannot be called with a block when asserting that a selector does not match"], errors.map(&:message).uniq end def test_substitution_values render_html '
foo
foo
' assert_select "div:match('id', ?)", /\d+/ do |elements| assert_equal 2, elements.size end assert_select "div" do assert_select ":match('id', ?)", /\d+/ do |elements| assert_equal 2, elements.size assert_select "#1" assert_select "#2" end end end def test_substitution_value_with_quotes render_html '' assert_select "input[placeholder=?]", 'placeholder with "quotes"' end def test_multiple_substitution_values render_html '' assert_select ":match('name', ?):match('value', ?)", /\w+\[\d+\]/, /\d+/ end def test_substitution_values_with_values_other_than_string_or_regexp render_html '
symbol
numeric
' assert_select "div:match('id', ?)", :id_string do |elements| assert_equal 1, elements.size end assert_select "div:match('id', ?)", 1 do |elements| assert_equal 1, elements.size end end def test_assert_select_root_html render_html "" assert_select "a" end def test_assert_select_root_xml render_xml '' assert_select "rss" end def test_nested_assert_select render_html '
foo
foo
' assert_select "div" do |elements| assert_equal 2, elements.size assert_select elements, "#1" assert_select elements, "#2" end assert_select "div" do assert_select "div" do |elements| assert_equal 2, elements.size # Testing in a group is one thing assert_select "#1,#2" # Testing individually is another. assert_select "#1" assert_select "#2" assert_select "#3", false end end assert_failure(/Expected at least 1 element matching "#4", found 0\./) do assert_select "div" do assert_select "#4" end end end def test_assert_select_text_match render_html '
foo
bar
' assert_select "div" do assert_nothing_raised { assert_select "div", "foo" } assert_nothing_raised { assert_select "div", "bar" } assert_nothing_raised { assert_select "div", /\w*/ } assert_nothing_raised { assert_select "div", text: /\w*/, count: 2 } assert_raise(Assertion) { assert_select "div", text: "foo", count: 2 } assert_nothing_raised { assert_select "div", html: "bar" } assert_nothing_raised { assert_select "div", html: "bar" } assert_nothing_raised { assert_select "div", html: /\w*/ } assert_nothing_raised { assert_select "div", html: /\w*/, count: 2 } assert_raise(Assertion) { assert_select "div", html: "foo", count: 2 } end end def test_assert_select_with_invalid_range render_html "
foo
" error = assert_raises(ArgumentError) { assert_select("div", 2..1) { nil } } assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message end def test_assert_select_with_invalid_minimum_and_maximum render_html "
foo
" error = assert_raises(ArgumentError) { assert_select("div", maximum: 0) { nil } } assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message error = assert_raises(ArgumentError) { assert_select("div", minimum: 2, maximum: 1) { nil } } assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message end def test_assert_select_text_equality_collapses_whitespace render_html "

Some\n line-broken\n text

" assert_nothing_raised do assert_select "p", { text: "Some line-broken text", }, "Whitespace was not collapsed from text" end render_html "

Some

line-broken

text

" assert_nothing_raised do assert_select "p", { text: "Someline-brokentext", }, "
was not removed from text" end end def test_assert_select_html_equality_respects_whitespace render_html "

Some\n line-broken\n text

" assert_nothing_raised do assert_select "p", { html: "Some\n line-broken\n text", }, "Whitespace was collapsed from html" end render_html "

Some

line-broken

text

" assert_nothing_raised do assert_select "p", { html: "Some

line-broken

text", }, "
was removed from html" end end # # Test assert_not_select. # def test_assert_not_select render_html '
' assert_not_select "p" assert_failure(/Expected exactly 0 elements matching "div", found 1/) { assert_not_select "div" } assert_failure(/Expected exactly 0 elements matching "div#1", found 1/) { assert_not_select "div#1" } end def test_assert_not_select_with_true render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", true } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_false render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", false } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_integer render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", 1 } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_range render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", 1..5 } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_count render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", count: 1 } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_minimum render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", minimum: 1 } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_maximum render_html '
' error = assert_raises(ArgumentError) { assert_not_select "div", maximum: 1 } assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message end def test_assert_not_select_with_block render_html "
foo
" error = assert_raises(ArgumentError) { assert_not_select("p") { nil } } assert_equal "Cannot be called with a block when asserting that a selector does not match", error.message end # # Test css_select. # def test_css_select render_html '
' assert_equal 2, css_select("div").size assert_equal 0, css_select("p").size end def test_nested_css_select render_html '
foo
foo
' assert_select "div:match('id', ?)", /\d+/ do |elements| assert_equal 1, css_select(elements[0], "div").size assert_equal 1, css_select(elements[1], "div").size end assert_select "div" do assert_equal 2, css_select("div").size css_select("div").each do |element| # Testing as a group is one thing assert_not css_select("#1,#2").empty? # Testing individually is another assert_not css_select("#1").empty? assert_not css_select("#2").empty? end end end # testing invalid selectors def test_assert_select_with_invalid_selector render_html 'hello' assert_raises Nokogiri::CSS::SyntaxError do assert_select("[href=http://example.com]") end end def test_css_select_with_invalid_selector render_html 'hello' assert_raises Nokogiri::CSS::SyntaxError do css_select("[href=http://example.com]") end end def test_nested_assert_select_with_match_failure_shows_nice_regex render_html '
foo
' error = assert_raises Minitest::Assertion do assert_select "div:match('id', ?)", /wups/ end assert_match "div:match('id', /wups/)", error.message end def test_feed_item_encoded render_xml <<-EOF Test 1

]]>
<p>Test 2</p>
EOF assert_select "channel item description" do assert_select_encoded do assert_select "p", count: 2, text: /Test/ end # Test individually. assert_select "description" do |elements| assert_select_encoded elements[0] do assert_select "p", "Test 1" end assert_select_encoded elements[1] do assert_select "p", "Test 2" end end end # Test that we only un-encode element itself. assert_select "channel item" do assert_select_encoded do assert_select "p", 0 end end end def test_feed_item_encoded_with_html_version # https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser # we use these results to assert that we're invoking the expected parser. input = CGI.escapeHTML("

12345

") html4_result = jruby? ? "

12345

" : "

12345

" html5_result = jruby? ? nil : "

12345

" render_xml(<<~XML) #{input} XML with_default_html_version(:html4) do assert_select "root contents" do assert_select_encoded do |contents| assert_equal(html4_result, contents.to_html) end assert_select_encoded(html_version: :html4) do |contents| assert_equal(html4_result, contents.to_html) end assert_select_encoded(html_version: :html5) do |contents| assert_equal(html5_result, contents.to_html) end if Rails::Dom::Testing.html5_support? end end with_default_html_version(:html5) do assert_select "root contents" do assert_select_encoded do |contents| assert_equal(html5_result, contents.to_html) end if Rails::Dom::Testing.html5_support? assert_select_encoded(html_version: :html4) do |contents| assert_equal(html4_result, contents.to_html) end assert_select_encoded(html_version: :html5) do |contents| assert_equal(html5_result, contents.to_html) end if Rails::Dom::Testing.html5_support? end end end def test_body_not_present_in_empty_document render_html "
" assert_select "body", 0 end def test_body_class_can_be_tested render_html '' assert_select ".foo" end def test_body_class_can_be_tested_with_html render_html '
' assert_select ".foo" end def test_assert_select_with_extra_argument render_html "Welcome
" assert_raises ArgumentError do assert_select "title", "Welcome", count: 1 end assert_select "title", text: "Welcome", count: 1 end def test_assert_select_on_blank_response render_html "" assert_select "div", 0 assert_failure(/Expected exactly 1 element matching "div", found 0./) do assert_select "div", 1 end end protected def render_html(html) fake_render(:html, html) end def render_xml(xml) fake_render(:xml, xml) end def fake_render(content_type, content) @html_document = if content_type == :xml Nokogiri::XML::Document.parse(content) else Rails::Dom::Testing.html_document.parse(content) end end def document_root_element @html_document.root end end rails-dom-testing-2.3.0/test/dom_assertions_test.rb0000644000004100000410000001445515024427055022517 0ustar www-datawww-data# frozen_string_literal: true require "test_helper" class DomAssertionsTest < ActiveSupport::TestCase Assertion = Minitest::Assertion include Rails::Dom::Testing::Assertions::DomAssertions def test_responds_to_assert_dom_equal assert respond_to?(:assert_dom_equal) end def test_dom_equal html = "" assert_dom_equal(html, html.dup) end def test_equal_doms_with_different_order_attributes attributes = %{} reverse_attributes = %{} assert_dom_equal(attributes, reverse_attributes) end def test_dom_not_equal assert_dom_not_equal("", "") end def test_unequal_doms_attributes_with_different_order_and_values attributes = %{} reverse_attributes = %{} assert_dom_not_equal(attributes, reverse_attributes) end def test_custom_message_is_used_in_failures message = "This is my message." e = assert_raises(Assertion) do assert_dom_equal("", "", message) end assert_equal e.message, message end def test_unequal_dom_attributes_in_children assert_dom_not_equal( %{}, %{} ) end def test_dom_equal_with_whitespace_strict canonical = %{hello world} assert_dom_not_equal(canonical, %{\nhello\n world}, strict: true) assert_dom_not_equal(canonical, %{ \n \n hello world}, strict: true) assert_dom_not_equal(canonical, %{\n\thello world}, strict: true) assert_dom_equal(canonical, %{hello world}, strict: true) end def test_dom_equal_with_whitespace canonical = %{hello world} assert_dom_equal(canonical, %{\nhello\n world}) assert_dom_equal(canonical, %{\nhello \nworld}) assert_dom_equal(canonical, %{ \n \n hello world}) assert_dom_equal(canonical, %{ \n hello world}) assert_dom_equal(canonical, %{ \n hello world\n\n}) assert_dom_equal(canonical, %{\n\thello world}) assert_dom_equal(canonical, %{\n\thello \n\tworld}) end def test_dom_equal_with_attribute_whitespace canonical = %(
) assert_dom_equal(canonical, %(
)) assert_dom_not_equal(canonical, %(
)) end def test_dom_equal_with_indentation canonical = %{hello cruel world} assert_dom_equal(canonical, <<-HTML) hello cruel world HTML assert_dom_equal(canonical, <<-HTML) hello cruel world HTML assert_dom_equal(canonical, <<-HTML) hello cruel world HTML end def test_dom_equal_with_surrounding_whitespace canonical = %{

Lorem ipsum dolor

sit amet, consectetur adipiscing elit

} assert_dom_equal(canonical, <<-HTML)

Lorem ipsum dolor

sit amet, consectetur adipiscing elit

HTML end def test_dom_not_equal_with_interior_whitespace with_space = %{hello world} without_space = %{helloworld} assert_dom_not_equal(with_space, without_space) end end class DomAssertionsHtmlParserSelectionTest < ActiveSupport::TestCase include DomTestingHelpers include Rails::Dom::Testing::Assertions::DomAssertions def setup super # https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser # we use these results to assert that we're invoking the expected parser. @input = "

12345

" @html4_result = jruby? ? "

12345

" : "

12345

" @html5_result = jruby? ? nil : "

12345

" end test "default value is html4" do assert_equal(:html4, Rails::Dom::Testing.default_html_version) end test "default html4, no version specified" do with_default_html_version(:html4) do assert_dom_equal(@html4_result, @input) assert_dom_not_equal(@html5_result, @input) end end test "default html4, html4 specified" do with_default_html_version(:html4) do assert_dom_equal(@html4_result, @input, html_version: :html4) assert_dom_not_equal(@html5_result, @input, html_version: :html4) end end test "default html4, html5 specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html4) do assert_dom_equal(@html5_result, @input, html_version: :html5) assert_dom_not_equal(@html4_result, @input, html_version: :html5) end end test "default html5, no version specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html5) do assert_dom_equal(@html5_result, @input) assert_dom_not_equal(@html4_result, @input) end end test "default html5, html4 specified" do with_default_html_version(:html5) do assert_dom_equal(@html4_result, @input, html_version: :html4) assert_dom_not_equal(@html5_result, @input, html_version: :html4) end end test "default html5, html5 specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html5) do assert_dom_equal(@html5_result, @input, html_version: :html5) assert_dom_not_equal(@html4_result, @input, html_version: :html5) end end test "raise NotImplementedError html5 when not supported" do Rails::Dom::Testing.stub(:html5_support?, false) do with_default_html_version(:html5) do assert_raises(NotImplementedError) { assert_dom_equal("a", "b") } assert_raises(NotImplementedError) { assert_dom_equal("a", "b", html_version: :html5) } assert_nothing_raised { assert_dom_equal(@html4_result, @input, html_version: :html4) } end end end test "default set to invalid" do with_default_html_version(:html9) do assert_raises(ArgumentError) { assert_dom_equal("a", "b") } end end test "invalid version specified" do assert_raises(ArgumentError) { assert_dom_equal("a", "b", html_version: :html9) } end end rails-dom-testing-2.3.0/MIT-LICENSE0000644000004100000410000000207115024427055016546 0ustar www-datawww-dataCopyright (c) 2013-2015 Kasper Timm Hansen MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rails-dom-testing-2.3.0/README.md0000644000004100000410000000614715024427055016401 0ustar www-datawww-data# Rails::Dom::Testing This gem is responsible for comparing HTML doms and asserting that DOM elements are present in Rails applications. Doms are compared via `assert_dom_equal` and `assert_dom_not_equal`. Elements are asserted via `assert_dom`, `assert_dom_encoded`, `assert_dom_email` and a subset of the dom can be selected with `css_select`. The gem is developed for Rails 4.2 and above, and will not work on previous versions. ## Usage ### Dom Assertions ```ruby assert_dom_equal '

Lingua França

', '

Lingua França

' assert_dom_not_equal '

Portuguese

', '

Danish

' ``` ### Selector Assertions ```ruby # implicitly selects from the document_root_element css_select '.hello' # => Nokogiri::XML::NodeSet of elements with hello class # select from a supplied node. assert_dom asserts elements exist. assert_dom document_root_element.at('.hello'), '.goodbye' # select from a supplied node. assert_not_dom asserts elements do not exist. assert_not_dom document_root_element.at('.hello'), '.goodbye' # elements in CDATA encoded sections can also be selected assert_dom_encoded '#out-of-your-element' # assert elements within an html email exists assert_dom_email '#you-got-mail' ``` The documentation in [selector_assertions.rb](https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb) goes into a lot more detail of how selector assertions can be used. ### HTML versions By default, assertions will use Nokogiri's HTML4 parser. If `Rails::Dom::Testing.default_html_version` is set to `:html5`, then the assertions will use Nokogiri's HTML5 parser. (If the HTML5 parser is not available on your platform, then a `NotImplementedError` will be raised.) When testing in a Rails application, the parser default can also be set by setting `Rails.application.config.dom_testing_default_html_version`. Some assertions support an `html_version:` keyword argument which can override the default for that assertion. For example: ``` ruby # compare DOMs built with the HTML5 parser assert_dom_equal(expected, actual, html_version: :html5) # compare DOMs built with the HTML4 parser assert_dom_not_equal(expected, actual, html_version: :html4) ``` Please see documentation for individual assertions for more details. ## Installation Add this line to your application's Gemfile: gem 'rails-dom-testing' And then execute: $ bundle Or install it yourself as: $ gem install rails-dom-testing ## Read more Under the hood the doms are parsed with Nokogiri, and you'll generally be working with these two classes: - [`Nokogiri::XML::Node`](http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node) - [`Nokogiri::XML::NodeSet`](http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/NodeSet) Read more about Nokogiri: - [Nokogiri](http://nokogiri.org) ## Contributing to Rails::Dom::Testing Rails::Dom::Testing is work of many contributors. You're encouraged to submit pull requests, propose features and discuss issues. See [CONTRIBUTING](CONTRIBUTING.md). ## License Rails::Dom::Testing is released under the [MIT License](MIT-LICENSE).