2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2019-08-08 10:09:24 -04:00
|
|
|
require_relative 'helper'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
class TestRDocGeneratorDarkfish < RDoc::TestCase
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
def setup
|
2012-11-26 23:28:14 -05:00
|
|
|
super
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@lib_dir = "#{@pwd}/lib"
|
|
|
|
$LOAD_PATH.unshift @lib_dir # ensure we load from this RDoc
|
|
|
|
|
|
|
|
@options = RDoc::Options.new
|
|
|
|
@options.option_parser = OptionParser.new
|
|
|
|
|
|
|
|
@tmpdir = File.join Dir.tmpdir, "test_rdoc_generator_darkfish_#{$$}"
|
|
|
|
FileUtils.mkdir_p @tmpdir
|
|
|
|
Dir.chdir @tmpdir
|
|
|
|
@options.op_dir = @tmpdir
|
|
|
|
@options.generator = RDoc::Generator::Darkfish
|
|
|
|
|
|
|
|
$LOAD_PATH.each do |path|
|
2012-11-26 23:28:14 -05:00
|
|
|
darkfish_dir = File.join path, 'rdoc/generator/template/darkfish/'
|
2010-12-19 22:22:49 -05:00
|
|
|
next unless File.directory? darkfish_dir
|
|
|
|
@options.template_dir = darkfish_dir
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@rdoc.options = @options
|
|
|
|
|
|
|
|
@g = @options.generator.new @store, @options
|
|
|
|
@rdoc.generator = @g
|
|
|
|
|
|
|
|
@top_level = @store.add_file 'file.rb'
|
|
|
|
@top_level.parser = RDoc::Parser::Ruby
|
|
|
|
@klass = @top_level.add_class RDoc::NormalClass, 'Klass'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@alias_constant = RDoc::Constant.new 'A', nil, ''
|
|
|
|
@alias_constant.record_location @top_level
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@top_level.add_constant @alias_constant
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
@klass.add_module_alias @klass, @klass.name, @alias_constant, @top_level
|
2011-07-30 20:19:00 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@meth = RDoc::AnyMethod.new nil, 'method'
|
|
|
|
@meth_bang = RDoc::AnyMethod.new nil, 'method!'
|
2017-10-09 22:01:00 -04:00
|
|
|
@meth_with_html_tag_yield = RDoc::AnyMethod.new nil, 'method_with_html_tag_yield'
|
|
|
|
@meth_with_html_tag_yield.block_params = '%<<script>alert("atui")</script>>, yield_arg'
|
2010-12-19 22:22:49 -05:00
|
|
|
@attr = RDoc::Attr.new nil, 'attr', 'RW', ''
|
|
|
|
|
|
|
|
@klass.add_method @meth
|
|
|
|
@klass.add_method @meth_bang
|
2017-10-09 22:01:00 -04:00
|
|
|
@klass.add_method @meth_with_html_tag_yield
|
2010-12-19 22:22:49 -05:00
|
|
|
@klass.add_attribute @attr
|
2011-07-30 20:19:00 -04:00
|
|
|
|
|
|
|
@ignored = @top_level.add_class RDoc::NormalClass, 'Ignored'
|
|
|
|
@ignored.ignore
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
@store.complete :private
|
|
|
|
|
|
|
|
@object = @store.find_class_or_module 'Object'
|
|
|
|
@klass_alias = @store.find_class_or_module 'Klass::A'
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2012-11-26 23:28:14 -05:00
|
|
|
super
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
$LOAD_PATH.shift
|
|
|
|
Dir.chdir @pwd
|
|
|
|
FileUtils.rm_rf @tmpdir
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate
|
2012-11-26 23:28:14 -05:00
|
|
|
top_level = @store.add_file 'file.rb'
|
2010-12-19 22:22:49 -05:00
|
|
|
top_level.add_class @klass.class, @klass.name
|
2022-03-13 06:44:09 -04:00
|
|
|
@klass.add_class RDoc::NormalClass, 'Inner'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@g.generate
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
assert_file 'index.html'
|
|
|
|
assert_file 'Object.html'
|
2022-03-13 06:44:09 -04:00
|
|
|
assert_file 'Klass.html'
|
|
|
|
assert_file 'Klass/Inner.html'
|
2012-11-26 23:28:14 -05:00
|
|
|
assert_file 'table_of_contents.html'
|
|
|
|
assert_file 'js/search_index.js'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2014-12-06 20:22:37 -05:00
|
|
|
assert_hard_link 'css/rdoc.css'
|
|
|
|
assert_hard_link 'css/fonts.css'
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
assert_hard_link 'fonts/SourceCodePro-Bold.ttf'
|
|
|
|
assert_hard_link 'fonts/SourceCodePro-Regular.ttf'
|
|
|
|
|
2016-09-07 18:23:38 -04:00
|
|
|
encoding = Regexp.escape Encoding::UTF_8.name
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
assert_match %r%<meta charset="#{encoding}">%, File.read('index.html')
|
|
|
|
assert_match %r%<meta charset="#{encoding}">%, File.read('Object.html')
|
2011-07-30 20:19:00 -04:00
|
|
|
|
|
|
|
refute_match(/Ignored/, File.read('index.html'))
|
2022-03-13 06:44:09 -04:00
|
|
|
summary = File.read('index.html')[%r[<summary.*Klass\.html.*</summary>.*</details>]m]
|
|
|
|
assert_match(%r[Klass/Inner\.html".*>Inner<], summary)
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
|
2022-03-21 12:00:54 -04:00
|
|
|
def test_generate_page
|
|
|
|
@store.add_file 'outer.rdoc', parser: RDoc::Parser::Simple
|
|
|
|
@store.add_file 'outer/inner.rdoc', parser: RDoc::Parser::Simple
|
|
|
|
@g.generate
|
|
|
|
assert_file 'outer_rdoc.html'
|
|
|
|
assert_file 'outer/inner_rdoc.html'
|
2022-03-13 08:16:43 -04:00
|
|
|
index = File.read('index.html')
|
|
|
|
re = %r[<summary><a href="\./outer_rdoc\.html">outer</a></summary>.*?</details>]m
|
|
|
|
assert_match(re, index)
|
|
|
|
summary = index[re]
|
|
|
|
assert_match %r[<a href="\./outer/inner_rdoc.html">inner</a>], summary
|
2022-03-21 12:13:06 -04:00
|
|
|
re = %r[<details open><summary><a href="\./outer_rdoc\.html">outer</a></summary>.*?</details>]m
|
|
|
|
assert_match(re, File.read('outer_rdoc.html'))
|
|
|
|
re = %r[<details open><summary><a href="\.\./outer_rdoc\.html">outer</a></summary>.*?</details>]m
|
|
|
|
assert_match(re, File.read('outer/inner_rdoc.html'))
|
2022-03-21 12:00:54 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def test_generate_dry_run
|
2012-11-26 23:28:14 -05:00
|
|
|
@g.dry_run = true
|
|
|
|
top_level = @store.add_file 'file.rb'
|
2010-12-19 22:22:49 -05:00
|
|
|
top_level.add_class @klass.class, @klass.name
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@g.generate
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
refute_file 'index.html'
|
|
|
|
refute_file 'Object.html'
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_static
|
|
|
|
FileUtils.mkdir_p 'dir/images'
|
|
|
|
FileUtils.touch 'dir/images/image.png'
|
|
|
|
FileUtils.mkdir_p 'file'
|
|
|
|
FileUtils.touch 'file/file.txt'
|
|
|
|
|
|
|
|
@options.static_path = [
|
|
|
|
File.expand_path('dir'),
|
|
|
|
File.expand_path('file/file.txt'),
|
|
|
|
]
|
|
|
|
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
assert_file 'images/image.png'
|
|
|
|
assert_file 'file.txt'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_static_dry_run
|
|
|
|
FileUtils.mkdir 'static'
|
|
|
|
FileUtils.touch 'static/image.png'
|
|
|
|
|
|
|
|
@options.static_path = [File.expand_path('static')]
|
|
|
|
@g.dry_run = true
|
|
|
|
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
refute_file 'image.png'
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def test_install_rdoc_static_file
|
2018-03-26 01:56:26 -04:00
|
|
|
src = Pathname File.expand_path(__FILE__, @pwd)
|
2013-09-18 19:33:36 -04:00
|
|
|
dst = File.join @tmpdir, File.basename(src)
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
@g.install_rdoc_static_file src, dst, options
|
|
|
|
|
|
|
|
assert_file dst
|
2021-09-05 22:54:09 -04:00
|
|
|
assert_hard_link dst
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_install_rdoc_static_file_missing
|
|
|
|
src = Pathname(__FILE__) + 'nonexistent'
|
|
|
|
dst = File.join @tmpdir, File.basename(src)
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
@g.install_rdoc_static_file src, dst, options
|
|
|
|
|
|
|
|
refute_file dst
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def test_setup
|
|
|
|
@g.setup
|
|
|
|
|
|
|
|
assert_equal [@klass_alias, @ignored, @klass, @object],
|
|
|
|
@g.classes.sort_by { |klass| klass.full_name }
|
|
|
|
assert_equal [@top_level], @g.files
|
2017-10-09 22:01:00 -04:00
|
|
|
assert_equal [@meth, @meth, @meth_bang, @meth_bang, @meth_with_html_tag_yield, @meth_with_html_tag_yield], @g.methods
|
2012-11-26 23:28:14 -05:00
|
|
|
assert_equal [@klass_alias, @klass, @object], @g.modsort
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_for
|
2012-11-26 23:28:14 -05:00
|
|
|
classpage = Pathname.new @options.template_dir + 'class.rhtml'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
template = @g.send(:template_for, classpage, true, RDoc::ERBIO)
|
2010-12-19 22:22:49 -05:00
|
|
|
assert_kind_of RDoc::ERBIO, template
|
|
|
|
|
|
|
|
assert_same template, @g.send(:template_for, classpage)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_template_for_dry_run
|
2012-11-26 23:28:14 -05:00
|
|
|
classpage = Pathname.new @options.template_dir + 'class.rhtml'
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
template = @g.send(:template_for, classpage, true, ERB)
|
2010-12-19 22:22:49 -05:00
|
|
|
assert_kind_of ERB, template
|
|
|
|
|
|
|
|
assert_same template, @g.send(:template_for, classpage)
|
|
|
|
end
|
|
|
|
|
2013-02-07 23:12:56 -05:00
|
|
|
def test_template_for_partial
|
|
|
|
partial = Pathname.new @options.template_dir + '_sidebar_classes.rhtml'
|
|
|
|
|
|
|
|
template = @g.send(:template_for, partial, false, RDoc::ERBPartial)
|
|
|
|
|
|
|
|
assert_kind_of RDoc::ERBPartial, template
|
|
|
|
|
|
|
|
assert_same template, @g.send(:template_for, partial)
|
|
|
|
end
|
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
def test_generated_method_with_html_tag_yield
|
|
|
|
top_level = @store.add_file 'file.rb'
|
|
|
|
top_level.add_class @klass.class, @klass.name
|
|
|
|
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
path = File.join @tmpdir, 'A.html'
|
|
|
|
|
|
|
|
f = open(path)
|
|
|
|
internal_file = f.read
|
|
|
|
method_name_index = internal_file.index('<span class="method-name">method_with_html_tag_yield</span>')
|
|
|
|
last_of_method_name_index = method_name_index + internal_file[method_name_index..-1].index('<div class="method-description">') - 1
|
|
|
|
method_name = internal_file[method_name_index..last_of_method_name_index]
|
2017-10-17 03:41:04 -04:00
|
|
|
f.close
|
2017-10-09 22:01:00 -04:00
|
|
|
|
|
|
|
assert_includes method_name, '{ |%<<script>alert("atui")</script>>, yield_arg| ... }'
|
|
|
|
end
|
|
|
|
|
2021-08-28 04:41:47 -04:00
|
|
|
def test_generated_filename_with_html_tag
|
2022-10-06 22:38:21 -04:00
|
|
|
filename = '"><em>should be escaped'
|
|
|
|
begin # in @tmpdir
|
|
|
|
File.write(filename, '')
|
|
|
|
rescue SystemCallError
|
|
|
|
# ", <, > chars are prohibited as filename
|
|
|
|
return
|
|
|
|
else
|
|
|
|
File.unlink(filename)
|
|
|
|
end
|
|
|
|
@store.add_file filename
|
2021-08-28 04:41:47 -04:00
|
|
|
doc = @store.all_files.last
|
|
|
|
doc.parser = RDoc::Parser::Simple
|
|
|
|
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
Dir.glob("*.html", base: @tmpdir) do |html|
|
|
|
|
File.read(File.join(@tmpdir, html)).scan(/.*should be escaped.*/) do |line|
|
|
|
|
assert_not_include line, "<em>", html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-18 00:48:12 -04:00
|
|
|
def test_template_stylesheets
|
|
|
|
css = Tempfile.create(%W'hoge .css', Dir.mktmpdir('tmp', '.'))
|
|
|
|
File.write(css, '')
|
2021-09-12 20:36:42 -04:00
|
|
|
css.close
|
2021-08-18 00:48:12 -04:00
|
|
|
base = File.basename(css)
|
|
|
|
refute_file(base)
|
|
|
|
|
|
|
|
@options.template_stylesheets << css
|
|
|
|
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
assert_file base
|
|
|
|
assert_include File.read('index.html'), %Q[href="./#{base}"]
|
|
|
|
end
|
|
|
|
|
2022-04-13 09:02:21 -04:00
|
|
|
def test_title
|
|
|
|
title = "RDoc Test".freeze
|
|
|
|
@options.title = title
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
assert_main_title(File.read('index.html'), title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_title_escape
|
|
|
|
title = %[<script>alert("RDoc")</script>].freeze
|
|
|
|
@options.title = title
|
|
|
|
@g.generate
|
|
|
|
|
|
|
|
assert_main_title(File.read('index.html'), title)
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Asserts that +filename+ has a link count greater than 1 if hard links to
|
|
|
|
# @tmpdir are supported.
|
|
|
|
|
|
|
|
def assert_hard_link filename
|
|
|
|
assert_file filename
|
|
|
|
|
|
|
|
src = @g.template_dir + '_head.rhtml'
|
|
|
|
dst = File.join @tmpdir, 'hardlinktest'
|
|
|
|
|
|
|
|
begin
|
|
|
|
FileUtils.ln src, dst
|
2013-11-19 09:34:20 -05:00
|
|
|
nlink = File.stat(dst).nlink if File.identical? src, dst
|
2013-09-18 19:33:36 -04:00
|
|
|
FileUtils.rm dst
|
2013-11-19 09:34:20 -05:00
|
|
|
return if nlink == 1
|
2013-09-18 19:33:36 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:47:33 -05:00
|
|
|
assert_operator File.stat(filename).nlink, :>, 1,
|
|
|
|
"#{filename} is not hard-linked"
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
2022-04-13 09:02:21 -04:00
|
|
|
def assert_main_title(content, title)
|
|
|
|
title = CGI.escapeHTML(title)
|
|
|
|
assert_equal(title, content[%r[<title>(.*?)<\/title>]im, 1])
|
|
|
|
assert_include(content[%r[<main\s[^<>]*+>\s*(.*?)</main>]im, 1], title)
|
|
|
|
end
|
2015-04-12 04:36:37 -04:00
|
|
|
end
|