Skip to content
This repository has been archived by the owner on Dec 28, 2017. It is now read-only.

Commit

Permalink
EpubCheck now uses class methods - specs also update for this
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cook committed Nov 1, 2011
1 parent 846b94e commit fce087d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
16 changes: 8 additions & 8 deletions lib/epub_validator/epub_check.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module EpubValidator
class EpubCheck
attr_accessor :message

def initialize(filename)
@filename = filename
@message = format_epubcheck_message(process_epub)
def self.process(filename)
checked = epubcheck(filename)
format_epubcheck_message(checked)
end

def process_epub
private

def self.epubcheck(epubfile)
epubcheck_jar = File.expand_path(File.dirname(__FILE__) + '/../epubcheck-1-2/epubcheck-1.2.jar')
epubcheck = `java -jar #{epubcheck_jar} "#{@filename}" 2>&1`
`java -jar #{epubcheck_jar} "#{epubfile}" 2>&1`
end

def format_epubcheck_message(message)
def self.format_epubcheck_message(message)
return ['Passed.'] if message.match('No errors or warnings detected')

m_array = message.split(/\n/)
Expand Down
32 changes: 15 additions & 17 deletions spec/epub_validator/epub_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@

module EpubValidator
describe EpubCheck do
context "when initialized" do
it "will return an instance of EpubCheck" do
EpubCheck.new('test.epub').should be_kind_of(EpubCheck)
end
end
context "when it tries to process a missing file" do
it "should return missing file error message" do
failed_message = 'FAILED!\n\nNo .epub file to check was specified in arguments!\n\nThe tool will EXIT!'
ce = EpubCheck.new('test.epub')
EpubCheck.any_instance.stub(:process_epub).and_return(failed_message)
ce.process_epub.should eq(failed_message)
it "should return a formatted I/O error message" do
message = "ERROR: test.epub: I/O error: test.epub (No such file or directory)"
formatted_message = ["FAILED!", "ERROR: test.epub: I/O error: test.epub (No such file or directory)"]
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(formatted_message)
end
end
context "when it processes a valid file" do
it "should return 'Passed.' message as an array" do
success_message = "Epubcheck Version 1.2\n\nNo errors or warnings detected\n"
ce = EpubCheck.new('test.epub')
ce.format_epubcheck_message(success_message).should eq(['Passed.'])
message = "Epubcheck Version 1.2\n\nNo errors or warnings detected\n"
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(['Passed.'])
end
end
context "when it processes an invalid file" do
it "should return error message as an array" do
failed_message = "Epubcheck Version 1.2\n\nERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing\n\nCheck finished with warnings or errors!"
formatted_failed_message = ["FAILED!", "ERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing"]
ce = EpubCheck.new('test.epub')
ce.format_epubcheck_message(failed_message).should eq(formatted_failed_message)
message = "Epubcheck Version 1.2\n\nERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing\n\nCheck finished with warnings or errors!"
formatted_message = ["FAILED!", "ERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing"]
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(formatted_message)
end
end
end
Expand Down

0 comments on commit fce087d

Please sign in to comment.