LCOV - code coverage report
Current view: top level - tests - catch_reporter_instruction.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 241 241
Test Date: 2025-05-30 15:24:13 Functions: 100.0 % 2 2
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2012-2024  Made to Order Software Corp.  All Rights Reserved
       2              : //
       3              : // https://snapwebsites.org/project/eventdispatcher
       4              : // contact@m2osw.com
       5              : //
       6              : // This program is free software: you can redistribute it and/or modify
       7              : // it under the terms of the GNU General Public License as published by
       8              : // the Free Software Foundation, either version 3 of the License, or
       9              : // (at your option) any later version.
      10              : //
      11              : // This program is distributed in the hope that it will be useful,
      12              : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14              : // GNU General Public License for more details.
      15              : //
      16              : // You should have received a copy of the GNU General Public License
      17              : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18              : 
      19              : // this diagnostic has to be turned off "globally" so the catch2 does not
      20              : // generate the warning on the floating point == operator
      21              : //
      22              : #pragma GCC diagnostic ignored "-Wfloat-equal"
      23              : 
      24              : /** \file
      25              :  * \brief This test manually checks a few core instructions.
      26              :  *
      27              :  * The main test to verify all the instructions is the executor one
      28              :  * (catch_reportor_executor.cpp). This test only verifies that we
      29              :  * can create a program and execute it step by step without using
      30              :  * the executor.
      31              :  *
      32              :  * The executor has many programs that are used to make sure that
      33              :  * all the instructions work as expected.
      34              :  */
      35              : 
      36              : // self
      37              : //
      38              : #include    "catch_main.h"
      39              : 
      40              : 
      41              : // reporter
      42              : //
      43              : #include    <eventdispatcher/reporter/instruction_factory.h>
      44              : 
      45              : #include    <eventdispatcher/reporter/state.h>
      46              : #include    <eventdispatcher/reporter/variable_string.h>
      47              : 
      48              : 
      49              : // eventdispatcher
      50              : //
      51              : #include    <eventdispatcher/exception.h>
      52              : 
      53              : 
      54              : // last include
      55              : //
      56              : #include    <snapdev/poison.h>
      57              : 
      58              : 
      59              : 
      60              : namespace
      61              : {
      62              : 
      63              : 
      64              : 
      65              : 
      66              : } // no name namespace
      67              : 
      68              : 
      69              : 
      70            4 : CATCH_TEST_CASE("reporter_instruction", "[instruction][reporter]")
      71              : {
      72            4 :     CATCH_START_SECTION("reporter_instruction: check label")
      73              :     {
      74            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t label(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
      75            1 :         CATCH_REQUIRE(label != nullptr);
      76            1 :         CATCH_REQUIRE(label->get_name() == "label");
      77            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
      78              :         //SNAP_CATCH2_NAMESPACE::reporter::state const safe_copy(s);
      79            1 :         label->func(s);
      80              :         //CATCH_REQUIRE(safe_copy == s); -- TODO?
      81            1 :     }
      82            4 :     CATCH_END_SECTION()
      83              : 
      84            4 :     CATCH_START_SECTION("reporter_instruction: check goto")
      85              :     {
      86            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
      87            1 :         CATCH_REQUIRE(s.get_statement_size() == 0);
      88              : 
      89            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
      90            1 :         CATCH_REQUIRE(inst != nullptr);
      91            1 :         CATCH_REQUIRE(inst->get_name() == "label");
      92            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
      93            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t;
      94            1 :         t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
      95            3 :         t.set_string("start");
      96            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
      97            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
      98            1 :         e->set_token(t);
      99            3 :         stmt->add_parameter("name", e);
     100            1 :         s.add_statement(stmt);
     101            1 :         CATCH_REQUIRE(s.get_statement_size() == 1);
     102              : 
     103            3 :         inst = SNAP_CATCH2_NAMESPACE::reporter::get_instruction("goto");
     104            1 :         CATCH_REQUIRE(inst != nullptr);
     105            1 :         CATCH_REQUIRE(inst->get_name() == "goto");
     106            1 :         stmt = std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst);
     107            1 :         e = std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>();
     108            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     109            1 :         e->set_token(t);
     110            3 :         stmt->add_parameter("label", e);
     111            1 :         s.add_statement(stmt);
     112            1 :         CATCH_REQUIRE(s.get_statement_size() == 2);
     113              : 
     114              :         // in this case we expect the state to include parameters (variables)
     115              :         // that were computed from the statement parameter (expressions)
     116              :         //
     117            1 :         SNAP_CATCH2_NAMESPACE::reporter::variable_string::pointer_t var_name(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::variable_string>("label"));
     118            3 :         var_name->set_string("start");
     119            1 :         CATCH_REQUIRE(var_name->get_type() == "string");
     120            1 :         s.add_parameter(var_name);
     121              : 
     122              :         // execute the goto
     123              :         //
     124            1 :         s.set_ip(1);
     125            1 :         CATCH_REQUIRE(s.get_ip() == 1); // goto is at position 1
     126            1 :         inst->func(s);
     127            1 :         CATCH_REQUIRE(s.get_ip() == 0); // back to 0 after the goto
     128              : 
     129            3 :         CATCH_REQUIRE(s.get_parameter("label") != nullptr);
     130            1 :         s.clear_parameters();
     131            3 :         CATCH_REQUIRE(s.get_parameter("label") == nullptr);
     132            1 :     }
     133            4 :     CATCH_END_SECTION()
     134              : 
     135            4 :     CATCH_START_SECTION("reporter_instruction: global variable")
     136              :     {
     137            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     138            3 :         CATCH_REQUIRE(s.get_variable("global") == nullptr);
     139              : 
     140            1 :         SNAP_CATCH2_NAMESPACE::reporter::variable::pointer_t var(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::variable_string>("global"));
     141            1 :         CATCH_REQUIRE(var != nullptr);
     142              :         //var->set_string("value");
     143            1 :         s.set_variable(var);
     144            3 :         CATCH_REQUIRE(s.get_variable("global") == var);
     145            1 :     }
     146            4 :     CATCH_END_SECTION()
     147              : 
     148            4 :     CATCH_START_SECTION("reporter_instruction: check call/return")
     149              :     {
     150            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     151              : 
     152            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t call_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("call"));
     153            1 :         CATCH_REQUIRE(call_inst != nullptr);
     154            1 :         CATCH_REQUIRE(call_inst->get_name() == "call");
     155            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t1;
     156            1 :         t1.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     157            3 :         t1.set_string("func_sample");
     158            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e1(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     159            1 :         e1->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     160            1 :         e1->set_token(t1);
     161            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t call_stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(call_inst));
     162            3 :         call_stmt->add_parameter("label", e1);
     163            1 :         s.add_statement(call_stmt);
     164              : 
     165            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t exit_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("exit"));
     166            1 :         CATCH_REQUIRE(exit_inst != nullptr);
     167            1 :         CATCH_REQUIRE(exit_inst->get_name() == "exit");
     168            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t exit_stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(exit_inst));
     169            1 :         s.add_statement(exit_stmt);
     170              : 
     171            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t label_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     172            1 :         CATCH_REQUIRE(label_inst != nullptr);
     173            1 :         CATCH_REQUIRE(label_inst->get_name() == "label");
     174            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t2;
     175            1 :         t2.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     176            3 :         t2.set_string("func_sample");
     177            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e2(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     178            1 :         e2->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     179            1 :         e2->set_token(t2);
     180            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t label_stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(label_inst));
     181            3 :         label_stmt->add_parameter("name", e2);
     182            1 :         s.add_statement(label_stmt);
     183              : 
     184            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t return_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("return"));
     185            1 :         CATCH_REQUIRE(return_inst != nullptr);
     186            1 :         CATCH_REQUIRE(return_inst->get_name() == "return");
     187            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t return_stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(return_inst));
     188            1 :         s.add_statement(return_stmt);
     189              : 
     190              :         // execute CALL func_sample
     191              :         //
     192            1 :         CATCH_REQUIRE(s.get_ip() == 0);
     193            1 :         SNAP_CATCH2_NAMESPACE::reporter::variable_string::pointer_t var_name(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::variable_string>("label"));
     194            3 :         var_name->set_string("func_sample");
     195            1 :         CATCH_REQUIRE(var_name->get_type() == "string");
     196            1 :         s.add_parameter(var_name);
     197            1 :         s.set_ip(1);        // TODO: somehow the executor will increase IP before calls to func()
     198            1 :         call_inst->func(s);
     199              : 
     200              :         // execute RETURN
     201              :         //
     202            1 :         s.clear_parameters();
     203            1 :         CATCH_REQUIRE(s.get_ip() == 2);
     204            1 :         return_inst->func(s);
     205              : 
     206              :         // execute EXIT
     207              :         //
     208            1 :         s.clear_parameters();
     209            1 :         CATCH_REQUIRE(s.get_ip() == 1);
     210            1 :         exit_inst->func(s);
     211            1 :     }
     212            4 :     CATCH_END_SECTION()
     213            4 : }
     214              : 
     215              : 
     216           10 : CATCH_TEST_CASE("reporter_instruction_error", "[instruction][reporter][error]")
     217              : {
     218           10 :     CATCH_START_SECTION("reporter_instruction_error: get unknown instruction")
     219              :     {
     220            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t unknown(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("unknown_instruction"));
     221            1 :         CATCH_REQUIRE(unknown == nullptr);
     222            1 :     }
     223           10 :     CATCH_END_SECTION()
     224              : 
     225           10 :     CATCH_START_SECTION("reporter_instruction_error: search non-existant label")
     226              :     {
     227            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     228            7 :         CATCH_REQUIRE_THROWS_MATCHES(
     229              :               s.get_label_position("unknown")
     230              :             , ed::runtime_error
     231              :             , Catch::Matchers::ExceptionMessage(
     232              :                       "event_dispatcher_exception: label \"unknown\" not found."));
     233            1 :     }
     234           10 :     CATCH_END_SECTION()
     235              : 
     236           10 :     CATCH_START_SECTION("reporter_instruction_error: search non-existant parameter")
     237              :     {
     238            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     239            3 :         CATCH_REQUIRE(s.get_parameter("unknown") == nullptr);
     240            3 :         CATCH_REQUIRE(s.get_parameter("unknown", false) == nullptr);
     241            8 :         CATCH_REQUIRE_THROWS_MATCHES(
     242              :               s.get_parameter("unknown", true)
     243              :             , ed::runtime_error
     244              :             , Catch::Matchers::ExceptionMessage(
     245              :                       "event_dispatcher_exception: parameter \"unknown\" is required."));
     246            1 :     }
     247           10 :     CATCH_END_SECTION()
     248              : 
     249           10 :     CATCH_START_SECTION("reporter_instruction_error: label without a \"name\" parameter (missing)")
     250              :     {
     251            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     252              : 
     253            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     254            1 :         CATCH_REQUIRE(inst != nullptr);
     255            1 :         CATCH_REQUIRE(inst->get_name() == "label");
     256            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     257              : 
     258            5 :         CATCH_REQUIRE_THROWS_MATCHES(
     259              :               s.add_statement(stmt)
     260              :             , ed::runtime_error
     261              :             , Catch::Matchers::ExceptionMessage(
     262              :                       "event_dispatcher_exception: the \"name\" parameter of the \"label\" statement is mandatory."));
     263            1 :     }
     264           10 :     CATCH_END_SECTION()
     265              : 
     266           10 :     CATCH_START_SECTION("reporter_instruction_error: label without a \"name\" parameter (misspelled)")
     267              :     {
     268            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     269              : 
     270            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     271            1 :         CATCH_REQUIRE(inst != nullptr);
     272            1 :         CATCH_REQUIRE(inst->get_name() == "label");
     273            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t;
     274            1 :         t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     275            3 :         t.set_string("start");
     276            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     277            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     278            1 :         e->set_token(t);
     279            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     280              : 
     281            9 :         CATCH_REQUIRE_THROWS_MATCHES(
     282              :               stmt->add_parameter("names", e) // misspelled ("names" instead of "name")
     283              :             , ed::runtime_error
     284              :             , Catch::Matchers::ExceptionMessage(
     285              :                       "event_dispatcher_exception: parameter \"names\" not accepted by \"label\"."));
     286            1 :     }
     287           10 :     CATCH_END_SECTION()
     288              : 
     289           10 :     CATCH_START_SECTION("reporter_instruction_error: label with name parameter not of type PRIMARY")
     290              :     {
     291            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     292              : 
     293            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     294            1 :         CATCH_REQUIRE(inst != nullptr);
     295            1 :         CATCH_REQUIRE(inst->get_name() == "label");
     296            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     297            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_ADD);
     298            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     299            3 :         stmt->add_parameter("name", e);
     300              : 
     301            5 :         CATCH_REQUIRE_THROWS_MATCHES(
     302              :               s.add_statement(stmt)
     303              :             , ed::runtime_error
     304              :             , Catch::Matchers::ExceptionMessage(
     305              :                       "event_dispatcher_exception: the value of the \"name\" parameter of the \"label\" statement cannot be dynamically computed."));
     306            1 :     }
     307           10 :     CATCH_END_SECTION()
     308              : 
     309           10 :     CATCH_START_SECTION("reporter_instruction_error: label with a name parameter of type INTEGER")
     310              :     {
     311            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     312              : 
     313            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     314            1 :         CATCH_REQUIRE(inst != nullptr);
     315            1 :         CATCH_REQUIRE(inst->get_name() == "label");
     316            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t;
     317            1 :         t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_INTEGER);
     318            1 :         t.set_integer(123);
     319            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     320            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     321            1 :         e->set_token(t);
     322            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     323            3 :         stmt->add_parameter("name", e);
     324              : 
     325            5 :         CATCH_REQUIRE_THROWS_MATCHES(
     326              :               s.add_statement(stmt)
     327              :             , ed::runtime_error
     328              :             , Catch::Matchers::ExceptionMessage(
     329              :                       "event_dispatcher_exception: the value of the \"name\" parameter of the \"label\" statement must be an identifier."));
     330            1 :     }
     331           10 :     CATCH_END_SECTION()
     332              : 
     333           10 :     CATCH_START_SECTION("reporter_instruction_error: label already defined")
     334              :     {
     335            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     336              : 
     337              :         {
     338            3 :             SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     339            1 :             CATCH_REQUIRE(inst != nullptr);
     340            1 :             CATCH_REQUIRE(inst->get_name() == "label");
     341            1 :             SNAP_CATCH2_NAMESPACE::reporter::token t;
     342            1 :             t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     343            3 :             t.set_string("duplicate");
     344            1 :             SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     345            1 :             e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     346            1 :             e->set_token(t);
     347            1 :             SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     348            3 :             stmt->add_parameter("name", e);
     349              : 
     350              :             // second time with the same name fails
     351              :             //
     352            9 :             CATCH_REQUIRE_THROWS_MATCHES(
     353              :                   stmt->add_parameter("name", e)
     354              :                 , ed::runtime_error
     355              :                 , Catch::Matchers::ExceptionMessage(
     356              :                           "event_dispatcher_exception: parameter \"name\" defined more than once."));
     357              : 
     358            1 :             s.add_statement(stmt);
     359              : 
     360            1 :             CATCH_REQUIRE(s.get_statement_size() == 1);
     361            1 :             CATCH_REQUIRE(s.get_statement(0) == stmt);
     362            1 :         }
     363              : 
     364              :         // trying to add another label with the same name fails
     365              :         {
     366            3 :             SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("label"));
     367            1 :             CATCH_REQUIRE(inst != nullptr);
     368            1 :             CATCH_REQUIRE(inst->get_name() == "label");
     369            1 :             SNAP_CATCH2_NAMESPACE::reporter::token t;
     370            1 :             t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     371            3 :             t.set_string("duplicate");
     372            1 :             SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     373            1 :             e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     374            1 :             e->set_token(t);
     375            1 :             SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     376            3 :             stmt->add_parameter("name", e);
     377              : 
     378            5 :             CATCH_REQUIRE_THROWS_MATCHES(
     379              :                   s.add_statement(stmt)
     380              :                 , ed::runtime_error
     381              :                 , Catch::Matchers::ExceptionMessage(
     382              :                           "event_dispatcher_exception: label \"duplicate\" already defined at position 0."));
     383              : 
     384            1 :             CATCH_REQUIRE(s.get_statement_size() == 1);
     385            1 :             CATCH_REQUIRE(s.get_statement(0) != stmt);
     386            4 :             CATCH_REQUIRE_THROWS_MATCHES(
     387              :                   s.get_statement(1)
     388              :                 , ed::out_of_range
     389              :                 , Catch::Matchers::ExceptionMessage(
     390              :                           "out_of_range: ip out of program not allowed."));
     391            1 :         }
     392              : 
     393              :         // make sure the second statement did not make it through
     394              :         //
     395            1 :         s.set_ip(0);
     396            1 :         s.set_ip(1);    // exit() does this!
     397            3 :         CATCH_REQUIRE_THROWS_MATCHES(
     398              :               s.set_ip(2)       // this is a bug
     399              :             , ed::out_of_range
     400              :             , Catch::Matchers::ExceptionMessage(
     401              :                       "out_of_range: ip out of program not allowed."));
     402            1 :     }
     403           10 :     CATCH_END_SECTION()
     404              : 
     405           10 :     CATCH_START_SECTION("reporter_instruction_error: \"return()\" does not accept any parameters")
     406              :     {
     407            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("return"));
     408            1 :         CATCH_REQUIRE(inst != nullptr);
     409            1 :         CATCH_REQUIRE(inst->get_name() == "return");
     410              : 
     411            1 :         SNAP_CATCH2_NAMESPACE::reporter::token t;
     412            1 :         t.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
     413            3 :         t.set_string("duplicate");
     414              : 
     415            1 :         SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t e(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
     416            1 :         e->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
     417            1 :         e->set_token(t);
     418              : 
     419            1 :         SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t stmt(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(inst));
     420            9 :         CATCH_REQUIRE_THROWS_MATCHES(
     421              :               stmt->add_parameter("void", e)
     422              :             , ed::runtime_error
     423              :             , Catch::Matchers::ExceptionMessage(
     424              :                       "event_dispatcher_exception: parameter \"void\" not accepted by \"return\"."));
     425            1 :     }
     426           10 :     CATCH_END_SECTION()
     427              : 
     428           10 :     CATCH_START_SECTION("reporter_instruction_error: \"run()\" cannot be called")
     429              :     {
     430            3 :         SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("run"));
     431            1 :         CATCH_REQUIRE(inst != nullptr);
     432            1 :         CATCH_REQUIRE(inst->get_name() == "run");
     433              : 
     434            1 :         SNAP_CATCH2_NAMESPACE::reporter::state s;
     435            3 :         CATCH_REQUIRE_THROWS_MATCHES(
     436              :               inst->func(s)
     437              :             , ed::implementation_error
     438              :             , Catch::Matchers::ExceptionMessage(
     439              :                       "implementation_error: run::func() was called when it should be intercepted by the executor."));
     440            1 :     }
     441           10 :     CATCH_END_SECTION()
     442           10 : }
     443              : 
     444              : 
     445              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

Snap C++ | List of projects | List of versions