2022-06-24 05:44:50 +00:00
|
|
|
#include "test_common.h"
|
2022-10-28 18:39:50 +08:00
|
|
|
TEST_START
|
2022-06-24 05:44:50 +00:00
|
|
|
|
2022-07-11 03:14:14 +00:00
|
|
|
#if PIKA_SYNTAX_EXCEPTION_ENABLE
|
2022-06-24 05:44:50 +00:00
|
|
|
TEST(except, try1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"try:\n"
|
|
|
|
" print('before raise')\n"
|
|
|
|
" raise \n"
|
|
|
|
" print('after raise')\n"
|
|
|
|
"except:\n"
|
|
|
|
" print('in except')\n"
|
|
|
|
"\n");
|
|
|
|
/* collect */
|
|
|
|
EXPECT_STREQ("BEGIN\r\n", log_buff[2]);
|
|
|
|
EXPECT_STREQ("before raise\r\n", log_buff[1]);
|
|
|
|
EXPECT_STREQ("in except\r\n", log_buff[0]);
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-11-04 14:23:54 +08:00
|
|
|
TEST(except, def_none) {
|
2022-06-24 05:44:50 +00:00
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"import pika_cjson\n"
|
2022-11-04 14:23:54 +08:00
|
|
|
"b = pika_cjson.Parse('')\n"
|
|
|
|
"if None == b:\n"
|
|
|
|
" print('None')\n"
|
2022-06-24 05:44:50 +00:00
|
|
|
"\n");
|
|
|
|
/* collect */
|
2022-11-04 14:23:54 +08:00
|
|
|
EXPECT_STREQ("None\r\n", log_buff[0]);
|
2022-06-24 05:44:50 +00:00
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(except, trydef1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
2022-06-26 07:30:52 +00:00
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Exception/trydef1.py");
|
2022-06-24 05:44:50 +00:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-06-26 07:30:52 +00:00
|
|
|
EXPECT_STREQ("BEGIN\r\n", log_buff[2]);
|
|
|
|
EXPECT_STREQ("a:1\r\n", log_buff[1]);
|
|
|
|
EXPECT_STREQ("in except\r\n", log_buff[0]);
|
2022-06-24 05:44:50 +00:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-06-26 07:30:52 +00:00
|
|
|
|
|
|
|
TEST(except, try1file) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Exception/try1.py");
|
|
|
|
/* collect */
|
|
|
|
EXPECT_STREQ("BEGIN\r\n", log_buff[2]);
|
|
|
|
EXPECT_STREQ("before raise\r\n", log_buff[1]);
|
|
|
|
EXPECT_STREQ("in except\r\n", log_buff[0]);
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-06-27 11:29:48 +08:00
|
|
|
}
|
2022-08-11 18:32:53 +08:00
|
|
|
|
|
|
|
TEST(except, for_loop) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"sum = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
|
|
|
" try:\n"
|
|
|
|
" if i == 3:\n"
|
|
|
|
" raise\n"
|
|
|
|
" print(i)\n"
|
|
|
|
" sum += i\n"
|
|
|
|
" except:\n"
|
|
|
|
" print('in except')\n"
|
|
|
|
"\n");
|
|
|
|
/* collect */
|
|
|
|
int sum = obj_getInt(pikaMain, "sum");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(sum, 42);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-30 14:47:32 +08:00
|
|
|
|
|
|
|
TEST(except, dict) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Exception/trydict.py");
|
|
|
|
/* collect */
|
|
|
|
int res = obj_getInt(pikaMain, "res");
|
|
|
|
EXPECT_EQ(res, 5);
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(except, len) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Exception/trylen.py");
|
|
|
|
/* collect */
|
|
|
|
int res = obj_getInt(pikaMain, "res");
|
|
|
|
EXPECT_EQ(res, 5);
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-11-04 14:23:54 +08:00
|
|
|
TEST(except, trycmodule1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"import GTestTask\n"
|
|
|
|
"try:\n"
|
|
|
|
" GTestTask.test_raise()\n"
|
|
|
|
" print('after failed')\n"
|
|
|
|
"except:\n"
|
|
|
|
" print('parse failed')\n"
|
|
|
|
"\n");
|
|
|
|
/* collect */
|
|
|
|
EXPECT_STREQ("BEGIN\r\n", log_buff[1]);
|
|
|
|
EXPECT_STREQ("parse failed\r\n", log_buff[0]);
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-08-11 18:32:53 +08:00
|
|
|
#endif
|
2022-10-28 18:39:50 +08:00
|
|
|
|
|
|
|
TEST_END
|