-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcoverage_test.rb
More file actions
385 lines (329 loc) · 14.2 KB
/
Copy pathcoverage_test.rb
File metadata and controls
385 lines (329 loc) · 14.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# encoding: utf-8
# Exercises code paths the original markup_test.rb does not reach:
# - The six fallback markdown gem procs (github/markdown, redcarpet,
# rdiscount, maruku, kramdown, bluecloth) executed against stubbed constants
# - The LoadError ("no suitable markdown gem found") raised when no gem is available
# - try_require's rescue clause
# - Implementation#render's NotImplementedError default
# - Implementation#match? without Linguist (and the lazy regexp memoization)
# - GitHub::Markup.markup_impl's duplicate-symbol guard
# - GitHub::Markup.render falling through to the raw content
# - GitHub::Markup.render_s with an unknown symbol and with nil content
# - GitHub::Markup.preload!
# - GitHub::Markup.language returning nil without Linguist
# - CommandImplementation block-arity branches (arity 1 vs 2)
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
require_relative 'test_helper'
require 'github-markup'
require 'github/markup'
require 'minitest/autorun'
class CoverageTest < Minitest::Test
def test_version_constant_is_defined
assert_kind_of String, GitHub::Markup::VERSION
assert_equal GitHub::Markup::VERSION, GitHub::Markup::Version
end
# --- markdown.rb fallback procs ----------------------------------------
def test_github_markdown_proc_uses_github_markdown_render
with_stub_const("GitHub::Markdown", fake_renderer_module(:render)) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("github/markdown").call("hi")
assert_equal "github_markdown:hi", result
end
end
def test_redcarpet_proc_renders_via_redcarpet
fake_html = Class.new
fake_md = Class.new do
def initialize(*); end
def render(content); "redcarpet:#{content}"; end
end
fake_module = Module.new
fake_module.const_set(:Render, Module.new.tap { |m| m.const_set(:HTML, fake_html) })
fake_module.const_set(:Markdown, fake_md)
with_stub_const("Redcarpet", fake_module) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("redcarpet").call("hi")
assert_equal "redcarpet:hi", result
end
end
def test_rdiscount_proc_renders_via_rdiscount
with_stub_const("RDiscount", instance_renderer_class(:to_html, prefix: "rdiscount")) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("rdiscount").call("hi")
assert_equal "rdiscount:hi", result
end
end
def test_maruku_proc_renders_via_maruku
with_stub_const("Maruku", instance_renderer_class(:to_html, prefix: "maruku")) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("maruku").call("hi")
assert_equal "maruku:hi", result
end
end
def test_kramdown_proc_renders_via_kramdown_document
fake_doc = instance_renderer_class(:to_html, prefix: "kramdown")
fake_module = Module.new.tap { |m| m.const_set(:Document, fake_doc) }
with_stub_const("Kramdown", fake_module) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("kramdown").call("hi")
assert_equal "kramdown:hi", result
end
end
def test_bluecloth_proc_renders_via_bluecloth
with_stub_const("BlueCloth", instance_renderer_class(:to_html, prefix: "bluecloth")) do
result = GitHub::Markup::Markdown::MARKDOWN_GEMS.fetch("bluecloth").call("hi")
assert_equal "bluecloth:hi", result
end
end
# --- markdown.rb load failure and try_require rescue --------------------
def test_markdown_load_raises_loaderror_when_no_gem_is_available
md = GitHub::Markup::Markdown.new
def md.try_require(_); false; end
assert_raises(LoadError) { md.load }
end
def test_try_require_returns_false_for_missing_gem
md = GitHub::Markup::Markdown.new
refute md.send(:try_require, "github_markup_definitely_not_a_real_gem_#{Time.now.to_i}")
end
# --- rdoc.rb legacy version branch --------------------------------------
# The `RDoc::VERSION < 4` branch in rdoc.rb is marked :nocov: in source
# because the modern RDoc::Markup::ToHtml constructor requires Options
# and the legacy zero-arg form has been broken since RDoc 4 (2013).
# --- implementation.rb default render and Linguist-less match? ---------
def test_base_implementation_render_raises_not_implemented_error
impl = GitHub::Markup::Implementation.new(/foo/, [])
assert_raises(NotImplementedError) { impl.render("README.foo", "anything") }
end
def test_match_uses_filename_extension_when_linguist_is_absent
impl_class = Class.new(GitHub::Markup::Implementation) do
def initialize; super(/md|markdown/, []); end
end
impl = impl_class.new
without_linguist do
assert impl.match?("README.md", nil)
# call again to cover the memoization branch in file_ext_regexp
assert impl.match?("README.markdown", nil)
refute impl.match?("README.txt", nil)
end
end
# --- markup.rb registration guard, render fallthroughs, preload! -------
def test_markup_impl_raises_when_symbol_already_registered
err = assert_raises(ArgumentError) do
GitHub::Markup.markup_impl(
::GitHub::Markups::MARKUP_MARKDOWN,
GitHub::Markup::Markdown.new
)
end
assert_match(/already defined/, err.message)
end
def test_render_returns_content_when_no_implementation_matches
raw = "no extension match here"
assert_equal raw, GitHub::Markup.render("README.unknown_ext_xyz", raw)
end
def test_render_s_returns_content_when_symbol_is_unknown
raw = "passthrough body"
assert_equal raw, GitHub::Markup.render_s(:not_a_real_markup_symbol, raw)
end
def test_render_s_raises_on_nil_content
assert_raises(ArgumentError) do
GitHub::Markup.render_s(::GitHub::Markups::MARKUP_MARKDOWN, nil)
end
end
def test_preload_calls_load_on_every_implementation
GitHub::Markup.preload!
# If preload! succeeded, every markup_impl reports a non-nil renderer or completes its load step.
GitHub::Markup.markup_impls.each do |impl|
assert_respond_to impl, :load
end
end
def test_language_returns_nil_without_linguist
without_linguist do
assert_nil GitHub::Markup.language("README.md", "anything")
end
end
# --- implementation.rb: Linguist-absent constructor + invalid-language raise
def test_implementation_initializes_without_linguist
without_linguist do
# Forces the else branch of `if defined?(::Linguist)` in initialize
impl = GitHub::Markup::Implementation.new(/foo/, ["AnythingGoesWithoutLinguist"])
assert_nil impl.languages
refute impl.match?("README.bar", nil)
end
end
def test_implementation_raises_for_unknown_linguist_language
err = assert_raises(RuntimeError) do
GitHub::Markup::Implementation.new(/foo/, ["DefinitelyNotALinguistLanguage"])
end
assert_match(/no match for language/, err.message)
end
# --- markups.rb wikicloth idempotent ESCAPED_TAGS<<'tt' both branches ---
def test_mediawiki_render_is_idempotent_for_escaped_tags
body = "==Hello==\nworld"
# First render adds 'tt'; second render hits the `else` branch of `unless include?('tt')`.
GitHub::Markup.render("README.mediawiki", body)
count_after_first = WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.count('tt')
GitHub::Markup.render("README.mediawiki", body)
count_after_second = WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.count('tt')
assert_equal 1, count_after_first, "first render should leave exactly one 'tt' entry"
assert_equal 1, count_after_second, "second render must not append a duplicate 'tt'"
end
# --- command_implementation.rb block arity branches --------------------
def test_command_block_with_arity_two_receives_rendered_and_content
captured = nil
impl = GitHub::Markup::CommandImplementation.new(
/covarity2/, ['Text'], 'test/fixtures/cat.sh', 'covarity2'
) do |rendered, content|
captured = [rendered, content]
"two:#{rendered.strip}:#{content}"
end
out = impl.render('README.covarity2', 'payload')
assert_equal ['payload', 'payload'], captured.map(&:strip)
assert_equal 'two:payload:payload', out
end
def test_command_block_with_arity_one_receives_only_rendered
captured = nil
impl = GitHub::Markup::CommandImplementation.new(
/covarity1/, ['Text'], 'test/fixtures/cat.sh', 'covarity1'
) do |rendered|
captured = rendered
"one:#{rendered.strip}"
end
out = impl.render('README.covarity1', 'payload')
assert_equal 'payload', captured.strip
assert_equal 'one:payload', out
end
def test_command_with_no_block_returns_rendered_output
impl = GitHub::Markup::CommandImplementation.new(
/covnoblock/, ['Text'], 'test/fixtures/cat.sh', 'covnoblock'
)
assert_equal 'hello', impl.render('README.covnoblock', 'hello').strip
end
def test_command_render_falls_back_to_content_when_command_returns_empty
impl = GitHub::Markup::CommandImplementation.new(
/covempty/, ['Text'], 'test/fixtures/empty.sh', 'covempty'
)
assert_equal 'fallback-body', impl.render('README.covempty', 'fallback-body')
end
def test_command_raises_when_subprocess_exits_non_zero
impl = GitHub::Markup::CommandImplementation.new(
/covfail/, ['Text'], 'test/fixtures/fail.sh', 'covfail'
)
assert_raises(GitHub::Markup::CommandError) { impl.render('README.covfail', 'payload') }
end
# --- commonmarker proc legacy option/extension mapping -----------------
def test_commonmarker_default_option_is_a_noop
capture = capture_commonmarker_call(commonmarker_opts: [:DEFAULT])
refute capture[:render].key?(:sourcepos)
refute capture[:parse].key?(:smart)
refute capture[:extension].key?(:footnotes)
end
def test_commonmarker_sourcepos_option_sets_render_sourcepos
capture = capture_commonmarker_call(commonmarker_opts: [:SOURCEPOS])
assert_equal true, capture[:render][:sourcepos]
end
def test_commonmarker_hardbreaks_option_enables_render_hardbreaks
capture = capture_commonmarker_call(commonmarker_opts: [:HARDBREAKS])
assert_equal true, capture[:render][:hardbreaks]
end
def test_commonmarker_nobreaks_option_disables_render_hardbreaks
# Combine with :HARDBREAKS so the assertion observes a transition true -> false
capture = capture_commonmarker_call(commonmarker_opts: [:HARDBREAKS, :NOBREAKS])
assert_equal false, capture[:render][:hardbreaks]
end
def test_commonmarker_smart_option_sets_parse_smart
capture = capture_commonmarker_call(commonmarker_opts: [:SMART])
assert_equal true, capture[:parse][:smart]
end
def test_commonmarker_github_pre_lang_option_sets_render_github_pre_lang
capture = capture_commonmarker_call(commonmarker_opts: [:GITHUB_PRE_LANG])
assert_equal true, capture[:render][:github_pre_lang]
end
def test_commonmarker_unsafe_option_enables_render_unsafe
capture = capture_commonmarker_call(commonmarker_opts: [:UNSAFE])
assert_equal true, capture[:render][:unsafe]
end
def test_commonmarker_footnotes_option_enables_extension_footnotes
capture = capture_commonmarker_call(commonmarker_opts: [:FOOTNOTES])
assert_equal true, capture[:extension][:footnotes]
end
def test_commonmarker_full_info_string_option_sets_render_full_info_string
capture = capture_commonmarker_call(commonmarker_opts: [:FULL_INFO_STRING])
assert_equal true, capture[:render][:full_info_string]
end
def test_commonmarker_accepts_legacy_no_op_options_without_raising
[
:VALIDATE_UTF8,
:LIBERAL_HTML_TAG,
:TABLE_PREFER_STYLE_ATTRIBUTES,
:STRIKETHROUGH_DOUBLE_TILDE,
].each do |opt|
capture = capture_commonmarker_call(commonmarker_opts: [opt])
refute capture[:render].key?(:sourcepos), "#{opt} should not alter render options"
refute capture[:parse].key?(:smart), "#{opt} should not alter parse options"
end
end
def test_commonmarker_unknown_option_raises_argument_error
err = assert_raises(ArgumentError) do
capture_commonmarker_call(commonmarker_opts: [:TOTALLY_FAKE_OPT])
end
assert_match(/unknown commonmarker option:.*TOTALLY_FAKE_OPT/, err.message)
end
def test_commonmarker_header_ids_extension_sets_empty_string_prefix
capture = capture_commonmarker_call(commonmarker_exts: [:header_ids])
assert_equal "", capture[:extension][:header_ids]
end
def test_commonmarker_unknown_extension_raises_argument_error
err = assert_raises(ArgumentError) do
capture_commonmarker_call(commonmarker_exts: [:not_a_real_ext])
end
assert_match(/unknown commonmarker extension:.*not_a_real_ext/, err.message)
end
private
# Invokes the commonmarker MARKDOWN_GEMS proc against a stubbed Commonmarker
# constant and returns the parse/render/extension options the proc passed to
# Commonmarker.to_html.
def capture_commonmarker_call(options)
captured = {}
fake = Module.new
fake.define_singleton_method(:to_html) do |content, **kwargs|
captured[:content] = content
opts = kwargs.fetch(:options, {})
captured[:parse] = opts[:parse] || {}
captured[:render] = opts[:render] || {}
captured[:extension] = opts[:extension] || {}
"stub-html"
end
with_stub_const("Commonmarker", fake) do
GitHub::Markup::Markdown::MARKDOWN_GEMS
.fetch("commonmarker")
.call("payload", options: options)
end
captured
end
def with_stub_const(path, value)
parts = path.split("::")
name = parts.pop
parent = parts.inject(Object) { |mod, part| mod.const_get(part) }
had_const = parent.const_defined?(name, false)
original = parent.const_get(name) if had_const
parent.send(:remove_const, name) if had_const
parent.const_set(name, value)
yield
ensure
parent.send(:remove_const, name) if parent.const_defined?(name, false)
parent.const_set(name, original) if had_const
end
def without_linguist
had_const = Object.const_defined?(:Linguist, false)
original = Object.const_get(:Linguist) if had_const
Object.send(:remove_const, :Linguist) if had_const
yield
ensure
Object.const_set(:Linguist, original) if had_const
end
def fake_renderer_module(method_name)
Module.new do
define_singleton_method(method_name) { |content| "github_markdown:#{content}" }
end
end
def instance_renderer_class(method_name, prefix:)
Class.new do
define_method(:initialize) { |content| @__coverage_content = "#{prefix}:#{content}" }
define_method(method_name) { @__coverage_content }
end
end
end