pikapython/port/linux/test/strs-test.cpp

76 lines
2.5 KiB
C
Raw Normal View History

2021-10-01 00:21:50 +08:00
#include "gtest/gtest.h"
extern "C" {
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
}
static void printInfo(const char* argName, char* argVal) {
2021-11-14 18:39:08 +08:00
printf("\t\t[info] %s: \"%s\"\r\n", argName, argVal);
2021-10-01 00:21:50 +08:00
}
static int mem;
TEST(strs, append) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_strBuff();
char* res = strsAppend(buffs, (char*)"a", (char*)"b");
EXPECT_STREQ((char*)"ab", res);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(strs, formatInt) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_strBuff();
char* res = strsFormat(buffs, 32, "test: %d", 3);
EXPECT_STREQ((char*)"test: 3", res);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-01 00:21:50 +08:00
}
TEST(strs, analizeDef) {
2021-11-14 18:39:08 +08:00
mem = pikaMemNow();
Args* buffs = New_args(NULL);
char currentClassName[] = "Compiler";
char line[] = " def analizeFile(pythonApiPath: str):";
printInfo("currentClassName", currentClassName);
char* defSentenceWithBlock =
strsRemovePrefix(buffs, line, (char*)" def ");
char* defSentence = strsDeleteChar(buffs, defSentenceWithBlock, ' ');
printInfo("defSentence", defSentence);
char* methodName = strsGetFirstToken(buffs, defSentence, '(');
printInfo("methodName", methodName);
char* methodObjPath = strsAppend(
buffs, strsAppend(buffs, currentClassName, (char*)"."), methodName);
printInfo("methodObjPath", methodObjPath);
char* returnType = strsCut(buffs, defSentence, '>', ':');
printInfo("returnType", returnType);
2021-10-01 00:21:50 +08:00
2021-11-14 18:39:08 +08:00
char* typeList = strsCut(buffs, defSentence, '(', ')');
printInfo("typeList", typeList);
if (0 != strGetSize(typeList)) {
int argNum = strCountSign(typeList, ',') + 1;
char* typeListBuff = strsCopy(buffs, typeList);
for (int i = 0; i < argNum; i++) {
char* typeDeclearation = strsPopToken(buffs, typeListBuff, ',');
printInfo("typeDeclearation", typeDeclearation);
char* argName = strsGetFirstToken(buffs, typeDeclearation, ':');
printInfo("argName", argName);
char* argType = strsGetLastToken(buffs, typeDeclearation, ':');
printInfo("argType", argType);
}
2021-10-01 00:21:50 +08:00
}
2021-11-14 18:39:08 +08:00
args_deinit(buffs);
return;
2021-10-01 00:21:50 +08:00
}
TEST(strs, format) {
2021-11-14 18:39:08 +08:00
Args* buffs = New_args(NULL);
char* fmt = strsFormat(buffs, 128, "test int: %d, float %f", 1, 34.2);
EXPECT_TRUE(strEqu((char*)"test int: 1, float 34.200000", fmt));
args_deinit(buffs);
2021-10-01 00:21:50 +08:00
}
TEST(strs, mem) {
2021-11-14 18:39:08 +08:00
EXPECT_EQ(pikaMemNow(), mem);
2021-10-01 00:21:50 +08:00
}