summaryrefslogtreecommitdiff
path: root/spec/integration_spec.rb
blob: ede62edf0673f2d06c8b8f68773b5eac66e4ab97 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require_relative 'spec_helper'

require 'asciidoctor/interdoc_reftext/processor'
require 'asciidoctor'
require 'corefines'

using Corefines::String::unindent
using Corefines::Hash::except

FIXTURES_DIR = File.expand_path('fixtures', __dir__)


describe 'Intengration Tests' do

  subject(:output) { Asciidoctor.convert(input, options) }

  let(:input) { '' }  # this is modified in #given
  let(:processor) { Asciidoctor::InterdocReftext::Processor.new }

  let(:options) {
    processor_ = processor
    {
      safe: :safe,
      header_footer: false,
      base_dir: FIXTURES_DIR,
      extensions: proc { tree_processor processor_ },
    }
  }

  context 'document with valid inter-document xref without reftext' do

    context 'without fragment' do
      it 'renders title of the referenced document as reftext' do
        given 'xref:doc-a.adoc#[]'
        should have_anchor href: 'doc-a.html', text: 'Document A'
      end
    end

    context 'with fragment' do
      it 'renders title of the referenced section with implicit id as reftext' do
        given 'xref:doc-a.adoc#_first_section[]'
        should have_anchor href: 'doc-a.html#_first_section', text: 'First Section'
      end

      it 'renders title of the referenced section with explicit id as reftext' do
        given 'xref:doc-a.adoc#sec2[]'
        should have_anchor href: 'doc-a.html#sec2', text: 'Second Section'
      end

      it 'renders reftext of the referenced section with explicit reftext' do
        given 'xref:doc-a.adoc#_third_section[]'
        should have_anchor href: 'doc-a.html#_third_section', text: '3rd Section'
      end
    end

    context 'with relative path' do
      subject(:output) do
        opts = options.except(:base_dir)
        Asciidoctor.load_file("#{FIXTURES_DIR}/b/doc-b.adoc", opts).convert
      end

      it 'resolves path relative to the current document' do
        should have_anchor href: 'c/doc-c.html', text: 'Document C'
      end
    end

    # Issue #3
    context 'when xref is inside a nested document' do
      it 'renders title of the referenced document as reftext' do
        given <<~ADOC
          |===
          a| xref:doc-a.adoc#[]
          |===
        ADOC
        should have_anchor href: 'doc-a.html', text: 'Document A'
      end

      context 'with relative path' do
        subject(:output) do
          opts = options.except(:base_dir).merge(safe: :unsafe)
          Asciidoctor.load_file("#{FIXTURES_DIR}/b/doc-b.adoc", opts).convert
        end

        it 'resolves path relative to the current document' do
          should have_anchor href: '../doc-a.html', text: 'Document A'
        end
      end
    end

    context 'when extension is not active' do
      specify 'renders path of the referenced document as reftext' do
        given 'xref:doc-a.adoc#[]', extensions: []
        # Note: Asciidoctor 1.5.6 and 1.5.6.1 behaves differently.
        should have_anchor href: 'doc-a.html', text: /doc-a/
      end
    end
  end

  context 'document with invalid inter-document xref without reftext' do

    context 'without fragment' do
      it 'renders path of the non-existent document as reftext' do
        given 'xref:missing.adoc#[]'
        # Note: Asciidoctor 1.5.6 and 1.5.6.1 behaves differently.
        should have_anchor href: 'missing.html', text: /missing/
      end
    end

    context 'with non-existent fragment' do
      it 'renders path of the referenced document as reftext' do
        given 'xref:doc-a.adoc#missing[]'
        # Note: Asciidoctor 1.5.6 and 1.5.6.1 behaves differently.
        should have_anchor href: 'doc-a.html#missing', text: /doc-a/
      end
    end
  end

  context 'document with valid inter-document xref with reftext' do
    it 'renders provided reftext' do
      given 'xref:doc-a.adoc#[My Title]'
      should have_anchor href: 'doc-a.html', text: 'My Title'
    end
  end

  #----------  Helpers  ----------

  def given(str, opts = {})
    input.replace(str)
    options.merge!(opts)
  end

  def have_anchor(href: nil, text: nil)
    have_tag('a', with: { href: href }, text: text)
  end
end