From 4f4d4380f24d51a77c8a39fa7ff8e87572dbbcbb Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 7 Jun 2019 19:34:04 +0900 Subject: [PATCH 1/4] feat: throw from as_* if type differs --- toml/value.hpp | 392 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 349 insertions(+), 43 deletions(-) diff --git a/toml/value.hpp b/toml/value.hpp index 76b3d38..84debfb 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -27,6 +27,16 @@ namespace detail region_base const& get_region(const value&); template void change_region(value&, Region&&); + +template +[[noreturn]] inline void throw_bad_cast(value_t actual, const ::toml::value& v) +{ + throw type_error(detail::format_underline(concat_to_string( + "[error] toml::value bad_cast to ", Expected), { + {std::addressof(get_region(v)), + concat_to_string("the actual type is ", actual)} + })); +} }// detail template @@ -634,47 +644,353 @@ class value template typename detail::toml_default_type::type&& cast() &&; - boolean const& as_boolean() const& noexcept {return this->boolean_;} - integer const& as_integer() const& noexcept {return this->integer_;} - floating const& as_floating() const& noexcept {return this->floating_;} - string const& as_string() const& noexcept {return this->string_;} - offset_datetime const& as_offset_datetime() const& noexcept {return this->offset_datetime_;} - local_datetime const& as_local_datetime() const& noexcept {return this->local_datetime_;} - local_date const& as_local_date() const& noexcept {return this->local_date_;} - local_time const& as_local_time() const& noexcept {return this->local_time_;} - array const& as_array() const& noexcept {return this->array_.value();} - table const& as_table() const& noexcept {return this->table_.value();} + // ------------------------------------------------------------------------ + // nothrow version - boolean & as_boolean() & noexcept {return this->boolean_;} - integer & as_integer() & noexcept {return this->integer_;} - floating & as_floating() & noexcept {return this->floating_;} - string & as_string() & noexcept {return this->string_;} - offset_datetime& as_offset_datetime() & noexcept {return this->offset_datetime_;} - local_datetime & as_local_datetime() & noexcept {return this->local_datetime_;} - local_date & as_local_date() & noexcept {return this->local_date_;} - local_time & as_local_time() & noexcept {return this->local_time_;} - array & as_array() & noexcept {return this->array_.value();} - table & as_table() & noexcept {return this->table_.value();} + boolean const& as_boolean (const std::nothrow_t&) const& noexcept {return this->boolean_;} + integer const& as_integer (const std::nothrow_t&) const& noexcept {return this->integer_;} + floating const& as_floating (const std::nothrow_t&) const& noexcept {return this->floating_;} + string const& as_string (const std::nothrow_t&) const& noexcept {return this->string_;} + offset_datetime const& as_offset_datetime(const std::nothrow_t&) const& noexcept {return this->offset_datetime_;} + local_datetime const& as_local_datetime (const std::nothrow_t&) const& noexcept {return this->local_datetime_;} + local_date const& as_local_date (const std::nothrow_t&) const& noexcept {return this->local_date_;} + local_time const& as_local_time (const std::nothrow_t&) const& noexcept {return this->local_time_;} + array const& as_array (const std::nothrow_t&) const& noexcept {return this->array_.value();} + table const& as_table (const std::nothrow_t&) const& noexcept {return this->table_.value();} - boolean && as_boolean() && noexcept {return std::move(this->boolean_);} - integer && as_integer() && noexcept {return std::move(this->integer_);} - floating && as_floating() && noexcept {return std::move(this->floating_);} - string && as_string() && noexcept {return std::move(this->string_);} - offset_datetime&& as_offset_datetime() && noexcept {return std::move(this->offset_datetime_);} - local_datetime && as_local_datetime() && noexcept {return std::move(this->local_datetime_);} - local_date && as_local_date() && noexcept {return std::move(this->local_date_);} - local_time && as_local_time() && noexcept {return std::move(this->local_time_);} - array && as_array() && noexcept {return std::move(this->array_.value());} - table && as_table() && noexcept {return std::move(this->table_.value());} + boolean & as_boolean (const std::nothrow_t&) & noexcept {return this->boolean_;} + integer & as_integer (const std::nothrow_t&) & noexcept {return this->integer_;} + floating & as_floating (const std::nothrow_t&) & noexcept {return this->floating_;} + string & as_string (const std::nothrow_t&) & noexcept {return this->string_;} + offset_datetime& as_offset_datetime(const std::nothrow_t&) & noexcept {return this->offset_datetime_;} + local_datetime & as_local_datetime (const std::nothrow_t&) & noexcept {return this->local_datetime_;} + local_date & as_local_date (const std::nothrow_t&) & noexcept {return this->local_date_;} + local_time & as_local_time (const std::nothrow_t&) & noexcept {return this->local_time_;} + array & as_array (const std::nothrow_t&) & noexcept {return this->array_.value();} + table & as_table (const std::nothrow_t&) & noexcept {return this->table_.value();} + + boolean && as_boolean (const std::nothrow_t&) && noexcept {return std::move(this->boolean_);} + integer && as_integer (const std::nothrow_t&) && noexcept {return std::move(this->integer_);} + floating && as_floating (const std::nothrow_t&) && noexcept {return std::move(this->floating_);} + string && as_string (const std::nothrow_t&) && noexcept {return std::move(this->string_);} + offset_datetime&& as_offset_datetime(const std::nothrow_t&) && noexcept {return std::move(this->offset_datetime_);} + local_datetime && as_local_datetime (const std::nothrow_t&) && noexcept {return std::move(this->local_datetime_);} + local_date && as_local_date (const std::nothrow_t&) && noexcept {return std::move(this->local_date_);} + local_time && as_local_time (const std::nothrow_t&) && noexcept {return std::move(this->local_time_);} + array && as_array (const std::nothrow_t&) && noexcept {return std::move(this->array_.value());} + table && as_table (const std::nothrow_t&) && noexcept {return std::move(this->table_.value());} + + // ======================================================================== + // throw version + // ------------------------------------------------------------------------ + // const reference + + boolean const& as_boolean() const& + { + if(this->type_ != value_t::Boolean) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->boolean_; + } + integer const& as_integer() const& + { + if(this->type_ != value_t::Integer) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->integer_; + } + floating const& as_floating() const& + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->floating_; + } + string const& as_string() const& + { + if(this->type_ != value_t::String) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->string_; + } + offset_datetime const& as_offset_datetime() const& + { + if(this->type_ != value_t::OffsetDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->offset_datetime_; + } + local_datetime const& as_local_datetime() const& + { + if(this->type_ != value_t::LocalDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_datetime_; + } + local_date const& as_local_date() const& + { + if(this->type_ != value_t::LocalDate) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_date_; + } + local_time const& as_local_time() const& + { + if(this->type_ != value_t::LocalTime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_time_; + } + array const& as_array() const& + { + if(this->type_ != value_t::Array) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->array_.value(); + } + table const& as_table() const& + { + if(this->type_ != value_t::Table) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->table_.value(); + } + + // ------------------------------------------------------------------------ + // nonconst reference + + boolean & as_boolean() & + { + if(this->type_ != value_t::Boolean) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->boolean_; + } + integer & as_integer() & + { + if(this->type_ != value_t::Integer) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->integer_; + } + floating & as_floating() & + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->floating_; + } + string & as_string() & + { + if(this->type_ != value_t::String) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->string_; + } + offset_datetime & as_offset_datetime() & + { + if(this->type_ != value_t::OffsetDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->offset_datetime_; + } + local_datetime & as_local_datetime() & + { + if(this->type_ != value_t::LocalDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_datetime_; + } + local_date & as_local_date() & + { + if(this->type_ != value_t::LocalDate) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_date_; + } + local_time & as_local_time() & + { + if(this->type_ != value_t::LocalTime) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->local_time_; + } + array & as_array() & + { + if(this->type_ != value_t::Array) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->array_.value(); + } + table & as_table() & + { + if(this->type_ != value_t::Table) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->table_.value(); + } + + // ------------------------------------------------------------------------ + // rvalue reference + + boolean && as_boolean() && + { + if(this->type_ != value_t::Boolean) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->boolean_); + } + integer && as_integer() && + { + if(this->type_ != value_t::Integer) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->integer_); + } + floating && as_floating() && + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->floating_); + } + string && as_string() && + { + if(this->type_ != value_t::String) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->string_); + } + offset_datetime && as_offset_datetime() && + { + if(this->type_ != value_t::OffsetDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->offset_datetime_); + } + local_datetime && as_local_datetime() && + { + if(this->type_ != value_t::LocalDatetime) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->local_datetime_); + } + local_date && as_local_date() && + { + if(this->type_ != value_t::LocalDate) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->local_date_); + } + local_time && as_local_time() && + { + if(this->type_ != value_t::LocalTime) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->local_time_); + } + array && as_array() && + { + if(this->type_ != value_t::Array) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->array_.value()); + } + table && as_table() && + { + if(this->type_ != value_t::Table) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->table_.value()); + } + + // ------------------------------------------------------------------------ + // as|is_float (deprecated) TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating() instead.") bool is_float() const noexcept {return this->is(value_t::Float);} + + // ------------------------------------------------------------------------ + // nothrow version + + TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating(std::nothrow) instead.") + floating& as_float(const std::nothrow_t&) & noexcept + { + return this->floating_; + } + TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating(std::nothrow) instead.") + floating&& as_float(const std::nothrow_t&) && noexcept + { + return std::move(this->floating_); + } + TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating(std::nothrow) instead.") + floating const& as_float(const std::nothrow_t&) const& noexcept + { + return this->floating_; + } + + // ------------------------------------------------------------------------ + // throw version + TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating() instead.") - floating& as_float() & noexcept {return this->floating_;} + floating& as_float() & + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->floating_; + } TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating() instead.") - floating&& as_float() && noexcept {return std::move(this->floating_);} + floating&& as_float() && + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return std::move(this->floating_); + } TOML11_MARK_AS_DEPRECATED("use toml::value::is_floating() instead.") - floating const& as_float() const& noexcept {return this->floating_;} + floating const& as_float() const& + { + if(this->type_ != value_t::Float) + { + detail::throw_bad_cast(this->type_, *this); + } + return this->floating_; + } + + // ------------------------------------------------------------------------ std::string comment() const { @@ -753,16 +1069,6 @@ void change_region(value& v, Region&& reg) return; } -template -[[noreturn]] inline void throw_bad_cast(value_t actual, const ::toml::value& v) -{ - throw type_error(detail::format_underline(concat_to_string( - "[error] toml::value bad_cast to ", Expected), { - {std::addressof(get_region(v)), - concat_to_string("the actual type is ", actual)} - })); -} - template struct switch_cast; template<> From 436af12815d3500f7888dc2380aa7fe314aeece4 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 7 Jun 2019 19:43:01 +0900 Subject: [PATCH 2/4] doc: update README --- README.md | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d96c15a..ee3bd99 100644 --- a/README.md +++ b/README.md @@ -573,29 +573,49 @@ if(v.is_integer() && v.as_integer() == 42) } ``` -The complete list of the functions is below. +`as_*` functions internally checks the current contained type for safety and +throws `toml::type_error` if the contained value type is different (after toml11 +v2.4.0). If you already confirmed that the value has the type you will cast +into, you can skip the additional checking by passing `std::nothrow` object to it. + +```cpp +toml::value v = /* ... */; +if(v.is_integer() && v.as_integer(std::nothrow) == 42) // never fail +{ + std::cout << "value is 42" << std::endl; +} +``` + +The full list of the functions is below. ```cpp namespace toml { class value { // ... - const boolean& as_boolean() const& noexcept; - const integer& as_integer() const& noexcept; - const floating& as_floating() const& noexcept; - const string& as_string() const& noexcept; - const offset_datetime& as_offset_datetime() const& noexcept; - const local_datetime& as_local_datetime() const& noexcept; - const local_date& as_local_date() const& noexcept; - const local_time& as_local_time() const& noexcept; - const array& as_array() const& noexcept; - const table& as_table() const& noexcept; + const boolean& as_boolean() const&; + const integer& as_integer() const&; + const floating& as_floating() const&; + const string& as_string() const&; + const offset_datetime& as_offset_datetime() const&; + const local_datetime& as_local_datetime() const&; + const local_date& as_local_date() const&; + const local_time& as_local_time() const&; + const array& as_array() const&; + const table& as_table() const&; // -------------------------------------------------------- // non-const version - boolean& as_boolean() & noexcept; + boolean& as_boolean() &; // ditto... // -------------------------------------------------------- // rvalue version - boolean&& as_boolean() && noexcept; + boolean&& as_boolean() &&; + // ditto... + + // -------------------------------------------------------- + // noexcept versions ... + const boolean& as_boolean(const std::nothrow_t&) const& noexcept; + boolean& as_boolean(const std::nothrow_t&) & noexcept; + boolean&& as_boolean(const std::nothrow_t&) && noexcept; // ditto... }; } // toml From 8befe3f1ada406e4ee6d53f1a46ba1ba440ab7ce Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 8 Jun 2019 19:20:09 +0900 Subject: [PATCH 3/4] test: add test for throw/nothrow versions of as_* --- tests/test_value.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 2 deletions(-) diff --git a/tests/test_value.cpp b/tests/test_value.cpp index 71f0083..8de184a 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -32,6 +32,8 @@ BOOST_AUTO_TEST_CASE(test_value_boolean) BOOST_CHECK_EQUAL(v2.cast(), false); BOOST_CHECK_EQUAL(v1.as_boolean(), true); BOOST_CHECK_EQUAL(v2.as_boolean(), false); + BOOST_CHECK_EQUAL(v1.as_boolean(std::nothrow), true); + BOOST_CHECK_EQUAL(v2.as_boolean(std::nothrow), false); v1 = false; v2 = true; @@ -122,6 +124,8 @@ BOOST_AUTO_TEST_CASE(test_value_integer) BOOST_CHECK_EQUAL(v2.cast(), 42u); BOOST_CHECK_EQUAL(v1.as_integer(), -42); BOOST_CHECK_EQUAL(v2.as_integer(), 42u); + BOOST_CHECK_EQUAL(v1.as_integer(std::nothrow), -42); + BOOST_CHECK_EQUAL(v2.as_integer(std::nothrow), 42u); v1 = 54; v2 = -54; @@ -212,6 +216,8 @@ BOOST_AUTO_TEST_CASE(test_value_float) BOOST_CHECK_CLOSE_FRACTION(v2.cast(), 3.14, 1e-2); BOOST_CHECK_EQUAL (v1.as_floating(), 3.14); BOOST_CHECK_CLOSE_FRACTION(v2.as_floating(), 3.14, 1e-2); + BOOST_CHECK_EQUAL (v1.as_floating(std::nothrow), 3.14); + BOOST_CHECK_CLOSE_FRACTION(v2.as_floating(std::nothrow), 3.14, 1e-2); v1 = 2.718f; v2 = 2.718; @@ -309,7 +315,9 @@ BOOST_AUTO_TEST_CASE(test_value_string) BOOST_CHECK_EQUAL(v1.as_string(), "foo"); BOOST_CHECK_EQUAL(v2.as_string(), "foo"); BOOST_CHECK_EQUAL(v3.as_string(), "foo"); - + BOOST_CHECK_EQUAL(v1.as_string(std::nothrow), "foo"); + BOOST_CHECK_EQUAL(v2.as_string(std::nothrow), "foo"); + BOOST_CHECK_EQUAL(v3.as_string(std::nothrow), "foo"); v1 = "bar"; v2 = "bar"; @@ -439,6 +447,8 @@ BOOST_AUTO_TEST_CASE(test_value_local_date) toml::local_date(2018, toml::month_t::Jan, 31)); BOOST_CHECK_EQUAL(v1.as_local_date(), toml::local_date(2018, toml::month_t::Jan, 31)); + BOOST_CHECK_EQUAL(v1.as_local_date(std::nothrow), + toml::local_date(2018, toml::month_t::Jan, 31)); v1 = toml::local_date(2018, toml::month_t::Apr, 1); @@ -503,6 +513,8 @@ BOOST_AUTO_TEST_CASE(test_value_local_time) v2.cast()); BOOST_CHECK_EQUAL(v1.as_local_time(), v2.as_local_time()); + BOOST_CHECK_EQUAL(v1.as_local_time(std::nothrow), + v2.as_local_time(std::nothrow)); v1 = toml::local_time(1, 30, 0, /*ms*/ 100, /*us*/ 0); @@ -557,6 +569,11 @@ BOOST_AUTO_TEST_CASE(test_value_local_datetime) toml::local_datetime( toml::local_date(2018, toml::month_t::Jan, 31), toml::local_time(12, 30, 45))); + BOOST_CHECK_EQUAL(v1.as_local_datetime(std::nothrow), + toml::local_datetime( + toml::local_date(2018, toml::month_t::Jan, 31), + toml::local_time(12, 30, 45))); + v1 = toml::local_datetime( toml::local_date(2018, toml::month_t::Apr, 1), @@ -628,6 +645,13 @@ BOOST_AUTO_TEST_CASE(test_value_offset_datetime) toml::local_time(12, 30, 45), toml::time_offset(9, 0) )); + BOOST_CHECK_EQUAL(v1.as_offset_datetime(std::nothrow), + toml::offset_datetime( + toml::local_date(2018, toml::month_t::Jan, 31), + toml::local_time(12, 30, 45), + toml::time_offset(9, 0) + )); + v1 = toml::offset_datetime( toml::local_date(2018, toml::month_t::Apr, 1), @@ -705,7 +729,11 @@ BOOST_AUTO_TEST_CASE(test_value_array) BOOST_CHECK_EQUAL(v1.as_array().at(2).as_integer(), 3); BOOST_CHECK_EQUAL(v1.as_array().at(3).as_integer(), 4); BOOST_CHECK_EQUAL(v1.as_array().at(4).as_integer(), 5); - + BOOST_CHECK_EQUAL(v1.as_array(std::nothrow).at(0).as_integer(), 1); + BOOST_CHECK_EQUAL(v1.as_array(std::nothrow).at(1).as_integer(), 2); + BOOST_CHECK_EQUAL(v1.as_array(std::nothrow).at(2).as_integer(), 3); + BOOST_CHECK_EQUAL(v1.as_array(std::nothrow).at(3).as_integer(), 4); + BOOST_CHECK_EQUAL(v1.as_array(std::nothrow).at(4).as_integer(), 5); BOOST_CHECK_EQUAL(v2.cast().at(0).cast(), 6); BOOST_CHECK_EQUAL(v2.cast().at(1).cast(), 7); @@ -794,6 +822,9 @@ BOOST_AUTO_TEST_CASE(test_value_table) BOOST_CHECK_EQUAL(v1.as_table().at("foo").as_integer(), 42); BOOST_CHECK_EQUAL(v1.as_table().at("bar").as_floating(), 3.14); BOOST_CHECK_EQUAL(v1.as_table().at("baz").as_string().str, "qux"); + BOOST_CHECK_EQUAL(v1.as_table(std::nothrow).at("foo").as_integer(), 42); + BOOST_CHECK_EQUAL(v1.as_table(std::nothrow).at("bar").as_floating(), 3.14); + BOOST_CHECK_EQUAL(v1.as_table(std::nothrow).at("baz").as_string().str, "qux"); v1 = toml::table{{"foo", 2.71}, {"bar", 54}, {"baz", "quux"}}; @@ -841,4 +872,125 @@ BOOST_AUTO_TEST_CASE(test_value_empty) toml::value v1; BOOST_CHECK(v1.is_uninitialized()); BOOST_CHECK(v1.is(toml::value_t::Empty)); + + { + bool thrown = false; + try + { + v1.as_boolean(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_integer(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_floating(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_string(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_offset_datetime(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_local_datetime(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_local_date(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_local_time(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_array(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } + { + bool thrown = false; + try + { + v1.as_table(); + } + catch(toml::type_error&) + { + thrown = true; + } + BOOST_CHECK(thrown); + } } From d6f3654185a6a472455762feef7f1b5e4b188b9d Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 8 Jun 2019 19:23:12 +0900 Subject: [PATCH 4/4] refactor: reduce test code by using CHECK_THROW --- tests/test_value.cpp | 130 ++++--------------------------------------- 1 file changed, 10 insertions(+), 120 deletions(-) diff --git a/tests/test_value.cpp b/tests/test_value.cpp index 8de184a..ba3065b 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -873,124 +873,14 @@ BOOST_AUTO_TEST_CASE(test_value_empty) BOOST_CHECK(v1.is_uninitialized()); BOOST_CHECK(v1.is(toml::value_t::Empty)); - { - bool thrown = false; - try - { - v1.as_boolean(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_integer(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_floating(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_string(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_offset_datetime(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_local_datetime(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_local_date(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_local_time(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_array(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } - { - bool thrown = false; - try - { - v1.as_table(); - } - catch(toml::type_error&) - { - thrown = true; - } - BOOST_CHECK(thrown); - } + BOOST_CHECK_THROW(v1.as_boolean(), toml::type_error); + BOOST_CHECK_THROW(v1.as_integer(), toml::type_error); + BOOST_CHECK_THROW(v1.as_floating(), toml::type_error); + BOOST_CHECK_THROW(v1.as_string(), toml::type_error); + BOOST_CHECK_THROW(v1.as_offset_datetime(), toml::type_error); + BOOST_CHECK_THROW(v1.as_local_datetime(), toml::type_error); + BOOST_CHECK_THROW(v1.as_local_date(), toml::type_error); + BOOST_CHECK_THROW(v1.as_local_time(), toml::type_error); + BOOST_CHECK_THROW(v1.as_array(), toml::type_error); + BOOST_CHECK_THROW(v1.as_table(), toml::type_error); }