Merge remote-tracking branch 'origin/master' into next

This commit is contained in:
John Wiegley 2018-07-19 22:01:31 -07:00
commit 5d02402291
No known key found for this signature in database
GPG key ID: C144D8F4F19FE630
5 changed files with 29 additions and 4 deletions

View file

@ -70,6 +70,7 @@ public:
std::size_t errors;
std::size_t count;
std::size_t sequence;
std::string last;
explicit parse_context_t(const path& cwd)
: current_directory(cwd), master(NULL), scope(NULL),

View file

@ -95,8 +95,9 @@ string source_context(const path& file,
struct error_count {
std::size_t count;
explicit error_count(std::size_t _count) : count(_count) {}
const char * what() const { return ""; }
std::string message;
explicit error_count(std::size_t _count, std::string _msg) : count(_count), message(_msg) {}
const char * what() const { return message.c_str(); }
};
} // namespace ledger

View file

@ -166,7 +166,7 @@ See LICENSE file included with the distribution for details and disclaimer.");
OPTION_(global_scope_t, version, DO() { // -v
parent->show_version_info(std::cout);
throw error_count(0); // exit immediately
throw error_count(0, ""); // exit immediately
});
};

View file

@ -282,6 +282,10 @@ void instance_t::parse()
std::cerr << _("Error: ") << err.what() << std::endl;
context.errors++;
if (! current_context.empty())
context.last = current_context + "\n" + err.what();
else
context.last = err.what();
}
}
@ -2030,7 +2034,8 @@ std::size_t journal_t::read_textual(parse_context_stack_t& context_stack)
TRACE_FINISH(parsing_total, 1);
if (context_stack.get_current().errors > 0)
throw error_count(context_stack.get_current().errors);
throw error_count(context_stack.get_current().errors,
context_stack.get_current().last);
return context_stack.get_current().count;
}

View file

@ -23,6 +23,24 @@ class JournalTestCase(unittest.TestCase):
self.assertEqual(str(post.account), "Expenses:Food")
self.assertEqual(post.amount, Amount("$21.34"))
def testParseError(self):
# TODO: ledger spits out parse errors to standard out.
# This should not happen, especially when the error
# has already been captured by a Python exception.
def fun():
read_journal_from_string("""
2012-03-01 KFC
Expenses:Food rsnetnirsnti
Assets:Cash
""")
self.assertRaises(RuntimeError, fun)
try:
fun()
except RuntimeError as e:
self.assertEquals(str(e).splitlines()[-1],
"No quantity specified for amount")
def suite():
return unittest.TestLoader().loadTestsFromTestCase(JournalTestCase)