require "test_helper" require "govspeak_test_helper" class GovspeakImagesBangTest < Minitest::Test include GovspeakTestHelper class Image attr_reader :alt_text, :url, :caption, :credit def initialize(attrs = {}) @alt_text = attrs[:alt_text] || "my alt" @url = attrs[:url] || "http://example.com/image.jpg" @caption = attrs[:caption] @credit = attrs[:credit] end end test "!!n syntax renders an image in options[:images]" do given_govspeak "!!1", images: [Image.new] do assert_html_output( "
\"my
", ) end end test "!!n syntax escapes alt text" do given_govspeak "!!1", images: [Image.new(alt_text: %(my alt '&"<>))] do assert_html_output( "
\"my
", ) end end test "!!n syntax renders nothing if not found" do doc = Govspeak::Document.new("!!1") assert_equal %(\n), doc.to_html end test "Image:image-id syntax renders nothing" do doc = Govspeak::Document.new("[Image:another-id]", images: [Image.new]) assert_equal %(\n), doc.to_html end test "!!n syntax adds image caption if given" do given_govspeak "!!1", images: [Image.new(caption: "My Caption & so on")] do assert_html_output( "
\"my
\n

My Caption & so on

", ) end end test "!!n syntax ignores a blank caption" do given_govspeak "!!1", images: [Image.new(caption: " ")] do assert_html_output( "
\"my
", ) end end test "¡¡n syntax adds image credit if given" do given_govspeak "!!1", images: [Image.new(credit: "My Credit & so on")] do assert_html_output( "
\"my
\n

Image credit: My Credit & so on

", ) end end test "!!n syntax ignores a blank credit" do given_govspeak "!!1", images: [Image.new(credit: " ")] do assert_html_output( "
\"my
", ) end end test "!!n syntax adds image caption and credit if given" do given_govspeak "!!1", images: [Image.new(caption: "My Caption & so on", credit: "My Credit & so on")] do assert_html_output( "
\"my
\n

My Caption & so on

\n

Image credit: My Credit & so on

", ) end end test "!!n syntax must start on a new line" do given_govspeak "some text !!1", images: [Image.new] do assert_html_output("

some text !!1

") end given_govspeak "!!1", images: [Image.new] do assert_html_output( "
\"my
", ) end given_govspeak "!!1 some text", images: [Image.new] do assert_html_output( "
\"my
\n

some text

", ) end given_govspeak "some text\n!!1\nsome more text", images: [Image.new] do assert_html_output <<~HTML

some text

my alt

some more text

HTML end end end