summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2020-01-18 16:38:38 -0700
committerGitHub <noreply@github.com>2020-01-18 16:38:38 -0700
commita959078c2e65522512732d9a5476f8f4156a517b (patch)
tree1f987b4bc1904812749b4b4eca8575607d169457 /spec
parentcead09c881199388bca86dfa30df43ca2c83a175 (diff)
resolves #210 fix deep interdoc xref when using Asciidoctor 2 (PR #221)
Diffstat (limited to 'spec')
-rw-r--r--spec/spec_helper.rb10
-rw-r--r--spec/xref_spec.rb101
2 files changed, 110 insertions, 1 deletions
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a179659..f8f8744 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,2 +1,10 @@
-require 'rspec'
require 'asciidoctor-epub3'
+
+RSpec.configure do |config|
+ # configure rspec here
+end
+
+RSpec::Matchers.define :have_size do |expected|
+ match {|actual| actual.size == expected }
+ failure_message {|actual| %(expected #{actual} to have size #{expected}, but was #{actual.size}) }
+end
diff --git a/spec/xref_spec.rb b/spec/xref_spec.rb
new file mode 100644
index 0000000..16235ed
--- /dev/null
+++ b/spec/xref_spec.rb
@@ -0,0 +1,101 @@
+require_relative 'spec_helper'
+require 'digest'
+require 'fileutils' unless defined? FileUtils
+
+describe 'Asciidoctor::Epub3::Converter - Xref' do
+ context 'inter-chapter' do
+ it 'should resolve xref to top of chapter' do |example|
+ signature = Digest::SHA1.hexdigest example.full_description
+ book_dir = File.join __dir__, %(book-#{signature})
+ begin
+ Dir.mkdir book_dir
+ book_file = File.join book_dir, 'book.adoc'
+ chapter_a_file = File.join book_dir, 'chapter-a.adoc'
+ chapter_b_file = File.join book_dir, 'chapter-b.adoc'
+
+ File.write chapter_a_file, <<~'EOS', encoding: 'UTF-8'
+ = Chapter A
+
+ This is chapter A.
+ There's not much too it.
+
+ Time to move on to <<chapter-b#>>.
+ EOS
+
+ File.write chapter_b_file, <<~'EOS', encoding: 'UTF-8'
+ = Chapter B
+
+ Not much to show here either.
+ EOS
+
+ File.write book_file, <<~'EOS', encoding: 'UTF-8'
+ = Book Title
+ :doctype: book
+ :idprefix:
+ :idseparator: -
+
+ include::chapter-a.adoc[]
+
+ include::chapter-b.adoc[]
+ EOS
+
+ doc = Asciidoctor.load_file book_file, backend: 'epub3', header_footer: true
+ doc.convert
+ spine_items = doc.references[:spine_items]
+ (expect spine_items).to have_size 2
+ chapter_a_content = doc.references[:spine_items][0].content
+ (expect chapter_a_content).to include '<a id="xref--chapter-b" href="chapter-b.xhtml" class="xref">Chapter B</a>'
+ ensure
+ FileUtils.rm_r book_dir, force: true, secure: true
+ end
+ end
+
+ it 'should resolve xref to section inside chapter' do |example|
+ signature = Digest::SHA1.hexdigest example.full_description
+ book_dir = File.join __dir__, %(book-#{signature})
+ begin
+ Dir.mkdir book_dir
+ book_file = File.join book_dir, 'book.adoc'
+ chapter_a_file = File.join book_dir, 'chapter-a.adoc'
+ chapter_b_file = File.join book_dir, 'chapter-b.adoc'
+
+ File.write chapter_a_file, <<~'EOS', encoding: 'UTF-8'
+ = Chapter A
+
+ This is chapter A.
+ There's not much too it.
+
+ Time to move on to <<chapter-b#getting-started>>.
+ EOS
+
+ File.write chapter_b_file, <<~'EOS', encoding: 'UTF-8'
+ = Chapter B
+
+ == Getting Started
+
+ Now we can really get to it!
+ EOS
+
+ File.write book_file, <<~'EOS', encoding: 'UTF-8'
+ = Book Title
+ :doctype: book
+ :idprefix:
+ :idseparator: -
+
+ include::chapter-a.adoc[]
+
+ include::chapter-b.adoc[]
+ EOS
+
+ doc = Asciidoctor.load_file book_file, backend: 'epub3', header_footer: true
+ doc.convert
+ spine_items = doc.references[:spine_items]
+ (expect spine_items).to have_size 2
+ chapter_a_content = doc.references[:spine_items][0].content
+ (expect chapter_a_content).to include '<a id="xref--chapter-b--getting-started" href="chapter-b.xhtml#getting-started" class="xref">Getting Started</a>'
+ ensure
+ FileUtils.rm_r book_dir, force: true, secure: true
+ end
+ end
+ end
+end