From 54c6b0c67c017977ca8554f12dfd305f0399976f Mon Sep 17 00:00:00 2001 From: Ayush Newatia Date: Wed, 16 Jun 2021 23:59:30 +0100 Subject: [PATCH] Complete test suite --- lib/bridgetown-svg-inliner/builder.rb | 2 +- .../liquid_attributes.rb | 2 +- test/fixtures/src/_data/site_metadata.yml | 2 +- test/fixtures/src/about.erb | 12 ++++ test/fixtures/src/assets/icons/thumbs_up.svg | 1 + test/fixtures/src/index.html | 20 +++++- test/test_bridgetown_svg_inliner.rb | 64 +++++++++++++++---- 7 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/src/about.erb create mode 100644 test/fixtures/src/assets/icons/thumbs_up.svg diff --git a/lib/bridgetown-svg-inliner/builder.rb b/lib/bridgetown-svg-inliner/builder.rb index 458d8da..ead9654 100644 --- a/lib/bridgetown-svg-inliner/builder.rb +++ b/lib/bridgetown-svg-inliner/builder.rb @@ -20,7 +20,7 @@ def build def render(path, html_attributes) file = File.read(site.in_source_dir(path)) xml = Nokogiri::XML(file) - html_attributes.each { |key, value| xml.root.set_attribute(key, value) } + html_attributes&.each { |key, value| xml.root.set_attribute(key, value) } xml.root.to_xml.html_safe end end diff --git a/lib/bridgetown-svg-inliner/liquid_attributes.rb b/lib/bridgetown-svg-inliner/liquid_attributes.rb index 38ec227..2f1e045 100644 --- a/lib/bridgetown-svg-inliner/liquid_attributes.rb +++ b/lib/bridgetown-svg-inliner/liquid_attributes.rb @@ -10,7 +10,7 @@ def initialize(attributes) @path = unescape_string(path) @args = args.scan(Liquid::TagAttributes).map do |arg| [arg[0], unescape_string(arg[1])] - end.to_h + end.to_h if args.present? end private diff --git a/test/fixtures/src/_data/site_metadata.yml b/test/fixtures/src/_data/site_metadata.yml index de68414..dbfbd23 100644 --- a/test/fixtures/src/_data/site_metadata.yml +++ b/test/fixtures/src/_data/site_metadata.yml @@ -1 +1 @@ -title: Override Me \ No newline at end of file +title: SVG Inliner diff --git a/test/fixtures/src/about.erb b/test/fixtures/src/about.erb new file mode 100644 index 0000000..a22d7cd --- /dev/null +++ b/test/fixtures/src/about.erb @@ -0,0 +1,12 @@ +--- +layout: default +template_engine: erb +--- + +
+ <%= svg "/assets/icons/thumbs_up.svg" %> +
+ +
+ <%= svg "/assets/icons/thumbs_up.svg", class: "icon", id: "upvote" %> +
\ No newline at end of file diff --git a/test/fixtures/src/assets/icons/thumbs_up.svg b/test/fixtures/src/assets/icons/thumbs_up.svg new file mode 100644 index 0000000..f60e4d1 --- /dev/null +++ b/test/fixtures/src/assets/icons/thumbs_up.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/fixtures/src/index.html b/test/fixtures/src/index.html index 50bd269..6acc79e 100644 --- a/test/fixtures/src/index.html +++ b/test/fixtures/src/index.html @@ -2,4 +2,22 @@ layout: default --- -Testing this plugin: {% sample_plugin %} +
+ {% svg "/assets/icons/thumbs_up.svg" %} +
+ +
+ {% svg "/assets/icons/thumbs_up.svg", class: "icon", id: "upvote" %} +
+ +
+ {% svg /assets/icons/thumbs_up.svg, class: icon %} +
+ +
+ {% assign svg_file = "thumbs_up" %} + {% assign svg_class_list = "icon icon--small" %} + + {% svg "/assets/icons/{{ svg_file }}.svg", class: "{{ svg_class_list }}" %} +
+ diff --git a/test/test_bridgetown_svg_inliner.rb b/test/test_bridgetown_svg_inliner.rb index 044af96..27bdbfb 100644 --- a/test/test_bridgetown_svg_inliner.rb +++ b/test/test_bridgetown_svg_inliner.rb @@ -4,28 +4,66 @@ class TestBridgetownSvgInliner < Minitest::Test def setup - @site = Bridgetown::Site.new(Bridgetown.configuration( - "root_dir" => root_dir, - "source" => source_dir, - "destination" => dest_dir, - "quiet" => true - )) + @site = Bridgetown::Site.new( + Bridgetown.configuration( + "root_dir" => root_dir, + "source" => source_dir, + "destination" => dest_dir, + "quiet" => true + ) + ) end - context "sample plugin" do + context "rendering an SVG using the liquid tag" do setup do - with_metadata title: "My Awesome Site" do + with_metadata title: "SVG Inliner" do @site.process - @contents = File.read(dest_dir("index.html")) + @contents = Nokogiri::HTML(File.read(dest_dir("index.html"))) end end - should "output the overridden metadata" do - assert_includes @contents, "My Awesome Site" + should "output the SVG file" do + assert_valid_svg "#base > svg" end - should "output the sample Liquid tag" do - assert_includes @contents, "This plugin works!" + should "output the SVG with attributes passed into the tag" do + assert_valid_svg "#with-attributes > svg.icon" + assert_valid_svg "#upvote" end + + should "output the SVG when quotes are not used in the arguments" do + assert_valid_svg "#without-quotes > svg.icon" + end + + should "output the SVG interpolating liquid variables in the arguments" do + assert_valid_svg "#with-liquid-variables > svg.icon.icon--small" + end + end + + context "rendering an SVG using the ERB helper" do + setup do + with_metadata title: "SVG Inliner" do + @site.process + @contents = Nokogiri::HTML(File.read(dest_dir("about.html"))) + end + end + + should "output the SVG file" do + assert_valid_svg "#base > svg" + end + + should "output the SVG with attributes passed into the helper" do + assert_valid_svg "#with-attributes > svg.icon" + assert_valid_svg "#upvote" + end + end + + private + + def assert_valid_svg(selector) + svg_node = @contents.at_css(selector) + + assert_equal "svg", svg_node.name + refute_nil svg_node.children.find { _1.name == "path" } end end