From 660f158882c25dc9c1064bc34510facbda6c2095 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sat, 30 Mar 2019 19:33:20 +0900 Subject: [PATCH] show appropriate error (KeyError) when reffered chapter is missing. Closes: #1284 --- lib/review/builder.rb | 4 +++- test/test_builder.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index d20040e6b..3b31b1c48 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -478,7 +478,9 @@ def get_chap(chapter = @chapter) def extract_chapter_id(chap_ref) m = /\A([\w+-]+)\|(.+)/.match(chap_ref) if m - return [@book.contents.detect { |chap| chap.id == m[1] }, m[2]] + ch = @book.contents.detect { |chap| chap.id == m[1] } + raise KeyError unless ch + return [ch, m[2]] end [@chapter, chap_ref] end diff --git a/test/test_builder.rb b/test/test_builder.rb index 9dce36acd..c6cb16bc5 100644 --- a/test/test_builder.rb +++ b/test/test_builder.rb @@ -80,6 +80,22 @@ def test_compile_inline_backslash assert_equal [:text, text], @b.compile_inline(text) end + def test_inline_missing_ref + b = Builder.new + chapter = ReVIEW::Book::Chapter.new(ReVIEW::Book::Base.load, 1, 'chap1', nil, StringIO.new) + b.bind(nil, chapter, nil) + e = assert_raises(ReVIEW::ApplicationError) { b.inline_list('unknown|list1') } + assert_equal ': error: unknown list: unknown|list1', e.message + e = assert_raises(ReVIEW::ApplicationError) { b.inline_table('unknown|table1') } + assert_equal ': error: unknown table: unknown|table1', e.message + e = assert_raises(ReVIEW::ApplicationError) { b.inline_img('unknown|img1') } + assert_equal ': error: unknown image: unknown|img1', e.message + e = assert_raises(ReVIEW::ApplicationError) { b.inline_column('unknown|column1') } + assert_equal ': error: unknown column: unknown|column1', e.message + e = assert_raises(ReVIEW::ApplicationError) { b.inline_fn('unknown|footnote1') } + assert_equal ': error: unknown footnote: unknown|footnote1', e.message + end + class XBuilder < Builder def list_header(id, caption) end