-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathmarkup_test.rb
138 lines (115 loc) · 5.9 KB
/
markup_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# encoding: utf-8
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
require 'github/markup'
require 'minitest/autorun'
require 'html/pipeline'
require 'nokogiri'
require 'nokogiri/diff'
def normalize_html(text)
text.strip
.gsub(/\s\s+/,' ')
.gsub(/\p{Pi}|\p{Pf}|"/u,'"')
.gsub("\u2026",'...')
end
def assert_html_equal(expected, actual, msg = nil)
assertion = Proc.new do
expected_doc = Nokogiri::HTML(expected) {|config| config.noblanks}
actual_doc = Nokogiri::HTML(actual) {|config| config.noblanks}
expected_doc.search('//text()').each {|node| node.content = normalize_html node.content}
actual_doc.search('//text()').each {|node| node.content = normalize_html node.content}
ignore_changes = {"+" => Regexp.union(/^\s*id=".*"\s*$/), "-" => nil}
expected_doc.diff(actual_doc) do |change, node|
if change != ' ' && !node.blank? then
break unless node.to_html =~ ignore_changes[change]
end
end
end
assert(assertion.call, msg)
end
class MarkupTest < Minitest::Test
class MarkupFilter < HTML::Pipeline::Filter
def call
filename = context[:filename]
GitHub::Markup.render(filename, File.read(filename)).strip.force_encoding("utf-8")
end
end
Pipeline = HTML::Pipeline.new [
MarkupFilter,
HTML::Pipeline::SanitizationFilter
]
Dir['test/markups/README.*'].each do |readme|
next if readme =~ /html$/
markup = readme.split('/').last.gsub(/^README\./, '')
define_method "test_#{markup}" do
skip "Skipping MediaWiki test because wikicloth is currently not compatible with JRuby." if markup == "mediawiki" && RUBY_PLATFORM == "java"
source = File.read(readme)
expected_file = "#{readme}.html"
expected = File.read(expected_file).rstrip
actual = Pipeline.to_html(nil, :filename => readme)
if source != expected
assert(source != actual, "#{markup} did not render anything")
end
diff = IO.popen("diff -u - #{expected_file}", 'r+') do |f|
f.write actual
f.close_write
f.read
end
if ENV['UPDATE']
File.open(expected_file, 'w') { |f| f.write actual }
end
assert_html_equal expected, actual, <<message
#{File.basename expected_file}'s contents are not html equal to output:
#{diff}
message
end
end
def test_knows_what_it_can_and_cannot_render
assert_equal false, GitHub::Markup.can_render?('README.html', '<h1>Title</h1>')
assert_equal true, GitHub::Markup.can_render?('README.markdown', '=== Title')
assert_equal false, GitHub::Markup.can_render?('README.cmd', 'echo 1')
assert_equal true, GitHub::Markup.can_render?('README.litcoffee', 'Title')
end
def test_each_render_has_a_name
assert_equal "markdown", GitHub::Markup.renderer('README.md', '=== Title').name
assert_equal "redcloth", GitHub::Markup.renderer('README.textile', '* One').name
assert_equal "rdoc", GitHub::Markup.renderer('README.rdoc', '* One').name
assert_equal "org-ruby", GitHub::Markup.renderer('README.org', '* Title').name
assert_equal "creole", GitHub::Markup.renderer('README.creole', '= Title =').name
assert_equal "wikicloth", GitHub::Markup.renderer('README.wiki', '<h1>Title</h1>').name
assert_equal "asciidoctor", GitHub::Markup.renderer('README.adoc', '== Title').name
assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst', 'Title').name
assert_equal "pod", GitHub::Markup.renderer('README.pod', '=head1').name
assert_equal "pod6", GitHub::Markup.renderer('README.pod6', '=begin pod').name
end
def test_rendering_by_symbol
markup = '`test`'
result = /<p><code>test<\/code><\/p>/
assert_match result, GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, markup).strip
assert_match result, GitHub::Markup.render_s(GitHub::Markups::MARKUP_ASCIIDOC, markup).split.join
end
def test_raises_error_if_command_exits_non_zero
GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, ['Java'], 'fail')
assert GitHub::Markup.can_render?('README.java', 'stop swallowing errors')
begin
GitHub::Markup.render('README.java', "stop swallowing errors", symlink: false)
rescue GitHub::Markup::CommandError => e
assert_equal "failure message", e.message.strip
else
fail "an exception was expected but was not raised"
end
end
def test_preserve_markup
content = "Noël"
assert_equal content.encoding.name, GitHub::Markup.render('Foo.rst', content).encoding.name
end
def test_commonmarker_options
assert_equal "<p>hello <!-- raw HTML omitted --> world</p>\n", GitHub::Markup.render("test.md", "hello <bad> world")
assert_equal "<p>hello <bad> world</p>\n", GitHub::Markup.render("test.md", "hello <bad> world", options: {commonmarker_opts: [:UNSAFE]})
assert_equal "<p>hello <!-- raw HTML omitted --> world</p>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "hello <bad> world")
assert_equal "<p>hello <bad> world</p>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "hello <bad> world", options: {commonmarker_opts: [:UNSAFE]})
assert_equal "<style>.red{color: red;}</style>\n", GitHub::Markup.render("test.md", "<style>.red{color: red;}</style>", options: {commonmarker_opts: [:UNSAFE]})
assert_equal "<style>.red{color: red;}</style>\n", GitHub::Markup.render("test.md", "<style>.red{color: red;}</style>", options: {commonmarker_opts: [:UNSAFE], commonmarker_exts: [:autolink, :table, :strikethrough]})
assert_equal "<style>.red{color: red;}</style>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "<style>.red{color: red;}</style>", options: {commonmarker_opts: [:UNSAFE]})
assert_equal "<style>.red{color: red;}</style>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "<style>.red{color: red;}</style>", options: {commonmarker_opts: [:UNSAFE], commonmarker_exts: [:autolink, :table, :strikethrough]})
end
end