summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMarat Radchenko <marat@slonopotamus.org>2020-02-01 20:34:37 +0300
committerGitHub <noreply@github.com>2020-02-01 20:34:37 +0300
commit7d4bccdd91a3db484d49c582738cb287c8657809 (patch)
tree5b2b3ea3e47e4a201303ff0a940059f4fecf3e5d /spec
parent927196f0bc33fd4300c09482e2eedc1752cc3699 (diff)
introduce to_epub test helper to reduce boilerplate code in tests (PR #287)
Diffstat (limited to 'spec')
-rw-r--r--spec/converter_spec.rb62
-rw-r--r--spec/fixtures/deep-include/chapter/chapter.adoc1
-rw-r--r--spec/fixtures/inter-chapter-xref-to-subsection/book.adoc8
-rw-r--r--spec/fixtures/inter-chapter-xref-to-subsection/chapter-a.adoc6
-rw-r--r--spec/fixtures/inter-chapter-xref-to-subsection/chapter-b.adoc5
-rw-r--r--spec/fixtures/inter-chapter-xref/book.adoc8
-rw-r--r--spec/fixtures/inter-chapter-xref/chapter-a.adoc6
-rw-r--r--spec/fixtures/inter-chapter-xref/chapter-b.adoc3
-rw-r--r--spec/spec_helper.rb28
-rw-r--r--spec/xref_spec.rb93
10 files changed, 85 insertions, 135 deletions
diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb
index a832066..4890a51 100644
--- a/spec/converter_spec.rb
+++ b/spec/converter_spec.rb
@@ -5,73 +5,31 @@ require_relative 'spec_helper'
describe Asciidoctor::Epub3::Converter do
describe '#convert' do
it 'converts empty file to epub without exceptions' do
- in_file = fixture_file 'empty.adoc'
- out_file = temp_file 'empty.epub'
- Asciidoctor.convert_file in_file,
- to_file: out_file,
- backend: 'epub3',
- header_footer: true,
- mkdirs: true
- expect(File).to exist(out_file)
+ to_epub 'empty.adoc'
end
it 'converts empty file to mobi without exceptions' do
- skip_if_darwin
-
- in_file = fixture_file 'empty.adoc'
- out_file = temp_file 'empty.mobi'
- Asciidoctor.convert_file in_file,
- to_file: out_file,
- backend: 'epub3',
- header_footer: true,
- mkdirs: true,
- attributes: { 'ebook-format' => 'mobi' }
- expect(File).to exist(out_file)
+ to_mobi 'empty.adoc'
end
it 'converts chapter with unicode title to unicode filename' do
- infile = fixture_file 'unicode/spine.adoc'
- outfile = temp_file 'unicode.epub'
- Asciidoctor.convert_file infile,
- to_file: outfile,
- backend: 'epub3',
- header_footer: true,
- mkdirs: true
- prev_zip_encoding = Zip.force_entry_names_encoding
- begin
- Zip.force_entry_names_encoding = 'UTF-8'
- Zip::File.open outfile do |zip|
- expect(zip.find_entry('OEBPS/test-é.xhtml')).not_to be_nil
- end
- ensure
- Zip.force_entry_names_encoding = prev_zip_encoding
+ _, out_file = to_epub 'unicode/spine.adoc'
+ Zip::File.open out_file do |zip|
+ expect(zip.find_entry('OEBPS/test-é.xhtml')).not_to be_nil
end
end
it 'uses current date as fallback when date attributes cannot be parsed' do
- in_file = fixture_file 'minimal/book.adoc'
- out_file = temp_file 'garbage.epub'
-
# TODO: assert that error log contains 'failed to parse revdate' error when we add test infrastructure for logs
- Asciidoctor.convert_file in_file,
- to_file: out_file,
- backend: 'epub3',
- header_footer: true,
- mkdirs: true,
- attributes: { 'revdate' => 'garbage' }
-
- book = GEPUB::Book.parse File.open(out_file)
+ book, = to_epub 'minimal/book.adoc', attributes: { 'revdate' => 'garbage' }
expect(book.metadata.date.content).not_to be_nil
end
it 'resolves deep includes relative to document that contains include directive' do
- book_file = fixture_file 'deep-include/book.adoc'
- spine_doc = Asciidoctor.load_file book_file, backend: 'epub3', header_footer: true, safe: Asciidoctor::SafeMode::UNSAFE
- spine_doc.convert
- spine_items = spine_doc.references[:spine_items]
- (expect spine_items).to have_size 1
- chapter_content = spine_items[0].content
- (expect chapter_content).to include '<p>Hello</p>'
+ book, = to_epub 'deep-include/book.adoc'
+ chapter = book.item_by_href '_chapter.xhtml'
+ expect(chapter).not_to be_nil
+ expect(chapter.content).to include '<p>Hello</p>'
end
end
end
diff --git a/spec/fixtures/deep-include/chapter/chapter.adoc b/spec/fixtures/deep-include/chapter/chapter.adoc
index fe5d519..a9c4e74 100644
--- a/spec/fixtures/deep-include/chapter/chapter.adoc
+++ b/spec/fixtures/deep-include/chapter/chapter.adoc
@@ -1,2 +1,3 @@
+= Chapter
include::subchapter/subchapter.adoc[]
diff --git a/spec/fixtures/inter-chapter-xref-to-subsection/book.adoc b/spec/fixtures/inter-chapter-xref-to-subsection/book.adoc
new file mode 100644
index 0000000..12af315
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref-to-subsection/book.adoc
@@ -0,0 +1,8 @@
+= Book Title
+:doctype: book
+:idprefix:
+:idseparator: -
+
+include::chapter-a.adoc[]
+
+include::chapter-b.adoc[]
diff --git a/spec/fixtures/inter-chapter-xref-to-subsection/chapter-a.adoc b/spec/fixtures/inter-chapter-xref-to-subsection/chapter-a.adoc
new file mode 100644
index 0000000..8ac5939
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref-to-subsection/chapter-a.adoc
@@ -0,0 +1,6 @@
+= Chapter A
+
+This is chapter A.
+There's not much too it.
+
+Time to move on to <<chapter-b#getting-started>>.
diff --git a/spec/fixtures/inter-chapter-xref-to-subsection/chapter-b.adoc b/spec/fixtures/inter-chapter-xref-to-subsection/chapter-b.adoc
new file mode 100644
index 0000000..fbc8a60
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref-to-subsection/chapter-b.adoc
@@ -0,0 +1,5 @@
+= Chapter B
+
+== Getting Started
+
+Now we can really get to it!
diff --git a/spec/fixtures/inter-chapter-xref/book.adoc b/spec/fixtures/inter-chapter-xref/book.adoc
new file mode 100644
index 0000000..12af315
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref/book.adoc
@@ -0,0 +1,8 @@
+= Book Title
+:doctype: book
+:idprefix:
+:idseparator: -
+
+include::chapter-a.adoc[]
+
+include::chapter-b.adoc[]
diff --git a/spec/fixtures/inter-chapter-xref/chapter-a.adoc b/spec/fixtures/inter-chapter-xref/chapter-a.adoc
new file mode 100644
index 0000000..738dbd2
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref/chapter-a.adoc
@@ -0,0 +1,6 @@
+= Chapter A
+
+This is chapter A.
+There's not much too it.
+
+Time to move on to <<chapter-b#>>.
diff --git a/spec/fixtures/inter-chapter-xref/chapter-b.adoc b/spec/fixtures/inter-chapter-xref/chapter-b.adoc
new file mode 100644
index 0000000..0312404
--- /dev/null
+++ b/spec/fixtures/inter-chapter-xref/chapter-b.adoc
@@ -0,0 +1,3 @@
+= Chapter B
+
+Not much to show here either.
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index e799af9..50e2547 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -2,6 +2,8 @@
require 'asciidoctor-epub3'
+Zip.force_entry_names_encoding = 'UTF-8'
+
RSpec.configure do |config|
config.before do
FileUtils.rm_r temp_dir, force: true, secure: true
@@ -75,6 +77,32 @@ RSpec.configure do |config|
# TODO: https://github.com/asciidoctor/asciidoctor-epub3/issues/236
skip '#236: Kindlegen is unavailable for-bit MacOS' if darwin_platform?
end
+
+ def convert input, opts = {}
+ input = fixture_file input if String === input
+ opts[:to_dir] = temp_dir unless opts.key? :to_dir
+ opts[:backend] = 'epub3'
+ opts[:header_footer] = true
+ opts[:mkdirs] = true
+ opts[:safe] = Asciidoctor::SafeMode::UNSAFE
+ Asciidoctor.convert_file input, opts
+ end
+
+ def to_epub input, opts = {}
+ doc = convert input, opts
+ output = Pathname.new doc.attr('outfile')
+ book = GEPUB::Book.parse output
+ [book, output]
+ end
+
+ def to_mobi input, opts = {}
+ skip_if_darwin
+ (opts[:attributes] ||= {})['ebook-format'] = 'mobi'
+ doc = convert input, opts
+ output = Pathname.new(doc.attr('outfile')).sub_ext '.mobi'
+ expect(output).to exist
+ output
+ end
end
RSpec::Matchers.define :have_size do |expected|
diff --git a/spec/xref_spec.rb b/spec/xref_spec.rb
index b5aa4e9..e96721c 100644
--- a/spec/xref_spec.rb
+++ b/spec/xref_spec.rb
@@ -1,94 +1,21 @@
# frozen_string_literal: true
require_relative 'spec_helper'
-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|
- FileUtils.mkdir_p temp_dir
- book_file = File.join temp_dir, 'book.adoc'
- chapter_a_file = File.join temp_dir, 'chapter-a.adoc'
- chapter_b_file = File.join temp_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
- # Only convert spine, not the whole book
- doc.convert
- spine_items = doc.references[:spine_items]
- (expect spine_items).to have_size 2
- # Convert chapter
- 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>'
+ it 'should resolve xref to top of chapter' do
+ book, = to_epub 'inter-chapter-xref/book.adoc'
+ chapter_a = book.item_by_href 'chapter-a.xhtml'
+ expect(chapter_a).not_to be_nil
+ expect(chapter_a.content).to include '<a id="xref--chapter-b" href="chapter-b.xhtml" class="xref">Chapter B</a>'
end
- it 'should resolve xref to section inside chapter' do |_example|
- FileUtils.mkdir_p temp_dir
- book_file = File.join temp_dir, 'book.adoc'
- chapter_a_file = File.join temp_dir, 'chapter-a.adoc'
- chapter_b_file = File.join temp_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
- # Only convert spine, not the whole book
- doc.convert
- spine_items = doc.references[:spine_items]
- (expect spine_items).to have_size 2
- # Convert chapter
- 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>'
+ it 'should resolve xref to section inside chapter' do
+ book, = to_epub 'inter-chapter-xref-to-subsection/book.adoc'
+ chapter_a = book.item_by_href 'chapter-a.xhtml'
+ expect(chapter_a).not_to be_nil
+ expect(chapter_a.content).to include '<a id="xref--chapter-b--getting-started" href="chapter-b.xhtml#getting-started" class="xref">Getting Started</a>'
end
end
end