Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/script_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,44 @@ TEST(ParserTest, ValidateScriptLargeError_Issue923)
auto result = BT::ValidateScript(script);
EXPECT_FALSE(result); // invalid script, but no crash
}

// https://github.com/BehaviorTree/BehaviorTree.CPP/issues/832
TEST(ParserTest, CompareWithNegativeNumber_Issue832)
{
BT::Ast::Environment environment = { BT::Blackboard::create(), {} };

auto GetResult = [&environment](const char* text) -> BT::Any {
return GetScriptResult(environment, text);
};

// "A != -1" should parse and evaluate correctly
EXPECT_EQ(GetResult("A:=0; A!=-1").cast<int>(), 1); // 0 != -1 is true
EXPECT_EQ(GetResult("A:=-1; A!=-1").cast<int>(), 0); // -1 != -1 is false
EXPECT_EQ(GetResult("A:=0; A==-1").cast<int>(), 0); // 0 == -1 is false
EXPECT_EQ(GetResult("A:=0; A>-1").cast<int>(), 1); // 0 > -1 is true
EXPECT_EQ(GetResult("A:=0; A<-1").cast<int>(), 0); // 0 < -1 is false

// Also test that ValidateScript accepts these expressions
EXPECT_TRUE(BT::ValidateScript("A:=0; A!=-1"));
EXPECT_TRUE(BT::ValidateScript("A:=0; A>-1"));

// Reproducer from the issue: precondition with negative literal
BT::BehaviorTreeFactory factory;
const std::string xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree>
<Sequence>
<Script code=" A:=0 " />
<AlwaysSuccess _failureIf="A!=-1"/>
</Sequence>
</BehaviorTree>
</root>
)";
auto tree = factory.createTreeFromText(xml_text);
// A==0, so A!=-1 is true, meaning _failureIf triggers => FAILURE
auto status = tree.tickWhileRunning();
EXPECT_EQ(status, BT::NodeStatus::FAILURE);
}
TEST(ParserTest, NewLine)
{
BT::BehaviorTreeFactory factory;
Expand Down
Loading