Files
metin2-server/tests/features/test_ava2_argument_split.py
T

42 lines
2.0 KiB
Python

"""Compile AVA2 text utilities and the legacy shop-label input guard."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT=Path(__file__).resolve().parents[2]
class Arguments(unittest.TestCase):
def test_tab_compression_preserves_spaces_and_empty_edges(self):
source=(ROOT/'src/server/game/src/utils.cpp').read_text(encoding='latin1')
body=source[source.index('// AVA2 search files'):source.index('static int global_time_gap')]
code=r'''
#include <cassert>
#include <ctime>
#include <cstdint>
using WORD=uint16_t;using BYTE=uint8_t;
#define ENABLE_SHOP_SEARCH_SYSTEM
#define ENABLE_OFFLINESHOP_SYSTEM
#include "src/server/game/src/utils.h"
'''+body+r'''
int main(){
assert(getInjectText(nullptr));
assert(!getInjectText("SELECT dragon") && !getInjectText("\xe9\xbe\x99"));
assert(getInjectText("a'b") && getInjectText("a\\b") && getInjectText("a; DROP"));
assert(getInjectText("a\n") && getInjectText("a\x7f"));
std::string longLabel(256,'a');assert(getInjectText(longLabel.c_str()));
std::vector<std::string> args;
split_argument_special("1\t\tpet\t20",args,"\t");assert((args==std::vector<std::string>{"1","pet","20"}));
split_argument_special("\tlong pet name\t",args,"\t");assert((args==std::vector<std::string>{"","long pet name",""}));
split_argument_special(nullptr,args,"\t");assert(args.size()==1 && args[0].empty());
split_argument_special("\t\t",args,"\t");assert(args.size()==2 && args[0].empty() && args[1].empty());
split_argument_special("a|b||c;",args,"|;");assert((args==std::vector<std::string>{"a","b","c",""}));
split_argument_special("a b",args,"");assert(args.size()==1 && args[0]=="a b");
}
'''
with tempfile.TemporaryDirectory() as tmp:
src=Path(tmp)/'probe.cpp';exe=Path(tmp)/'probe';src.write_text(code)
subprocess.run(['c++','-std=c++23','-fsanitize=undefined,address','-fno-sanitize-recover=all','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
subprocess.run([str(exe)],check=True)
if __name__=='__main__':unittest.main()