Skip to content

Commit bb54c2e

Browse files
committed
support setattr and getattr
run tests() in cases is ok
1 parent bfebd75 commit bb54c2e

18 files changed

Lines changed: 159 additions & 31 deletions

File tree

examples/unittest/test1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
class TestUnittestAssertions(unittest.TestCase):
55
def testEqual(self):
6+
print("in testEqual...")
67
self.assertEqual(0, 0)
78

89
def testTrue(self):
10+
print("in testTrue...")
911
self.assertTrue(True)
1012

1113
def testFalse(self):
14+
print("in testFalse...")
1215
self.assertFalse(False)
1316

1417

package/PikaStdLib/PikaStdData.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class String:
6363
def __str__(self) -> str: ...
6464
def __len__(self) -> int: ...
6565
def encode(self) -> bytes: ...
66-
def startwith(self, prefix: str) -> int: ...
67-
def endwith(self, suffix: str) -> int: ...
66+
def startswith(self, prefix: str) -> int: ...
67+
def endswith(self, suffix: str) -> int: ...
6868
def isdigit(self) -> int: ...
6969
def islower(self) -> int: ...
7070
def isalnum(self) -> int: ...

package/PikaStdLib/PikaStdData_String.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ char* PikaStdData_String___str__(PikaObj* self) {
6666
return obj_getStr(self, "str");
6767
}
6868

69-
int PikaStdData_String_startwith(PikaObj* self, char* prefix) {
69+
int PikaStdData_String_startswith(PikaObj* self, char* prefix) {
7070
char* str = obj_getStr(self, "str");
7171
char* p = prefix;
7272
int i = 0;
@@ -79,7 +79,7 @@ int PikaStdData_String_startwith(PikaObj* self, char* prefix) {
7979
return 1;
8080
}
8181

82-
int PikaStdData_String_endwith(PikaObj* self, char* suffix) {
82+
int PikaStdData_String_endswith(PikaObj* self, char* suffix) {
8383
char* str = obj_getStr(self, "str");
8484
int len1 = strlen(str);
8585
int len2 = strlen(suffix);

package/PikaStdLib/PikaStdLib.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class SysObj:
8282
@staticmethod
8383
def exec(code: str): ...
8484

85+
@staticmethod
86+
def getattr(obj: object, name: str) -> any: ...
87+
88+
@staticmethod
89+
def setattr(obj: object, name: str, val: any): ...
90+
8591

8692
class RangeObj:
8793
def __next__(self) -> any: ...

package/PikaStdLib/PikaStdLib_SysObj.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,28 @@ void PikaStdLib_SysObj_exec(PikaObj* self, char* code) {
508508
__platform_printf("[Error] PIKA_EXEC_ENABLE is not enabled.\r\n");
509509
#endif
510510
}
511+
512+
Arg* PikaStdLib_SysObj_getattr(PikaObj* self, PikaObj* obj, char* name) {
513+
Arg* res = NULL;
514+
if (NULL == obj) {
515+
obj_setErrorCode(self, 1);
516+
__platform_printf("[Error] getattr: can not get attr of NULL.\r\n");
517+
return NULL;
518+
}
519+
res = arg_copy(obj_getArg(obj, name));
520+
return res;
521+
}
522+
523+
void PikaStdLib_SysObj_setattr(PikaObj* self,
524+
PikaObj* obj,
525+
char* name,
526+
Arg* val) {
527+
if (NULL == obj) {
528+
obj_setErrorCode(self, 1);
529+
__platform_printf("[Error] setattr: obj is null.\r\n");
530+
goto exit;
531+
}
532+
obj_setArg(obj, name, val);
533+
exit:
534+
return;
535+
}

package/configparser/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# configparser

package/configparser/configparser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def _parse(self):
1010
lines = content.split('\n')
1111
for line in lines:
1212
line = String(line)
13-
if line.startwith('#'):
13+
if line.startswith('#'):
1414
continue
15-
if line.startwith(';'):
15+
if line.startswith(';'):
1616
continue
17-
if line.startwith('['):
17+
if line.startswith('['):
1818
section = String(line.replace('[', ''))
1919
section = section.replace(']', '')
2020
self.config_dict[section] = {}

package/unittest/unittest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _unittest
2+
from PikaStdData import String
23

34

45
class TestResult:
@@ -70,8 +71,13 @@ def assertIn(self, x, y):
7071
msg = "Expected %r to be in %r" % (x, y)
7172
assert x in y, msg
7273

73-
def run(self, result, suite_name):
74-
_unittest._case_run(self, result, suite_name)
74+
def run(self, result: TestResult, suite_name):
75+
for name in dir(self):
76+
if String(name).startswith("test"):
77+
result.testsRun += 1
78+
self.test_fn = getattr(self, name)
79+
print("[ RUN ] %s.%s" % (suite_name, name))
80+
self.test_fn()
7581

7682

7783
class TestSuite:
@@ -82,7 +88,7 @@ def __init__(self, name):
8288
def addTest(self, case):
8389
self._tests.append(case)
8490

85-
def run(self, result):
91+
def run(self, result: TestResult):
8692
for case in self._tests:
8793
case.run(result, self.name)
8894
return result
@@ -93,13 +99,13 @@ def run(self, suite: TestSuite):
9399
res = TestResult()
94100
_ = suite.run(res)
95101
print("----------------------------------------------------------------------")
96-
print("Ran %d tests\n" % res.testsRun)
102+
print("[----------] %d tests from %s\n" % res.testsRun, suite.name)
97103
if res.failuresNum > 0 or res.errorsNum > 0:
98104
s = "FAILED"
99105
s += " (%d errors, %d failures)" % (res.errorsNum, res.failuresNum)
100106
print(s)
101107
else:
102-
msg = "OK"
108+
msg = "[==========]"
103109
if res.skippedNum > 0:
104110
msg += " (skipped=%d)" % res.skippedNum
105111
print(msg)

port/linux/.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}"
2+
"C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}",
3+
"files.associations": {
4+
"pikastdlib_sysobj.h": "c"
5+
}
36
}

port/linux/package/pikascript/PikaStdData.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class String:
6363
def __str__(self) -> str: ...
6464
def __len__(self) -> int: ...
6565
def encode(self) -> bytes: ...
66-
def startwith(self, prefix: str) -> int: ...
67-
def endwith(self, suffix: str) -> int: ...
66+
def startswith(self, prefix: str) -> int: ...
67+
def endswith(self, suffix: str) -> int: ...
6868
def isdigit(self) -> int: ...
6969
def islower(self) -> int: ...
7070
def isalnum(self) -> int: ...

0 commit comments

Comments
 (0)