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 : // self
25 : //
26 : #include "catch_main.h"
27 :
28 :
29 : // reporter
30 : //
31 : #include <eventdispatcher/reporter/statement.h>
32 :
33 : #include <eventdispatcher/reporter/instruction_factory.h>
34 :
35 :
36 : // eventdispatcher
37 : //
38 : #include <eventdispatcher/exception.h>
39 :
40 :
41 : // last include
42 : //
43 : #include <snapdev/poison.h>
44 :
45 :
46 :
47 : namespace
48 : {
49 :
50 :
51 :
52 :
53 : } // no name namespace
54 :
55 :
56 :
57 1 : CATCH_TEST_CASE("reporter_statement", "[statement][reporter]")
58 : {
59 1 : CATCH_START_SECTION("reporter_statement: verify basic program")
60 : {
61 3 : SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t if_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("if"));
62 1 : SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t s(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(if_inst));
63 1 : CATCH_REQUIRE(s->get_instruction() == if_inst);
64 :
65 3 : s->set_filename("this-filename.rprtr");
66 1 : CATCH_REQUIRE(s->get_filename() == "this-filename.rprtr");
67 :
68 1 : s->set_line(1041);
69 1 : CATCH_REQUIRE(s->get_line() == 1041);
70 :
71 1 : CATCH_REQUIRE(s->get_location() == "this-filename.rprtr:1041: ");
72 :
73 : // create an expression with 3.1 + 2.9
74 : //
75 1 : SNAP_CATCH2_NAMESPACE::reporter::token t1;
76 1 : t1.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
77 3 : t1.set_string("unordered_label");
78 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c1(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
79 1 : c1->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
80 1 : c1->set_token(t1);
81 3 : s->add_parameter("unordered", c1);
82 :
83 1 : SNAP_CATCH2_NAMESPACE::reporter::token t2;
84 1 : t2.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
85 3 : t2.set_string("less_label");
86 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c2(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
87 1 : c2->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
88 1 : c2->set_token(t2);
89 3 : s->add_parameter("less", c2);
90 :
91 1 : SNAP_CATCH2_NAMESPACE::reporter::token t3;
92 1 : t3.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
93 3 : t3.set_string("equal_label");
94 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c3(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
95 1 : c3->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
96 1 : c3->set_token(t3);
97 3 : s->add_parameter("equal", c3);
98 :
99 1 : SNAP_CATCH2_NAMESPACE::reporter::token t4;
100 1 : t4.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_IDENTIFIER);
101 3 : t4.set_string("greater_label");
102 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c4(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
103 1 : c4->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
104 1 : c4->set_token(t4);
105 3 : s->add_parameter("greater", c4);
106 :
107 3 : CATCH_REQUIRE(s->get_parameter("unordered") == c1);
108 3 : CATCH_REQUIRE(s->get_parameter("less") == c2);
109 3 : CATCH_REQUIRE(s->get_parameter("equal") == c3);
110 3 : CATCH_REQUIRE(s->get_parameter("greater") == c4);
111 3 : CATCH_REQUIRE(s->get_parameter("not-added") == SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t());
112 1 : }
113 1 : CATCH_END_SECTION()
114 1 : }
115 :
116 :
117 4 : CATCH_TEST_CASE("reporter_statement_error", "[statement][reporter][error]")
118 : {
119 4 : CATCH_START_SECTION("reporter_statement_error: statement without instruction")
120 : {
121 6 : CATCH_REQUIRE_THROWS_MATCHES(
122 : SNAP_CATCH2_NAMESPACE::reporter::statement(nullptr)
123 : , ed::implementation_error
124 : , Catch::Matchers::ExceptionMessage(
125 : "implementation_error: an instruction must always be attached to a statement."));
126 : }
127 4 : CATCH_END_SECTION()
128 :
129 4 : CATCH_START_SECTION("reporter_statement_error: parameter already defined")
130 : {
131 3 : SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t print_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("print"));
132 1 : SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t s(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(print_inst));
133 1 : CATCH_REQUIRE(s->get_instruction() == print_inst);
134 :
135 1 : SNAP_CATCH2_NAMESPACE::reporter::token t1;
136 1 : t1.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_DOUBLE_STRING);
137 3 : t1.set_string("this is our message");
138 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c1(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
139 1 : c1->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
140 1 : c1->set_token(t1);
141 3 : s->add_parameter("message", c1);
142 :
143 9 : CATCH_REQUIRE_THROWS_MATCHES(
144 : s->add_parameter("message", c1)
145 : , ed::runtime_error
146 : , Catch::Matchers::ExceptionMessage(
147 : "event_dispatcher_exception: parameter \"message\" defined more than once."));
148 :
149 1 : s->verify_parameters();
150 1 : }
151 4 : CATCH_END_SECTION()
152 :
153 4 : CATCH_START_SECTION("reporter_statement_error: unknown parameter")
154 : {
155 3 : SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t print_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("print"));
156 1 : SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t s(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(print_inst));
157 1 : CATCH_REQUIRE(s->get_instruction() == print_inst);
158 :
159 1 : SNAP_CATCH2_NAMESPACE::reporter::token t1;
160 1 : t1.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_DOUBLE_STRING);
161 3 : t1.set_string("this could be anything really");
162 1 : SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c1(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
163 1 : c1->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
164 1 : c1->set_token(t1);
165 :
166 9 : CATCH_REQUIRE_THROWS_MATCHES(
167 : s->add_parameter("unknown", c1)
168 : , ed::runtime_error
169 : , Catch::Matchers::ExceptionMessage(
170 : "event_dispatcher_exception: parameter \"unknown\" not accepted by \"print\"."));
171 1 : }
172 4 : CATCH_END_SECTION()
173 :
174 4 : CATCH_START_SECTION("reporter_statement_error: missing parameter")
175 : {
176 3 : SNAP_CATCH2_NAMESPACE::reporter::instruction::pointer_t print_inst(SNAP_CATCH2_NAMESPACE::reporter::get_instruction("print"));
177 1 : SNAP_CATCH2_NAMESPACE::reporter::statement::pointer_t s(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::statement>(print_inst));
178 1 : CATCH_REQUIRE(s->get_instruction() == print_inst);
179 :
180 : //SNAP_CATCH2_NAMESPACE::reporter::token t1;
181 : //t1.set_token(SNAP_CATCH2_NAMESPACE::reporter::token_t::TOKEN_DOUBLE_STRING);
182 : //t1.set_string("this could be anything really");
183 : //SNAP_CATCH2_NAMESPACE::reporter::expression::pointer_t c1(std::make_shared<SNAP_CATCH2_NAMESPACE::reporter::expression>());
184 : //c1->set_operator(SNAP_CATCH2_NAMESPACE::reporter::operator_t::OPERATOR_PRIMARY);
185 : //c1->set_token(t1);
186 :
187 3 : CATCH_REQUIRE_THROWS_MATCHES(
188 : s->verify_parameters()
189 : , ed::runtime_error
190 : , Catch::Matchers::ExceptionMessage(
191 : "event_dispatcher_exception: parameter \"message\" is required by \"print\"."));
192 1 : }
193 4 : CATCH_END_SECTION()
194 4 : }
195 :
196 :
197 : // vim: ts=4 sw=4 et
|