1 #include <gtest/gtest.h>
5 #include "../sample_nodes/dummy_nodes.h"
12 auto input = lexy::zstring_input<lexy::utf8_encoding>(text);
15 if(result.has_value())
17 auto exprs =
LEXY_MOV(result).value();
18 for(
auto i = 0u; i < exprs.size() - 1; ++i)
32 auto Parse = [](
const char* str) {
37 auto result = Parse(
"628");
38 EXPECT_TRUE(result.is_success());
39 EXPECT_EQ(result.value().cast<
int>(), 628);
41 result = Parse(
"-628");
42 EXPECT_TRUE(result.is_success());
43 EXPECT_EQ(result.value().cast<
int>(), -628);
45 result = Parse(
"0x100");
46 EXPECT_TRUE(result.is_success());
47 EXPECT_EQ(result.value().cast<
int>(), 256);
49 result = Parse(
"0X100");
50 EXPECT_TRUE(result.is_success());
51 EXPECT_EQ(result.value().cast<
int>(), 256);
53 result = Parse(
"3.14");
54 EXPECT_TRUE(result.is_success());
55 EXPECT_EQ(result.value().cast<
double>(), 3.14);
57 result = Parse(
"-3.14");
58 EXPECT_TRUE(result.is_success());
59 EXPECT_EQ(result.value().cast<
double>(), -3.14);
61 result = Parse(
"3.14e2");
62 EXPECT_TRUE(result.is_success());
63 EXPECT_EQ(result.value().cast<
double>(), 314);
65 result = Parse(
"3.14e-2");
66 EXPECT_TRUE(result.is_success());
67 EXPECT_EQ(result.value().cast<
double>(), 0.0314);
69 result = Parse(
"3e2");
70 EXPECT_TRUE(result.is_success());
71 EXPECT_EQ(result.value().cast<
double>(), 300);
73 result = Parse(
"3e-2");
74 EXPECT_TRUE(result.is_success());
75 EXPECT_EQ(result.value().cast<
double>(), 0.03);
77 result = Parse(
"'hello world '");
78 EXPECT_TRUE(result.is_success());
79 EXPECT_EQ(result.value().cast<std::string>(),
"hello world ");
81 result = Parse(
"true");
82 EXPECT_TRUE(result.is_success());
83 EXPECT_EQ(result.value().cast<
int>(), 1);
85 result = Parse(
"false");
86 EXPECT_TRUE(result.is_success());
87 EXPECT_EQ(result.value().cast<
int>(), 0);
90 TEST(ParserTest, AnyTypes_Failing)
92 std::istringstream null_sink;
94 auto Parse = [](
const char* str) {
99 EXPECT_TRUE(!Parse(
"0X100g").is_success());
101 EXPECT_TRUE(!Parse(
"0X100.").is_success());
103 EXPECT_TRUE(!Parse(
"3foo").is_success());
105 EXPECT_TRUE(!Parse(
"65.").is_success());
107 EXPECT_TRUE(!Parse(
"65.43foo").is_success());
109 EXPECT_TRUE(!Parse(
"foo").is_success());
121 EXPECT_EQ(GetResult(
"x:= 3; y:=5; x+y").cast<double>(), 8.0);
122 EXPECT_EQ(variables->getKeys().size(), 2);
123 EXPECT_EQ(variables->get<
double>(
"x"), 3.0);
124 EXPECT_EQ(variables->get<
double>(
"y"), 5.0);
126 EXPECT_EQ(GetResult(
"x+=1").cast<double>(), 4.0);
127 EXPECT_EQ(variables->get<
double>(
"x"), 4.0);
129 EXPECT_EQ(GetResult(
"x += 1").cast<double>(), 5.0);
130 EXPECT_EQ(variables->get<
double>(
"x"), 5.0);
132 EXPECT_EQ(GetResult(
"x-=1").cast<double>(), 4.0);
133 EXPECT_EQ(variables->get<
double>(
"x"), 4.0);
135 EXPECT_EQ(GetResult(
"x -= 1").cast<double>(), 3.0);
136 EXPECT_EQ(variables->get<
double>(
"x"), 3.0);
138 EXPECT_EQ(GetResult(
"x*=2").cast<double>(), 6.0);
139 EXPECT_EQ(variables->get<
double>(
"x"), 6.0);
141 EXPECT_EQ(GetResult(
"-x").cast<double>(), -6.0);
143 EXPECT_EQ(GetResult(
"x/=2").cast<double>(), 3.0);
144 EXPECT_EQ(variables->get<
double>(
"x"), 3.0);
146 EXPECT_EQ(GetResult(
"y").cast<double>(), 5.0);
147 EXPECT_EQ(GetResult(
"y/2").cast<double>(), 2.5);
148 EXPECT_EQ(GetResult(
"y*2").cast<double>(), 10.0);
149 EXPECT_EQ(GetResult(
"y-x").cast<double>(), 2.0);
151 EXPECT_EQ(GetResult(
"y & x").cast<double>(), (5 & 3));
152 EXPECT_EQ(GetResult(
"y | x").cast<double>(), (5 | 3));
153 EXPECT_EQ(GetResult(
"y ^ x").cast<double>(), (5 ^ 3));
155 EXPECT_ANY_THROW(
auto res = GetResult(
"y ^ 5.1").cast<double>());
158 EXPECT_EQ(GetResult(
"A:='hello'; B:=' '; C:='world'; A+B+C").cast<std::string>(),
"hell"
162 EXPECT_EQ(variables->getKeys().size(), 5);
163 EXPECT_EQ(variables->get<std::string>(
"A"),
"hello");
164 EXPECT_EQ(variables->get<std::string>(
"B"),
" ");
165 EXPECT_EQ(variables->get<std::string>(
"C"),
"world");
168 EXPECT_TRUE(!GetResult(
"A= ' right'; "
173 EXPECT_EQ(variables->getKeys().size(), 5);
174 EXPECT_EQ(variables->get<std::string>(
"A"),
" right");
175 EXPECT_EQ(variables->get<std::string>(
"B"),
" center ");
176 EXPECT_EQ(variables->get<std::string>(
"C"),
"left ");
179 EXPECT_ANY_THROW(GetResult(
"x=A"));
180 EXPECT_ANY_THROW(GetResult(
"x='msg'"));
181 EXPECT_ANY_THROW(GetResult(
"A=1.0"));
184 EXPECT_ANY_THROW(GetResult(
" 'hello' = 'world' "));
185 EXPECT_ANY_THROW(GetResult(
" 'hello' = 2.0 "));
186 EXPECT_ANY_THROW(GetResult(
" 3.0 = 2.0 "));
188 size_t prev_size = variables->getKeys().size();
189 EXPECT_ANY_THROW(GetResult(
"new_var=69"));
190 EXPECT_EQ(variables->getKeys().size(), prev_size);
193 EXPECT_EQ(GetResult(
"x < y").cast<int>(), 1);
194 EXPECT_EQ(GetResult(
"y > x").cast<int>(), 1);
195 EXPECT_EQ(GetResult(
"y != x").cast<int>(), 1);
196 EXPECT_EQ(GetResult(
"y == x").cast<int>(), 0);
198 EXPECT_EQ(GetResult(
" 'hello' == 'hello'").cast<int>(), 1);
199 EXPECT_EQ(GetResult(
" 'hello' != 'world'").cast<int>(), 1);
200 EXPECT_EQ(GetResult(
" 'hello' < 'world'").cast<int>(), 1);
201 EXPECT_EQ(GetResult(
" 'hello' > 'world'").cast<int>(), 0);
203 EXPECT_NE(GetResult(
"x > y").cast<int>(), 1);
204 EXPECT_NE(GetResult(
"y < x").cast<int>(), 1);
205 EXPECT_NE(GetResult(
"y == x").cast<int>(), 1);
207 EXPECT_EQ(GetResult(
"y == x ? 'T' : 'F'").cast<std::string>(),
"F");
208 EXPECT_EQ(GetResult(
"y != x ? 'T' : 'F'").cast<std::string>(),
"T");
210 EXPECT_EQ(GetResult(
"y == x").cast<int>(), 0);
211 EXPECT_EQ(GetResult(
"y == 5").cast<int>(), 1);
212 EXPECT_EQ(GetResult(
"x == 3").cast<int>(), 1);
214 EXPECT_EQ(GetResult(
" true ").cast<int>(), 1);
215 EXPECT_EQ(GetResult(
" 'true' ").cast<std::string>(),
"true");
217 GetResult(
"v1:= true; v2:= false");
218 EXPECT_EQ(variables->get<
int>(
"v1"), 1);
219 EXPECT_EQ(variables->get<
int>(
"v2"), 0);
221 EXPECT_EQ(GetResult(
" v2 = true ").cast<int>(), 1);
222 EXPECT_EQ(GetResult(
" v2 = !false ").cast<int>(), 1);
223 EXPECT_EQ(GetResult(
" v2 = !v2 ").cast<int>(), 0);
225 EXPECT_EQ(GetResult(
"v1 && v2").cast<int>(), 0);
226 EXPECT_EQ(GetResult(
"v1 || v2").cast<int>(), 1);
228 EXPECT_EQ(GetResult(
"(y == x) && (x == 3)").cast<int>(), 0);
229 EXPECT_EQ(GetResult(
"(y == x) || (x == 3)").cast<int>(), 1);
231 EXPECT_EQ(GetResult(
" y == x && x == 3 ").cast<int>(), 0);
232 EXPECT_EQ(GetResult(
" y == x || x == 3 ").cast<int>(), 1);
235 EXPECT_EQ(GetResult(
" par1:='3'; par2:=3; par1==par2").cast<int>(), 1);
236 EXPECT_EQ(GetResult(
" par1:='3'; par2:=4; par1!=par2").cast<int>(), 1);
239 TEST(ParserTest, NotInitializedComparison)
250 EXPECT_ANY_THROW(GetResult(
"x < 0"));
251 EXPECT_ANY_THROW(GetResult(
"x == 0"));
252 EXPECT_ANY_THROW(GetResult(
"x > 0"));
254 EXPECT_ANY_THROW(GetResult(
"x + 1"));
255 EXPECT_ANY_THROW(GetResult(
"x += 1"));
273 environment.enums = std::make_shared<BT::EnumsTable>();
279 GetResult(
"C:=BLUE");
281 EXPECT_EQ(GetResult(
"A==B").cast<int>(), 1);
282 EXPECT_EQ(GetResult(
"A!=C").cast<int>(), 1);
284 EXPECT_EQ(GetResult(
"A").cast<Color>(), RED);
285 EXPECT_EQ(GetResult(
"B").cast<Color>(), RED);
286 EXPECT_EQ(GetResult(
"C").cast<Color>(), BLUE);
295 <root BTCPP_format="4" >
296 <BehaviorTree ID="MainTree">
297 <Script code = "A:=THE_ANSWER; color1:=RED; color2:=BLUE; color3:=GREEN" />
315 const auto& blackboard = tree.subtrees.front()->blackboard;
316 ASSERT_EQ(blackboard->get<
int>(
"A"), 42);
317 ASSERT_EQ(blackboard->get<
int>(
"color1"), RED);
318 ASSERT_EQ(blackboard->get<
int>(
"color2"), BLUE);
319 ASSERT_EQ(blackboard->get<
int>(
"color3"), GREEN);
330 double percent =
self.getInput<
double>(
"percentage").
value();
332 auto res =
self.getInput(
"deviceType", devType);
335 throw std::runtime_error(res.error());
340 self.setOutput(
"isLowBattery", (percent < 25));
342 std::cout <<
"Device: " << devType <<
" Level: " << percent << std::endl;
346 TEST(ParserTest, Enums_Issue_523)
351 <root BTCPP_format="4" >
352 <BehaviorTree ID="PowerManagerT">
354 <Script code=" deviceA:=BATT; deviceB:=CONTROLLER; battery_level:=30 "/>
355 <CheckLevel deviceType="{deviceA}" percentage="{battery_level}" isLowBattery="{isLowBattery}"/>
356 <SaySomething message="FIRST low batteries!" _skipIf="!isLowBattery" />
358 <Script code=" battery_level:=20 "/>
359 <CheckLevel deviceType="{deviceA}" percentage="{battery_level}" isLowBattery="{isLowBattery}"/>
360 <SaySomething message="SECOND low batteries!" _skipIf="!isLowBattery" />
377 const auto& blackboard = tree.subtrees.front()->blackboard;
378 ASSERT_EQ(blackboard->get<
int>(
"deviceA"),
BATT);
379 ASSERT_EQ(blackboard->get<
int>(
"deviceB"),
CONTROLLER);
380 ASSERT_EQ(blackboard->get<
bool>(
"isLowBattery"),
true);
397 return { BT::OutputPort<uint8_t>(
"find_enemy") };
406 <root BTCPP_format="4" >
407 <BehaviorTree ID="PowerManagerT">
409 <SampleNode595 find_enemy="{find_enemy}" />
410 <TestA _skipIf="find_enemy==0"/>
415 std::array<int, 1> counters;
420 const auto status = tree.tickWhileRunning();
423 ASSERT_EQ(0, counters[0]);
431 <root BTCPP_format="4" >
432 <BehaviorTree ID="Main">
433 <Script code="A:=5; B:=6"/>
441 ASSERT_EQ(tree.rootBlackboard()->get<
int>(
"A"), 5);
442 ASSERT_EQ(tree.rootBlackboard()->get<
int>(
"B"), 6);