From 67eed0ac523733cd1f3674feecc76e6231b3069d Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 5 Sep 2021 09:00:10 +0800 Subject: [PATCH] debug if_assert --- src/.vscode/launch.json | 2 +- .../pikascript/pikascript-core/PikaBlock.c | 4 +- .../pikascript/pikascript-core/PikaBlock.h | 2 +- .../pikascript/pikascript-core/PikaIf.c | 1 + .../pikascript/pikascript-core/PikaObj.c | 2 +- src/test/block-test.cpp | 39 ++++++++++++++++--- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json index 44ba3fe54..c1b336da0 100644 --- a/src/.vscode/launch.json +++ b/src/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/../build/src/test/pikascript_test", "args": [ // "--gtest_filter=object_test.test2" - "--gtest_filter=block.assert1" + // "--gtest_filter=block.init" // "--gtest_filter=sysObj.print" // "--gtest_filter=object_test.a_b" ], diff --git a/src/package/pikascript/pikascript-core/PikaBlock.c b/src/package/pikascript/pikascript-core/PikaBlock.c index 9fe5b8300..b350cbbde 100644 --- a/src/package/pikascript/pikascript-core/PikaBlock.c +++ b/src/package/pikascript/pikascript-core/PikaBlock.c @@ -12,9 +12,9 @@ void block_deinit(PikaObj *self) obj_deinit(self); } -PikaObj *block_init() +PikaObj *block_init(Args *args) { - PikaObj *self = New_TinyObj(NULL); + PikaObj *self = New_TinyObj(args); obj_setStr(self, "mode", ""); obj_setStr(self, "assert", ""); obj_setStr(self, "body", ""); diff --git a/src/package/pikascript/pikascript-core/PikaBlock.h b/src/package/pikascript/pikascript-core/PikaBlock.h index 2a075d473..d58e58007 100644 --- a/src/package/pikascript/pikascript-core/PikaBlock.h +++ b/src/package/pikascript/pikascript-core/PikaBlock.h @@ -3,7 +3,7 @@ #include "PikaObj.h" void block_deinit(PikaObj *self); -PikaObj *block_init(); +PikaObj *block_init(Args *args); char *block_popLine(PikaObj *self); void block_pushLine(PikaObj *self, char *line); char *block_getAssert(PikaObj *self); diff --git a/src/package/pikascript/pikascript-core/PikaIf.c b/src/package/pikascript/pikascript-core/PikaIf.c index 2864b30c8..d83fd043f 100644 --- a/src/package/pikascript/pikascript-core/PikaIf.c +++ b/src/package/pikascript/pikascript-core/PikaIf.c @@ -8,6 +8,7 @@ void if_setAssert(PikaObj *self, char *line) { Args *buffs = New_strBuff(); char *assert = strsRemovePrefix(buffs, line, "if "); + assert = strsGetFirstToken(buffs, assert, ':'); block_setAssert(self, assert); block_setMode(self, "if"); args_deinit(buffs); diff --git a/src/package/pikascript/pikascript-core/PikaObj.c b/src/package/pikascript/pikascript-core/PikaObj.c index e6c476ccd..8c6f2de18 100644 --- a/src/package/pikascript/pikascript-core/PikaObj.c +++ b/src/package/pikascript/pikascript-core/PikaObj.c @@ -602,7 +602,7 @@ Args *obj_runDirect(PikaObj *self, char *cmd) if (strIsStartWith(cmd, "if ")) { obj_setInt(self, "_isInBlock", 1); - obj_setObjWithoutClass(self, "_block", block_init()); + obj_setObjWithoutClass(self, "_block", block_init); PikaObj *block = obj_getObj(self, "_block", 0); if_setAssert(block, cmd); /* this line processed ok */ diff --git a/src/test/block-test.cpp b/src/test/block-test.cpp index 46cf74299..d36405c8c 100644 --- a/src/test/block-test.cpp +++ b/src/test/block-test.cpp @@ -4,18 +4,20 @@ extern "C" #include "PikaBlock.h" #include "dataStrs.h" #include "PikaIf.h" +#include "TinyObj.h" +#include "PikaObj.h" } TEST(block, init) { - PikaObj *block = block_init(); + PikaObj *block = block_init(NULL); block_deinit(block); EXPECT_EQ(pikaMemNow(), 0); } TEST(block, push) { - PikaObj *block = block_init(); + PikaObj *block = block_init(NULL); block_pushLine(block, (char *)"line1"); block_pushLine(block, (char *)"line2"); block_deinit(block); @@ -24,7 +26,7 @@ TEST(block, push) TEST(block, pop) { - PikaObj *block = block_init(); + PikaObj *block = block_init(NULL); block_pushLine(block, (char *)"line1"); block_pushLine(block, (char *)"line2"); char *line1 = block_popLine(block); @@ -39,7 +41,7 @@ TEST(block, pop) TEST(block, assert1) { - PikaObj *block = block_init(); + PikaObj *block = block_init(NULL); { block_setAssert(block, (char *)"1"); int res = block_checkAssert(block); @@ -56,10 +58,12 @@ TEST(block, assert1) TEST(block, if1) { - PikaObj *block = block_init(); + PikaObj *block = block_init(NULL); if_setAssert(block, (char *)"if 1 :"); if_pushLine(block, (char *)" print('hello')"); if_pushLine(block, (char *)" print('hello2')"); + char *assert = block_getAssert(block); + ASSERT_STREQ((char *)"1 ", assert); int res = block_checkAssert(block); char *line1 = block_popLine(block); ASSERT_STREQ((char *)"print('hello')", line1); @@ -69,4 +73,29 @@ TEST(block, if1) block_deinit(block); } +TEST(block, if2) +{ + PikaObj *obj = New_TinyObj(NULL); + obj_run(obj, (char *)"if 1 :"); + obj_deinit(obj); +} + +TEST(block, if3) +{ + PikaObj *obj = New_TinyObj(NULL); + obj_run(obj, (char *)"if 1 :"); + obj_run(obj, (char *)" print('hello')"); + obj_run(obj, (char *)" print('hello2')"); + PikaObj *block = obj_getObj(obj, (char *)"_block", 0); + char *mode = block_getMode(block); + ASSERT_STREQ((char *)"if", mode); + char *assert = block_getAssert(block); + ASSERT_STREQ((char *)"1 ", assert); + char *line1 = block_popLine(block); + ASSERT_STREQ((char *)"print('hello')", line1); + char *line2 = block_popLine(block); + ASSERT_STREQ((char *)"print('hello2')", line2); + obj_deinit(obj); +} + void block_pushLine(PikaObj *self, char *line);