Skip to content

Commit 12409ee

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 3bc04cf commit 12409ee

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tests/script_parser_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,44 @@ TEST(ParserTest, ValidateScriptLargeError_Issue923)
483483
auto result = BT::ValidateScript(script);
484484
EXPECT_FALSE(result); // invalid script, but no crash
485485
}
486+
487+
// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/832
488+
TEST(ParserTest, CompareWithNegativeNumber_Issue832)
489+
{
490+
BT::Ast::Environment environment = { BT::Blackboard::create(), {} };
491+
492+
auto GetResult = [&environment](const char* text) -> BT::Any {
493+
return GetScriptResult(environment, text);
494+
};
495+
496+
// "A != -1" should parse and evaluate correctly
497+
EXPECT_EQ(GetResult("A:=0; A!=-1").cast<int>(), 1); // 0 != -1 is true
498+
EXPECT_EQ(GetResult("A:=-1; A!=-1").cast<int>(), 0); // -1 != -1 is false
499+
EXPECT_EQ(GetResult("A:=0; A==-1").cast<int>(), 0); // 0 == -1 is false
500+
EXPECT_EQ(GetResult("A:=0; A>-1").cast<int>(), 1); // 0 > -1 is true
501+
EXPECT_EQ(GetResult("A:=0; A<-1").cast<int>(), 0); // 0 < -1 is false
502+
503+
// Also test that ValidateScript accepts these expressions
504+
EXPECT_TRUE(BT::ValidateScript("A:=0; A!=-1"));
505+
EXPECT_TRUE(BT::ValidateScript("A:=0; A>-1"));
506+
507+
// Reproducer from the issue: precondition with negative literal
508+
BT::BehaviorTreeFactory factory;
509+
const std::string xml_text = R"(
510+
<root BTCPP_format="4">
511+
<BehaviorTree>
512+
<Sequence>
513+
<Script code=" A:=0 " />
514+
<AlwaysSuccess _failureIf="A!=-1"/>
515+
</Sequence>
516+
</BehaviorTree>
517+
</root>
518+
)";
519+
auto tree = factory.createTreeFromText(xml_text);
520+
// A==0, so A!=-1 is true, meaning _failureIf triggers => FAILURE
521+
auto status = tree.tickWhileRunning();
522+
EXPECT_EQ(status, BT::NodeStatus::FAILURE);
523+
}
486524
TEST(ParserTest, NewLine)
487525
{
488526
BT::BehaviorTreeFactory factory;

0 commit comments

Comments
 (0)