# encoding: utf-8
class Nanoc::Extra::LinkCollectorTest < Nanoc::TestCase
def test_all
# Create dummy data
File.open('file-a.html', 'w') do |io|
io << %(A 1
)
io << %(A 2
)
io << %(
)
io << %(A 4)
io << %(A 5
)
end
File.open('file-b.html', 'w') do |io|
io << %(B 1
)
io << %(B 2
)
io << %(B 3
)
end
# Create validator
collector = Nanoc::Extra::LinkCollector.new(%w( file-a.html file-b.html ))
# Test
hrefs_with_filenames = collector.filenames_per_href
hrefs = hrefs_with_filenames.keys
assert_includes hrefs, 'http://example.com/'
assert_includes hrefs, 'https://example.com/'
assert_includes hrefs, 'stuff/'
refute_includes hrefs, nil
assert_includes hrefs, 'mailto:bob@example.com'
assert_includes hrefs, '../stuff'
assert_includes hrefs, '/stuff'
refute_includes hrefs, 'https://example.com/with-fragment#moo'
assert_includes hrefs, 'https://example.com/with-fragment'
end
def test_external
# Create dummy data
File.open('file-a.html', 'w') do |io|
io << %(A 1
)
io << %(A 2
)
io << %(
)
end
File.open('file-b.html', 'w') do |io|
io << %(B 1
)
io << %(B 2
)
io << %(B 3
)
end
# Create validator
collector = Nanoc::Extra::LinkCollector.new(%w( file-a.html file-b.html ), :external)
# Test
hrefs_with_filenames = collector.filenames_per_href
hrefs = hrefs_with_filenames.keys
assert_includes hrefs, 'http://example.com/'
assert_includes hrefs, 'https://example.com/'
refute_includes hrefs, 'stuff/'
assert_includes hrefs, 'mailto:bob@example.com'
refute_includes hrefs, '../stuff'
refute_includes hrefs, '/stuff'
end
def test_internal
# Create dummy data
File.open('file-a.html', 'w') do |io|
io << %(A 1
)
io << %(A 2
)
io << %(
)
end
File.open('file-b.html', 'w') do |io|
io << %(B 1
)
io << %(B 2
)
io << %(B 3
)
end
# Create validator
collector = Nanoc::Extra::LinkCollector.new(%w( file-a.html file-b.html ), :internal)
# Test
hrefs_with_filenames = collector.filenames_per_href
hrefs = hrefs_with_filenames.keys
refute_includes hrefs, 'http://example.com/'
refute_includes hrefs, 'https://example.com/'
assert_includes hrefs, 'stuff/'
refute_includes hrefs, 'mailto:bob@example.com'
assert_includes hrefs, '../stuff'
assert_includes hrefs, '/stuff'
end
end