forked from github/markup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkup_test.rb
116 lines (98 loc) · 3.93 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
# 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!
text.gsub!(/\s\s+/,' ')
text.gsub!(/\p{Pi}|\p{Pf}|"/u,'"')
text.gsub!("\u2026",'...')
text
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
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')
assert_equal true, GitHub::Markup.can_render?('README.markdown')
assert_equal true, GitHub::Markup.can_render?('README.rmd')
assert_equal true, GitHub::Markup.can_render?('README.Rmd')
assert_equal false, GitHub::Markup.can_render?('README.cmd')
assert_equal true, GitHub::Markup.can_render?('README.litcoffee')
end
def test_each_render_has_a_name
assert_equal "markdown", GitHub::Markup.renderer('README.md').name
assert_equal "redcloth", GitHub::Markup.renderer('README.textile').name
assert_equal "rdoc", GitHub::Markup.renderer('README.rdoc').name
assert_equal "org-ruby", GitHub::Markup.renderer('README.org').name
assert_equal "creole", GitHub::Markup.renderer('README.creole').name
assert_equal "wikicloth", GitHub::Markup.renderer('README.wiki').name
assert_equal "asciidoctor", GitHub::Markup.renderer('README.adoc').name
assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst').name
assert_equal "pod", GitHub::Markup.renderer('README.pod').name
end
def test_raises_error_if_command_exits_non_zero
GitHub::Markup.command('test/fixtures/fail.sh', /fail/, 'fail')
assert GitHub::Markup.can_render?('README.fail')
begin
GitHub::Markup.render('README.fail', "stop swallowing errors")
rescue GitHub::Markup::CommandError => e
assert_equal "failure message", e.message
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
end