Add format tests

This commit is contained in:
Evil Eye 2025-08-23 19:51:31 +02:00
parent 06ef34cbdf
commit 0ab0e9abd7
2 changed files with 71 additions and 1 deletions

View File

@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <array>
#include <sstream>
#include "testutils.hpp"
@ -126,6 +128,31 @@ player -> addSpell "fire_bite", 645
PositionCell "Rabenfels, Taverne" 4480.000 3968.000 15820.000 0
End)mwscript";
const std::string sScript5 = R"mwscript(Begin messagebox_format_script
float fVal
set fVal to 12.34
MessageBox "hello world"
MessageBox "%.0f" fVal
MessageBox "a %03.0f b" fVal
MessageBox "%+04.0f" fVal
MessageBox "%+4.0f" fVal
MessageBox "%+ 4.0f" fVal
MessageBox "%0+ 4.0f" fVal
MessageBox "%0+ #4.0f" fVal
MessageBox "%- 5.0f" fVal
MessageBox "%g" fVal
MessageBox "%.3g" fVal
MessageBox "%.5g" fVal
MessageBox "%#.5g" fVal
MessageBox "%-5g" fVal
MessageBox "%- 5g" fVal
End)mwscript";
const std::string sIssue587 = R"mwscript(Begin stalresetScript
@ -579,6 +606,43 @@ End)mwscript";
EXPECT_FALSE(!compile(sScript4));
}
TEST_F(MWScriptTest, mwscript_test_messagebox_format)
{
if (const auto script = compile(sScript5))
{
TestInterpreterContext context;
run(*script, context);
using std::string_view_literals::operator""sv;
constexpr std::array expected{
"hello world"sv,
"12"sv,
"a 012 b"sv,
"+012"sv,
" +12"sv,
" +12"sv,
"+012"sv,
"+12."sv,
" 12 "sv,
"12.34"sv,
"12.3"sv,
"12.34"sv,
"12.340"sv,
"12.34"sv,
" 12.34"sv,
};
for (std::size_t i = 0; i < context.getMessages().size(); i++)
{
std::string_view message = context.getMessages()[i];
EXPECT_EQ(expected.at(i), message);
}
}
else
{
FAIL();
}
}
TEST_F(MWScriptTest, mwscript_test_587)
{
EXPECT_FALSE(!compile(sIssue587));

View File

@ -145,8 +145,11 @@ namespace
{
LocalVariables mLocals;
std::map<ESM::RefId, GlobalVariables> mMembers;
std::vector<std::string> mMessages;
public:
const std::vector<std::string>& getMessages() { return mMessages; }
ESM::RefId getTarget() const override { return ESM::RefId(); }
int getLocalShort(int index) const override { return mLocals.getShort(index); }
@ -161,7 +164,10 @@ namespace
void setLocalFloat(int index, float value) override { mLocals.setFloat(index, value); }
void messageBox(std::string_view message, const std::vector<std::string>& buttons) override {}
void messageBox(std::string_view message, const std::vector<std::string>& buttons) override
{
mMessages.emplace_back(message);
}
void report(const std::string& message) override {}