class ChapterTest

Public Instance Methods

do_test_index(content, klass, list_method, ref_method, opts = {}) { |ch| ... } click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 245
def do_test_index(content, klass, list_method, ref_method, opts = {})
  Dir.mktmpdir do |dir|
    path = File.join(dir, opts[:filename] || 'chapter.re')

    book = Book::Base.new(dir)

    File.open(path, 'w') do |o|
      o.print content
    end
    ch = Book::Chapter.new(book, 1, 'chapter', path)

    assert ch.__send__(ref_method, 'abc')
    assert ch.__send__(ref_method, 'def')
    assert_raises ReVIEW::KeyError do
      ch.__send__(ref_method, nil)
    end
    assert_raises ReVIEW::KeyError do
      ch.__send__(ref_method, 'others')
    end
    assert_raises ReVIEW::KeyError do
      ch.__send__(ref_method, 'not exist id')
    end

    yield(ch) if block_given?
  end
end
test_bibpaper() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 167
  def test_bibpaper
    do_test_index("//bibpaper
//bibpaper [abc][text...]
//bibpaper [def][text...]
//bibpaper [xyz]
//list [def]
//list [others]
", Book::BibpaperIndex, :bibpaper_index, :bibpaper, :filename => 'bib.re')
    assert_raises FileNotFound do
      do_test_index('', Book::BibpaperIndex, :bibpaper_index, :bibpaper, :filename => 'bib')
    end
  end
test_column_index() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 201
  def test_column_index
    do_test_index("= dummy1
===[column]{abc} aaaa
= dummy2
===[column] def
== dummy3
", Book::ColumnIndex, :column_index, :column, :propagate => false)
  end
test_dirname_and_basename() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 13
def test_dirname_and_basename
  ch = Book::Chapter.new(nil, nil, nil, nil, nil)
  assert_equal nil, ch.dirname
  assert_equal nil, ch.basename

  ch = Book::Chapter.new(nil, nil, nil, '/foo/bar', nil)
  assert_equal '/foo', ch.dirname
  assert_equal 'bar', ch.basename

  ch = Book::Chapter.new(nil, nil, nil, 'bar', nil)
  assert_equal '.', ch.dirname
  assert_equal 'bar', ch.basename
end
test_footnote_index() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 151
  def test_footnote_index
    content = <<E
//footnote
//footnote [abc][text...]
//footnote [def][text...]
//footnote [xyz]
//list [def]
//list [others]
E
    do_test_index(content, Book::FootnoteIndex, :footnote_index, :footnote) do |ch|
      assert_raises ReVIEW::KeyError do
        ch.footnote('xyz')
      end
    end
  end
test_headline_index() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 181
  def test_headline_index
    do_test_index("==
== abc
== def
=== def
//table others
", Book::HeadlineIndex, :headline_index, :headline, :propagate => false)
  end
test_headline_index_nullsection() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 192
  def test_headline_index_nullsection
    do_test_index("== abc
==== dummy
== def
", Book::HeadlineIndex, :headline_index, :headline, :propagate => false)
  end
test_image() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 211
  def test_image
    do_test_index("//image
//image [abc]
//image [def]
//list [def]
//list [others]
", Book::ImageIndex, :image_index, :image)

    do_test_index("//numberlessimage
//numberlessimage [abc]
//numberlessimage [def]
//list [def]
//list [others]
", Book::NumberlessImageIndex, :numberless_image_index, :image, :propagate => false)

    do_test_index("//image
//numberlessimage [abc]
//image [def]
//list [def]
//list [others]
", Book::ImageIndex, :image_index, :image)

    do_test_index("//image
//numberlessimage [abc]
//image [def]
//list [def]
//list [others]
", Book::NumberlessImageIndex, :numberless_image_index, :image, :propagate => false)
  end
test_initialize() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 5
def test_initialize
  ch = Book::Chapter.new(:book, :number, :name, '/foo/bar', :io)
  assert_equal :book, ch.book
  assert_equal :number, ch.number
  assert_equal '/foo/bar', ch.path
  assert_equal "#<ReVIEW::Book::Chapter number /foo/bar>", ch.inspect
end
test_lines() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 65
def test_lines
  lines = ["1\n", "2\n", "3"]
  tf = Tempfile.new('chapter_test')
  tf.print lines.join('')
  tf.close

  book = Book::Base.new(nil)
  ch = Book::Chapter.new(book, nil, nil, tf.path)
  assert_equal lines, ch.lines

  lines = ["1\n", "2\n", "3"]
  tf1 = Tempfile.new('chapter_test1')
  tf1.print lines.join('')
  tf1.close
  tf2 = Tempfile.new('chapter_test2')
  tf2.puts lines.join('')
  tf2.puts lines.join('')
  tf2.close

  ch = Book::Chapter.new(book, nil, nil, tf1.path, tf2.path)
  assert_equal lines, ch.lines # XXX: OK?
end
test_list_index() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 131
  def test_list_index
    do_test_index("//list
//listnum [abc]
//list [def]
//table [def]
//table [others]
", Book::ListIndex, :list_index, :list)
  end
test_name() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 27
def test_name
  ch = Book::Chapter.new(nil, nil, 'foo', nil)
  assert_equal 'foo', ch.name

  ch = Book::Chapter.new(nil, nil, 'foo.bar', nil)
  assert_equal 'foo', ch.name

  # ch = Book::Chapter.new(nil, nil, nil, nil)
  # assert_raises(TypeError) { ch.name } # XXX: OK?
end
test_on_CHAPS?() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 109
def test_on_CHAPS?
  mktmpbookdir 'CHAPS' => "chapter1.re\nchapter2.re",
               'chapter1.re' => '12345', 'preface.re' => 'abcde' do |dir, book, files|
    ch1 = Book::Chapter.new(book, 1, 'chapter1', files['chapter1.re'])
    pre = Book::Chapter.new(book, nil, 'preface', files['preface.re'])

    assert ch1.on_CHAPS?
    assert !pre.on_CHAPS?

    ch2_path = File.join(dir, 'chapter2.er')
    File.open(ch2_path, 'w') {}
    ch2 = Book::Chapter.new(book, 2, 'chapter2', ch2_path)

    ch3_path = File.join(dir, 'chapter3.er')
    File.open(ch3_path, 'w') {}
    ch3 = Book::Chapter.new(book, 3, 'chapter3', ch3_path)

    assert ch2.on_CHAPS?
    assert !ch3.on_CHAPS?
  end
end
test_open() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 38
def test_open
  ch = Book::Chapter.new(nil, nil, nil, __FILE__, :io)
  assert_equal :io, ch.open
  assert_equal [:io], ch.open {|io| [io] }
end
test_size() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 44
def test_size
  ch = Book::Chapter.new(nil, nil, nil, __FILE__, :io)
  assert_equal File.size(__FILE__), ch.size

  File.open(__FILE__, 'r') do |i|
    ch = Book::Chapter.new(nil, nil, nil, nil, i)
    assert_equal File.size(__FILE__), ch.size
  end
end
test_table_index() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 141
  def test_table_index
    do_test_index("//table
//table [abc]
//table [def]
//list [def]
//list [others]
", Book::TableIndex, :table_index, :table)
  end
test_title() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 54
def test_title
  io = StringIO.new
  book = Book::Base.new(nil)
  ch = Book::Chapter.new(book, nil, nil, nil, io)
  assert_equal '', ch.title

  io = StringIO.new("=1\n=2\n")
  ch = Book::Chapter.new(book, nil, nil, nil, io)
  assert_equal '1', ch.title
end
test_volume() click to toggle source
# File ../../../../../test/test_book_chapter.rb, line 88
def test_volume
  content = "abc\ndef"
  tf1 = Tempfile.new('chapter_test1')
  tf1.print content
  tf1.close
  tf2 = Tempfile.new('chapter_test2')
  tf2.print content
  tf2.print content
  tf2.close

  book = Book::Base.new(nil)
  ch = Book::Chapter.new(book, nil, nil, tf1.path)
  assert ch.volume
  assert_equal content.gsub(/\s/, '').size, ch.volume.bytes

  book = Book::Base.new(nil)
  ch = Book::Chapter.new(book, nil, nil, tf1.path, tf2)
  assert ch.volume
  assert_equal content.gsub(/\s/, '').size, ch.volume.bytes # XXX: OK?
end