Skip to content

Commit e75f9ba

Browse files
facontidavideclaude
andcommitted
Add regression test for #832: script compare with negative number
Comparing against negative literals (e.g., "A!=-1") used to throw "operator cannot be mixed with previous operators" because the unary minus prefix was in a conflicting operator group. This was resolved by the #1029 fix (string_concat operand change). This commit adds a regression test covering both direct script evaluation and XML precondition usage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b3dbe30 commit e75f9ba

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tests/script_parser_test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,43 @@ TEST(ParserTest, Issue595)
424424
ASSERT_EQ(0, counters[0]);
425425
}
426426

427+
// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/832
428+
TEST(ParserTest, CompareWithNegativeNumber_Issue832)
429+
{
430+
BT::Ast::Environment environment = { BT::Blackboard::create(), {} };
431+
432+
auto GetResult = [&environment](const char* text) -> BT::Any {
433+
return GetScriptResult(environment, text);
434+
};
435+
436+
// "A != -1" should parse and evaluate correctly
437+
EXPECT_EQ(GetResult("A:=0; A!=-1").cast<int>(), 1); // 0 != -1 is true
438+
EXPECT_EQ(GetResult("A:=-1; A!=-1").cast<int>(), 0); // -1 != -1 is false
439+
EXPECT_EQ(GetResult("A:=0; A==-1").cast<int>(), 0); // 0 == -1 is false
440+
EXPECT_EQ(GetResult("A:=0; A>-1").cast<int>(), 1); // 0 > -1 is true
441+
EXPECT_EQ(GetResult("A:=0; A<-1").cast<int>(), 0); // 0 < -1 is false
442+
443+
// Also test that ValidateScript accepts these expressions
444+
EXPECT_TRUE(BT::ValidateScript("A:=0; A!=-1"));
445+
EXPECT_TRUE(BT::ValidateScript("A:=0; A>-1"));
446+
447+
// Reproducer from the issue: precondition with negative literal
448+
BT::BehaviorTreeFactory factory;
449+
const std::string xml_text = R"(
450+
<root BTCPP_format="4">
451+
<BehaviorTree>
452+
<Sequence>
453+
<Script code=" A:=0 " />
454+
<AlwaysSuccess _failureIf="A!=-1"/>
455+
</Sequence>
456+
</BehaviorTree>
457+
</root>
458+
)";
459+
auto tree = factory.createTreeFromText(xml_text);
460+
// A==0, so A!=-1 is true, meaning _failureIf triggers => FAILURE
461+
auto status = tree.tickWhileRunning();
462+
EXPECT_EQ(status, BT::NodeStatus::FAILURE);
463+
}
427464
TEST(ParserTest, NewLine)
428465
{
429466
BT::BehaviorTreeFactory factory;

0 commit comments

Comments
 (0)