Skip to content

Commit

Permalink
#47 test added
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 6, 2024
1 parent 4164741 commit 44455b9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 29 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source 'https://rubygems.org'
gemspec

gem 'minitest', '5.21.1', require: false
gem 'rake', '13.1.0', require: false
gem 'rubocop', '1.60.2', require: false
gem 'rubocop-rspec', '2.26.1', require: false
12 changes: 11 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ def version
Gem::Specification.load(Dir['*.gemspec'].first).version
end

task default: %i[clean jekylls rubocop copyright]
task default: %i[clean test jekylls rubocop copyright]

require 'rake/testtask'
desc 'Run all unit tests'
Rake::TestTask.new(:test) do |test|
Rake::Cleaner.cleanup_files(['coverage'])
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.warning = true
test.verbose = false
end

task :jekylls do
sh 'cd test-jekylls; make'
Expand Down
54 changes: 26 additions & 28 deletions lib/jekyll-plantuml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,43 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'jekyll'
require 'digest'
require 'fileutils'

# Jekyll main module.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2014-2024 Yegor Bugayenko
# License:: MIT
module Jekyll
# The main class
class PlantumlBlock < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@html = (markup or '').strip
end
class Jekyll::PlantumlBlock < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@html = (markup or '').strip
end

def render(context)
site = context.registers[:site]
name = Digest::MD5.hexdigest(super)
unless File.exist?(File.join(site.dest, "uml/#{name}.svg"))
uml = File.join(site.source, "uml/#{name}.uml")
svg = File.join(site.source, "uml/#{name}.svg")
if File.exist?(svg)
puts "File #{svg} already exists (#{File.size(svg)} bytes)"
else
FileUtils.mkdir_p(File.dirname(uml))
File.open(uml, 'w') do |f|
f.write("@startuml\n")
f.write(super)
f.write("\n@enduml")
end
system("plantuml -tsvg #{uml}") or raise "PlantUML error: #{super}"
site.static_files << Jekyll::StaticFile.new(
site, site.source, 'uml', "#{name}.svg"
)
puts "\nFile #{svg} created (#{File.size(svg)} bytes)"
def render(context)
site = context.registers[:site]
name = Digest::MD5.hexdigest(super)
unless File.exist?(File.join(site.dest, "uml/#{name}.svg"))
uml = File.join(site.source, "uml/#{name}.uml")
svg = File.join(site.source, "uml/#{name}.svg")
if File.exist?(svg)
puts "File #{svg} already exists (#{File.size(svg)} bytes)"
else
FileUtils.mkdir_p(File.dirname(uml))
File.open(uml, 'w') do |f|
f.write("@startuml\n")
f.write(super)
f.write("\n@enduml")
end
system("plantuml -tsvg #{uml}") or raise "PlantUML error: #{super}"
site.static_files << Jekyll::StaticFile.new(
site, site.source, 'uml', "#{name}.svg"
)
puts "\nFile #{svg} created (#{File.size(svg)} bytes)"
end
"<p><object data='#{site.baseurl}/uml/#{name}.svg' type='image/svg+xml' #{@html} class='plantuml'></object></p>"
end
"<p><object data='#{site.baseurl}/uml/#{name}.svg' type='image/svg+xml' #{@html} class='plantuml'></object></p>"
end
end

Expand Down
27 changes: 27 additions & 0 deletions test/test__helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# Copyright (c) 2014-2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

$stdout.sync = true

require 'simplecov'
SimpleCov.start

49 changes: 49 additions & 0 deletions test/test_plantuml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

# (The MIT License)
#
# Copyright (c) 2014-2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'liquid'
require 'tmpdir'
require 'minitest/autorun'
require_relative 'test_helper'
require_relative '../lib/jekyll-plantuml'

# PlantumlBlock test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2023-2024 Yegor Bugayenko
# License:: MIT
class PlantumlBlockTest < Minitest::Test
def test_failure
Dir.mktmpdir 'test' do |dir|
template = Liquid::Template.parse("{% plantuml %}[A] -> [B]{% endplantuml %}")
config = {
'destination' => File.join(dir, 'dest'),
'source' => File.join(dir, 'source'),
}
context = Liquid::Context.new({}, {}, {site: Jekyll::Site.new(Jekyll.configuration(config))})
block = template.root.nodelist.first
output = block.render(context)
assert(output.start_with?('<p><object'))
end
end
end

0 comments on commit 44455b9

Please sign in to comment.