require 'test_helper' require 'epubmaker' require 'review/epubmaker' class EPUBMakerTest < Test::Unit::TestCase include EPUBMaker def setup @producer = Producer.new @producer.merge_config( 'bookname' => 'sample', 'title' => 'Sample Book', 'epubversion' => 2, 'urnid' => 'http://example.jp/', 'date' => '2011-01-01', 'language' => 'en', 'titlepage' => nil ) @output = StringIO.new end def test_initialize assert Producer.new end def test_resource_en @producer.merge_config('language' => 'en') assert_equal 'Table of Contents', @producer.res.v('toctitle') end def test_resource_ja @producer.merge_config('language' => 'ja') assert_equal '目次', @producer.res.v('toctitle') end def test_mimetype @producer.mimetype(@output) assert_equal 'application/epub+zip', @output.string end def test_container @producer.container(@output) expect = < EOT assert_equal expect, @output.string end def test_stage1_opf @producer.opf(@output) expect = < Sample Book en 2011-01-01 http://example.jp/ EOT assert_equal expect, @output.string end def test_stage1_opf_escape @producer.config['title'] = 'Sample<>Book' @producer.opf(@output) expect = < Sample<>Book en 2011-01-01 http://example.jp/ EOT assert_equal expect, @output.string end def test_stage1_ncx @producer.ncx(@output) expect = < Sample Book Sample Book EOT assert_equal expect, @output.string end def test_stage1_ncx_escape @producer.config['title'] = 'Sample<>Book' @producer.ncx(@output) expect = < Sample<>Book Sample<>Book EOT assert_equal expect, @output.string end def stage2 # add one item @producer.contents << Content.new({ 'file' => 'ch01.html', 'title' => 'CH01', 'level' => 1 }) end def test_stage2_add_l1item stage2 expect = EPUBMaker::Content.new('ch01.html', 'ch01-html', 'application/xhtml+xml', 'CH01', 1) assert_equal expect, @producer.contents[0] end def test_stage2_opf stage2 @producer.opf(@output) expect = < Sample Book en 2011-01-01 http://example.jp/ EOT assert_equal expect, @output.string end def test_stage2_ncx stage2 @producer.ncx(@output) expect = < Sample Book Sample Book CH01 EOT assert_equal expect, @output.string end def stage3 # add more items @producer.contents << Content.new({ 'file' => 'ch01.html', 'title' => %Q(CH01<>&"), 'level' => 1 }) @producer.contents << Content.new({ 'file' => 'ch02.html', 'title' => 'CH02', 'level' => 1 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S1', 'title' => 'CH02.1', 'level' => 2 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S1.1', 'title' => 'CH02.1.1', 'level' => 3 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.1', 'title' => 'CH02.1.1.1', 'level' => 4 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.1.1', 'title' => 'CH02.1.1.1.1', 'level' => 5 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.2', 'title' => 'CH02.1.1.2', 'level' => 4 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S2', 'title' => 'CH02.2', 'level' => 2 }) @producer.contents << Content.new({ 'file' => 'ch02.html#S2.1', 'title' => 'CH02.2.1', 'level' => 3 }) @producer.contents << Content.new({ 'file' => 'ch03.html', 'title' => 'CH03', 'level' => 1 }) @producer.contents << Content.new({ 'file' => 'ch03.html#S1', 'title' => 'CH03.1', 'level' => 2 }) @producer.contents << Content.new({ 'file' => 'ch03.html#S1.1', 'title' => 'CH03.1.1', 'level' => 3 }) @producer.contents << Content.new({ 'file' => 'ch04.html', 'title' => 'CH04', 'level' => 1 }) @producer.contents << Content.new({ 'file' => 'sample.png' }) @producer.contents << Content.new({ 'file' => 'sample.jpg' }) @producer.contents << Content.new({ 'file' => 'sample.JPEG' }) @producer.contents << Content.new({ 'file' => 'sample.SvG' }) @producer.contents << Content.new({ 'file' => 'sample.GIF' }) @producer.contents << Content.new({ 'file' => 'sample.css' }) end def test_stage3_add_various_items stage3 expect = [ Content.new('ch01.html', 'ch01-html', 'application/xhtml+xml', %Q(CH01<>&"), 1), Content.new('ch02.html', 'ch02-html', 'application/xhtml+xml', 'CH02', 1), Content.new('ch02.html#S1', 'ch02-html#S1', 'html#s1', 'CH02.1', 2), Content.new('ch02.html#S1.1', 'ch02-html#S1-1', '1', 'CH02.1.1', 3), Content.new('ch02.html#S1.1.1', 'ch02-html#S1-1-1', '1', 'CH02.1.1.1', 4), Content.new('ch02.html#S1.1.1.1', 'ch02-html#S1-1-1-1', '1', 'CH02.1.1.1.1', 5), Content.new('ch02.html#S1.1.2', 'ch02-html#S1-1-2', '2', 'CH02.1.1.2', 4), Content.new('ch02.html#S2', 'ch02-html#S2', 'html#s2', 'CH02.2', 2), Content.new('ch02.html#S2.1', 'ch02-html#S2-1', '1', 'CH02.2.1', 3), Content.new('ch03.html', 'ch03-html', 'application/xhtml+xml', 'CH03', 1), Content.new('ch03.html#S1', 'ch03-html#S1', 'html#s1', 'CH03.1', 2), Content.new('ch03.html#S1.1', 'ch03-html#S1-1', '1', 'CH03.1.1', 3), Content.new('ch04.html', 'ch04-html', 'application/xhtml+xml', 'CH04', 1), Content.new('sample.png', 'sample-png', 'image/png'), Content.new('sample.jpg', 'sample-jpg', 'image/jpeg'), Content.new('sample.JPEG', 'sample-JPEG', 'image/jpeg'), Content.new('sample.SvG', 'sample-SvG', 'image/svg+xml'), Content.new('sample.GIF', 'sample-GIF', 'image/gif'), Content.new('sample.css', 'sample-css', 'text/css') ] assert_equal expect, @producer.contents end def test_stage3_opf stage3 @producer.opf(@output) expect = < Sample Book en 2011-01-01 http://example.jp/ EOT assert_equal expect, @output.string end def test_stage3_ncx stage3 @producer.ncx(@output) expect = < Sample Book Sample Book CH01<>&" CH02 CH02.1 CH02.1.1 CH02.1.1.1 CH02.1.1.1.1 CH02.1.1.2 CH02.2 CH02.2.1 CH03 CH03.1 CH03.1.1 CH04 EOT assert_equal expect, @output.string end def test_stage3_mytoc stage3 @producer.mytoc(@output) expect = < Table of Contents

Table of Contents

EOT assert_equal expect, @output.string end def test_stage3_flat @producer.merge_config('epubmaker' => { 'flattoc' => true, 'flattocindent' => false }) stage3 @producer.mytoc(@output) expect = < Table of Contents

Table of Contents

EOT assert_equal expect, @output.string end def test_stage3_cover stage3 @producer.cover(@output) expect = < Sample Book

Sample Book

EOT assert_equal expect, @output.string end def test_stage3_cover_escape stage3 @producer.config['title'] = 'Sample<>Book' @producer.cover(@output) expect = < Sample<>Book

Sample<>Book

EOT assert_equal expect, @output.string end def test_stage3_cover_with_image stage3 @producer.config['coverimage'] = 'sample.png' @producer.cover(@output) expect = < Sample Book
Sample Book
EOT assert_equal expect, @output.string end def test_stage3_cover_with_image_escape stage3 @producer.config['title'] = 'Sample<>Book' @producer.config['coverimage'] = 'sample.png' @producer.cover(@output) expect = < Sample<>Book
Sample<>Book
EOT assert_equal expect, @output.string end def test_colophon_default @producer.config['aut'] = ['Mr.Smith'] @producer.config['pbl'] = ['BLUEPRINT'] @producer.config['isbn'] = '9784797372274' @producer.colophon(@output) expect = < Colophon

Sample Book

published by Jan. 1, 2011

AuthorMr.Smith
PublisherBLUEPRINT
ISBN978-4-79737-227-4
EOT assert_equal expect, @output.string end def test_colophon_default_escape_and_multiple @producer.config['title'] = '<&Sample Book>' @producer.config['subtitle'] = 'Sample<>Subtitle' @producer.config['aut'] = ['Mr.Smith', 'Mr.&Anderson'] @producer.config['pbl'] = ['BLUEPRINT', 'COPY<>EDIT'] @producer.config['isbn'] = '9784797372274' @producer.config['rights'] = ['COPYRIGHT 2016 <>', '& REVIEW'] @producer.colophon(@output) expect = < Colophon

<&Sample Book>
Sample<>Subtitle

published by Jan. 1, 2011

AuthorMr.Smith, Mr.&Anderson
PublisherBLUEPRINT, COPY<>EDIT
ISBN978-4-79737-227-4
EOT assert_equal expect, @output.string end def test_colophon_history @producer.config['aut'] = ['Mr.Smith'] @producer.config['pbl'] = ['BLUEPRINT'] @producer.config['pht'] = ['Mrs.Smith'] @producer.merge_config('language' => 'ja') @producer.config['history'] = [['2011-08-03', '2012-02-15'], ['2012-10-01'], ['2013-03-01']] epub = @producer.instance_eval { @epub } result = epub.colophon_history expect = <<-EOT

2011年8月3日 初版第1刷 発行

2012年2月15日 初版第2刷 発行

2012年10月1日 第2版第1刷 発行

2013年3月1日 第3版第1刷 発行

EOT assert_equal expect, result end def test_colophon_history_freeformat @producer.config['aut'] = ['Mr.Smith'] @producer.config['pbl'] = ['BLUEPRINT'] @producer.config['pht'] = ['Mrs.Smith'] @producer.merge_config('language' => 'ja') @producer.config['history'] = [['2011年8月3日 ver 1.1.0発行'], ['2011年10月12日 ver 1.2.0発行'], ['2012年1月31日 ver 1.2.1発行']] epub = @producer.instance_eval { @epub } result = epub.colophon_history expect = <<-EOT

2011年8月3日 ver 1.1.0発行

2011年10月12日 ver 1.2.0発行

2012年1月31日 ver 1.2.1発行

EOT assert_equal expect, result end def test_colophon_pht @producer.config['aut'] = ['Mr.Smith'] @producer.config['pbl'] = ['BLUEPRINT'] @producer.config['pht'] = ['Mrs.Smith'] @producer.colophon(@output) expect = < Colophon

Sample Book

published by Jan. 1, 2011

AuthorMr.Smith
PublisherBLUEPRINT
Director of PhotographyMrs.Smith
EOT assert_equal expect, @output.string end def test_isbn13 @producer.config['isbn'] = '9784797372274' assert_equal '978-4-79737-227-4', @producer.isbn_hyphen end def test_isbn10 @producer.config['isbn'] = '4797372273' assert_equal '4-79737-227-3', @producer.isbn_hyphen end def test_isbn_nil @producer.config['isbn'] = nil assert_equal nil, @producer.isbn_hyphen end def test_title @producer.config['aut'] = ['Mr.Smith'] @producer.config['pbl'] = ['BLUEPRINT'] @producer.titlepage(@output) expect = < Sample Book

Sample Book



Mr.Smith





BLUEPRINT

EOT assert_equal expect, @output.string end def test_title_single_value_param @producer.config['aut'] = 'Mr.Smith' @producer.config['pbl'] = 'BLUEPRINT' @producer.titlepage(@output) expect = < Sample Book

Sample Book



Mr.Smith





BLUEPRINT

EOT assert_equal expect, @output.string end def test_epub_unsafe_id content = Content.new({ 'file' => 'sample.png' }) assert_equal 'sample-png', content.id content = Content.new({ 'file' => 'sample-&()-=+@:,漢字.png' }) assert_equal 'sample-_25_26_25_28_25_29-_25_3D_25_2B_25_40_25_3A_25_2C_25_E6_25_BC_25_A2_25_E5_25_AD_25_97-png', content.id end end