# encoding: UTF-8
require 'test_helper'
require 'govspeak_test_helper'
class GovspeakImagesTest < Minitest::Test
include GovspeakTestHelper
def build_image(attrs = {})
attrs[:alt_text] ||= "my alt"
attrs[:url] ||= "http://example.com/image.jpg"
attrs[:id] ||= "image-id"
attrs
end
test "Image:image-id syntax renders an image in options[:images]" do
given_govspeak "[Image:image-id]", images: [build_image] do
assert_html_output(
%{} +
%{
} +
%{}
)
end
end
test "Image:image-id syntax escapes alt text" do
given_govspeak "[Image:image-id]", images: [build_image(alt_text: %{my alt '&"<>})] do
assert_html_output(
%{} +
%{
} +
%{}
)
end
end
test "Image:image-id syntax renders nothing if not found" do
doc = Govspeak::Document.new("[Image:another-id]")
assert_equal %{\n}, doc.to_html
end
test "Image:image-id syntax adds image caption if given" do
given_govspeak "[Image:image-id]", images: [build_image(caption: 'My Caption & so on')] do
assert_html_output(
%{} +
%{
\n} +
%{
My Caption & so on
} +
%{}
)
end
end
test "Image:image-id syntax ignores a blank caption" do
given_govspeak "[Image:image-id]", images: [build_image(caption: ' ')] do
assert_html_output(
%{} +
%{
} +
%{}
)
end
end
test "Image:image-id syntax adds image credit if given" do
given_govspeak "[Image:image-id]", images: [build_image(credit: 'My Credit & so on')] do
assert_html_output(
%{} +
%{
\n} +
%{
Image credit: My Credit & so on
} +
%{}
)
end
end
test "Image:image-id syntax ignores a blank credit" do
given_govspeak "[Image:image-id]", images: [build_image(credit: ' ')] do
assert_html_output(
%{} +
%{
} +
%{}
)
end
end
test "Image:image-id syntax adds image caption and credit if given" do
given_govspeak "[Image:image-id]", images: [build_image(caption: 'My Caption & so on', credit: 'My Credit & so on')] do
assert_html_output(
%{} +
%{
\n} +
%{} +
%{
My Caption & so on
\n} +
%{
Image credit: My Credit & so on
} +
%{} +
%{}
)
end
end
test "Image is not inserted when it does not start on a new line" do
given_govspeak "some text [Image:image-id]", images: [build_image] do
assert_html_output("
some text [Image:image-id]
")
end
given_govspeak "[Image:image-id]", images: [build_image] do
assert_html_output(
%{} +
%{
} +
%{}
)
end
given_govspeak "[Image:image-id] some text", images: [build_image] do
assert_html_output(
%{} +
%{