require 'spec_helper' describe Softcover::Builders::Epub do before(:all) do generate_book @file_to_be_removed = path('html/should_be_removed.html') File.write(@file_to_be_removed, '') silence { `softcover build:epub` } @builder = Softcover::Builders::Epub.new @builder.build! end after(:all) { remove_book } subject(:builder) { @builder } it "should be valid" do output = `softcover epub:validate` expect(output).to match(/No errors or warnings/) end describe "mimetype file" do it "should exist in the right directory" do expect(path('epub/mimetype')).to exist end it "should have the right contents" do File.open('epub/mimetype') do |f| expect(f.read).to match(/application\/epub\+zip/) end end end describe "META-INF" do it "should have the right container file" do expect('epub/META-INF/container.xml').to exist end it "should have an iBooks XML file" do expect('epub/META-INF/com.apple.ibooks.display-options.xml').to exist end it "should have the right contents" do File.open('epub/META-INF/container.xml') do |f| expect(f.read).to match(/rootfile full-path="OEBPS\/content.opf"/) end end end describe "OEBPS" do describe "contents" do describe "opf file" do let(:content_opf) { 'epub/OEBPS/content.opf' } let(:doc) { Nokogiri.XML(File.read(content_opf)) } subject { content_opf } it { should exist } it "should have the chapter right items" do expect(doc.css('item#a_chapter')).not_to be_empty expect(doc.css('item#another_chapter')).not_to be_empty end it "should have the right TOC chapter refs" do toc_refs = doc.css('itemref').map { |node| node['idref'] } expect(toc_refs).to eq %w[cover frontmatter a_chapter another_chapter yet_another_chapter] end it "should have the right title" do expect(doc.to_xml).to match(/>#{builder.manifest.title}#{author}#{copyright} #{author}' expect(doc.to_xml).to include meta end end context "stylesheets directory" do it "should have a Pygments CSS file" do expect('epub/OEBPS/styles/pygments.css').to exist end it "should have a page template file" do expect('epub/OEBPS/styles/page-template.xpgt').to exist end it "should have a PolyTeXnic CSS file" do expect('epub/OEBPS/styles/softcover.css').to exist end it "should have an EPUB CSS file" do expect('epub/OEBPS/styles/epub.css').to exist end end end describe "spine toc" do subject(:toc) { 'epub/OEBPS/toc.ncx' } let(:doc) { Nokogiri::XML(File.read(toc)) } it { should exist } it "should contain the right filenames in the right order" do filenames = %w[frontmatter_fragment.html a_chapter_fragment.html another_chapter_fragment.html yet_another_chapter_fragment.html] source_files = doc.css('content').map { |node| node['src'] } expect(source_files).to eql(filenames) end it "should have the right title" do expect(doc.to_xml).to match(/>#{builder.manifest.title}