Skip to content

Commit

Permalink
Complete test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushn21 committed Jun 16, 2021
1 parent adfc8bd commit 54c6b0c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/bridgetown-svg-inliner/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/bridgetown-svg-inliner/liquid_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/src/_data/site_metadata.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
title: Override Me
title: SVG Inliner
12 changes: 12 additions & 0 deletions test/fixtures/src/about.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
layout: default
template_engine: erb
---

<figure id="base">
<%= svg "/assets/icons/thumbs_up.svg" %>
</figure>

<figure id="with-attributes">
<%= svg "/assets/icons/thumbs_up.svg", class: "icon", id: "upvote" %>
</figure>
1 change: 1 addition & 0 deletions test/fixtures/src/assets/icons/thumbs_up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion test/fixtures/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@
layout: default
---

Testing this plugin: {% sample_plugin %}
<figure id="base">
{% svg "/assets/icons/thumbs_up.svg" %}
</figure>

<figure id="with-attributes">
{% svg "/assets/icons/thumbs_up.svg", class: "icon", id: "upvote" %}
</figure>

<figure id="without-quotes">
{% svg /assets/icons/thumbs_up.svg, class: icon %}
</figure>

<figure id="with-liquid-variables">
{% assign svg_file = "thumbs_up" %}
{% assign svg_class_list = "icon icon--small" %}

{% svg "/assets/icons/{{ svg_file }}.svg", class: "{{ svg_class_list }}" %}
</figure>

64 changes: 51 additions & 13 deletions test/test_bridgetown_svg_inliner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<title>My Awesome Site</title>"
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

0 comments on commit 54c6b0c

Please sign in to comment.