aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/parser.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-12-07 03:36:21 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-12-07 03:36:21 +0300
commit00863566ec4601c65c435b74e575d49546a1c707 (patch)
tree479a0a6e96aba8191c7a65ea9bee2f4d5e3a4aba /test/unit_tests/parser.cpp
parentadd stress_test.py (diff)
downloadmath-server-00863566ec4601c65c435b74e575d49546a1c707.tar.gz
math-server-00863566ec4601c65c435b74e575d49546a1c707.zip
split server into multiple components
In a vague attempt to make header files more readable, split server/ into a number of components. Also, refactor the unit tests to use the "Data-driven test cases" of Boost.Test.
Diffstat (limited to '')
-rw-r--r--test/unit_tests/parser.cpp125
1 files changed, 83 insertions, 42 deletions
diff --git a/test/unit_tests/parser.cpp b/test/unit_tests/parser.cpp
index 11f48d3..bf31223 100644
--- a/test/unit_tests/parser.cpp
+++ b/test/unit_tests/parser.cpp
@@ -1,48 +1,89 @@
-#include <server/parser.hpp>
+#include <server/parser/error.hpp>
+#include <server/parser/parser.hpp>
+#include <boost/test/data/test_case.hpp>
+#include <boost/test/data/monomorphic.hpp>
#include <boost/test/unit_test.hpp>
-BOOST_AUTO_TEST_CASE(test_parser_exec) {
- using namespace math::server;
+#include <string>
+#include <string_view>
+#include <vector>
- {
- Parser parser{""};
- BOOST_CHECK_THROW(parser.exec(), parser::Error);
- }
- {
- Parser parser{"1"};
- BOOST_TEST(parser.exec() == 1);
- }
- {
- Parser parser{" 1 + "};
- BOOST_CHECK_THROW(parser.exec(), parser::Error);
- }
- {
- Parser parser{" 1 + 2 "};
- BOOST_TEST(parser.exec() == 3);
- }
- {
- Parser parser{" 2 * 1 + 3 "};
- BOOST_TEST(parser.exec() == 5);
- }
- {
- Parser parser{" 2 * (1 + 3) "};
- BOOST_TEST(parser.exec() == 8);
- }
- {
- Parser parser{" 2 * (1 + 3 "};
- BOOST_CHECK_THROW(parser.exec(), parser::Error);
- }
- {
- Parser parser{" 2 * (1 + 3) )"};
- BOOST_CHECK_THROW(parser.exec(), parser::Error);
- }
- {
- Parser parser{" 2 * (1 + 3 * (1 - -3)) "};
- BOOST_TEST(parser.exec() == 26);
- }
- {
- Parser parser{" -2 * ---- (3 + -100e-1) "}; // Looks weird, but also works in e.g. Python
- BOOST_TEST(parser.exec() == 14);
+BOOST_AUTO_TEST_SUITE(parser_tests)
+
+namespace bdata = boost::unit_test::data;
+using math::server::Parser;
+using math::server::ParserError;
+
+namespace {
+namespace exec::valid {
+
+const std::vector<std::string_view> input{
+ "1",
+ " 1 + 2 ",
+ " 2 * 1 + 3 ",
+ " 2 * (1 + 3) ",
+ " 2 * (1 + 3 * (1 - -3)) ",
+ " -2 * ---- (3 + -100e-1) ", // Looks weird, but also works in e.g. Python
+};
+
+const std::vector<double> expected{
+ 1,
+ 3,
+ 5,
+ 8,
+ 26,
+ 14,
+};
+
+}
+
+namespace exec::invalid {
+
+const std::vector<std::string_view> input{
+ "",
+ " 1 + ",
+ " 2 * (1 + 3 ",
+ " 2 * (1 + 3) )",
+};
+
+const std::vector<std::string> error_msg{
+ "server error: parser error: expected '-', '(' or a number",
+ "server error: parser error: expected '-', '(' or a number",
+ "server error: parser error: missing closing ')'",
+ "server error: parser error: expected a binary operator",
+};
+
+}
+}
+
+BOOST_DATA_TEST_CASE(
+ test_exec_valid,
+ bdata::make(exec::valid::input) ^ exec::valid::expected,
+ input,
+ expected) {
+
+ Parser parser{input};
+ BOOST_TEST(parser.exec() == expected);
+}
+
+BOOST_DATA_TEST_CASE(
+ test_exec_invalid,
+ bdata::make(exec::invalid::input) ^ exec::invalid::error_msg,
+ input,
+ error_msg) {
+
+ BOOST_REQUIRE_THROW(do {
+ Parser parser{input};
+ parser.exec();
+ } while (0), ParserError);
+
+ try {
+ Parser parser{input};
+ parser.exec();
+ } catch (const ParserError& e) {
+ BOOST_TEST(error_msg == e.what());
}
}
+
+BOOST_AUTO_TEST_SUITE_END()