class BookTest

Public Instance Methods

assert_same_path(expected, result, *options) click to toggle source
# File ../../../../../test/test_book.rb, line 6
def assert_same_path(expected, result, *options)
  require 'pathname'
  ex_path = Pathname(expected).realpath
  re_path = Pathname(result).realpath
  assert_equal ex_path, re_path, *options
end
test_appendix() click to toggle source
# File ../../../../../test/test_book.rb, line 343
def test_appendix
  mktmpbookdir do |dir, book, files|
    assert_equal nil, book.appendix
  end

  mktmpbookdir 'POSTDEF' => '' do |dir, book, files|
    assert_equal nil, book.appendix
  end

  mktmpbookdir 'POSTDEF' => 'chapter1',
               'chapter1.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.appendix
    assert_equal '', book.appendix.name
    assert_equal 1, book.appendix.chapters.size
    assert_equal "chapter1", book.appendix.chapters.first.name
    assert_equal files['chapter1.re'], book.appendix.chapters.first.path
    assert_equal 1, book.appendix.chapters.first.number
  end

  mktmpbookdir 'POSTDEF' => "chapter1\n\nchapter2",
               'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.appendix
    assert_equal '', book.appendix.name
    assert_equal 2, book.appendix.chapters.size
    assert_equal "chapter1", book.appendix.chapters.first.name
    assert_equal files['chapter1.re'], book.appendix.chapters.first.path
    assert_equal "chapter2", book.appendix.chapters.last.name
    assert_equal files['chapter2.re'], book.appendix.chapters.last.path
    assert_equal 1, book.appendix.chapters.first.number
    assert_equal 2, book.appendix.chapters.last.number
  end

  mktmpbookdir 'POSTDEF' => "chapter1 chapter2",
               'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.appendix
    assert_equal '', book.appendix.name
    assert_equal 2, book.appendix.chapters.size # XXX: OK?
    assert_equal 1, book.appendix.chapters.first.number
    assert_equal 2, book.appendix.chapters.last.number
  end

  mktmpbookdir 'POSTDEF' => 'not_exist' do |dir, book, files|
    assert_raises FileNotFound do
      assert_equal nil, book.appendix
    end
  end

  mktmpbookdir 'catalog.yml' => "APPENDIX:\n  - p01.re",
               'p01.re' => '= appendix' do |dir, book, files|
    assert_equal 'appendix', book.appendix.chapters.first.title
    assert_equal 1, book.appendix.chapters.first.number
  end
end
test_basedir() click to toggle source
# File ../../../../../test/test_book.rb, line 519
def test_basedir
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert_equal dir, book.basedir
  end
end
test_chapters() click to toggle source
# File ../../../../../test/test_book.rb, line 432
def test_chapters
  mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3" do |dir, book, files|
    chapters = book.chapters
    assert_equal 3, chapters.size

    ch_names = %w(ch1 ch2 ch3)
    tmp = []
    book.each_chapter {|ch| tmp << ch.name }
    assert_equal ch_names, tmp

    ch_names.each do |name|
      assert book.chapter(name)
      assert_equal name, book.chapter(name).name
    end

    assert_raises ReVIEW::KeyError do
      book.chapter('not exist')
    end
  end

  mktmpbookdir 'CHAPS' => "ch1.txt\nch2.txt\n\nch3.txt" do |dir, book, files|
    chapters = book.chapters
    assert_equal 3, chapters.size

    ch_names = %w(ch1 ch2 ch3)
    tmp = []
    book.each_chapter {|ch| tmp << ch.name }
    assert_equal ch_names, tmp

    ch_names.each do |name|
      assert book.chapter(name)
      assert_equal name, book.chapter(name).name
    end

    assert_raises ReVIEW::KeyError do
      book.chapter('not exist')
    end
  end
end
test_ext() click to toggle source
# File ../../../../../test/test_book.rb, line 43
def test_ext
  book = Book::Base.new(File.dirname(__FILE__))
  assert_equal '.re', book.ext
end
test_next_chapter() click to toggle source
# File ../../../../../test/test_book.rb, line 472
def test_next_chapter
  mktmpbookdir 'CHAPS' => "ch1\nch2" do |dir, book, files|
    chapter = book.chapter('ch1')
    assert_equal book.chapter('ch2'), book.next_chapter(chapter)

    chapter = book.chapter('ch2')
    assert_equal nil, book.next_chapter(chapter)
  end
end
test_page_metric() click to toggle source
# File ../../../../../test/test_book.rb, line 526
def test_page_metric
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert_equal ReVIEW::Book::PageMetric::A5, book.page_metric
  end
end
test_page_metric_config() click to toggle source
# File ../../../../../test/test_book.rb, line 533
def test_page_metric_config
  mktmpbookdir('config.yml'=>"bookname: book\npage_metric: B5\n") do |dir, book, files|
    book = Book::Base.new(dir)
    config_file = File.join(dir,"config.yml")
    book.load_config(config_file)
    assert_equal ReVIEW::Book::PageMetric::B5, book.page_metric
  end
end
test_page_metric_config_array() click to toggle source
# File ../../../../../test/test_book.rb, line 542
def test_page_metric_config_array
  mktmpbookdir('config.yml'=>"bookname: book\npage_metric: [46, 80, 30, 74, 2]\n") do |dir, book, files|
    book = Book::Base.new(dir)
    config_file = File.join(dir,"config.yml")
    book.load_config(config_file)
    assert_equal ReVIEW::Book::PageMetric::B5, book.page_metric
  end
end
test_parse_chapters() click to toggle source
# File ../../../../../test/test_book.rb, line 169
  def test_parse_chapters
    mktmpbookdir 'CHAPS' => '' do |dir, book, files|
      parts = book.instance_eval { parse_chapters }
      assert_equal 0, parts.size
    end

    mktmpbookdir 'CHAPS' => "chapter1.re\nchapter2\n" do |dir, book, files|
      parts = book.instance_eval { parse_chapters }
      assert_equal 1, parts.size

      assert_equal nil, parts[0].number
      assert_equal 2, parts[0].chapters.size
      chaps = parts[0].chapters.map {|ch| [ch.number, ch.name, ch.path] }
      expect = [
        [1, 'chapter1', File.join(dir, 'chapter1.re')],
        [2, 'chapter2', File.join(dir, 'chapter2')],
      ]
      assert_equal expect, chaps
    end

    mktmpbookdir 'CHAPS' => "part1_chapter1.re
part1_chapter2.re


part2_chapter1.re
part2_chapter2.re
part2_chapter3.re

part3_chapter1.re
" do |dir, book, files|
      parts = book.instance_eval { parse_chapters }
      assert_equal 3, parts.size

      assert_equal nil, parts[0].number
      assert_equal 2, parts[0].chapters.size
      chaps = parts[0].chapters.map {|ch| [ch.number, ch.name, ch.path] }
      expect = [
        [1, 'part1_chapter1', File.join(dir, 'part1_chapter1.re')],
        [2, 'part1_chapter2', File.join(dir, 'part1_chapter2.re')],
      ]
      assert_equal expect, chaps

      assert_equal nil, parts[1].number
      assert_equal 3, parts[1].chapters.size
      chaps = parts[1].chapters.map {|ch| [ch.number, ch.name, ch.path] }
      expect = [
        [3, 'part2_chapter1', File.join(dir, 'part2_chapter1.re')],
        [4, 'part2_chapter2', File.join(dir, 'part2_chapter2.re')],
        [5, 'part2_chapter3', File.join(dir, 'part2_chapter3.re')],
      ]
      assert_equal expect, chaps

      assert_equal nil, parts[2].number
      assert_equal 1, parts[2].chapters.size
      chaps = parts[2].chapters.map {|ch| [ch.number, ch.name, ch.path] }
      expect = [
        [6, 'part3_chapter1', File.join(dir, 'part3_chapter1.re')],
      ]
      assert_equal expect, chaps
    end
  end
test_parse_chpaters_with_parts_file() click to toggle source
# File ../../../../../test/test_book.rb, line 232
def test_parse_chpaters_with_parts_file
  n_test = 0
  [
    [
      # 期待されるパートの数, :chapter_fileの内容, :part_fileの内容, 期待されるパートタイトルのリスト
      2,
      "part1_chapter1.re\n\npart2_chpater1.re\n",
      "part1\npart2\npart3\n",
      %w(part1 part2),
    ],
    [
      3,
      "part1_chapter1.re\n\npart2_chapter1.re\n\npart3_chapter1.re",
      "part1\n",
      [
        "part1",
        "", # XXX: OK?
        ""
      ],
    ],
    [
      1,
      "part1_chapter1.re\n",
      "",
      [
        "", # XXX: OK?
      ],
    ],
    [
      1,
      "part1_chapter1.re\n",
      nil,
      [
        "",
      ],
    ],
  ].each do |n_parts, chaps_text, parts_text, part_names|
    n_test += 1
    Dir.mktmpdir do |dir|
      book = Book::Base.new(dir)
      chaps_path = File.join(dir, 'CHAPS')
      File.open(chaps_path, 'w') {|o| o.print chaps_text }
      unless parts_text.nil?
        parts_path = File.join(dir, 'PART')
        File.open(parts_path, 'w') {|o| o.print parts_text }
      end

      parts = book.instance_eval { parse_chapters }
      assert_equal n_parts, parts.size, "\##{n_test}"
      assert_equal part_names, parts.map {|p| p.name }, "\##{n_test}"
    end
  end
end
test_parts() click to toggle source
# File ../../../../../test/test_book.rb, line 407
def test_parts
  mktmpbookdir do |dir, book, files|
    assert book.parts.empty?
    assert !book.part(0)
    assert !book.part(1)

    tmp = []
    book.each_part { tmp << true }
    assert tmp.empty?
  end

  mktmpbookdir 'CHAPS' => "ch1\nch2\n\nch3", 'PART' => "foo\nbar\n" do |dir, book, files|
    parts = book.parts
    assert_equal 2, parts.size
    assert !book.part(0)
    assert_equal "foo", book.part(1).name
    assert_equal "bar", book.part(2).name
    assert !book.part(3)

    tmp = []
    book.each_part {|p| tmp << p.number }
    assert_equal [1, 2], tmp
  end
end
test_postscripts() click to toggle source
# File ../../../../../test/test_book.rb, line 397
def test_postscripts
  mktmpbookdir 'catalog.yml' => "POSTDEF:\n  - b01.re",
               'b01.re' => '= back' do |dir, book, files|
    assert_kind_of Book::Part, book.postscripts
    assert_equal 1, book.postscripts.chapters.size
    assert_equal 'back', book.postscripts.chapters.first.title
    assert_equal nil, book.postscripts.chapters.first.number
  end
end
test_prefaces() click to toggle source
# File ../../../../../test/test_book.rb, line 286
def test_prefaces
  mktmpbookdir do |dir, book, files|
    assert_equal nil, book.prefaces
  end

  mktmpbookdir 'PREDEF' => '' do |dir, book, files|
    assert_equal nil, book.prefaces # XXX: OK?
  end

  mktmpbookdir 'PREDEF' => 'chapter1',
               'chapter1.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.prefaces
    assert_equal '', book.prefaces.name
    assert_equal 1, book.prefaces.chapters.size
    assert_equal "chapter1", book.prefaces.chapters.first.name
    assert_equal files['chapter1.re'], book.prefaces.chapters.first.path
  end

  mktmpbookdir 'PREDEF' => "chapter1\n\nchapter2",
               'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.prefaces
    assert_equal '', book.prefaces.name
    assert_equal 2, book.prefaces.chapters.size
    assert_equal "chapter1", book.prefaces.chapters.first.name
    assert_equal files['chapter1.re'], book.prefaces.chapters.first.path
    assert_equal "chapter2", book.prefaces.chapters.last.name
    assert_equal files['chapter2.re'], book.prefaces.chapters.last.path
  end

  mktmpbookdir 'PREDEF' => "chapter1 chapter2",
               'chapter1.re' => '', 'chapter2.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.prefaces
    assert_equal '', book.prefaces.name
    assert_equal 2, book.prefaces.chapters.size # XXX: OK?
  end

  mktmpbookdir 'PREDEF' => 'not_exist' do |dir, book, files|
    assert_raises FileNotFound do
      assert_equal nil, book.prefaces
    end
  end

  mktmpbookdir 'PREDEF' => 'chapter1.re',
               'chapter1.re' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.prefaces
    assert_equal '', book.prefaces.name
    assert_equal 1, book.prefaces.chapters.size
  end

  mktmpbookdir 'PREDEF' => 'chapter1.txt',
               'chapter1.txt' => '' do |dir, book, files|
    assert_kind_of Book::Part, book.prefaces
    assert_equal '', book.prefaces.name
    assert_equal 1, book.prefaces.chapters.size
  end
end
test_prev_chapter() click to toggle source
# File ../../../../../test/test_book.rb, line 482
def test_prev_chapter
  mktmpbookdir 'CHAPS' => "ch1\nch2" do |dir, book, files|
    chapter = book.chapter('ch2')
    assert_equal book.chapter('ch1'), book.prev_chapter(chapter)

    chapter = book.chapter('ch1')
    assert_equal nil, book.prev_chapter(chapter)
  end
end
test_read_APPENDIX() click to toggle source
# File ../../../../../test/test_book.rb, line 94
def test_read_APPENDIX
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert_equal "", book.read_APPENDIX

    post_path = File.join(dir, 'POSTDEF')
    re1_path = File.join(dir, "123#{book.ext}")
    re2_path = File.join(dir, "456#{book.ext}")

    File.open(post_path, 'w') {|o| o.print "abc\n" }
    File.open(re1_path, 'w') {|o| o.print "123\n" }
    File.open(re2_path, 'w') {|o| o.print "456\n" }

    assert_equal "abc\n", book.read_APPENDIX

    File.unlink(post_path)
    assert_equal "#{re1_path}\n#{re2_path}", book.read_APPENDIX

    File.unlink(re1_path)
    assert_equal "#{re2_path}", book.read_APPENDIX

    File.unlink(re2_path)
    assert_equal "", book.read_APPENDIX
  end
end
test_read_CHAPS() click to toggle source
# File ../../../../../test/test_book.rb, line 48
def test_read_CHAPS
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert_equal "", book.read_CHAPS

    chaps_path = File.join(dir, 'CHAPS')
    re1_path = File.join(dir, "123#{book.ext}")
    re2_path = File.join(dir, "456#{book.ext}")

    File.open(chaps_path, 'w') {|o| o.print "abc\n" }
    File.open(re1_path, 'w') {|o| o.print "123\n" }
    File.open(re2_path, 'w') {|o| o.print "456\n" }

    assert_equal "abc\n", book.read_CHAPS

    File.unlink(chaps_path)
    assert_equal "#{re1_path}\n#{re2_path}", book.read_CHAPS

    File.unlink(re1_path)
    assert_equal "#{re2_path}", book.read_CHAPS

    File.unlink(re2_path)
    assert_equal "", book.read_CHAPS
  end
end
test_read_PART() click to toggle source
# File ../../../../../test/test_book.rb, line 74
def test_read_PART
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert !book.part_exist?
    assert_raises Errno::ENOENT do # XXX: OK?
      book.read_PART
    end

    chaps_path = File.join(dir, 'PART')
    chaps_content = "abc\n"
    File.open(chaps_path, 'w') {|o| o.print chaps_content }

    assert book.part_exist?
    assert_equal chaps_content, book.read_PART

    File.open(chaps_path, 'w') {|o| o.print "XYZ\n" }
    assert_equal chaps_content, book.read_PART
  end
end
test_read_POSTDEF() click to toggle source
# File ../../../../../test/test_book.rb, line 120
def test_read_POSTDEF
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert_equal "", book.read_POSTDEF

    post_path = File.join(dir, 'POSTDEF')
    re1_path = File.join(dir, "123#{book.ext}")
    re2_path = File.join(dir, "456#{book.ext}")

    File.open(post_path, 'w') {|o| o.print "abc\n" }
    File.open(re1_path, 'w') {|o| o.print "123\n" }
    File.open(re2_path, 'w') {|o| o.print "456\n" }

    assert_equal "", book.read_POSTDEF

    File.unlink(post_path)
    assert_equal "", book.read_POSTDEF
  end
end
test_read_bib() click to toggle source
# File ../../../../../test/test_book.rb, line 140
def test_read_bib
  Dir.mktmpdir do |dir|
    book = Book::Base.new(dir)
    assert !book.bib_exist?
    assert_raises Errno::ENOENT do # XXX: OK?
      book.read_bib
    end

    bib_path = File.join(dir, "bib#{book.ext}")
    File.open(bib_path, 'w') {|o| o.print "abc\n" }

    assert book.bib_exist?
    assert_equal "abc\n", book.read_bib
  end
end
test_s_update_rubyenv() click to toggle source
# File ../../../../../test/test_book.rb, line 13
def test_s_update_rubyenv
  save_load_path = $LOAD_PATH.dup

  Dir.mktmpdir do |dir|
    Book.update_rubyenv(dir)
    assert_equal save_load_path, $LOAD_PATH
  end

  Dir.mktmpdir do |dir|
    local_lib_path = File.join(dir, 'lib')
    Dir.mkdir(local_lib_path)
    Book.update_rubyenv(dir)
    assert_equal save_load_path, $LOAD_PATH
  end

  num = rand(99999)
  test_const = "ReVIEW__BOOK__TEST__#{num}"
  begin
    Dir.mktmpdir do |dir|
      File.open(File.join(dir, 'review-ext.rb'), 'w') do |o|
        o.puts "#{test_const} = #{num}"
      end
      Book.update_rubyenv(dir)
      assert_equal num, Object.class_eval { const_get(test_const) }
    end
  ensure
    Object.class_eval { remove_const(test_const) }
  end
end
test_setConfig() click to toggle source
# File ../../../../../test/test_book.rb, line 163
def test_setConfig
  book = Book::Base.new(File.dirname(__FILE__))
  book.config = :test
  assert_equal :test, book.config
end
test_setParameter() click to toggle source

backward compatible

# File ../../../../../test/test_book.rb, line 157
def test_setParameter
  book = Book::Base.new(File.dirname(__FILE__))
  book.config = :test
  assert_equal :test, book.config
end
test_volume() click to toggle source
# File ../../../../../test/test_book.rb, line 492
def test_volume
  mktmpbookdir do |dir, book, files|
    assert book.volume
    assert_equal 0, book.volume.bytes
    assert_equal 0, book.volume.chars
    assert_equal 0, book.volume.lines
  end

  mktmpbookdir 'CHAPS' => 'chapter1.re', 'chapter1.re' => '12345' do |dir, book, files|
    assert book.volume
    assert book.volume.bytes > 0
    assert book.volume.chars > 0
    assert book.volume.lines > 0
  end

  mktmpbookdir 'preface.re' => '12345' do |dir, book, files|

    Dir.chdir(dir) do
      book2 = Book::Base.new('.')
      assert book2.volume
      assert book2.volume.bytes > 0
      assert book2.volume.chars > 0
      assert book2.volume.lines > 0
    end
  end
end