145 lines
3.3 KiB
Factor
145 lines
3.3 KiB
Factor
USING:
|
|
accessors arrays assocs
|
|
calendar
|
|
continuations
|
|
destructors
|
|
fry kernel
|
|
math
|
|
namespaces
|
|
python python.ffi python.stdlib.builtin python.stdlib.sys
|
|
sequences
|
|
strings tools.test ;
|
|
IN: python.tests
|
|
|
|
py-initialize
|
|
|
|
: py-test ( result quot -- )
|
|
'[ _ with-destructors ] unit-test ; inline
|
|
|
|
[ t ] [ Py_GetVersion string? ] unit-test
|
|
|
|
[ "os" ] [ "os" import PyModule_GetName ] py-test
|
|
|
|
[ t ] [ "os" import "getpid" getattr { } py-call 0 > ] py-test
|
|
|
|
[ t ] [ Py_IsInitialized ] py-test
|
|
|
|
! Importing
|
|
[ { "ImportError" "No module named kolobi" } ] [
|
|
[ "kolobi" import ] [ [ type>> ] [ message>> ] bi 2array ] recover
|
|
] py-test
|
|
|
|
! Tuples
|
|
[ 2 ] [ 2 <py-tuple> py-tuple-size ] py-test
|
|
|
|
: py-date>factor ( py-obj -- timestamp )
|
|
{ "year" "month" "day" } [ getattr >factor ] with map
|
|
first3 0 0 0 instant <timestamp> ;
|
|
|
|
! ! Datetimes
|
|
[ t ] [
|
|
[ py-date>factor ] "date" py-type-dispatch get set-at
|
|
"datetime" import
|
|
"date" getattr "today" getattr
|
|
{ } py-call
|
|
today instant >>gmt-offset =
|
|
] py-test
|
|
|
|
! Unicode
|
|
[ "غثههح" ] [
|
|
"os.path" import "basename" getattr { "غثههح" } py-call
|
|
] py-test
|
|
|
|
! Instance variables
|
|
[ 7 ] [
|
|
"datetime" import "timedelta" getattr
|
|
{ 7 } >py call-object "days" getattr >factor
|
|
] py-test
|
|
|
|
! Create a dictonary
|
|
[ 0 ] [ <py-dict> py-dict-size ] py-test
|
|
|
|
! Dictionary with object keys
|
|
[ 1 ] [
|
|
<py-dict> dup 0 >py 33 >py py-dict-set-item py-dict-size
|
|
] py-test
|
|
|
|
! Dictionary with string keys
|
|
[ 1 ] [
|
|
<py-dict> [ "foo" 33 >py py-dict-set-item-string ] [ py-dict-size ] bi
|
|
] py-test
|
|
|
|
! Get dictionary items
|
|
[ 33 ] [
|
|
<py-dict> "tjaba"
|
|
[ 33 >py py-dict-set-item-string ]
|
|
[ py-dict-get-item-string >factor ] 2bi
|
|
] py-test
|
|
|
|
! Nest dicts
|
|
[ 0 ] [
|
|
<py-dict> "foo"
|
|
[ <py-dict> py-dict-set-item-string ]
|
|
[ py-dict-get-item-string ] 2bi
|
|
py-dict-size
|
|
] py-test
|
|
|
|
! Nested tuples
|
|
[ 3 ] [
|
|
1 <py-tuple> dup 0 3 <py-tuple> py-tuple-set-item
|
|
0 py-tuple-get-item py-tuple-size
|
|
] py-test
|
|
|
|
! Round tripping!
|
|
[ { "foo" { 99 77 } } ] [ { "foo" { 99 77 } } >py >factor ] py-test
|
|
|
|
[ H{ { "foo" "bar" } { 3 4 } } ] [
|
|
H{ { "foo" "bar" } { 3 4 } } >py >factor
|
|
] py-test
|
|
|
|
! Kwargs
|
|
[ 2014 10 22 ] [
|
|
"datetime" import "date" getattr
|
|
{ } { "year" 2014 "month" 10 "day" 22 } py-call2
|
|
[ year>> ] [ month>> ] [ day>> ] tri
|
|
] py-test
|
|
|
|
SYMBOLS: year month day ;
|
|
|
|
[ 2014 10 22 ] [
|
|
"datetime" import "date" getattr
|
|
{ } { year 2014 month 10 day 22 } py-call2
|
|
[ year>> ] [ month>> ] [ day>> ] tri
|
|
] py-test
|
|
|
|
! Modules
|
|
[ t ] [ "os" import PyModule_GetDict py-dict-size 200 > ] py-test
|
|
|
|
! Reference counting tests
|
|
[ 2 ] [ 3 <py-tuple> getrefcount >factor ] py-test
|
|
|
|
[ -2 ] [
|
|
H{ { "foo" 33 } { "bar" 44 } } >py
|
|
[ "foo" py-dict-get-item-string getrefcount >factor ]
|
|
[
|
|
'[
|
|
500 [ _ "foo" py-dict-get-item-string drop ] times
|
|
] with-destructors
|
|
]
|
|
[ "foo" py-dict-get-item-string getrefcount >factor ] tri -
|
|
] py-test
|
|
|
|
[ -2 ] [
|
|
"abcd" >py <1py-tuple>
|
|
[ 0 py-tuple-get-item getrefcount >factor ]
|
|
[
|
|
[ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
|
|
]
|
|
[ 0 py-tuple-get-item getrefcount >factor ] tri -
|
|
] py-test
|
|
|
|
! Tests for builtins
|
|
[ 10 ] [ 10 range >factor length ] py-test
|
|
|
|
[ t ] [ "os" import "getpid" getattr callable >factor ] py-test
|