summaryrefslogtreecommitdiff
path: root/spec/diagram_spec.rb
blob: 32733fefbd9f8868270259f5b42e8c672f92df86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Asciidoctor Diagram Integration', if: (RSpec::ExampleGroupHelpers.gem_available? 'asciidoctor-diagram'), &(proc do
  it 'should locate generated diagram when :to_dir is set and imagesdir is not set' do
    require 'asciidoctor-diagram'
    input_file = Pathname.new fixture_file 'diagrams.adoc'
    pdf = to_pdf input_file, safe: :unsafe, attributes: { 'sequence-diagram-name' => 'sequence-diagram-a' }, analyze: :image
    (expect pdf.images).to have_size 1
    (expect Pathname.new output_file 'sequence-diagram-a.png').to exist
    (expect Pathname.new output_file '.asciidoctor/diagram/sequence-diagram-a.png.cache').to exist
    (expect Pathname.new fixture_file 'sequence-diagram-a.png').not_to exist
    (expect Pathname.new fixture_file 'sequence-diagram-a.png.cache').not_to exist
  end

  it 'should generate diagram into imagesdir relative to output dir' do
    require 'asciidoctor-diagram'
    input_file = Pathname.new fixture_file 'diagrams.adoc'
    pdf = to_pdf input_file, safe: :unsafe, attributes: { 'imagesdir' => 'images', 'sequence-diagram-name' => 'sequence-diagram-b' }, analyze: :image
    (expect pdf.images).to have_size 1
    (expect Pathname.new output_file 'images/sequence-diagram-b.png').to exist
    (expect Pathname.new output_file '.asciidoctor/diagram/sequence-diagram-b.png.cache').to exist
    (expect Pathname.new fixture_file 'images/sequence-diagram-b.png').not_to exist
    (expect Pathname.new fixture_file 'images/sequence-diagram-b.png.cache').not_to exist
  end

  it 'should be able to control display size of image using pdfwidth attribute on diagram block' do
    require 'asciidoctor-diagram'
    pdf = to_pdf <<~END, safe: :unsafe, attributes: { 'docdir' => fixtures_dir, 'outdir' => output_dir, 'imagesdir' => 'images' }, analyze: :image

    [plantuml,pdfwidth-test,png,pdfwidth=1in]
    ....
    scale 4
    start
    :Work;
    end
    ....
    END

    (expect Pathname.new output_file 'images/pdfwidth-test.png').to exist
    images = pdf.images
    (expect images).to have_size 1
    (expect images[0][:intrinsic_width].to_f).to eql 288.0
    (expect images[0][:width].to_f).to eql 72.0
  end

  it 'should allow font family used for diagram to be remapped' do
    require 'asciidoctor-diagram'
    with_tmp_file '.cfg', contents: %(skinparam defaultFontName M+ 1p Fallback\n) do |tmp_file|
      Dir.chdir File.dirname tmp_file do
        pdf = to_pdf <<~END, attributes: { 'imagesdir' => '' }, analyze: true
        :pdf-theme: default-with-font-fallbacks
        :plantumlconfig: #{File.basename tmp_file}

        [plantuml,font-test,svg]
        ....
        card カード
        ....
        END

        text = pdf.text[0]
        (expect text[:string]).to eql 'カード'
        (expect text[:font_name]).to eql 'mplus-1p-regular'
        (expect (File.file? 'font-test.svg')).to be true
        (expect (File.directory? '.asciidoctor')).to be true
      end
    end
  end

  it 'should not crash when both Asciidoctor Diagram and pdfmark are active' do
    require 'asciidoctor-diagram'
    input_file = Pathname.new fixture_file 'diagrams.adoc'
    pdfmark_file = Pathname.new output_file 'diagrams.pdfmark'
    pdf = to_pdf input_file, safe: :unsafe, attributes: { 'pdfmark' => '', 'sequence-diagram-name' => 'sequence-diagram-c' }, analyze: :image
    (expect pdf.images).to have_size 1
    (expect pdfmark_file).to exist
  end
end)

describe 'Asciidoctor Kroki Integration', if: (RSpec::ExampleGroupHelpers.gem_available? 'asciidoctor-kroki'), &(proc do
  # NOTE: asciidoctor-kroki not honoring :to_dir option; see https://github.com/Mogztter/asciidoctor-kroki/issues/371
  it 'should locate generated diagram in output directory' do
    require 'asciidoctor-kroki'
    input_file = Pathname.new fixture_file 'diagrams.adoc'
    attributes = {
      'sequence-diagram-name' => 'sequence-diagram-d',
      'kroki-fetch-diagram' => '',
      # imagesdir and imagesoutdir required until fixes are applied to Asciidoctor Kroki
      'imagesdir' => output_dir,
      'imagesoutdir' => output_dir,
    }
    pdf = to_pdf input_file, safe: :unsafe, attributes: attributes, analyze: :image
    (expect pdf.images).to have_size 1
    (expect pdf.images[0][:data].length).to be > 5000
    diagram_file = Dir[File.join output_dir, '*.png'][0]
    (expect diagram_file).not_to be_nil
  end

  # NOTE: asciidoctor-kroki not honoring :to_dir option; see https://github.com/Mogztter/asciidoctor-kroki/issues/371
  it 'should overwrite generated diagram file on subsequent invocations' do
    require 'asciidoctor-kroki'
    input_file = Pathname.new fixture_file 'diagrams.adoc'
    attributes = {
      'sequence-diagram-name' => 'sequence-diagram-e',
      'kroki-fetch-diagram' => '',
      # imagesdir and imagesoutdir required until fixes are applied to Asciidoctor Kroki
      'imagesdir' => output_dir,
      'imagesoutdir' => output_dir,
    }
    2.times do
      pdf = to_pdf input_file, safe: :unsafe, attributes: attributes, analyze: :image
      (expect pdf.images).to have_size 1
      (expect pdf.images[0][:data].length).to be > 5000
      diagram_file = Dir[File.join output_dir, '*.png'][0]
      (expect diagram_file).not_to be_nil
    end
  end
end)