From 16cda3321e6f6a794c4ca0e93b9778e11f529a07 Mon Sep 17 00:00:00 2001 From: Julien JEMINE Date: Fri, 9 Dec 2022 13:12:59 +0100 Subject: [PATCH 1/2] Added tests for Boolean parsing --- test/CMakeLists.txt | 2 + test/oatpp/AllTestsMain.cpp | 2 + .../oatpp/parser/json/mapping/BooleanTest.cpp | 78 +++++++++++++++++++ .../oatpp/parser/json/mapping/BooleanTest.hpp | 54 +++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 test/oatpp/parser/json/mapping/BooleanTest.cpp create mode 100644 test/oatpp/parser/json/mapping/BooleanTest.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ee3c999e..bb43cc3c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -75,6 +75,8 @@ add_executable(oatppAllTests oatpp/parser/json/mapping/DeserializerTest.hpp oatpp/parser/json/mapping/EnumTest.cpp oatpp/parser/json/mapping/EnumTest.hpp + oatpp/parser/json/mapping/BooleanTest.cpp + oatpp/parser/json/mapping/BooleanTest.hpp oatpp/parser/json/mapping/UnorderedSetTest.cpp oatpp/parser/json/mapping/UnorderedSetTest.hpp oatpp/web/protocol/http/encoding/ChunkedTest.cpp diff --git a/test/oatpp/AllTestsMain.cpp b/test/oatpp/AllTestsMain.cpp index c315451d..62769ce6 100644 --- a/test/oatpp/AllTestsMain.cpp +++ b/test/oatpp/AllTestsMain.cpp @@ -22,6 +22,7 @@ #include "oatpp/parser/json/mapping/DTOMapperPerfTest.hpp" #include "oatpp/parser/json/mapping/DTOMapperTest.hpp" #include "oatpp/parser/json/mapping/EnumTest.hpp" +#include "oatpp/parser/json/mapping/BooleanTest.hpp" #include "oatpp/parser/json/mapping/UnorderedSetTest.hpp" #include "oatpp/encoding/UnicodeTest.hpp" @@ -122,6 +123,7 @@ void runTests() { OATPP_RUN_TEST(oatpp::test::core::provider::PoolTemplateTest); OATPP_RUN_TEST(oatpp::test::parser::json::mapping::EnumTest); + OATPP_RUN_TEST(oatpp::test::parser::json::mapping::BooleanTest); OATPP_RUN_TEST(oatpp::test::parser::json::mapping::UnorderedSetTest); diff --git a/test/oatpp/parser/json/mapping/BooleanTest.cpp b/test/oatpp/parser/json/mapping/BooleanTest.cpp new file mode 100644 index 00000000..a8162005 --- /dev/null +++ b/test/oatpp/parser/json/mapping/BooleanTest.cpp @@ -0,0 +1,78 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ***************************************************************************/ + +#include "BooleanTest.hpp" + +#include "oatpp/parser/json/mapping/ObjectMapper.hpp" + +#include "oatpp/core/macro/codegen.hpp" + +namespace oatpp +{ +namespace test +{ +namespace parser +{ +namespace json +{ +namespace mapping +{ +void BooleanTest::onRun() +{ + oatpp::parser::json::mapping::ObjectMapper mapper; + + { + OATPP_LOGI(TAG, "Serialize true to string..."); + auto value = mapper.writeToString(Boolean(true)); + OATPP_ASSERT(value == "true"); + OATPP_LOGI(TAG, "OK"); + } + + { + OATPP_LOGI(TAG, "Serialize false to string..."); + auto value = mapper.writeToString(Boolean(false)); + OATPP_ASSERT(value == "false"); + OATPP_LOGI(TAG, "OK"); + } + + { + OATPP_LOGI(TAG, "Deserialize true string..."); + Boolean value = mapper.readFromString("true"); + OATPP_ASSERT(static_cast(value)); + OATPP_LOGI(TAG, "OK"); + } + + { + OATPP_LOGI(TAG, "Deserialize false string..."); + Boolean value = mapper.readFromString("false"); + OATPP_ASSERT(!static_cast(value)); + OATPP_LOGI(TAG, "OK"); + } +} + +} // namespace mapping +} // namespace json +} // namespace parser +} // namespace test +} // namespace oatpp \ No newline at end of file diff --git a/test/oatpp/parser/json/mapping/BooleanTest.hpp b/test/oatpp/parser/json/mapping/BooleanTest.hpp new file mode 100644 index 00000000..a2ff2884 --- /dev/null +++ b/test/oatpp/parser/json/mapping/BooleanTest.hpp @@ -0,0 +1,54 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ***************************************************************************/ + +#ifndef oatpp_test_parser_json_mapping_BooleanTest_hpp +#define oatpp_test_parser_json_mapping_BooleanTest_hpp + +#include "oatpp-test/UnitTest.hpp" + +namespace oatpp +{ +namespace test +{ +namespace parser +{ +namespace json +{ +namespace mapping +{ + +class BooleanTest : public UnitTest +{ +public: + BooleanTest() : UnitTest("TEST[parser::json::mapping::BooleanTest]") {} + void onRun() override; +}; + +} // namespace mapping +} // namespace json +} // namespace parser +} // namespace test +} // namespace oatpp + +#endif /* oatpp_test_parser_json_mapping_BooleanTest_hpp */ From 919f97185c060abfb8d1121fff4a64b321716dd2 Mon Sep 17 00:00:00 2001 From: Julien JEMINE Date: Fri, 9 Dec 2022 13:26:27 +0100 Subject: [PATCH 2/2] Fixed parsing of "false" --- src/oatpp/core/data/mapping/ObjectMapper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/oatpp/core/data/mapping/ObjectMapper.hpp b/src/oatpp/core/data/mapping/ObjectMapper.hpp index ba11ef56..40ac07fd 100644 --- a/src/oatpp/core/data/mapping/ObjectMapper.hpp +++ b/src/oatpp/core/data/mapping/ObjectMapper.hpp @@ -126,7 +126,7 @@ public: auto type = Wrapper::Class::getType(); oatpp::parser::Caret caret(str); auto result = read(caret, type).template cast(); - if(!result) { + if(result == nullptr) { throw oatpp::parser::ParsingError(caret.getErrorMessage(), caret.getErrorCode(), caret.getPosition()); } return result;