summaryrefslogtreecommitdiff
path: root/spec/integration_spec.rb
blob: 883bbb2d8eda4d3c709fd4ff2f68038354aefcff (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
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#[]'
        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#_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#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#_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

    context 'when extension is not active' do
      specify 'renders path of the referenced document as reftext' do
        given 'xref:doc-a#[]', extensions: []
        should have_anchor href: 'doc-a.html', text: 'doc-a.html'
      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#[]'
        should have_anchor href: 'missing.html', text: 'missing.html'
      end
    end

    context 'with non-existent fragment' do
      it 'renders path of the referenced document as reftext' do
        given 'xref:doc-a#missing[]'
        should have_anchor href: 'doc-a.html#missing', text: 'doc-a.html'
      end
    end
  end

  context 'document with valid inter-document xref with reftext' do
    it 'renders provided reftext' do
      given 'xref:doc-a#[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