# frozen_string_literal: true
require 'test_helper'
class TestExtensions < Minitest::Test
def setup
@markdown = fixtures_file('table.md')
end
def test_uses_specified_extensions
QiitaMarker.render_html(@markdown, :DEFAULT, %i[]).tap do |out|
assert_includes out, '| a'
assert_includes out, '| x'
assert_includes out, '~~hi~~'
end
QiitaMarker.render_html(@markdown, :DEFAULT, %i[table]).tap do |out|
refute_includes out, '| a'
%w[
a | c | x].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end
QiitaMarker.render_html(@markdown, :DEFAULT, %i[strikethrough]).tap do |out|
assert_includes out, '| a'
refute_includes out, '~~hi~~'
assert_includes out, 'hi'
end
doc = QiitaMarker.render_doc('~a~ ~~b~~ ~~~c~~~', :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
assert_equal("~a~ b ~~~c~~~
\n", doc.to_html)
html = QiitaMarker.render_html('~a~ ~~b~~ ~~~c~~~', :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
assert_equal("~a~ b ~~~c~~~
\n", html)
QiitaMarker.render_html(@markdown, :DEFAULT, %i[table strikethrough]).tap do |out|
refute_includes out, '| a'
refute_includes out, '| x'
refute_includes out, '~~hi~~'
end
end
def test_extensions_with_renderers
doc = QiitaMarker.render_doc(@markdown, :DEFAULT, %i[table])
doc.to_html.tap do |out|
refute_includes out, '| a'
%w[ a | c | x].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end
HtmlRenderer.new.render(doc).tap do |out|
refute_includes out, '| a'
%w[ a | c | x].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end
doc = QiitaMarker.render_doc('~a~ ~~b~~ ~~~c~~~', :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
assert_equal("~a~ b ~~~c~~~
\n", HtmlRenderer.new.render(doc))
end
def test_bad_extension_specifications
assert_raises(TypeError) { QiitaMarker.render_html(@markdown, :DEFAULT, 'nope') }
assert_raises(TypeError) { QiitaMarker.render_html(@markdown, :DEFAULT, ['table']) }
assert_raises(ArgumentError) { QiitaMarker.render_html(@markdown, :DEFAULT, %i[table bad]) }
end
def test_comments_are_kept_as_expected
assert_equal " <xmp>\n",
QiitaMarker.render_html(" \n", :UNSAFE, %i[tagfilter])
end
def test_table_prefer_style_attributes
assert_equal(<<~HTML, QiitaMarker.render_html(<<~MD, :TABLE_PREFER_STYLE_ATTRIBUTES, %i[table]))
aaa |
bbb |
ccc |
ddd |
eee |
fff |
ggg |
hhh |
iii |
jjj |
HTML
aaa | bbb | ccc | ddd | eee
:-- | --- | :-: | --- | --:
fff | ggg | hhh | iii | jjj
MD
end
def test_plaintext
assert_equal(<<~HTML, QiitaMarker.render_doc(<<~MD, :DEFAULT, %i[table strikethrough]).to_plaintext)
Hello ~there~.
| a |
| --- |
| b |
HTML
Hello ~~there~~.
| a |
| - |
| b |
MD
end
end