Skip to content

Commit d0aedda

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 498af67 commit d0aedda

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/script_parser_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,44 @@ TEST(ParserTest, OperatorAssociativity_Issue1029)
460460
EXPECT_EQ(GetResult("A .. ' ' .. B").cast<std::string>(), "hello world");
461461
}
462462

463+
// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/832
464+
TEST(ParserTest, CompareWithNegativeNumber_Issue832)
465+
{
466+
BT::Ast::Environment environment = { BT::Blackboard::create(), {} };
467+
468+
auto GetResult = [&environment](const char* text) -> BT::Any {
469+
return GetScriptResult(environment, text);
470+
};
471+
472+
// "A != -1" should parse and evaluate correctly
473+
EXPECT_EQ(GetResult("A:=0; A!=-1").cast<int>(), 1); // 0 != -1 is true
474+
EXPECT_EQ(GetResult("A:=-1; A!=-1").cast<int>(), 0); // -1 != -1 is false
475+
EXPECT_EQ(GetResult("A:=0; A==-1").cast<int>(), 0); // 0 == -1 is false
476+
EXPECT_EQ(GetResult("A:=0; A>-1").cast<int>(), 1); // 0 > -1 is true
477+
EXPECT_EQ(GetResult("A:=0; A<-1").cast<int>(), 0); // 0 < -1 is false
478+
479+
// Also test that ValidateScript accepts these expressions
480+
EXPECT_TRUE(BT::ValidateScript("A:=0; A!=-1"));
481+
EXPECT_TRUE(BT::ValidateScript("A:=0; A>-1"));
482+
483+
// Reproducer from the issue: precondition with negative literal
484+
BT::BehaviorTreeFactory factory;
485+
const std::string xml_text = R"(
486+
<root BTCPP_format="4">
487+
<BehaviorTree>
488+
<Sequence>
489+
<Script code=" A:=0 " />
490+
<AlwaysSuccess _failureIf="A!=-1"/>
491+
</Sequence>
492+
</BehaviorTree>
493+
</root>
494+
)";
495+
auto tree = factory.createTreeFromText(xml_text);
496+
// A==0, so A!=-1 is true, meaning _failureIf triggers => FAILURE
497+
auto status = tree.tickWhileRunning();
498+
EXPECT_EQ(status, BT::NodeStatus::FAILURE);
499+
}
500+
463501
// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/923
464502
// Regression test: ValidateScript must not crash on large invalid scripts
465503
// that produce error messages exceeding any fixed-size buffer.

0 commit comments

Comments
 (0)