Sha256: fdc6b33cc7bed272cbd0d65cd5762f125ea807dbfe09ed9992947a61be47e850

Contents?: true

Size: 1.87 KB

Versions: 17

Compression:

Stored size: 1.87 KB

Contents

require 'test_helper'

class TestNode < Minitest::Test
  def setup
    @doc = CommonMarker.render_doc('Hi *there*, I am mostly text!')
  end

  def test_walk
    nodes = []
    @doc.walk do |node|
      nodes << node.type
    end
    assert_equal [:document, :paragraph, :text, :emph, :text, :text, :text], nodes
  end

  def test_each
    nodes = []
    @doc.first_child.each do |node|
      nodes << node.type
    end
    assert_equal [:text, :emph, :text, :text], nodes
  end

  def test_deprecated_each_child
    nodes = []
    @doc.first_child.each_child do |node|
      nodes << node.type
    end
    assert_equal [:text, :emph, :text, :text], nodes
  end

  def test_select
    nodes = @doc.first_child.select { |node| node.type == :text }
    assert_equal CommonMarker::Node, nodes.first.class
    assert_equal [:text, :text, :text], nodes.map(&:type)
  end

  def test_map
    nodes = @doc.first_child.map(&:type)
    assert_equal [:text, :emph, :text, :text], nodes
  end

  def test_insert_illegal
    assert_raises NodeError do
      @doc.insert_before(@doc)
    end
  end

  def test_to_html
    assert_equal "<p>Hi <em>there</em>, I am mostly text!</p>\n", @doc.to_html
  end

  def test_html_renderer
    renderer = HtmlRenderer.new
    result = renderer.render(@doc)
    assert_equal "<p>Hi <em>there</em>, I am mostly text!</p>\n", result
  end

  def test_walk_and_set_string_content
    @doc.walk do |node|
      if node.type == :text && node.string_content == 'there'
        node.string_content = 'world'
      end
    end
    result = HtmlRenderer.new.render(@doc)
    assert_equal "<p>Hi <em>world</em>, I am mostly text!</p>\n", result
  end

  def test_walk_and_delete_node
    @doc.walk do |node|
      if node.type == :emph
        node.insert_before(node.first_child)
        node.delete
      end
    end
    assert_equal "<p>Hi there, I am mostly text!</p>\n", @doc.to_html
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
commonmarker-0.14.9 test/test_node.rb
commonmarker-0.14.8 test/test_node.rb
commonmarker-0.14.7 test/test_node.rb
commonmarker-0.14.6 test/test_node.rb
commonmarker-0.14.5 test/test_node.rb
commonmarker-0.14.4 test/test_node.rb
commonmarker-0.14.3 test/test_node.rb
commonmarker-0.14.2 test/test_node.rb
commonmarker-0.14.1 test/test_node.rb
commonmarker-0.14.0 test/test_node.rb
commonmarker-0.13.0 test/test_node.rb
commonmarker-0.12.0 test/test_node.rb
commonmarker-0.11.0 test/test_node.rb
commonmarker-0.10.0 test/test_node.rb
commonmarker-0.9.2 test/test_node.rb
commonmarker-0.9.1 test/test_node.rb
commonmarker-0.9.0 test/test_node.rb